首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使odeint成功?

如何使odeint成功?
EN

Stack Overflow用户
提问于 2012-02-27 13:46:39
回答 1查看 10K关注 0票数 5

我是个蟒蛇初学者,目前我使用odeint来计算耦合的ODE系统,但是,当我运行时,python总是告诉我

代码语言:javascript
复制
>>> 
Excess work done on this call (perhaps wrong Dfun type).
Run with full_output = 1 to get quantitative information.
>>> 

因此,我必须改变我的时间步骤和最后一次,以使其可集成。要做到这一点,我需要尝试不同的组合,这是相当痛苦的。有人能告诉我如何让odeint自动改变时间步长和最后一次来成功地集成这个ode系统吗?

下面是称为odeint的代码的一部分:

代码语言:javascript
复制
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的函数。

EN

回答 1

Stack Overflow用户

发布于 2012-02-27 21:07:28

编辑:我注意到您已经得到了一个答案:complex ODE systems in scipy

odeint不适用于复值方程.我得到了

代码语言:javascript
复制
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求解器来求解您的方程:

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9466046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档