你能检查二阶runge-kutta方法的应用吗?

Can you check the application of second order runge-kutta method?

def second(y, z, y0, z0, t0, tf, dt):
N = (tf-t0)/dt
  y.append(y0)
  for i in range(int(N)):
    t1 = t0 + dt
    y1 = y0 + z0*dt
    zk1 = z(t0, y0, z0)
    zk2 = z(t0+1/2*dt, y0+1/2*dt, z0+1/2*zk1*dt)
    z1 = z0 + zk2*dt
    y.append(y1)
    t0 = t1
    y0 = y1
    z0 = z1
  return y

我得到这样的结果:[1, 1, -8.0, -26.0, -269.0]。你能检查一下我是否了解如何应用二阶龙格库塔方法吗?

您想实现显式中点法

k1 = f(t,u)
k2 = f(t+dt/2, u+k1*dt/2)
u_next = u+k2*dt

对于二阶方程y''=a(t,y,y')。为此,您设置 z=y'u=[y,z]。在组件中你得到

k1y = z
k1z = a(t,y,z)
k2y = z + k1z*dt/2
k2z = a(t+dt/2, y+k1y*dt/2, z+k1z*dt/2)

y_next = y + k2y*dt
z_next = z+k2z*dt

然后要使用二阶方程的一阶系统的特殊结构,您消去 k1y,k2y 得到

k1z = a(t,y,z)
k2z = a(t+dt/2, y+z*dt/2, z+k1z*dt/2)
y_next = y + z*dt + k1z*dt^2/2
z_next = y + k2z*dt

现在,与您的代码相比,存在一些差异。