import numpy as np import matplotlib.pyplot as plt Nsteps = 10000 x0 = 0 y0 = 0 # Question 1 def randomWalk(x0, y0, Nsteps): x = [x0] y = [y0] for i in range(Nsteps): R = 1 theta = 2*np.pi*np.random.random() x0 += R*np.cos(theta) y0 += R*np.sin(theta) x.append(x0) y.append(y0) return [x, y] # Question 2 rwx1, rwy1 = randomWalk(x0, y0, Nsteps) plt.plot(rwx1, rwy1) rwx2, rwy2 = randomWalk(x0, y0, Nsteps) plt.plot(rwx2, rwy2) plt.xlabel('X') plt.ylabel('Y') plt.savefig('randomW.png') plt.close() # Question 3 def levyFlight(x0, y0, Nsteps): x = [x0] y = [y0] for i in range(Nsteps): R = np.random.pareto(2) theta = 2*np.pi*np.random.random() x0 += R*np.cos(theta) y0 += R*np.sin(theta) x.append(x0) y.append(y0) return [x, y] lfx1, lfy1 = levyFlight(x0, y0, Nsteps) plt.plot(lfx1, lfy1) lfx2, lfy2 = levyFlight(x0, y0, Nsteps) plt.plot(lfx2, lfy2) plt.xlabel('X') plt.ylabel('Y') plt.savefig('levy.png') plt.close() # Question 4 def stats(x, y): x0 = x[0] y0 = y[0] rmed = [] for i in range(len(x)): rmed.append(np.sqrt((x[i]-x[0])**2+(y[i]-y[0])**2)) return rmed rmed1 = stats(rwx1, rwy1) rmed2 = stats(lfx1, lfy1) plt.plot(rmed1) plt.plot(rmed2) plt.ylabel('||r(t)-r(0)||') plt.xlabel('temps') plt.savefig('rmed.png') plt.close()