我正在使用以下代碼來模擬一組粒子的運動,其中一個引數p
確定給定粒子移動與否的概率,并生成一個影片圖:
# Compara??o entre random walk e difus?o em 1d
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.patches as mpatches
import random
M = 100 # Número de walkers
L = 50 # Tamanho da malha
# A cada intervalo de tempo, mover o walker e propagar a difus?o
p = 0.22 # Probabilidade de andar, difusividade em μm2/s
pinv = 1.0-p
nsteps = 2001 # Número de intervalos de tempo
# Iniciando os walkers
x = np.zeros(M) # Posi??o inicial dos walkers nos eixos x, y e z
Z = [(0,0,0) for i in range (M)]
edgesrw = np.array(range(-L,L 1))-0.5
xc = 0.5*(edgesrw[:-1] edgesrw[1:])
#%%
def animate(it):
global x
x = get_data(Z, M)
# Trajetória dos walkers nos eixos x, y e z
if (np.mod(it,noutput)==0):
A = np.float64(Z)
plot._offsets3d = (A[:,0], A[:,1], A[:,2])
ax.set_title('Tempo = {}, p = {}'.format(it, str(round(p, 4))))
return plot
def get_data(Z, M):
# Atualizar a posi??o de todos os walkers
for iw in range(M):
rndx = random.random()
dx = -1*(rndx<p) 1*(rndx>pinv)
rndy = random.random()
dy = -1*(rndy<p) 1*(rndy>pinv)
rndz = random.random()
dz = -1*(rndz<p) 1*(rndz>pinv)
x, y, z = Z[iw]
Z[iw] = x dx, y dy, z dz
return Z
plt.ion()
noutput = 5
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
fig.set_size_inches(6, 6)
ax.set_xlim((-50, 50))
ax.set_ylim((-50, 50))
ax.set_zlim((-50, 50))
ax.set_xlabel('Distancia percorrida (x)')
ax.set_ylabel('Distancia percorrida (y)')
ax.set_zlabel('Distancia percorrida (z)')
subs1 = mpatches.Patch(color = 'blue', label = "Ca2\u207A")
ax.legend(handles = [subs1])
x = get_data(Z, M)
plot = ax.scatter (*zip(*Z), marker = 'o', s = 3, color = 'blue')
ani = animation.FuncAnimation(fig = fig, func = animate, frames = nsteps, interval = 50)
ani.save('íons Ca2 , Dab constante.gif')
plt.show()
現在,p
是一個常數值。這段代碼給了我以下結果:
我現在想要做的是,而不是總是將p
其用作常數值,而是使用以下等式隨時間更新它:
其中 p0 是之前的常數值,t 是時間(由it
代碼中的引數計算),alpha 是另一個常數值。我知道當 t 等于 0 時,這個等式不起作用,在這種情況下,我會認為 p 等于 p0。
因此,我定義了p0
and alpha
,以及將驗證是否it
等于零的條件,然后決定要做什么:
# Compara??o entre random walk e difus?o em 1d
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.patches as mpatches
import random
M = 100 # Número de walkers
L = 50 # Tamanho da malha
# A cada intervalo de tempo, mover o walker e propagar a difus?o
p = 0.22 # Probabilidade de andar, difusividade em μm2/s
p0 = 0.22
pinv = 1.0-p
nsteps = 2001 # Número de intervalos de tempo
alpha = 0.76 # Slope da curva experimental
# Iniciando os walkers
x = np.zeros(M) # Posi??o inicial dos walkers nos eixos x, y e z
Z = [(0,0,0) for i in range (M)]
edgesrw = np.array(range(-L,L 1))-0.5
xc = 0.5*(edgesrw[:-1] edgesrw[1:])
#%%
def animate(it):
global x
x = get_data(Z, M)
# Trajetória dos walkers nos eixos x, y e z
if (np.mod(it,noutput)==0):
if it == 0:
p = p0
else:
p = p0*it**(alpha-1)
pinv = 1.0-p
A = np.float64(Z)
plot._offsets3d = (A[:,0], A[:,1], A[:,2])
ax.set_title('Tempo = {}, p = {}'.format(it, str(round(p, 4))))
return plot
def get_data(Z, M):
# Atualizar a posi??o de todos os walkers
for iw in range(M):
rndx = random.random()
dx = -1*(rndx<p) 1*(rndx>pinv)
rndy = random.random()
dy = -1*(rndy<p) 1*(rndy>pinv)
rndz = random.random()
dz = -1*(rndz<p) 1*(rndz>pinv)
x, y, z = Z[iw]
Z[iw] = x dx, y dy, z dz
return Z
plt.ion()
noutput = 5
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
fig.set_size_inches(6, 6)
ax.set_xlim((-50, 50))
ax.set_ylim((-50, 50))
ax.set_zlim((-50, 50))
ax.set_xlabel('Distancia percorrida (x)')
ax.set_ylabel('Distancia percorrida (y)')
ax.set_zlabel('Distancia percorrida (z)')
subs1 = mpatches.Patch(color = 'blue', label = "Ca2\u207A")
ax.legend(handles = [subs1])
x = get_data(Z, M)
plot = ax.scatter (*zip(*Z), marker = 'o', s = 3, color = 'blue')
ani = animation.FuncAnimation(fig = fig, func = animate, frames = nsteps, interval = 50)
ani.save('íons Ca2 , alpha = {}.gif'.format(alpha))
plt.show()
但我很確定這個位置是錯誤的,因為我沒有得到預期的結果(我預計粒子的整體傳播會減少,因為p
根據等式應該隨著時間的推移而減少)。
uj5u.com熱心網友回復:
函式內部的 變數p
和函式是本地的:這意味著您為該函式計算的值和函式內部的值不會在全域范圍內“共享”。因此,每次從內部呼叫時,您都在計算具有初始全域值和的新資料。pinv
animate
animate
p
pinv
get_data(Z, M)
animate
p
pinv
在這里,我進行了修改get_data
以接收更新的值。另請注意,我已更改內部命令的順序animate
:
# Compara??o entre random walk e difus?o em 1d
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.patches as mpatches
import random
M = 100 # Número de walkers
L = 50 # Tamanho da malha
# A cada intervalo de tempo, mover o walker e propagar a difus?o
p = 0.22 # Probabilidade de andar, difusividade em μm2/s
p0 = 0.22
pinv = 1.0-p
nsteps = 2001 # Número de intervalos de tempo
alpha = 0.76 # Slope da curva experimental
# Iniciando os walkers
x = np.zeros(M) # Posi??o inicial dos walkers nos eixos x, y e z
Z = [(0,0,0) for i in range (M)]
edgesrw = np.array(range(-L,L 1))-0.5
xc = 0.5*(edgesrw[:-1] edgesrw[1:])
#%%
def animate(it):
global x
# Trajetória dos walkers nos eixos x, y e z
if (np.mod(it,noutput)==0):
if it == 0:
p = p0
else:
p = p0*it**(alpha-1)
pinv = 1.0-p
x = get_data(Z, M, p, pinv)
A = np.float64(Z)
plot._offsets3d = (A[:,0], A[:,1], A[:,2])
ax.set_title('Tempo = {}, p = {}'.format(it, str(round(p, 4))))
def get_data(Z, M, p, pinv):
# Atualizar a posi??o de todos os walkers
for iw in range(M):
rndx = random.random()
dx = -1*(rndx<p) 1*(rndx>pinv)
rndy = random.random()
dy = -1*(rndy<p) 1*(rndy>pinv)
rndz = random.random()
dz = -1*(rndz<p) 1*(rndz>pinv)
x, y, z = Z[iw]
Z[iw] = x dx, y dy, z dz
return Z
plt.ion()
noutput = 5
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
fig.set_size_inches(6, 6)
ax.set_xlim((-50, 50))
ax.set_ylim((-50, 50))
ax.set_zlim((-50, 50))
ax.set_xlabel('Distancia percorrida (x)')
ax.set_ylabel('Distancia percorrida (y)')
ax.set_zlabel('Distancia percorrida (z)')
subs1 = mpatches.Patch(color = 'blue', label = "Ca2\u207A")
ax.legend(handles = [subs1])
x = get_data(Z, M, p, pinv)
plot = ax.scatter (*zip(*Z), marker = 'o', s = 3, color = 'blue')
ani = animation.FuncAnimation(fig = fig, func = animate, frames = nsteps, interval = 50)
# ani.save('íons Ca2 , alpha = {}.gif'.format(alpha))
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/472005.html
標籤:Python matplotlib 动画片 粒子 粒子系统
上一篇:移動球體影片
下一篇:圍繞矩形路徑回圈SVG文本影片