Stack Exchange
IndexError: index 2 is out of bounds for axis 0 with size 2
Im trying to solve 3 differential equations,but I keep getting the following error:
This is my code:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
T=np.arange(0, 7, 1e-4)

k1=0.92
k2=0.47
k3=0.25
m = 50
def SistemaEqDif(Y,t):
    s=Y[0]
    b=Y[1]
    l=Y[2]
    dEdt = -k1*s
    dCdt = (k1/(0.05*m))-k2*b
    dldt = k2*b-k3*l
    return [dEdt, dCdt,dldt]
C0 = 0
E0 = 1 
Y0 = [E0, C0]
Y = odeint(SistemaEqDif,Y0,T)
plt.plot(T,Y[:,0],'g')
plt.plot(T,Y[:,1],'r')
plt.plot(T,Y[:,2],'b')

And this is the error:
File "/Users/arihalpern/untitled29.py", line 21, in SistemaEqDif
    l=Y[2]
IndexError: index 2 is out of bounds for axis 0 with size 2


The length of Y is 2 not 3.

how can I correct this?

You have Y0 = [E0, C0].  If the system is three-dimensional, then Y0 must have three values, so something like Y0 = [E0, C0, l0] where l0 is the initial condition for l(t).

Thank you so much! It worked

(Answer courtsey of Warren Weckesser, provided in the comments to the question.)
You have Y0 = [E0, C0]. If the system is three-dimensional, then Y0 must have three values, so something like Y0 = [E0, C0, l0] where l0 is the initial condition for l(t).
