问题#1A。 [5分]假设蚂蚁采取步骤(X,Y),每秒一个,其中,在每个步骤蚂蚁,x和y来自正态分布为0的平均值和为1.0mm的标准偏差随机漫步(假设这下面所有的问题)。绘制蚂蚁的路径的跟踪了一个多小时的课程。
mean = 0
sd = 1.0
side = np.random.normal()
step = np.random.normal()
for side, step in np.random.normal(1,3600): #3600 seconds in one hour
side += 1
step += 1
x.append(side) #one step in x direction
y.append(step) #one step in y direction
plt.plot(x,y,color = "darkpink")
有与您发布的代码服务器的问题,
np.random.normal
你会看到,前两个位置参数都为平均值和标准偏差。第三个参数实际上是形状,这可以作为一个关键字参数访问。norm(0,1)
你会仅仅指刚要转变平均从0到1个norm(1,1)
。码:
import numpy as np
import matplotlib.pyplot as plt
path = steps = np.random.normal(size=(3600,2))
# pos(n) = pos(n-1) + step(n)
for n in range(path.shape[0]-1):
path[n+1] += path[n]
# Compact way to plot x and y: (3600,2) -> (2,3600) and the * expand along the first axis
plt.plot(*path.T)
plt.show()