繁体   English   中英

python 2D中的布朗运动

[英]Brownian motion in python 2D

我想创建一个布朗运动模拟我的粒子将从 (0,0),原点开始,然后我为 x 和 y 方向创建了 NumPy 随机数组,例如,x = [-2,1,3] 和y = [0,-2,1]。 由于粒子从原点(第 0 个点)开始,它将在方向上以 -2 和 y(第 1 个点)为 0 到下一个点,然后对于第 2 个点,它将向右移动 1 个单位(+ x 中的 1)和左侧的 -2 个单位(y 中的 -2)。 我的问题是我将如何做到让每个点都充当新的原点,就像添加向量一样。 我想我需要某种 for 循环,但不确定我将如何设置它。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation as am
np.random.seed(69420)
N=1000 #Number of steps
# Have the particle at the origin in 2D (x,y) plane
# Each step will have a lenght of 1 unit. Lets call it meters
# Each step is one second
# And this simulation will run for 100 seconds
## Now how to make a particle randomly walk???
t=100 # time interval
dt=t/N # delta time from each step
dx = np.random.randint(-5,5,N,dtype=int)# This would be our detla x for each step
dy = np.random.randint(-5,5,N,dtype=int)
dx_0 = np.zeros(N, dtype=int)
dy_0 = np.zeros(N,dtype=int)
X = dx_0+dx
Y = dy_0+dy
print(X)
xdis = np.cumsum(X)  #Total distance travled after N steps
ydis = np.cumsum(Y)
plt.plot(dx_0,dy_0,'ko')
plt.plot(xdis,ydis,'b-')
plt.show()

这是我到目前为止。 任何帮助表示赞赏。

您需要从(0,0)开始执行N步。 使用变量prev = [0,0]跟踪您的步骤,并对每个步骤进行更改。 简化代码:

prev = [0,0]
for i in range(N):
    new_x = prev[0]+dx[i]
    new_y = prev[1]+dy[i]
    # do what you need with new_x and new_y
    prev = [new_x, new_y]

既然您似乎希望绘制整个步行的图表,那么只需总结所有这些步骤?

origin = np.array([0, 0])

x_series = np.cumsum(dx) + origin[0]

# (Similarly for y)

这仅适用于 1 个随机游走者。 如果您有多个随机游走者 - 他们每个人都会有一个起点和单独的dx -s 链。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM