我正在学习如何在python上编写代码,并且我正在试图弄清楚如何在特定时间从ODE系统中找到解决方案的总和。
例如,这是来自SciPy Cookbook的例子被称为“为僵尸启示录建模”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天的活着的人口和僵尸人口的总和)。有什么办法可以做到这一点吗?
发布于 2020-03-27 02:58:17
你只需要为活体群体做S[4]
,为僵尸群体做Z[4]
。S
和Z
是求解每个时刻t
的常微分方程组后这些变量的近似值。
请记住,值可能不是int
,因此解决方案可能对物理问题没有意义:
print(S[4])
499.99899899996575
print(Z[4])
1.036906736082604e-09
你可能会想到,在第4天,有一个人变成了僵尸,但还没有完全变成僵尸。
希望这能有所帮助
https://stackoverflow.com/questions/60879019
复制相似问题