![](/img/trans.png)
[英]How to solve differential equation using Python builtin function odeint?
[英]How to solve this differential equation using scipy odeint?
我正在嘗試使用scipy odeint解決以下微分方程式,但沒有取得很大的成功:
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)
def Ip(t):
return np.sqrt(2)*Imag*(np.sin(w*t+phi-theta)-np.exp(-t/tau)*np.sin(phi-theta))
B = lambda Ip: Ip/(53.05+0.55*abs(Ip))
def L(B):
return derivative(B,Ip(t))*377.2
def dI(t):
return derivative(Ip,t)
def f(y,t):
Rb = 8.
N = 240.
Is = y[0]
f0 = (1/(L(B)+0.002))*((dI(t)*L(B)/N)-Rb*y[0])
return [f0]
yinit = [0]
sol = odeint(f,yinit,t)
print sol[:,0]
我不斷收到以下錯誤:
odepack.error: Result from function call is not a proper array of floats.
ValueError: object too deep for desired array
odepack.error: Result from function call is not a proper array of floats.
我應該怎么做才能正確運行腳本?
怎樣用ode
而不是odeint
您有一個非常類似的問題: 如何使odeint成功?
您對此功能有疑問:
def L(B):
return derivative(B,Ip(t))*377.2
請注意, t
表示之前定義的全局變量,它是一個numpy數組。 我認為你需要重新考慮你如何定義你的函數及其參數-應該t
也是一個參數L
? 照原樣, f
返回包含數組的列表,即使其第一個參數包含單個元素也是如此:
In [10]: f([1], 0)
Out[10]:
[array([ -2.28644086e+10, -2.28638809e+10, -2.28633064e+10, ...,
-1.80290012e+09, -1.80271510e+09, -1.80258446e+09])]
這將導致odeint
斷裂。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.