【发布时间】:2020-07-07 18:43:25
【问题描述】:
我正在学习如何在 python 上编写代码,并且试图弄清楚如何在特定时间从 ODE 系统中找到解决方案的总和。
例如,这是来自 SciPy Cookbook 的示例,名为“Modelling a Zombie Apocalypse”https://scipy-cookbook.readthedocs.io/items/Zombie_Apocalypse_ODEINT.html
这是来自网站的部分代码:
# 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, 5., 1000) # time grid
# solve the DEs
soln = odeint(f, y0, t)
S = soln[:, 0]
Z = soln[:, 1]
R = soln[:, 2]
# plot results
plt.figure()
plt.plot(t, S, label='Living')
plt.plot(t, Z, label='Zombies')
plt.xlabel('Days from outbreak')
plt.ylabel('Population')
plt.title('Zombie Apocalypse - No Init. Dead Pop.; No New Births.')
plt.legend(loc=0)
从这个模型中,假设我想知道在 4 天的时间里有多少僵尸和人活着(即:4 天的时候活着的人口和僵尸人口的总和)。有没有办法我可以做到这一点?
【问题讨论】:
标签: python python-3.x math scipy