Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

ExcerciseSheet5

October 23, 2019

1 Excercise 1
1.1 Task 1
[1]: import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import time

For the first task I generated a random walk using (pseudo) generated number by pythons random
module.
q Im starting with (0, 0) for the distance to the endpoint is easily calculated as |~R| =
2
xend + y2end . I’m simulating 400 steps with a stepsize of 1.

[2]: ##number of positions


N_end =400

##counter
N = 0

#initialize positions
walk_x = [0]
walk_y = [0]

while N <=N_end:
angle = 2*np.pi*np.random.random()
walk_x.append(walk_x[-1]+ np.cos(angle))
walk_y.append(walk_y[-1]+ np.sin(angle))
N += 1

##squared distance between start and finish


R = walk_x[-1]**2 + walk_y[-1]**2

plt.plot(walk_x,walk_y, 'g')
plt.plot([0, walk_x[-1]], [0,walk_y[-1]],'r--', label='$R\simeq${0:.2f}'.
,→format(np.sqrt(R)))

plt.plot([0],[0], 'o', label='origin')

1
plt.plot(walk_x[-1],walk_y[-1], 'o', label='final step')

plt.xlabel('x')
plt.ylabel('y')
plt.legend()

[2]: <matplotlib.legend.Legend at 0xf4a410>

1.2 Task 2
Here I’m generating M configurations of a random
walk
with a fixed number of steps. Fur-
thermore, I’m calculating the expectation value of R , ( R ) and the estimated error ∆ =
2 2 2
q
h( R2 )2 i − h R2 i2 . I wasn’t sure about the estimated error since it seemed like ∆ ' R2 , so I also

calculated
∆ − R2


delta =
h R2 i

and used the condition δ ≤ 0.01 to determine M.


[15]: def sample_walk(N_end, stepsize=1):

dev = 1

## contains <R^2>
Rs = []

2
M = 0

N_end =N_end

while (dev > 0.01):


M += 1
N = 0

##Not using lists sincethe path doesn't matter, only start and end!
walk_x = 0
walk_y = 0
while N <=N_end:
angle = 2*np.pi*np.random.random()
walk_x = walk_x+ stepsize*np.cos(angle)
walk_y = walk_y+ stepsize*np.sin(angle)
N += 1
Rs.append( walk_x**2 + walk_y**2)
RMS = sum(Rs)/M
RMS2 =sum(el**2 for el in Rs)/M
delta = np.sqrt(RMS2 - RMS**2)
dev = abs(delta-RMS)/RMS
return RMS, RMS2, delta, dev,M

[16]: sample_walk(1000)

[16]: (966.7342573641242,
1851537.121752084,
957.5813267763315,
0.009467886875912442,
738)

1.3 Task 3

of the walk length Nend . I plot


I’m doing the same thing as in Task 2, just for different
values
2
thereults of different values of the walk length against R and according to the lecture this
should follow D · N. Through a bit if trying I saw that D = (δstep )2 (normal diffusion).

[6]: sample_sizes = [ 50,60,70, 90,100,125,150,200,225, 250,275,300, 400, 500]


plt.plot(sample_sizes, [sample_walk(s)[0] for s in sample_sizes], 'o',
,→mfc='none')

plt.plot(range(0,500), label = r"Prediction: $\left\langle R^2


,→\right\rangle=(\delta s)^2 N $")

plt.xlabel('Walk length $N$')


plt.ylabel(r"$ \left\langle R^2 \right\rangle $")

3
plt.legend()

[6]: <matplotlib.legend.Legend at 0xbe624d0>

2 Excerise 2
2.1 Task 1
Now I’ll do the same steps just fora self avoidng self walk od disks with radius 2. Therefore the
minimum stepsize has to be 2 and I’ll choose that one for the simulations. Otherwise it’s analogous
to before.
[13]: N_end =100
N = 0

##need the full lists for checking overlap


walk_x = [0]
walk_y = [0]
radius = 2
##necessary step size: radius 2 --> 4*

def check_overlap(walk_x,walk_y,x_step, y_step, radius):


'''

4
checks if the new disk at (x_step, y_step) ith acertain radius has any
,→ overlap with a previous point,
if so returns True else False
'''
overlap = False

n = len(walk_x)

for i in range(n):
##I'm starting from the last disk since the likelihodd of overlap is
,→definitely larger than other disks

if np.square(walk_x[n-1-i]-x_step) + np.square(walk_y[n-1-i]-y_step) <


,→4*radius**2:

overlap = True
break

#print(overlap)
return overlap

while N <=N_end:

angle = 2*np.pi*np.random.random()
x_step = walk_x[-1]+ 2*radius*np.cos(angle)
y_step = walk_y[-1]+ 2*radius*np.sin(angle)

#check for overlap:


if check_overlap(walk_x, walk_y, x_step, y_step, radius) == False:
walk_x.append(x_step)
walk_y.append(y_step)
N += 1
else:
continue

[62]:
plt.clf()

fig, ax = plt.subplots()

R = walk_x[-1]**2 + walk_y[-1]**2

ax.plot(walk_x,walk_y, marker='o', ms = '4')


ax.plot([0, walk_x[-1]], [0,walk_y[-1]],'r--', label='$R\simeq${0:.2f}'.
,→format(np.sqrt(R)))

5
ax.plot([0],[0], 'o', label='origin')
ax.plot(walk_x[-1],walk_y[-1], 'o', label='final step')

##generate some disks


for p in range(len(walk_x)):
circ = patches.Circle((walk_x[p], walk_y[p]), 2, alpha=0.6, fc='seagreen',
,→linewidth=1,

edgecolor='black', clip_on=False)
ax.add_patch(circ)

plt.xlabel('x')
plt.ylabel('y')
plt.legend()

[62]: <matplotlib.legend.Legend at 0xead8b50>

<Figure size 432x288 with 0 Axes>

2.2 Task 2
Now I’ll do the same as before and generate M configurations and plt N against R2 . However,

there were two problems: * δ converges A LOT slower, so in order to keep the computation time
slow I lowered the condition to δ ≤ 0.3. * With increasing N, the path would get basicall stuck and

6
finding an allowed next step would be very hard up to impossible. Thus, if a new step could be
found within 1.5 seconds, I would discard the configuration and start a new one (this of course,
favours certain kind of configurations slightly).
For the plot N against R2 I found a page claiming R2 ∝ N 3/2 , so I plotted that too. How-


ever, given the problems described above and the limited sampling size this cannot be shown
convincingly. I tried to show it better, but it would take way too long to compute.

[11]: def sample_walk_avoid(N_end, r=2, dev_lim=0.01):


dev = 1
Rs = []
M = 0
N_end =N_end
radius = r
Rs = [0]

while dev > dev_lim:

M +=1
N = 0
walk_x = [0]
walk_y = [0]

stuck = False
t1= time.time()

#print(M)

while len(walk_x) <=N_end:

###often the walk will get trapped, if so: create new walk!
if time.time()-t1 >1.5:
stuck = True
break

angle = 2*np.pi*np.random.random()
x_step = walk_x[-1]+ 2*radius*np.cos(angle)
y_step = walk_y[-1]+ 2*radius*np.sin(angle)

#check for overlap:


#print('checking overlap')
if check_overlap(walk_x = walk_x, walk_y = walk_y,
x_step = x_step, y_step= y_step,
radius= radius) == False:
walk_x.append(x_step)
walk_y.append(y_step)

7
N += 1
else:
continue

if stuck == True:
M -= 1
continue

Rs.append( walk_x[-1]**2 + walk_y[-1]**2)


RMS = sum(Rs)/M
RMS2 =sum(el**2 for el in Rs)/M
delta = np.sqrt(RMS2 - RMS**2)
dev = abs(delta-RMS)/RMS
#print(dev)
return RMS, RMS2, delta, dev

[78]: sample_sizes = [15,20, 30, 40, 50,60,80,100,200,300]


values = [sample_walk_avoid(N_end=s, dev_lim=0.3)[0] for s in sample_sizes]
#plt.plot([8*el**(3/2) for el in range(0,200)], label = r"Fit: $\left\langle R^2
,→\right\rangle \propto N^{3/2}$")
plt.plot([7.5*el**(3/2) for el in range(0,350)], '--')
#plt.plot([10*el**(3/2) for el in range(0,200)])
plt.fill_between(range(350), [6*el**(3/2) for el in range(0,350)], [9*el**(3/2)
,→for el in range(0,350)],

alpha=0.4, label = r"Fit: $\left\langle R^2 \right\rangle


,→\propto N^{3/2}$")
plt.plot(sample_sizes, values, 'o',)

plt.xlabel('Walk length $N$')


plt.ylabel(r"$ \left\langle R^2 \right\rangle $")
plt.xlim(-10,310)
plt.legend()

[78]: <matplotlib.legend.Legend at 0x10edbf90>

8
9

You might also like