使用Python解决带有时间延迟的ODE问题

7

有人能给我一些建议吗,如何在Python中解决一个带有时间延迟的常微分方程(ODE)?使用scipy.integrate.odeint似乎无法实现。我正在寻找的应该类似于:

# the constants in the equation
b = 1/50
d = 1/75
a = 0.8
G = 10 ** (-2)
tau = 0.5
u = [b, d, tau, a, G]

# enter initial conditions
N0 = 0.1
No0 = 10
w = [N0, No0]

def logistic(w, t, u):
    N, No = w
    b, d, tau, a, G = u
    dNdt = b * (No(t) - N(t) ) * (N(t) / No(t) ) - d * N(t - tau)
    dNodt = G * (a * No(t) - N(t) ) * (N(t) / No(t) )
    return [dNdt, dNodt]

# create timescale
# create timescale
stoptime = 1000.0
numpoints = 10000
t = np.linspace(0, stoptime, numpoints)

# in my previous code I would use scipy.integrate.odeint here to integrate my 
# equations, but with a time-delay that doesn't work (I think)
soln = ...

N(t)、N(t-tau)等是函数的时间参数。是否存在解决这些类型方程的好库?非常感谢!

1个回答

5
我是 JiTCDDE 的作者,它可以解决时滞微分方程问题,并且与 scipy.ode 大致相似。你可以使用命令 pip3 install jitcdde来安装它。据我所知,Python中的其他现有 DDE 库可能存在缺陷或基于已弃用的依赖关系。
以下代码将对您的问题进行集成:
from jitcdde import t, y, jitcdde
import numpy as np

# the constants in the equation
b = 1/50
d = 1/75
a = 0.8
G = 10**(-2)
tau = 0.5

# the equation
f = [    
    b * (y(1) - y(0)) * y(0) / y(1) - d * y(0, t-tau),
    G * (a*y(1) - y(0)) * y(0) / y(1)
    ]

# initialising the integrator
DDE = jitcdde(f)

# enter initial conditions
N0 = 0.1
No0 = 10
DDE.add_past_point(-1.0, [N0,No0], [0.0, 0.0])
DDE.add_past_point( 0.0, [N0,No0], [0.0, 0.0])

# short pre-integration to take care of discontinuities
DDE.step_on_discontinuities()

# create timescale
stoptime = 1000.0
numpoints = 100
times = DDE.t + np.linspace(1, stoptime, numpoints)

# integrating
data = []
for time in times:
    data.append( DDE.integrate(time) )

谢谢,你的库看起来很有前途,我一定会尝试使用! - ThePiIsALie
@ThePiIsALie:我明白了。那么你必须确实调整你的代码。但我假设我的最小示例版本能够工作? - Wrzlprmft
它能解决像这个这样的问题吗? - user6043040
1
@user6043040:不是直接的。你需要先将问题的空间方面离散化(任何求解器都必须这样做)。此外,请注意,对于偏微分方程,由于它们的规则结构,您通常可以通过使用并行化技术来加速处理,而JiTCDDE不支持此类情况(因为它没有针对这种情况进行过优化)。 - Wrzlprmft
1
@jjrr:不,我把它删掉了,因为它过于复杂和不符合Python的风格。现在你可以直接导入这些符号。请看我的编辑。 - Wrzlprmft
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接