Skip to main content
 首页 » 编程设计

python之继续运行到 "TypeError: ' numpy.float6 4' object is not callable"

2025年02月15日14kevingrace

一直报错

TypeError: 'numpy.float64' object is not callable"

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

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。我正在将这些函数设计成一个小型函数库,以供稍后用作示例。

请您参考如下方法:

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

错误意味着某些代码正在尝试执行类似 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() 评估为一个数字,接下来的 (metata...) 看起来像是一个函数调用。如果我删除 dy[3] 的最后两行,它就会运行。该行 continuation 处缺少某些内容。