0

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()
Amardeep Mishra
  • 129
  • 1
  • 4
  • 16

1 Answers1

0

Your error is here:

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[1])     <<<<<< HERE
    #print(t)
return x,t,u

And same here:

 r=ode(f[0]).set_integrator('dopri5',method='bdf')

You try to access a function object like an array. What you probably want is to access the return value like an array:

f(p1, p2, p3)[0] 
rbf
  • 541
  • 4
  • 16
  • I am afraid, thats not the case, the error is in line- r=ode(f[0]).set_integrator('dopri5',method='bdf'). Because I am getting the very same error even after modifying the code like- u.append(f(r.t,[r.y[0],r.y[1]],param)[1]) – Amardeep Mishra Feb 13 '18 at 05:43
  • Yes, same mistake there. I overlooked the usage of "f" in that line. – rbf Feb 13 '18 at 05:46
  • Hi @Florian! I dont think I cant pass any arguments there in f in that line, dont you think? – Amardeep Mishra Feb 13 '18 at 05:50
  • But you have to, because the function expects arguments – rbf Feb 13 '18 at 05:51
  • ok, when I pass initial values to f() in that line, I still get the error-_dop.error: failed in processing argument list for call-back fcn. – Amardeep Mishra Feb 13 '18 at 05:53
  • Please provide a backtrace – rbf Feb 13 '18 at 05:54
  • Hi @Florian, I have modified the code, and upon running it I am getting that error which I posted in my last comment. Also the error seems to be coming from the line, r.integrate(r.t+dt). _dop.error: failed in processing argument list for call-back fcn. – Amardeep Mishra Feb 13 '18 at 06:00
  • 1.) your error description is not a traceback. 2.) The error you are posting seem to point to an invalid usage of the ode function. Something different from the first error – rbf Feb 13 '18 at 06:00
  • Traceback (most recent call last): File "cont_inside_f.py", line 33, in x,t,u=solver(0,1e-2,10,[0,0],[a,eta,k,lam]) File "cont_inside_f.py", line 23, in solver r.integrate(r.t+dt) File "/usr/local/lib/python2.7/dist-packages/scipy/integrate/_ode.py", line 408, in integrate self.f_params, self.jac_params) File "/usr/local/lib/python2.7/dist-packages/scipy/integrate/_ode.py", line 1032, in run tuple(self.call_args) + (f_params,))) _dop.error: failed in processing argument list for call-back fcn. – Amardeep Mishra Feb 13 '18 at 06:03
  • That's what I assumed. The initial error, that you described is fixed and now you are stumbling into the next one. Just a normal development process. Actually I can't help you with the usage of the ode function. If you need more information about it, you might want to read the documentation about ode and what parameters this object expects. – rbf Feb 13 '18 at 06:08