In the first part we have dealt with differential equations
of the form
$$
y'(x) = f(x,y),
$$
where $y(x_0) = y_0.$ We were given the function
$f(x,y)$ and the numbers $x_0,y_0.$ And we started to
solve it.
We had side-stepped the important question: does such an equation
always possess a solution? And even if it does, is the solution unique?
The following theorem makes this problem
meaningful.
We shall not prove this in this course.
In the first part, we have discussed Taylor's method (including its first order
version, Euler's method). To get a reasonable approximation with
Taylor's method in most real life problems, we need to use a high order (say 4-th order).
But then we need to compute high order
derivatives, which may be quite complicated. Runge-Kutta method aims to approximate Taylor's method
without this complication. Instead, it needs to evaluate $f(x,y)$ at
many points.
Just like Taylor's method, we have different Runge-Kutta methods for
different orders. The general form is rather complicated. In this page we shall present only the
2-nd and 4-th order methods.
We start from the given point $(x_0,y_0).$ We are given a step size $h,$ and we want to approximate
$y(x)$ at the points $x_1,...,x_n,$ where $x_i = x_0+h\,i.$ Thus, the output of the Runge-Kutta methods
consists of $(x_i,y_i)$ for $i=0,...,n.$
The 2-nd order
Runge-Kutta method is given by
$$\begin{eqnarray*}
k_1 & = & h\,f(x_i,y_i)\\
k_2 & = & h\,f\left(x_i+\frac{h}{2},y_i+\frac{k_1}{2}\right)\\
y_{i+1} & = & y_i + k_2.
\end{eqnarray*}$$
This method has the same order of precision as 2-nd order Taylor method.
To see this use the 2-variable Taylor expansion:
$$
f(x+\delta x, y+\delta y) = f(x,y)+\delta x f_1(x,y)+ \delta y f_2(x,y) +\mbox{ 2-nd order terms}.
$$
where $f_1,f_2$ are partial derivatives of $f$
w.r.t. its first and
second arguments, respectively.
We shall apply this
to $f\left(x_i+\frac{h}{2},y_i+\frac{k_1}{2}\right).$ To simplify
notation, we shall write $f,$ $f_1$ etc to
mean $f(x_i,y_i),$ $f_1(x_i, y_i)$ etc,
respectively. Then we see that
$$\begin{eqnarray*}
k_2 & =& h\left[f + \frac{h}{2} f_1 + \frac{k_1}{2} f_2 +
h^2\mbox{-terms}\right]\\
& =& hf + \frac{h^2}{2} f_1 + \frac{h\,k_1}{2} f_2 + h^3\mbox{-terms}\\
& =& hf + \frac{h^2}{2} f_1 + \frac{h^2}{2} f f_2 + h^3\mbox{-terms},
\end{eqnarray*}$$
since $k_1 = hf.$
So the second order Runge-Kutta method is
$$
y_{i+1} =
y_i + hf + \frac{h^2}{2} f_1 + \frac{h^2}{2} f f_2 + h^3\mbox{-terms}
$$
Let's compare this with the 2-nd order Taylor's method:
$$\begin{eqnarray*}
y_{i+1} = y_i + y_i'h + \frac{y_i''}{2}h^2
& =& y_i + hf + \frac{y_i''}{2}h^2.
\end{eqnarray*}$$
Now by the multivariate chain rule,
$$
y_i'' = f_1 + f_2y_i' = f_1+ff_2.
$$
So the 2nd order Taylor's method becomes
$$
y_{i+1} = y_i + hf + \frac{h^2}{2}(f_1+ff_2),
$$
which coincides with the 2nd order Runge-Kutta method (ignoring $h^3$-terms).
The most popular Runge-Kutta method is
the 4-th order version:
$$\begin{eqnarray*}
k_1 & = & hf(x_i,y_i)\\
k_2 & = & hf\left(x_i+\frac{h}{2},y_i+\frac{k_1}{2}\right)\\
k_3 & = & hf\left(x_i+\frac{h}{2},y_i+\frac{k_2}{2}\right)\\
k_4 & = & hf(x_i+h,y_i+k_3)\\
y_{i+1} & = & y_i + \frac{1}{6}(k_1 + 2k_2 + 2k_3 + k_4).
\end{eqnarray*}$$
This has the same order of precision as the fourth order Taylor method.
The proof of this fact follows the same argument as above, but is
rather messy.
Here is a simple code for performing a single RK4 step:
rk4 = function(f, x,y, h) {
k1 = h * f(x,y)
k2 = h*f(x+h/2, y+k1/2)
k3 = h * f(x=h/2, y+k2/2)
k4 = h*f(x+h, y+k3)
y + (k1+2*k2+2*k3+k4)/6
}
We may use it like this:
f = function(x,y) sin(x+y)
x = seq(0,1,len=10)
y = numeric(10); y[1] = 1
for(i in 2:length(x)) {
y[i] = rk4(f,x[i-1],y[i-1],x[i]-x[i-1])
}