Reputation: 143
I keep getting the error
TypeError: 'numpy.float64' object is not callable"
I thought I have been careful to not repeat variables within functions etc., but this error keeps popping up whenever I try to execute
scipy.integrate.odeint(doublepend,fnaught,t,args=(L,9.81,M))
I'm not sure what's going on. See below for the rest of the code.
import scipy as scipy
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import math as mt
import numpy as np
def singlepend(thetarray,time,Length,gravity,mass):
import numpy as np
theta,delatheta=y
dtheta=np.zeros_like(y)
#theta solved
dtheta[0]=deltatheta
dtheta[1]=(g/L)*np.sin(theta)
return dy
def doublepend(y,tim,Langles,g,Mangles):
import numpy as np
thetanaught,deltatheta,phinaught,deltaphi=y
mtheta,mphi=Mangles
print mtheta
Ltheta,Lphi=Langles
dy=np.zeros_like(y)
#theta solved
dy[0]=deltatheta
dy[1]=-mphi*np.sin(thetanaught-phinaught)*((deltatheta**2)*np.cos(thetanaught-phinaught)+(deltaphi**2)*Lphi/Lphi)/(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos(thetanaught-phinaught)))\
+mphi*g*np.sin(phinaught)*np.cos(thetanaught-phinaught)/(Ltheta*(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos(thetanaught-phinaught))))\
-(mtheta+mphi)*g*np.sin(thetanaught)/(Ltheta*(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos(thetanaught-phinaught))))
#phi solved
dy[2]=deltaphi
dy[3]=mphi*(deltaphi**2)*np.sin(thetanaught-phinaught)/(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos(thetanaught-phinaught)))\
+(mtheta+mphi)*g*np.sin(thetanaught)*np.cos(thetanaught-phinaught)/(Lphi*(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos(thetanaught-phinaught))))\
(mtheta+mphi)*Ltheta*(deltatheta**2)*np.sin(thetanaught-phinaught)/(Lphi*(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos*(thetanaught-phinaught))))\
-(mtheta+mphi)*g*np.sin(phinaught)/(Lphi*(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos(thetanaught-phinaught))))
return dy
#initial conditions
m1=1
m2=1
L1=1
L2=1
M=(m1,m2)
L=(L1,L2)
phi1naught=np.array(np.pi/2)
phi2naught=np.array(np.pi/2)
phi1dotnaught=np.array(0)
phi2dotnaught=np.array(0)
fnaught=(phi1naught,phi1dotnaught,phi2naught,phi2dotnaught)
t=np.linspace(0,20,100001)
f=scipy.integrate.odeint(doublepend,fnaught,t,args=(L,9.81,M))
I realize that I do not need to import numpy multiple times. I am designing those functions to become a small library of functions to be used later as examples.
Upvotes: 2
Views: 4849
Reputation: 231665
We need to see some of the stack, but even with that it may be hard to identify which variable is giving problems.
The error means that some code is trying to do something like x(args)
, i.e. use x
as a function. But contrary to expectations x
is a number, not a function.
The solution is to identify what variable is causing the problem, and trace that back to your code.
Offhand the inputs to odeint
look fine:
odeint(doublepend,fnaught,t,args=(L,9.81,M))
Of these only the first, doublepend
has to be a function. It appears to be that, but I'd couple check just before the odeint
call.
fnaught
is the initial condition. You appear to create a tuple of values. I'd make an array, but I think odeint
will do that automatically.
t
is an array
args
gets a tuple of numbers. Those get passed to your doublepend
.
Does doublepend(fnaught, 0, L, 9.81, M)
run? What does it return?
The other answer thinks the problem lies in that doublepend
function. A stack trace would make that clear, as would the above test calc.
In [13]: doublepend(fnaught, 0, L, 9.81, M)
1
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-f8aa70c3f962> in <module>()
----> 1 doublepend(fnaught, 0, L, 9.81, M)
<ipython-input-10-3c95347f392b> in doublepend(y, tim, Langles, g, Mangles)
13 #phi solved
14 dy[2]=deltaphi
---> 15 dy[3]=mphi*(deltaphi**2)*np.sin(thetanaught-phinaught)/(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos(thetanaught-phinaught))) +(mtheta+mphi)*g*np.sin(thetanaught)*np.cos(thetanaught-phinaught)/(Lphi*(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos(thetanaught-phinaught)))) (mtheta+mphi)*Ltheta*(deltatheta**2)*np.sin(thetanaught-phinaught)/(Lphi*(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos*(thetanaught-phinaught)))) -(mtheta+mphi)*g*np.sin(phinaught)/(Lphi*(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos(thetanaught-phinaught))))
16
17 return dy
TypeError: 'numpy.float64' object is not callable
OK - the error is somewhere in that long and ugly dy[3]
expression. Break it up, and figure out where you are using ()
for something that is a number rather than function.
This looks suspicious
np.cos(thetanaught-phinaught)))) (mtheta+mphi)
np.cos()
evaluates to a number, and the next (methata...)
looks like an function call. It runs if I delete the last 2 lines of dy[3]
. Something is missing at that line continuation`.
Upvotes: 1