gpt4 book ai didi

python - 求解前一时间步的 ODE 函数(延迟微分方程)

转载 作者:行者123 更新时间:2023-11-28 22:16:20 28 4
gpt4 key购买 nike

我有这组微分方程:

dy/dt = a*y   - b*x*y  
dx/dt = b*x*y - c*y(t - t_0)

t_0是一个常数时间,当 t<t_0 时,该项被忽略。 。给定初始条件和所有系数,如何使用 numpy/scipy 在 python 中解决这个问题?

编辑:y(t-t_0)值为 y当时t-t_0 ,不是yt-t_0

最佳答案

似乎对全局变量 sol_y 执行插值也有效:

import numpy as np
from scipy.integrate import ode
import matplotlib.pyplot as plt

def dudt(t, u, params):
x, y = u
a, b, c, t0 = params

dydt = a*y - b*x*y
if t <= t0:
dxdt = b*x*y
else:
dxdt = b*x*y - c*get_old_y(t-t0)

return [dxdt, dydt]

def get_old_y(old_t):
return np.interp(old_t, sol_t, sol_y)

def jac_dudt(t, u, params):
x, y = u
a, b, c, t0 = params
jac = [[ b*y, b*x-c],
[-b*y, a-b*y]]
return jac

# parameters
t0 = 1
params = 1, 1, 2, t0

u0 = [1, 2]

t_end = 3*t0
dt = 0.05

# integration
r = ode(dudt, jac_dudt).set_integrator("vode",
method="adams",
with_jacobian=True)

r.set_initial_value(u0, 0).set_f_params(params).set_jac_params(params)

sol_t, sol_x, sol_y = [], [], []
while r.successful() and r.t < t_end:
r.integrate(r.t + dt)
sol_x.append(r.y[0])
sol_y.append(r.y[1])
sol_t.append(r.t)

# graph
plt.plot(sol_t, sol_x, '-|', label='x(t)')
plt.plot(sol_t, sol_y, '-|', label='y(t)')
plt.legend(); plt.xlabel('time'); plt.ylabel('solution');

带有示例参数的输出图为:

output graph

关于python - 求解前一时间步的 ODE 函数(延迟微分方程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52243443/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com