【发布时间】:2013-11-16 01:25:00
【问题描述】:
当我在一个简单的微分方程中包含一个导函数时,我不断收到一长串错误,如下面的代码所示。我做错了什么?
import numpy as np
from scipy.misc import derivative
from scipy.integrate import odeint
Imag = 16000.
w = 2*np.pi*60
tau = .05
theta = 1.52
phi = theta - np.radians(90)
t = np.linspace(0,.1,10000)
Ip = np.sqrt(2)*Imag*(np.sin(w*t+phi-theta)-np.exp(-t/tau)*np.sin(phi-theta))
def dI(t):
return derivative(Ip,t)
def f(y,t):
Rb = 8.
N = 240.
Is = y[0]
f0 = (dI(t)/N)-Rb*y[0]
return [f0]
yinit = [0]
sol = odeint(f,yinit,t)
print sol[:,0]
错误输出中似乎重复了好几次的小片段如下:
val += weights[k]*func(x0+(k-ho)*dx,*args)
TypeError: 'numpy.ndarray' object is not callable
odepack.error: Error occurred while calling the Python function named f
编辑:上述代码在 scipy 0.9.0 中运行没有错误,但在 0.12.0 中出现上述错误
Edit2:既然错误已经解决,我需要在积分器中添加第二个函数,如下所示:
B = lambda Ip: Ip/(53.05+0.55*abs(Ip))
L = lambda Ip: derivative(B,Ip)*377.2
def f(y,t):
Rb = 8.
N = 240.
Is = y[0]
f0 = (1/(L+0.002))*((dI(t)*L/N)-Rb*y[0])
return [f0]
我该怎么做?
【问题讨论】:
-
这里给出一个例子:[stackoverflow.com/questions/17533751/…
-
这对我毫无帮助。我对这种方法还没有兴趣。在这一点上,我希望脚本运行没有错误。