Solving a system of ODE's with RK4 correctly

329 Views Asked by At

I want to applying the 4th order Runge-Kutta method on a system of first order ODE's.

  1. So first I write the system of first order ODEs which is $$\begin{align}x'&=v\\ x''&=v'=f(t,x,v)\end{align}$$ with initial condition $$x(t_0)=x_0,\quad x'(t_0)=v_0$$

  2. Then I compute the first $v_1$ by using the Runge Kutta method on the second ODE $f(t,x,v)$ starting with $x_0$

  3. Then I compute the first $x_1$ (position) by using the Runge-Kutta method on the first ODE $f(v)=v$ with $v_0$.

  4. Then I do step 2 again but with $x_1$ and then step 3 with $v_1$ and so on I keep repeating. The resulting $x$ vector is then the values for $x(t)$.

Is that correct?

2

There are 2 best solutions below

2
On BEST ANSWER

No, you should use Runge-Kutta on both equations at the same time.

$$\eqalign{ k_{1x} &= v_0 \cr k_{1v} &= f(x_0)\cr k_{2x} &= v_0 + h k_{1v}/2\cr k_{2v} &= f(x_0 + h k_{1x}/2)\cr k_{3x} &= v_0 + h k_{2v}/2\cr k_{3v} &= f(x_0 + h k_{2x}/2)\cr k_{4x} &= v_0 + h k_{3v}\cr k_{4v} &= f(x_0 + h k_{3x})\cr x_1 &= x_0 + h (k_{1x} + 2k_{2x} + 2k_{3x} + k_{4x}) /6\cr v_1 &= v_0 + h (k_{1v} + 2k_{2v} + 2k_{3v} + k_{4v}) /6\cr }$$

1
On

Assume you have in general a vector function for the accelerations of the form $$ {\bf \dot{v}} = f(t,{\bf x},{\bf v})$$ Where $t$ is time, ${\bf x}$ are positions and ${\bf v}$ are velocity vectors.

Imagine there is another function for the velocities $${\bf \dot{x}} = g(t,{\bf x},{\bf v}) = {\bf v}$$

Apply the RK4 method to both the functions at the same time for each sub-step

$$\begin{aligned} {\bf Q}_0 & = {\bf v}_0 & {\bf K}_0 & = f(t_0,{\bf x}_0,{\bf Q}_0) \\ {\bf Q}_1 & = {\bf v}_0 + \frac{h}{2} {\bf K}_0 & {\bf K}_1 & = f(t_0+\frac{h}{2},{\bf x}_0+\frac{h}{2} {\bf Q}_0,{\bf Q}_1) \\ {\bf Q}_2 & = {\bf v}_0 + \frac{h}{2} {\bf K}_1 & {\bf K}_2 & = f(t_0+\frac{h}{2},{\bf x}_0+\frac{h}{2} {\bf Q}_1,{\bf Q}_2) \\ {\bf Q}_3 & = {\bf v}_0 + h {\bf K}_2 & {\bf K}_3 & = f(t_0+h,{\bf x}_0+h {\bf Q}_2,{\bf Q}_3) \end{aligned}$$

Now we can calculate the steps in positions and velocities to go from time $t_0$ to $t_1=t_0+h$

$$ \Delta {\bf x} = \frac{h}{6} \left( {\bf Q}_0 + 2{\bf Q}_1 + 2{\bf Q}_2 + {\bf Q}_3 \right) $$ $$ \Delta {\bf v} = \frac{h}{6} \left( {\bf K}_0 + 2{\bf K}_1 + 2{\bf K}_2 + {\bf K}_3 \right) $$

NOTE: The position step can be simplified to $\Delta {\bf x} = h \left( {\bf v}_0 + \frac{h}{6} ( {\bf K}_0+{\bf K}_1+{\bf K}_2 ) \right)$