# Runge-Kutta 4 for systems of equations

This question is part of an assignment in numerical methods class. I am supposed to find the position and velocity of a spaceship flying around the Earth and Moon. I am given initial values of the position and speed, and functions that describe the acceleration of the spaceship, so this can be solved using the Runge-Kutta methods.

Description

The position of the spaceship can be described as $(x(t), y(t))$ for any time point $t$. Similarly, the velocity is $(x'(t), y'(t))$ — so you can see, the spaceship moves in two-dimensional space. Its acceleration is described by the following system of differential equations

$x'' = f(x, y, x', y')$
$y'' = g(x, y, x', y')$

(From now on I'll use the notations $u = x'$ and $w= y'$.)

I am also given the initial values $x_0, y_0, u_0, w_0$.

Final approximation of position

In the RK4 method, you calculate four intermediate approximations, $k_1, k_2, k_3, k_4$; the final approximation will be given by a weighted average of these intermediate approximations:

$$u_{i+1} = u_i + \frac{1}{6}(k_1 + 2k_2 + 2k_3 + k_4)$$

Assuming I already calculated the intermediate approximations (I'll get to that), the speeds $u$ and $w$ are simply calculated using the above method. How about the positions, though? I don't have functions that describe the speed ($f$ and $g$ describe the acceleration). It is correct for me to say the following?

$x_{i+1} = x_i + u_i \cdot \Delta t$
$y_{i+1} = y_i + w_i \cdot \Delta t$

Intermediate approximations of position

Given that I need to approximate four things ($x, y, u, w$), I will need four different sets of intermediate approximations; the first one ($k_1$) would be $k_{1x}, k_{1y}, k_{1u}, k_{1w}$.

The intermediate approximations for the speed are easy to calculate (because I know the accelerations $f$ and $g$). However, for calculating $k_{1x}$ and $k_{1y}$, I don't know how the approximations should be done. This is what I tried, but it did not work properly (over time, the spaceship would get very far away from the Moon and Earth when it shouldn't have):

$k_{1x} = \Delta t \cdot u_i$
$k_{1y} = \Delta t \cdot w_i$

What would be a better way to calculate the intermediate and final approximations of the position?

-
You seem to be misunderstanding the use of RK. What you're supposed to do is set up four (possibly interrelated) first-order differential equations, and then apply RK to those four... – J. M. May 17 '12 at 1:29

You need to build a system of four first order coupled differential equations. Given that $u = x'$ and $v = y'$. So then the first set of equations becomes

$$u' = f(x, y, u, v)$$ $$v' = g(x, y, u, v)$$

The second set of equations are trivial, however, necessary (as already shown):

$$x' = u$$ $$y' = v$$

If we let $\mathbf{x} = \left[ \begin{array}{cccc} x & y & u & v \end{array} \right]^T$ and let $f(\mathbf{x})$ and $g(\mathbf{x})$ be the vector equivalents of $f()$ and $g()$. Then we can write our system as follows:

$$\left[ \begin{array}{c} x' \\ y' \\ u' \\ v' \end{array} \right] = \left[ \begin{array}{c} u \\ v \\ f(\mathbf{x}) \\ g(\mathbf{x}) \end{array} \right]$$

Further more if we define:

$$\mathbf{h(\mathbf{x})} = \left[ \begin{array}{c} u \\ v \\ f(\mathbf{x}) \\ g(\mathbf{x}) \end{array} \right]$$

then the system may be expressed as

$$\mathbf{x}' = \mathbf{h(\mathbf{x})}$$

For Runge-Kutta, we then use the equation four times to give us vector values of $\mathbf{k}_1$ through $\mathbf{k}_4$ and then use the combination formula to determine the next state $\mathbf{x}_{k+1}$.

-