代码3

steps = np.linspace(0, 100, 101)
x2_ave = np.zeros(101)
x_y0 = np.zeros(101)
x_now = np.zeros(500)
x2_now = np.zeros(500)

for i in range(100):
    for j in range(500):
        ruler = np.random.rand()
        if ruler<=0.5:
            x_now[j] = x_now[j] + 1
        else:
            x_now[j] = x_now[j] - 1
        x2_now[j] = x_now[j]**2

    average2 = sum(x2_now)/500
    x2_ave[i+1] = average2
    
para = np.polyfit(steps, x2_ave,1)
poly = np.poly1d(para)
y_fit = poly(steps)

plt.scatter(steps, x2_ave,s=2)
plt.plot(steps, y_fit, 'r', label = 'fit line')
plt.legend(loc='upper left')

plt.xlim(0,100)
plt.ylim(0,100)
plt.grid(True)
plt.xlabel('step number(= time)')
plt.ylabel('')
plt.title(' of 500 walkers')

plt.show()

你可能感兴趣的:(代码3)