odeint:数学建模的可变时间步长

时间:2018-03-28 00:26:22

标签: python python-3.x differential-equations odeint

我使用Sci Py zombie代码作为示例。

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]

1 个答案:

答案 0 :(得分:0)

为什么不这样做?

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