Runtime error with odeint when using scipy

85 Views Asked by At

I'm getting a runtime error when trying to solve the following coupled ODEs. I understand what the error is asking, which is if you look below: RuntimeError: The size of the array returned by func (6) does not match the size of y0 (7)

How do I fix this, my goal is to just return an array that contains solutions to the coupled OEDs.

We want to solve the following coupled ODE's,


$$\frac{dS}{dt} = \Lambda - \mu S(t) - \beta S(t) \frac{P(t)}{N(t)} - \zeta \lambda S(t)$$
$$\frac{dE}{dt} = \beta S(t) \frac{P(t)}{N(t)} + \beta V(t) \frac{P(t)}{N(t)} - (\mu + \epsilon) E(t)$$
$$\frac{dP}{dt} = \epsilon E(t) - \ (\gamma + \mu + \alpha) P(t)$$
$$\frac{dR}{dt} = \gamma P(t) + \zeta V(t) - \ \mu R(t)$$
$$\frac{dV}{dt} = \zeta \lambda S(t) - \beta V(t) \frac{P(t)}{N(t)} - \zeta V(t) - \mu V(t)$$
$$\frac{dD}{dt} = \alpha P(t)$$
$$\frac{dN}{dt} = \frac{dS}{dt} + \frac{dE}{dt} + \frac{dP}{dt} + \frac{dR}{dt} + \frac{dV}{dt} + \frac{dD}{dt}$$

import numpy as np
import matplotlib.pyplot as plt
import scipy as sp 
from scipy.integrate import odeint
from scipy.integrate import solve_ivp

def dSdx(x, S):
    y1, y2, y3, y4, y5, y6, y7 = S
    return [(1/967) - (1/480)*y1 - (1/800)*y1*(y3/y7) - (1/8338)*0.88*y1,
           
            1/800*y1*(y3/y7) + 1/800*y5*(y3/y7) - (1/480 + 1/7)*y2,
           
            1/7*y2 - (1/14 + 1/480 + 1/650)*y3,
            
            1/14*y3 + 1/8338*y5 - 1/480*y4,
           
            1/8338*0.88*y1 - 1/800*y5*(y3/y7) - 1/8338*y5 - 1/480*y5,
           
            1/658*y3]

y1_0 = 4389227
y2_0 = 6342
y3_0 = 9280016
y4_0 = 9038327
y5_0 = 11512
y6_0 = 21708016
y7_0 = 26096985
S_0 = (y1_0, y2_0, y3_0, y4_0, y5_0, y6_0, y7_0)

x = np.linspace(0, 1, 100)
sol = odeint(dSdx, y0=S_0, t=x, tfirst=True)
sol





RuntimeError                              Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12596/3129603718.py in <module>
     29 
     30 x = np.linspace(0, 1, 100)
---> 31 sol = odeint(dSdx, y0=S_0, t=x, tfirst=True)
     32 sol

~\anaconda3\lib\site-packages\scipy\integrate\odepack.py in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg, tfirst)
    239     t = copy(t)
    240     y0 = copy(y0)
--> 241     output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
    242                              full_output, rtol, atol, tcrit, h0, hmax, hmin,
    243                              ixpr, mxstep, mxhnil, mxordn, mxords,

RuntimeError: The size of the array returned by func (6) does not match the size of y0 (7).
```