A.3 Initial value problems

In this section, we look at how to solve the general problem of a set of first order coupled differential equations:

\begin{align} \frac{dy_i}{dx} = f_i(x, y_1,y_2,\ldots ) \tag{A.4}\end{align}

where the functions \(f_i\) are known, and the unknown functions, \(y_i\), are known at a certain value \(x=x_0\):

\begin{align} y_0(x_0) &= c_0 \\ y_1(x_0) &= c_1 \tag{A.5}\end{align}

The Euler Method

The simplest way to solve IVP problems is to use the Euler method. Although this is not a recommended method, as it is easily unstable, it is very useful in understanding the basic idea behind more complex algorithms. The basic idea is given by re-writing a derivative

\begin{align} \frac{dy}{dx} = f(x,y) \tag{A.6}\end{align}

as

\begin{align} \frac{\Delta y}{\Delta x } = f(x,y) \tag{A.7}\end{align}

At a small distance \(h=\Delta x\) from \(x_0\), we thus have:

\begin{align} k_1&=\Delta xf(x_0,y(x_0))=hf(x_0,y(x_0)) \\ y(x_0+h) &= y(x_0) + k_1 \tag{A.8}\end{align}

which yields an estimate of the functions at \(x_0+h\), which can be used iteratively to get the function at any value of \(x\). It can be shown (by comparing with a power series expansion) that the error in the estimate is of order \(h^2\).

The Runge-Kutta method

The Runge-Kutta (RK) method builds upon the Euler method to yield a better estimate of \(y(x)\) after the step. The second order RK method uses an estimate of the derivative of the function at the middle of the interval to correct the result from a simple Euler step.

\begin{align} k_1 &= hf(x_0,y_0) \\ k_2 &= hf(x_0+\frac{1}{2}h,y_0+\frac{1}{2}k_1) \\ y(x_0+h) &= y(x_0)+k_2 \tag{A.9}\end{align}

It can be shown that the error in \(y\) is of order \(h^3\) for this method. In general the \(n\)th order RK method has an error of order \(h^{n+1}\).

By far the most common Runge-Kutta method is the 4th order method (RK4), given by:

\begin{align} \vec{k}_1 &= h\vec{f}(x_0,\vec{y}(x_0)) \\ \vec{k}_2 &= h\vec{f}(x_0+\frac{1}{2}h,\vec{y}(x_0)+\frac{1}{2}\vec{k}_1)\\ \vec{k}_3 &= h\vec{f}(x_0+\frac{1}{2}h,\vec{y}(x_0)+\frac{1}{2}\vec{k}_2) \\ \vec{k}_4 &= h\vec{f}(x_0+h,\vec{y}(x_0)+\vec{k}_3) \\ \vec{y}(x_0+h) &= \vec{y}(x_0)+\frac{1}{6}\vec{k}_1 +\frac{1}{3}\vec{k}_2 +\frac{1}{3}\vec{k}_3 +\frac{1}{6} \vec{k}_4 \tag{A.10}\end{align}

where we have generalized the \(k_i\) to vectors, for a system of ODEs (each equation above is true by component).

Example A-1

Solving for the motion of a simple harmonic oscillator with a friction term

The friction term is proportional to velocity (and in the direction opposite of motion):

\begin{align*} m\frac{d^2x}{dt^2}+c\frac{dx}{dt}+kx = 0 \end{align*}

We can re-write this as:

\begin{align*} \frac{dy_0}{dx} &= y_1 \nonumber\\ \frac{dy_1}{dx} &= -\frac{1}{m}(ky_0+cy_1) \end{align*}

Here \(y_0\) is really \(x(t)\), the position of the mass as a function of time, and \(y_1\) is really \(\frac{dx}{dt}\), the speed as a function of \(t\). Let’s say that we know the position and velocity at \(t=0\), we can then use the RK4 method to solve for the position as a function of \(t\).