【发布时间】:2017-06-22 20:59:30
【问题描述】:
我不断收到错误
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。我正在将这些函数设计成一个小型函数库,以供以后用作示例。
【问题讨论】:
-
您不需要在所有函数中都导入 numpy,您已经在模块级别导入了它。
-
请在您的问题中包含您的整个堆栈跟踪。
-
那是很多密集的代码。我们确实需要查看完整的堆栈跟踪,您可以将其放在代码块中以保留格式。如果代码是专注于您的问题的minimal reproducible example,您的问题会好得多。
-
你真的应该自己寻找明显的错别字。在您的代码中查找此位
*np.cos*。看起来不太对,是吗?如果您查看从前一行到该行的中断,您会发现右括号紧跟在左括号之后。这对您有什么建议? -
将您的代码分解成小块,并确保每个小块都能正常工作。将复杂的公式分解为更简单的术语。将变量名称分配给更简单的术语,并根据这些变量来表达复杂的公式。编写一个运行的简单示例,然后逐步修改它(检查它是否在每次修改后运行)。通过这样做,您将避免当前看到的许多错误。