After having searched couple other similar problems, I could not find a proper solution to my case. In my problem, function f returns a list and I wish to pass one element of this list to the ode solver. However when I do so, I am getting this error. Actually I need to plot the other element that function f throws. That is the reason why I have coded this way.
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import ode
from numpy import tanh,array,sin,cos
def f(t,Y,param):
x1,x2=Y[0],Y[1]
a,eta,k,lam=param[0],param[1],param[2],param[3]
e=x1-2
de=-2*x1+a*x2+sin(x1)
s=de+lam*e
u=(1/(a*cos(2*x1)))*(-eta*tanh(s)-k*s-(-2*x1+a*x2+sin(x1))*cos(x1)+2*(-2*x1+a*x2+sin(x1))+a*x2*cos(x1))
x1dot=-2*x1+a*x2+sin(x1)
x2dot=-x2*cos(x1)+cos(2*x1)*u
x=[x1dot,x2dot]
return [x,u]
def solver(t0,dt,t1,y0,param):
x,u=[[] for i in range(2)],[]
r=ode(f(t0,y0,param)[0]).set_integrator('dopri5',method='bdf')
r.set_initial_value(y0,t0).set_f_params(param)
while r.successful() and r.t<t1:
r.integrate(r.t+dt)
for i in range(2):
x[i].append(r.y[i])
t.append(r.t)
u.append(f(r.t,[r.y[0],r.y[1]],param)[1])
#print(t)
return x,t,u
if __name__=='__main__':
a,eta,k,lam=2,1.2,3,2
x,t,u=solver(0,1e-2,10,[0,0],[a,eta,k,lam])
for i in range(3):
if i!=2:
plt.subplot(3,1,i+1)
plt.plot(t,x[i])
else:
plt.subplot(3,1,i+1)
plt.plot(t,u)
plt.show()