我模拟二维随机游走,与方向0 <θ<2π和T = 1000步。 我已经有这模拟的是单走,重复它的12倍,并节省了每次运行到顺序命名文本文件的代码:
a=np.zeros((1000,2), dtype=np.float)
print a # Prints array with zeros as entries
# Single random walk
def randwalk(x,y): # Defines the randwalk function
theta=2*math.pi*rd.rand()
x+=math.cos(theta);
y+=math.sin(theta);
return (x,y) # Function returns new (x,y) coordinates
x, y = 0., 0. # Starting point is the origin
for i in range(1000): # Walk contains 1000 steps
x, y = randwalk(x,y)
a[i,:] = x, y # Replaces entries of a with (x,y) coordinates
# Repeating random walk 12 times
fn_base = "random_walk_%i.txt" # Saves each run to sequentially named .txt
for j in range(12):
rd.seed() # Uses different random seed for every run
x, y = 0., 0.
for i in range(1000):
x, y = randwalk(x,y)
a[i,:] = x, y
fn = fn_base % j # Allocates fn to the numbered file
np.savetxt(fn, a) # Saves run data to appropriate text file
现在,我想对所有12个阶层来计算均方位移。 要做到这一点,我最初的想法是,每个文本文件中的数据导入回到一个numpy的阵列,例如:
infile="random_walk_0.txt"
rw0dat=np.genfromtxt(infile)
print rw0dat
然后以某种方式操纵阵列发现的均方位移。
有没有一种更有效的方式去与我有什么发现的MSD?