2012-02-27 84 views
5

我是一個Python初學者,目前使用SciPy的的odeint計算耦合ODE系統,但是,當我運行,蟒蛇殼總是告訴我,如何使odeint成功?

>>> 
Excess work done on this call (perhaps wrong Dfun type). 
Run with full_output = 1 to get quantitative information. 
>>> 

所以,我不得不改變我的時間步,也是最後一次,以使其可整合。要做到這一點,我需要嘗試不同的組合,這非常痛苦。任何人都可以告訴我,我怎樣才能要求odeint自動更改時間步驟和最終時間,以便成功集成此Ode系統?

和這裏是代碼的一部分,其已經呼籲odeint:

def main(t, init_pop_a, init_pop_b, *args, **kwargs): 
    """ 
    solve the obe for a given set of parameters 
    """ 
    # construct initial condition 
    # initially, rho_ee = 0 
    rho_init = zeros((16,16))*1j ######## 
    rho_init[1,1] = init_pop_a 
    rho_init[2,2] = init_pop_b 
    rho_init[0,0] = 1 - (init_pop_a + init_pop_b)######## 
    rho_init_ravel, params = to_1d(rho_init) 
    # perform the integration 
    result = odeint(wrapped_bloch3, rho_init_ravel, t, args=args) 
         # BUG: need to pass kwargs 
    # rewrap the result 
    return from_1d(result, params, prepend=(len(t),)) 

things = [2*pi, 20*pi, 0,0, 0,0, 0.1,100] 
Omega_a, Omega_b, Delta_a, Delta_b, \ 
init_pop_a, init_pop_b, tstep, tfinal = things 
args = (Delta_a, Delta_b, Omega_a, Omega_b) 
t = arange(0, tfinal + tstep, tstep) 
data = main(t, init_pop_a, init_pop_b, *args) 

plt.plot(t,abs(data[:,4,4])) 

其中wrapped_bloch3是函數計算DY/DT。

+2

你能否提供更多的代碼,尤其是呼叫到odeint? – 2012-02-27 14:05:55

+0

您將不得不添加比您提供的更多細節來獲得幫助:您正在使用什麼類型的ODE?他們是否僵硬?你是否提供雅可比函數來「odeint」?你確定這是合理的嗎? – talonmies 2012-02-27 14:07:44

+0

感謝您的回放,並且我更新了我的問題:) – user1233157 2012-02-27 14:40:20

回答

1

編輯:我注意到你已經有了答案在這裏:complex ODE systems in scipy

odeint不復值方程工作。我得到

from scipy.integrate import odeint 
import numpy as np 
def func(t, y): 
    return 1 + 1j 
t = np.linspace(0, 1, 200) 
y = odeint(func, 0, t) 
# -> This outputs: 
# 
# TypeError: can't convert complex to float 
# odepack.error: Result from function call is not a proper array of floats. 

您可以通過其他的ODE求解器解決您的公式:

from scipy.integrate import ode 
import numpy as np 

def myodeint(func, y0, t): 
    y0 = np.array(y0, complex) 
    func2 = lambda t, y: func(y, t) # odeint has these the other way :/ 
    sol = ode(func2).set_integrator('zvode').set_initial_value(y0, t=t[0]) 
    y = [sol.integrate(tp) for tp in t[1:]] 
    y.insert(0, y0) 
    return np.array(y) 

def func(y, t, alpha): 
    return 1j*alpha*y 

alpha = 3.3 
t = np.linspace(0, 1, 200) 
y = myodeint(lambda y, t: func(y, t, alpha), [1, 0, 0], t)