继续运行“TypeError:'numpy.float64'对象不可调用”

时间:2017-02-06 00:39:59

标签: python numpy scipy

我一直收到错误

  

TypeError:'numpy.float64'对象不可调用“

我以为我一直小心不要在函数等中重复变量,但每当我尝试执行时,这个错误就会一直弹出

scipy.integrate.odeint(doublepend,fnaught,t,args=(L,9.81,M))

我不确定发生了什么。请参阅下面的其余代码。

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))

我意识到我不需要多次导入numpy。我正在设计这些函数,使其成为一个小函数库,以后将作为示例使用。

1 个答案:

答案 0 :(得分:1)

我们需要查看一些堆栈,但即便如此,可能很难确定哪个变量存在问题。

错误意味着某些代码正在尝试执行x(args)之类的操作,即使用x作为函数。但与期望相反x是一个数字,而不是一个函数。

解决方案是确定导致问题的变量,并将其追溯到您的代码。

指向odeint的输入看起来很好:

odeint(doublepend,fnaught,t,args=(L,9.81,M))

其中只有第一个,doublepend必须是一个函数。似乎是这样,但我会在odeint来电之前检查。

fnaught是初始条件。您似乎创建了一个值元组。我制作了一个数组,但我认为odeint会自动完成。

t是一个数组

args获得一组数字。这些内容会传递给您的doublepend

doublepend(fnaught, 0, L, 9.81, M)会运行吗?它又回归了什么?

另一个答案认为问题在于doublepend功能。堆栈跟踪可以清楚地表明,就像上面的测试计算一样。

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

好的 - 错误是在那个冗长而丑陋的dy[3]表达式中的某个地方。分解它,找出你在()使用数字而非功能的地方。

这看起来很可疑

np.cos(thetanaught-phinaught))))   (mtheta+mphi)

np.cos()评估为一个数字,下一个(methata...)看起来像一个函数调用。如果我删除dy[3]的最后两行,它就会运行。那条线延续了一些东西。