You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中Runge-Kutta 4求解三阶微分方程组结果异常排查

RK4求解三阶ODE结果与欧拉法差异过大的问题排查

我编写了用于求解三阶ODEs的Runge-Kutta 4(RK4)代码,但计算结果与欧拉法求解同一微分方程组的结果差异显著。

欧拉法的结果如下所示:
欧拉法结果

而使用附上的RK4代码得到的结果却大相径庭:
RK4结果

import matplotlib.pyplot as plt    
umax = 0.53
Km = 0.12
Yxs = 0.4
S1f = 4
Yxp = 0.4
D=0.5      #
X1f=0.2
P1f=0.1

# Equations:


#du/dt=V(u,t)
def V(u,t):
    X1, S1, P1, vx, vs, vp = u
    return np.array([ vx,vs,vp,
            D*(X1f - X1)+(umax*(1-np.exp(-S1/Km)))*X1, 
            D*(S1f - S1)-(umax*(1-np.exp(-S1/Km))*X1)/Yxs, 
            D*(P1f - P1)+(umax*(1-np.exp(-S1/Km)))*X1*Yxp ])

def rk4(f, u0, t0, tf , n):
    t = np.linspace(t0, tf, n+1)
    u = np.array((n+1)*[u0])
    h = (t[1]-t[0])/n
    for i in range(n):
        k1 = h * f(u[i], t[i])    
        k2 = h * f(u[i] + 0.5 * k1, t[i] + 0.5*h)
        k3 = h * f(u[i] + 0.5 * k2, t[i] + 0.5*h)
        k4 = h * f(u[i] + k3, t[i] + h)
        u[i+1] = u[i] + (k1 + 2*(k2 + k3 ) + k4) / 6
    return u, t

u, t  = rk4(V, np.array([0., 0., 0. , 0., 1. , 0.]) , 0. , 40. , 4000)
x,y,z, vx,vs,vp  = u.T
# plt.plot(t, x, t,y)
plt.plot(t, x, t,y,t,z)
plt.grid('on')

plt.show() 

已多次检查代码,但仍未找到结果与欧拉法差异巨大的原因,请求协助排查。

内容的提问来源于stack exchange,提问作者Juan Saknussem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 16:01:27