用Python设置布朗粒子运动的动画?

2024-06-26 10:46:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用python创建一个Langevin模拟。目前,我有一个代码,可以根据Langevin方程更新单个点的x和y坐标,并在两个数组(x数组和y数组)中返回所有这些位置。我想将这些位置变化绘制为移动散点图,以显示粒子如何随时间移动。我该怎么做?以下是我目前的情况:

# IMPORT STATEMENTS

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation



# CONSTANTS

v = 3.12e-5                # swimming speed of B. Subtilis [m/s]
M = 1                      # moment of B. Subtilis 
k = 1.38e-23               # Boltzmann constant [m^2kg/s^2K]
T = 293                    # Room temperature [K]
eta = 0.1                  # viscosity of water [Pa s]
a = 2e-6                   # spherical cell radius [m]
Dr = k*T/8*np.pi*eta*a**3   # rotational diffusion coefficient of B. Subtilis
 

# ADJUSTABLE PARAMETERS

B = 1       # strength of the magnetic field [T]
t = 100     # time over which motion is observed [s]
dt = 1      # time step between recorded positions
N = 1000    # number of cells 
 
#INITIAL CONDITIONS

theta_i = 0   # initial swimming orientation [radians]
xi = 0.001  # initial x position [m]
yi = 0.001 # initial y position [m]
x = []
y = [] 



# MAIN SCRIPT

for i in range (0,t,dt): 
    theta_j = (theta_i + M*B*np.sin(theta) + np.sqrt(2*Dr)*ksi)*dt
    xj = (xi + v*np.cos(theta))*dt
    yj = (yi + v*np.sin(theta))*dt 
    x.append(xj)
    y.append(yj)
    theta_i = theta_j
    xi = xj
    yi = yj 

Tags: ofimportmatplotlibasnpdt数组initial