python-3.x - 使用 scipy.integrate.solve_bvp 将初始值问题调整为边界值问题?
问题描述
我想将初始值问题 ( IVP ) 调整为使用scipy.integrate.solve_bvp
. 此处提出了类似的问题,但我没有遵循答案中解释的所有内容。下面关于SIR 模型的示例取自本网站。这里,初始条件y0
取为S
,I
和R
的初始值x0[0]
。这个 ODE 系统由下面的函数给出,它在从到的区间内SIR
返回。[dS/dt, dI/dt, dR/dt]
x[0]
x[-1]
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import solve_ivp, solve_bvp
def SIR(t, y, prms):
S = y[0]
I = y[1]
R = y[2]
beta, gamma = prms
# return [dS/dt, dI/dt, dR/dt]
return np.array([-beta * S * I, beta * S * I - gamma * I, gamma * I])
infected = np.array([1, 3, 6, 25, 73, 222, 294, 258, 237, 191, 125, 69, 27, 11, 4])
xt = np.arange(infected.size).astype(int)
xw = 0.2 # spacing between points on x-axis (elapsed time)
t_eval = np.arange(xt[0], xt[-1]+xw, xw)
x0 = [xt[0], xt[-1]]
y0 = [762, 1, 0] # S0, I0, R0, beginning of outbreak
N = sum(y0) # population total
prms = [0.01,0.1] # beta, gamma
sol = solve_ivp(SIR, x0, y0, method='LSODA', t_eval=t_eval, args=(prms,))
fig, ax = plt.subplots()
ax.plot(sol.t, sol.y[0], label='S')
ax.plot(sol.t, sol.y[1], label='I')
ax.plot(sol.t, sol.y[2], label='R')
ax.plot(xt, infected, label='OG data')
ax.grid(color='k', linestyle=':', alpha=0.3)
fig.legend(loc='lower center', ncol=4, mode='expand')
plt.show()
plt.close(fig)
作为健全性检查,运行上面的代码会产生下图:
现在,假设我想添加另一个边界条件 - 比如说x1
和y1
- 在x0[-1]
.
y0 = [0, 200, N-200] # S1, I1, R1, end of graph of outbreak; values from eye-ing the graph # N-200 \approx 550
从 的文档solve_bvp
看来,它bc
必须是可调用的边界条件。和的其他参数solve_ivp
也solve_bvp
显得不同。我如何使用这个玩具示例以这种方式解决 BVP?
解决方案
推荐阅读
- authentication - Auth0 - 使用动态回调的 OAuth 登录
- forms - 发布请求后找不到 Flask 页面
- amazon-dynamodb - 按字符串数组搜索 DynamoDB 字符串集属性
- function - 如何在 foreach 并行循环中调用函数
- terraform - ECS集群基于CPU/内存的扩缩容任务
- certbot - Certbot 忽略 webroot 文件夹
- javascript - 在 ReactJS 中从 AD 合作伙伴加载外部脚本
- react-native - 禁用“捕捉”到起始位置滚动视图反应本机
- python - 添加基于现有多个列的新列意外地创建了一列列表
- python - 在线图上创建密度条