I am trying to pass an array into an ODE and then solve that ODE using the ODEINT function. However, I get the following error:
RuntimeError: The size of the array returned by func (50) does not match the size of y0 (1)
Here is my sample code:
from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt
#First ODE
def eq1 (x,t):
return (0.5*(x)+2*x**2)
#Second ODE
def eq2 (y,t,m):
p = np.sqrt(y)+m*y**2
return p
t = np.linspace(0,1,50)
IC = [2] # Initial Condition
m = [0.1]*len(t) # A random variable
#Solver for the first ODE
one = odeint(eq1,IC,t)
plt.plot (t,one)
#Solver for the Second ODE
two = odeint (eq2,IC,t,(m,))
plt.plot (t,two)
The first solver works fine, but the second one throws an error. Based on the error message, I understand that the dimension of the IC is not same as that of the variable m. Hence, if I comment out the lines related to the solver of the first ODE and change IC as IC = [2]*len(m)
, the error message goes away. How3ever, two.shape
is (50,50). I do not know which dimension of the solution (two) is the actual answer.
I would really appreciate some guidance. Thank You!
To my knowledge there is no way to pass an array as a parameter for an ODE function in Python. Funnily enough I think it's possible in Matlab. The only way to solve your problem (but inefficiently) is to use a FOR loop.