在python中求解具有复系数的ODE

时间:2016-03-26 12:29:34

标签: python

我有一个具有复系数的耦合ODE系统。我已经想出如何用数值系数来数值求解系统,例如,

u0' = u1

u1' = u0

为此,我使用scipy.integrate.odeint类,如下所示,

def vectorfield(w,t):
    u0,u1 = w

    #create f = [x1',y1',...]

    f = [u1, u0]
    return f

wsol = odeint(vectorfield, w0, t)

其中w0是w的初始值,例如: w0 = [1,1]

然而现在我需要解决一个具有复杂系数的系统,比如说

u0' = i*u1
u1' = i*u0

然而,当我尝试使用与实数值系数相同的方法时,我在结果中得不到任何复数,只有每个时间步的实数值得出的答案。 如何更改上述代码以考虑复杂系数?

提前致谢。

编辑,这是我的完整代码示例:

#coupled differential equations
from scipy.integrate import odeint
from scipy.integrate import ode
import numpy as np
import matplotlib.pyplot as plt
import math
from scipy import eye



# Setting initital conditions


u_initial = [1,1]


def vectorfield(w,t):

    u0,u1 = w


    #create f = [x1',y1',...]

    f = [1j*u1*u1.conjugate()+u0,1j*u0*u0.conjugate()+u1]
    return f

abserr = 1.0e-8
relerr = 1.0e-6

stoptime = 10
numpoints = 300

t = [stoptime * float(i) / (numpoints - 1) for i in range(numpoints)]


w0 = u_initial #set initial conditions

wsol = odeint(vectorfield, w0, t,
          atol=abserr, rtol=relerr)

0 个答案:

没有答案