首页 > 解决方案 > 使用 scipy.integrate.solve_bvp 将初始值问题调整为边界值问题?

问题描述

我想将初始值问题 ( IVP ) 调整使用scipy.integrate.solve_bvp. 此处提出了类似的问题,但我没有遵循答案中解释的所有内容。下面关于SIR 模型的示例取自本网站。这里,初始条件y0取为S,IR的初始值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)

作为健全性检查,运行上面的代码会产生下图:

IVP 示例

现在,假设我想添加另一个边界条件 - 比如说x1y1- 在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_ivpsolve_bvp显得不同。我如何使用这个玩具示例以这种方式解决 BVP?

标签: python-3.xscipyodedifferential-equationsboundary

解决方案


推荐阅读