How to solve this differential equation using scipy odeint?

别等时光非礼了梦想. 提交于 2019-12-02 10:21:50

问题


I am trying to solve the following differential equation using scipy odeint without much success:

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]

I keep getting the following error:

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.

What should I do to run the script without errors?


回答1:


what about using ode instead of odeint

there is a question quite similiar to yours: How to make odeint successful?




回答2:


You have a problem with this function:

def L(B):
    return derivative(B,Ip(t))*377.2

Note that t refers to the global variable defined earlier, which is a numpy array. I think you need to rethink how you define your functions and their arguments--should t also be an argument to L? As it is, f returns a list containing an array, even when its first argument contains a single element:

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])]

That will cause odeint to break.



来源:https://stackoverflow.com/questions/20019427/how-to-solve-this-differential-equation-using-scipy-odeint

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!