繁体   English   中英

如何使用scipy 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM