2 방향 랜덤 워크를 시뮬레이션 중입니다. < θ < 2π 및 T = 1000 단계. 파이썬에서 2 차원 무작위 걸음의 평균 제곱 변위 계산
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 개의 볼넷을 통해 평균 제곱 변위를 계산하려면 : 이미 하나의 거리를 시뮬레이션 그것을 12 번 반복하고, 각 순차적라는 이름의 텍스트 파일로 실행 저장하는 코드가 있습니다. 이렇게하려면 내 초기 생각은 다시 NumPy와 배열 등으로 각 텍스트 파일에서 데이터를 가져올 수 있었다 :
infile="random_walk_0.txt"
rw0dat=np.genfromtxt(infile)
print rw0dat
그리고 어떻게 든 평균 제곱 변위를 찾기 위해 배열을 조작 할 수 있습니다.
내가 갖고있는 것으로 MSD를 찾는 더 효율적인 방법이 있습니까?
왜 텍스트 파일로 저장하고 다시 가져와야합니까? 스크립트에서이 작업을 수행 할 수 있습니까? – heidi
@heidi 그것이 작업을 위해 수행해야하는 작업입니다. 모든 실행을 위해 별도의 텍스트 파일이 있어야합니다. –