0
$\begingroup$

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?

$\endgroup$
  • 3
    $\begingroup$ Well you would convert the fourth order equation to a system of first order equations and then follow the same recipe. $\endgroup$ – Moo Jan 22 '18 at 4:54
  • $\begingroup$ @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 $\endgroup$ – Abhishek Agarwal Jan 22 '18 at 5:25
  • 1
    $\begingroup$ Can you post the ODE and ICs? $\endgroup$ – Moo Jan 22 '18 at 5:28
  • $\begingroup$ Consider the ODEs in this question: math.stackexchange.com/questions/1120984/… How will I write the corresponding equations for k1,...k4 for the above? $\endgroup$ – Abhishek Agarwal Jan 23 '18 at 3:31
  • $\begingroup$ 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. $\endgroup$ – LutzL Jan 25 '18 at 10:02
3
+50
$\begingroup$

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} $$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.