代码3

作者: xuqiuhao | 来源:发表于2017-01-02 13:58 被阅读0次
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('<x^2>')
plt.title('<x^2> of 500 walkers')

plt.show()

相关文章

网友评论

      本文标题:代码3

      本文链接:https://www.haomeiwen.com/subject/aduwvttx.html