Professional Documents
Culture Documents
Excercise Sheet
Excercise Sheet
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.
##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
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)))
1
plt.plot(walk_x[-1],walk_y[-1], 'o', label='final step')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
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
dev = 1
## contains <R^2>
Rs = []
2
M = 0
N_end =N_end
##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
3
plt.legend()
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
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
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)
[62]:
plt.clf()
fig, ax = plt.subplots()
R = walk_x[-1]**2 + walk_y[-1]**2
5
ax.plot([0],[0], 'o', label='origin')
ax.plot(walk_x[-1],walk_y[-1], 'o', label='final step')
edgecolor='black', clip_on=False)
ax.add_patch(circ)
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
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.
M +=1
N = 0
walk_x = [0]
walk_y = [0]
stuck = False
t1= time.time()
#print(M)
###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)
7
N += 1
else:
continue
if stuck == True:
M -= 1
continue
8
9