# Solve fourth order ODE using fourth order Runge-Kutta method

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

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?

• @Moo I understand that but it would be helpful for me to understand the solving through an example and also to verify the result once I am done coding the same. I am unable to find any resource which would help me with theis Jan 22, 2018 at 5:25
• Consider the ODEs in this question: math.stackexchange.com/questions/1120984/… How will I write the corresponding equations for k1,...k4 for the above? Jan 23, 2018 at 3:31
• See stackoverflow.com/questions/41996542/… for a discussion of the algorithm from the book "Numerical recipes in C". Starting with dimensions 3-4 it makes sense to treat vectors as vectors. Jan 25, 2018 at 10:02

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}