# How to use Runge–Kutta methods in a second order ODE

Consider a second order equation $$F=ma=m\ddot{x}$$.

In the language of Euler's method

1. $$\ddot{x}(t+dt)=F(t,x(t),\dot x(t))$$
2. $$\dot{x}(t+dt)=\dot x(t)+\ddot x(t)dt$$
3. $$x(t+dt)=x(t)+\dot x(t)dt$$

Basically, the entire iteration was the second order force equation $$F(t,x(t),\dot x(t))$$.

However, here one wanted to apply Runge–Kutta method(standard "RK4", not Runge–Kutta–Nyström or else) to solve that equation. For notation simplicity, let $$v\sim \dot x$$, $$a\sim \ddot x$$(velocity and acceleration).

From $$x(t),v(t),a(t)$$:

$$ka1=F(t,x(t),\dot x(t))$$; $$kv1=v(t)+a(t)\frac{dt}{2}$$; $$kx1=x(t)+v(t)\frac{dt}{2}$$;

$$ka2=F(t+\frac{dt}{2},kx1, kv1)$$; $$kv2=kv1+ka1\cdot \frac{dt}{2}$$; $$kx2=kx1+kv1\cdot \frac{dt}{2}$$;

$$ka3=F(t+\frac{dt}{2},kx2, kv2)$$; $$kv3=kv2+ka2\cdot dt$$; $$kx3=kx2+kv2\cdot dt$$; and finally

$$ka4=F(t+dt,kx3, kv3)$$;

The final acceleration was collected to be

Iteration Update Method 01

$$\ddot x(t+dt)=(ka1+ka2\cdot 2+ka3\cdot 2+ka4)/6$$; where

$$\dot x(t+dt)=\dot x(t)+\ddot x(t)dt$$;

$$x(t+dt)=x(t)+\dot x(t)dt$$

with such loop the iteration was able to run in RK4 method and things worked very nicely and coincided with the analytical calculation for an example which Euler method was known to fail.(Tested)

However, my question arise from a mistake where it made the code fail to work. It went like this:

In addition to the $$\ddot x(t+dt)$$ condition, one further to average the speed directly,

Iteration Update Method 02

$$\ddot x(t+dt)=(ka1+ka2\cdot 2+ka3\cdot 2+ka4)/6$$;

$$\dot x(t+dt)=(\dot x(t)+kv1\cdot 2+kv2\cdot 2+kv3)/6$$; and

$$x(t+dt)=x(t)+\dot x(t)dt$$;

Notice that the new update method attempted to use the $$\dot x(t)$$ to take care of the "overflow". Intuitively, not only the acceleration $$\ddot x$$ was "RK4"-ed, but also, the velocity $$\dot x$$ was "RK4"-ed as well. However, thought still able to produce a sensible graph, the Iteration Update Method 02 was proven to have failed after the analysis.

Question 1: Why Iteration Update Method 01 worked? Even though it approximated $$\frac{d^2y(t)}{dt}=f(t,y(t),\dot y(t))$$ instead of $$\frac{d y(t)}{dt}=f(t,y(t))$$?

Question 2: Why Iteration Update Method 02 failed? Shouldn't it work better since it averaged more stuff?

I think there is a mistake in your post: the first equation should be $$\ddot{x}(t) = F(t, \dot{x}(t), x(t))$$ (and not $$\ddot{x}(t + dt)$$).
Also, in the computation of $$kv_i$$ and $$kx_i$$, it should be $$kv_i = v(t) + ka_{i-1}\Delta t$$ and $$kx_i = x(t) + kv_i\Delta t$$ (where $$\Delta t = dt$$ or $$\frac{dt}{2}$$). $$kv_i$$ does not use $$kv_{i-1}$$ (except in the computation of $$ka_{i-1}$$), and same thing for $$kx_i$$.
Question 1: in the differential equation $$\ddot{x}(t) = F(t, \dot{x}(t), x(t))$$, if you consider the vector $$Y(t) = \begin{pmatrix}x(t)\\\dot{x}(t)\end{pmatrix}$$, then you have $$\dot{Y}(t) = \begin{pmatrix}\dot{x}(t)\\\ddot{x}(t)\end{pmatrix} = \begin{pmatrix}\dot{x}(t)\\F(t, \dot{x}(t), x(t))\end{pmatrix} = \tilde{F}(t, Y(t))$$. That explains why the method works (since the formulas will be the same as with a first order differential equation). Actually, the approximation should be better with the corrections I proposed.
Question 2: With your formulas, a bit of simplification gets you $$v(t+dt) = v(t) + \frac{dt}{6}\left(5a(t) + 2ka_1 + 2ka_2\right)$$ which is not coherent.
Even with the corrections I proposed, it is incorrect. My guess is that you should compute $$x(t+dt)$$ and $$\dot{x}(t+dt)$$ as a whole (as in the vector $$Y(t)$$) instead of trying to compute them one after the other. The system $$\left\{\begin{array}{rcl}\dot{x}(t+dt) &= &\dot{x}(t) + \ddot{x}(t)dt\\ x(t+dt) &=&x(t) + \dot{x}(t)dt\end{array}\right.$$ can be seen as $$Y(t+dt) = Y(t) + \dot{Y}(t)dt$$.
• $\ddot{x}(t+dt)=F(t,x(t),\dot x(t))$ was correct. That part was Euler and $\ddot{x}(t+dt)$ was saved and only being used at $t+2dt$, i.e. the update $a(t)=\ddot x(t-dt)$. $kv1$ was the "RK4" updates for calculating $ka2$, that's why it used set of $x$, i.e. it's being forward $dt/2$ as to calculating $ka2$. The $ka1$, $kv1$ and $kx1$ was basically from the previous $x(t)=x(t+dt)$ updates. The equation was written in loop, so the current $v(t+dt)=v(t)+a(t)dt$ was actually $v(t)=v(t-dt)+a(t-dt)dt$, i.e. the updating sequence was $\ddot x->\dot x->x$. Mar 19 at 11:05