You are on page 1of 12

Fourth Order Runge-Kutta

Intro First Order Second Fourth Printable

Contents
Introduction
The Fourth Order-Runge Kutta Method.
Visualizing the Fourth Order Runge-Kutta Method
The irst slope, k1 (and inding y1)
The second slope, k2 (and inding y2)
The third slope, k3 (and inding y3)
The fourth slope, k4
The inal slope (a weighted average of previous slopes) — (and inding y*(t0+h))
Example 1: Approximation of First Order Differential Equation with No Input Using MATLAB
Key Concept: Fourth Order Runge-Kutta Algorithm
Key Concept: Accuracy of the Fourth Order Runge-Kutta Algorithm
An Alternate form of the Fourth Order Runge-Kutta

Introduction
In the last section it was shown that using two estimates of the slope (i.e., Second Order Runge Kutta; using slopes at the beginning and
midpoint of the time step, or using the slopes at the beginninng and end of the time step) gave an approximation with greater accuracy than
using just a single slope (i.e., First Order Runge Kutta; using only the slope at the beginning of the interval). It seems reasonable, then, to
assume that using even more estimates of the slope would result in even more accuracy. It turns out this is true, and leads to higher-order
Runge-Kutta methods. Third order methods can be developed (but are not discussed here). Instead we will restrict ourselves to the much
more commonly used Fourth Order Runge-Kutta technique, which uses four approximations to the slope. It is important to understand these
lower order methods before starting on the fourthe order method.

If you are interested in the details of the derivation of the Fourth Order Runge-Kutta Methods, check a Numerical Methods Textbook (like
Applied Numerical Methods, by Carnahan, Luther and Wilkes)

The Fourth Order-Runge Kutta Method.


To review the problem at hand: we wisth to approximate the solution to a irst order differential equation given by

dy(t)

= y (t) = f (y(t), t), with y(t0 ) = y0
dt

(starting from some known initial condition, y(t₀)=y₀). The development of the Fourth Order Runge-Kutta method closely follows those for
the Second Order, and will not be covered in detail here. As with the second order technique there are many variations of the fourth order
method, and they all use four approximations to the slope. We will use the following slope approximations to estimate the slope at some time
t₀ (assuming we only have an approximation to y(t₀) (which we call y*(t₀)).

k1 = f (y (t0 ), t0 )

h h

k2 = f (y (t0 ) + k1 , t0 + )
2 2

h h

k3 = f (y (t0 ) + k2 , t0 + )
2 2


k4 = f (y (t0 ) + k3 h, t0 + h)

Each of these slope estimates can be described verbally.


k1 is the slope at the beginning of the time step (this is the same as k1 in the irst and second order methods).

If we use the slope k1 to step halfway through the time step, then k2 is an estimate of the slope at the midpoint. This is the same as the slope, k2, from the second

order midpoint method. This slope proved to be more accurate than k1 for making new approximations for y(t).

If we use the slope k2 to step halfway through the time step, then k3 is another estimate of the slope at the midpoint.

Finally, we use the slope, k3, to step all the way across the time step (to t₀+h), and k4 is an estimate of the slope at the endpoint.

We then use a weighted sum of these slopes to get our inal estimate of y*(t₀+h)

k1 + 2k2 + 2k3 + k4 1 1 1 1
∗ ∗ ∗
y (t0 + h) = y (t0 ) + h = y (t0 ) + ( k1 + k2 + k3 + k4 ) h
6 6 3 3 6


= y (t0 ) + mh where m is a weighted average slope approximation

Note: these weights are chosen to get the desired accuracy of the approximation, just as with the second order method. The details are not given here.
The conceptual idea is similar to the second order endpoint method which used an equal weighting of the slopes at the beginning and end of
the interval. Here the weighting of the midpoint slopes (k2 and k3) is higher than those at the endpoints (k1 and k4), since we expect these to
be a better estimate of the slope when going from y*(t₀) to y*(t₀+h).
As an example consider the differential equation

dy(t) dy(t)
+ 2y(t) = 0 or = −2y(t)
dt dt

with the initial condition set as y(0)=3. To get from the initial value at t=0 to an estimate at t=h, follow the procedure outlined below

y (0) = −2y(0) expression f or derivative at t = 0

k1 = −2y(0) derivative at t = 0

h h

y1 ( ) = y (0) + k1 intermediate estimate of f unction at t = h/2 (using k1 )
2 2

h
k2 = −2y1 ( ) estimate of slope at t = h/2
2

h h

y2 ( ) = y (0) + k2 anotherintermediate estimate of f unction at t = h/2 (using k2 )
2 2

h
k3 = −2y2 ( ) another estimate of slope at t = h/2
2


y3 (h) = y (0) + k3 h an estimate of f unction at t = h (using k3 )

k4 = −2y3 (h) estimate of slope at t = h

k1 + 2k2 + 2k3 + k4

y (h) = y(0) + h estimate of y(h)
6

in general to go from an estimate at t₀ to an estimate at t₀+h,



k1 = −2y (t0 ) approximate derivative at t = t0

h h

y1 (t0 + ) = y (t0 ) + k1 intermediate estimate of f unction at t = t0 + h/2 (using k1 )
2 2

h
k2 = −2y1 ( ) estimate of slope at t = t0 + h/2
2

h h

y2 (t0 + ) = y (t0 ) + k2 another intermediate estimate of f unction at t = t0 + h/2 (using k2 )
2 2

h
k3 = −2y2 ( ) another estimate of slope at t = t0 + h/2
2


y3 (t0 + h) = y (t0 ) + k3 h an estimate of f unction at t = t0 + h (using k3 )

k4 = −2y3 (t0 + h) estimate of slope at t = t0 + h

k1 + 2k2 + 2k3 + k4
∗ ∗
y (t0 + h) = y (t0 ) + h estimate of y(t0 + h)
6

Visualizing the Fourth Order Runge-Kutta Method


The Fourth Order Runge-Kutta method is fairly complicated. This section of the text is an attempt to help to visualize the process; you
should feel free to skip it if it already makes sense to you and go on to the example that follows.

We will use the same problem as before. We will call the initial time t₀ and set t₀=0. We will then estimate a solution of the differential
equation

dy(t)

= y (t) = −2y(t), with y(0) = 3
dt

after one time step, i.e., at t=t₀+h or t=h, since t₀=0.


The first slope, k1 (and finding y1)

Given y(0), we ind the slope, y'(0) using the equation stated above. We call this slope k1.

k1 = −2y(t0 ) = −2y(0) = −6

We use this slope to estimate the value of the function midway through the intervale, i.e., y(½h). We call this estimate y1, y1=y(0)+k1½h=2.4
Us ng k to est mate y
1 1
3
Exact Solut on
Slope=k =-6
1

2.5

y(t) y 1 =y(0)+k 1 h/2=2.4

1.5
0 0.1=h/2 0.2=h
t me (S)

The second slope, k2 (and finding y2)

Given y1, we can use it to estimate the slope at the midpoint of the interval, t=½h. We call this slope k2.

k2 = −2y1 = −4.8

Note that this is an estimate of the slope at t=½h and we use it to ind another estimate of y(½h), that we call y2, with y2=y(0)+k2½h=2.52
Us ng k to est mate y
2 2
3
Exact Solut on
Slope=k =-4.8
2

y 2 =y(0)+k 2 h/2=2.52
2.5

y(t)
2

1.5
0 0.1=h/2 0.2=h
t me (S)

The third slope, k3 (and finding y3)

Given y2, we can use it to ind another estimate the slope at t=½h that we will call k3.

k3 = −2y2 = −5.04

We use this estimate of the slope at t=½h to ind an estimate of the function at the end of the interval, y(h). We call this estimate y3, with
y3=y(0)+k3h=1.992
Us ng k 3 to est mate y 3
3
Exact Solut on
Slope=k =-5.04
3

2.5

y(t)
2
y 3 =y(0)+k 3 h=1.992

1.5
0 0.1=h/2 0.2=h
t me (S)

The fourth slope, k4

We use y3 to ind an estimate of the slope at the end of the interval, t=h.

k4 = −2y3 = −3.9840

The final slope (a weighted average of previous slopes) — (and finding y*(t0+h))

We use all of our estimates of the slope in a weighted average that we will use to generate our inal estimate for y(h). We give the midpoint
slope estimates twice as much weight as the endpoint estimates. We de ine this weighted estimate as

1 1 1 1
m = k1 + k2 + k3 + k4
6 3 3 6

and use it to generate our inal estimate


1 1 1 1

y (h) = y(0) + ( k1 + k2 + k3 + k4 ) h = y(0) + m ⋅ h
6 3 3 6

Us ng m to est mate y(h)


3
Exact Solut on
Slope=m=-4.944

2.5

y(t)
2
y *(h)=y(0)+(k 1 +2k 2 +2k*3+k 4 )h/6=y(0)+mh=2.0112

1.5
0 0.1=h/2 0.2=h
t me (S)
The value of this inal estimate for the given example is y*(h)=2.0112. This is quite close to the exact solution y(h)=3e-2(0.2)=2.0110. Note: As
stated previously, we generally won't know the exact solution as we do in this case.

We can repeat this process using this estimate as our starting point to generate a solution over time.

Example 1: Approximation of First Order Differential Equation with No Input Using MATLAB

We can use MATLAB to perform the calculation described above.A simple loop accomplishes this:

% Solve y'(t)=-2y(t) with y0=3, 4th order Runge Kutta


y0 = 3; % Initial Condition
h=0.2; % Time step
t = 0:h:2; % t goes from 0 to 2 seconds.
yexact = 3*exp(-2*t) % Exact solution (in general we won't know this
ystar = zeros(size(t)); % Preallocate array (good coding practice)

ystar(1) = y0; % Initial condition gives solution at t=0.


for i=1:(length(t)-1)

k1 = -2*ystar(i) % Approx for y gives approx for deriv


y1 = ystar(i)+k1*h/2; % Intermediate value (using k1)

k2 = -2*y1 % Approx deriv at intermediate value.


y2 = ystar(i)+k2*h/2; % Intermediate value (using k2)

k3 = -2*y2 % Another approx deriv at intermediate value.


y3 = ystar(i)+k3*h; % Endpoint value (using k3)

k4 = -2*y3 % Approx deriv at endpoint value.

ystar(i+1) = ystar(i) + (k1+2*k2+2*k3+k4)*h/6; % Approx soln


end
plot(t,yexact,t,ystar);
legend('Exact','Approximate');

The MATLAB commands match up easily with the steps of the algorithm. A slight variation of the code was used to show
the effect of the size of hon the accuracy of the solution (see image below). Note that larger values of h result in poorer
approximations, but that the solutions are much better than those obtained with the Second Order Runge-Kutta for the
same value of h. This isn't obvious for small h, but is for the curve where h=0.8.
Exact and Approx mate Solut on, h var es
3
Exact
2.5 h=0.05
h=0.2
h=0.8
2

1.5

y, y*
1

0.5

-0.5

-1
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
T me (S)

Key Concept: Fourth Order Runge-Kutta Algorithm

For a irst order ordinary differential equation de ined by

dy(t)
= f (y(t), t)
dt

to progress from a point at t=t₀, y*(t₀), by one time step, h, follow these steps (repetitively).

k1 = f (y (t0 ), t0 ) approximate derivative at t = t0

h h

y1 (t0 + ) = y (t0 ) + k1 intermediate estimate of f unction at t = t0 + h/2 (using k1 )
2 2

h h
k2 = f (y1 (t0 + ) , t0 + ) estimate of slope at t = t0 + h/2
2 2

h h

y2 (t0 + ) = y (t0 ) + k2 another intermediate estimate of f unction at t = t0 + h/2 (using k2 )
2 2

h h
k3 = f (y2 (t0 + ) , t0 + ) another estimate of slope at t = t0 + h/2
2 2


y3 (t0 + h) = y (t0 ) + k3 h an estimate of f unction at t = t0 + h (using k3 )

k4 = f (y3 (t0 + h) , t0 + h) estimate of slope at t = t0 + h

k1 + 2k2 + 2k3 + k4
∗ ∗
y (t0 + h) = y (t0 ) + h estimate of y(t0 + h)
6

Notes:
an initial value of the function must be given to start the algorithm.
see the MATLAB program on this page for an example.

Key Concept: Accuracy of the Fourth Order Runge-Kutta Algorithm


The global error of the Fourth Order Runge-Kutta algorithm is O(h4).
This is not proven here, but the proof is similar to that for the Second Order Runge-Kutta

An Alternate form of the Fourth Order Runge-Kutta


The Second Order Runge-Kutta had more than one form (because the technique is derived from an underspeci ied set of equations).
Likewise, the Fourth Order Runge-Kutta has (in initely many) other forms. A common one is given below, without proof, simply to show that
other (possibly very different) forms of the Fourth Order Rung-Kutta can be formulated:

k1 = f (y (t0 ), t0 )

h h

k2 = f (y (t0 ) + k1 , t0 + )
3 3

h 2

k3 = f (y (t0 ) − k1 + k2 h, t0 + h)
3 3


k4 = f (y (t0 ) + k1 h − k2 h + k3 h, t0 + h)

k1 + 3k2 + 3k3 + k4
∗ ∗
y (t0 + h) = y (t0 ) + h
8

References

© Copyright 2005 to 2015 Erik Cheever This page may be freely used for educational
purposes.
Comments? Questions? Suggestions? Corrections?
Erik Cheever Department of Engineering Swarthmore College

You might also like