【问题标题】:How to find the value of an ODE system at a specific time如何在特定时间找到 ODE 系统的值
【发布时间】: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


    【解决方案1】:

    您只需要为活着的人口执行S[4],为僵尸人口执行Z[4]SZ 是在每次求解 ODE 系统 t 后这些变量的近似值。

    请记住,值可能不是int,因此解决方案可能对物理问题没有意义:

    print(S[4])
    499.99899899996575
    print(Z[4])
    1.036906736082604e-09
    

    你可能会想到,第 4 天有一个人变成了僵尸,但还没有完全变身。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 2021-12-18
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2020-04-24
      • 2017-08-16
      相关资源
      最近更新 更多