我以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]发布于 2018-03-28 06:06:35
为什么不直接这么做呢?
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-processinghttps://stackoverflow.com/questions/49524297
复制相似问题