2

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.

6
  • 1
    You don't need to import numpy in all of your functions, you've already imported it at the module level. Commented Feb 6, 2017 at 0:44
  • 2
    Please include your entire stack trace in your question. Commented Feb 6, 2017 at 0:45
  • That's a lot of dense code. We really need to see the full stack trace, you can put it in a code block to preserve formatting. And your question would be much better if the code was a minimal reproducible example that focused on your problem. Commented Feb 6, 2017 at 1:04
  • 1
    You really should look for obvious typos yourself. Look for this bit *np.cos* in your code. Doesn't look right, does it? And if you look at the break from the line before to that line you'll find closing parentheses immediately followed by opening ones. What does that suggest to you? Commented Feb 6, 2017 at 1:23
  • 1
    Break your code up into little pieces and make sure each little piece works. Break your complicated formulas into simpler terms. Assign variable names to the simpler terms and express the complicated formula in terms of those variables. Write a simple example that runs, and then modify it incrementally (checking that it runs after each modification). By doing so, you'll avoid many of the errors you are currently seeing. Commented Feb 6, 2017 at 1:37

1 Answer 1

1

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`.

Sign up to request clarification or add additional context in comments.

1 Comment

You were absolutely correct. Although there was one other typo, the largest problem was my negligence of a + at the beginning of the second line of dy[3].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.