Professional Documents
Culture Documents
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)
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)
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 )
k1 + 2k2 + 2k3 + k4
∗
y (h) = y(0) + h estimate of y(h)
6
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 )
k1 + 2k2 + 2k3 + k4
∗ ∗
y (t0 + h) = y (t0 ) + h estimate of y(t0 + h)
6
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
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
1.5
0 0.1=h/2 0.2=h
t me (S)
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)
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)
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
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:
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)
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 )
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.
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