![](/img/trans.png)
[英]Scipy odeint - different values of arguments in each timestep tx
[英]odeint: Variable timestep for mathematical modelling
我以Sci Py僵尸代码为例。
ODEint的问题是我要进行长时间集成(在这种情况下为0到500,但对于长达数千年的其他项目)则是一次性集成。 我想实现一个较长的输出时间尺度,但要实现一个较小的积分时间尺度,并将生成的样本数更改为1,但要不断更新该时间尺度,以便可以在每个循环中将新点写入文件。 也就是说,我不希望它一次完成从0到500的积分,而是在变化的时间范围内循环。 可以实施吗?
# zombie apocalypse modeling
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
plt.ion()
plt.rcParams['figure.figsize'] = 10, 8
P = 0 # birth rate
d = 0.0001 # natural death percent (per day)
B = 0.0095 # transmission percent (per day)
G = 0.0001 # resurect percent (per day)
A = 0.0001 # destroy percent (per day)
# solve the system dy/dt = f(y, t)
def f(y, t):
Si = y[0]
Zi = y[1]
Ri = y[2]
# the model equations (see Munz et al. 2009)
f0 = P - B*Si*Zi - d*Si
f1 = B*Si*Zi + G*Ri - A*Si*Zi
f2 = d*Si + A*Si*Zi - G*Ri
return [f0, f1, f2]
# initial conditions
S0 = 500. # initial population
Z0 = 0 # initial zombie population
R0 = 0 # initial death population
y0 = [S0, Z0, R0] # initial condition vector
t = np.linspace(0, 50, 1000) # time grid
# solve the DEs
soln = odeint(f, y0, t)
S = soln[:, 0]
Z = soln[:, 1]
R = soln[:, 2]
为什么不这样做呢?
for k in range(50):
t = np.linspace(k, k+1, 20)
sol = odeint(f, y0, t)
y0 = sol[-1] #initial value for next segment
S,Z,R = sol.T
# output to file, other post-processing
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.