Adapting initial-value problem to boundary-value problem using scipy.integrate.solve_bvp?

≡放荡痞女 提交于 2020-04-18 06:32:12

问题


I would like to adapt an initial-value-problem (IVP) to a boundary-value-problem (BVP) using scipy.integrate.solve_bvp. A similar question was asked here, but I do not follow everything explained in the answer. The example below regarding the SIR model was taken from this website. Here, the initial condition y0 is taken to be the initial value of S, I, and R at time x0[0]. This system of ODEs is given by the function SIR below, which returns [dS/dt, dI/dt, dR/dt] over the interval from x[0] to 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)

As a sanity-check, running the code above produces the figure below:

Now, suppose I would like to add another boundary condition - say x1 and y1 - to be evaluated at 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

From the documentation of solve_bvp, it appears that bc must be callable boundary conditions. The other parameters of solve_ivp and solve_bvp also appear different. How can I use this toy-example to solve a BVP in this way?

来源:https://stackoverflow.com/questions/60924050/adapting-initial-value-problem-to-boundary-value-problem-using-scipy-integrate-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!