[Math] Solve fourth order ODE using fourth order Runge-Kutta method

numerical methodsordinary differential equationsrunge-kutta-methods

I have been trying to solve some ODEs in C language using RK4 method. So far I have been able to solve first and second order ODEs by using direct formulae as follows:

First order ODE:

$$x_{n+1} = x_n + h\\
y_{n+1} = y_n + \frac{1}{6}(k_1 + 2k_2 + 2k_3 + k_4)
$$
where $$
k_1 = h f(x_n, y_n)\\
k_2 = h f(x_n + h/2, y_n + k_1/2)\\
k_3 = h f(x_n + h/2, y_n + k_2/2)\\
k_4 = h f(x_n + h, y_n + k_3)
$$

Second Order ODE

enter image description here

However I am unable to find any direct formulae to solve higher order ODEs(3rd and 4th order). Is there a straight forward way to implement RK4 for higher order ODEs?

Best Answer

As suggested in the comments, higher-order ODEs are rewritten as a system of first-order ODEs. To illustrate this, an example is provided below.

Let us consider the ODE $x^{(4)} + x^{(3)} + x'' + x' + x = y(t)$, with $x(t_0) = x_0$, $x'(t_0) = \dot x_0$, $x''(t_0) = \ddot x_0$, $x^{(3)}(t_0) = \dddot x_0$. Using the vector notation $\boldsymbol{x} = (x,x',x'',x^{(3)})^\top = (x_1,x_2,x_3,x_4)^\top$, the ODE is rewritten $$ \underbrace{ \left( \begin{array}{c} x'_1 \\ x'_2 \\ x'_3 \\ x'_4 \end{array} \right)}_{\boldsymbol{x}'} = \underbrace{ \left( \begin{array}{c} x_2 \\ x_3 \\ x_4 \\ y(t) - x_4 - x_3 - x_2 - x_1 \end{array} \right)}_{\boldsymbol{f}(\boldsymbol{x},t)} , $$ with the initial condition $\boldsymbol{x}(t_0) = (x_0,\dot x_0,\ddot x_0, \dddot x_0)^\top = \boldsymbol{x}_0$. Then the Runge-Kutta 4 method writes $$ \boldsymbol{x}_{n+1} = \boldsymbol{x}_{n} + \frac{h}{6} \left(\boldsymbol{k}_1 + 2\boldsymbol{k}_2 + 2\boldsymbol{k}_3 + \boldsymbol{k}_4\right), $$ where $$ \begin{aligned} \boldsymbol{k}_1 &= \boldsymbol{f}(\boldsymbol{x}_n, t_n) \, ,\\ \boldsymbol{k}_2 &= \boldsymbol{f}(\boldsymbol{x}_n + \tfrac{h}{2}\boldsymbol{k}_1, t_n + \tfrac{h}{2}) \, ,\\ \boldsymbol{k}_3 &= \boldsymbol{f}(\boldsymbol{x}_n + \tfrac{h}{2}\boldsymbol{k}_2, t_n + \tfrac{h}{2}) \, ,\\ \boldsymbol{k}_4 &= \boldsymbol{f}(\boldsymbol{x}_n + h \boldsymbol{k}_3, t_n + h ) \, . \end{aligned} $$

Related Question