Project Template 2
Project Template 2
Department of Mathematics
Second Year Honours Project
Abstract
In this project, we first study mumeric methods for ordinary differential equations such
as Euler’s Method, Improved Euler Method and classical Runge-Kutta Method. Differential
equations are used in mathematical modeling problems. Linear differential equations can be
solved analytically but most real life applications are approximate solution. Euler and Runge-
Kutta Methods are explained and illustrated as useful numerical methods for solving nonlinear
differential equations. Accuracy of a numerical method depends on the step size used and
degree of nonlinearity of the equations. Although better accuracy can be obtained with
smaller step size, this takes more computational effort and time. Authors use the MATLAB
software to overcome this challenge and to avoid computational error. The Runge-Kutta
Method is more applicable and accurate for diverse classes of differential equations.
1.1 Introdrction
An ordinary differential equation (ODE) of the first order is of the form F (x, y, y ′ ) = 0 and
can be written in the explict form y ′ = f (x, y). An initial value problem for the equation is
of the form
(1) y ′ = f (x, y) , y(x0 ) = y0
where x0 and y0 are given, y ′ is a function of the independent variable x and the dependent
variable y. We assume that the problem has a unique solution on some open interval a < x < b
containing x0 . From calculus we know that the derivative y ′ (x) of y(x) is the slope of y(x).
Hence a solution curve of (1) that passes through a point (x0 , y0 ) have, at that point, the
slope y ′ (x0 ) equal to the value of f at that point;
1
y ′ (x0 ) = f (x0 , y0 )
Using this fast, we can develop numeric methods for obtaining approximate solutions of ODEs
(1).
Figure 1.2: Euler’s Method for approximating the solution to the initial value problem
y(x) − y0 = m(x − x0 )
y(x) = y0 + m(x − x0 )
2
Setting x = x0 yields approximate solution value at x1 namely
y1 = y0 + f (x0 , y0 )(x1 , x0 )
y1 = y0 + hf (x0 , y0 )
We can use the same idea, at x2 , tangent line to the solution curve through (x1 , y1 )
y2 = y1 + hf (x1 , y1 )
yn+1 = yn + hf (xn , yn ), n = 0, 1
y′ = 1 + y2, y(0) = 0
Use Euler’s Method with (a) h = 0.1 and h = 0.2 to obtain an approximation to y(1). Given
that the exact solution is y(x) = tanx at (0, 0). Compare the error.
Solution:
y′ = 1 + y2, y(0) = 0, x0 = 0, y0 = 0
f (x, y) = 1 + y 2 , f (xn , yn ) = 1 + yn 2
3
(a) h = 0.1
xn = xn−1 + h
1 1
Number of iterlation, n = = = 10
h 0.1
yn+1 = yn + 0.1(1 + yn 2 )
Hence,
x = 0.1,
y1 = y0 + 0.1(1 + y0 2 ) = 0 + 0.1(1 + 02 ) = 0.1
x = 0.2,
y2 = y1 + 0.1(1 + y1 2 ) = 0.1 + 0.1(1 + 0.12 ) = 0.201
(b) h = 0.2
1
yn+1 = yn + 0.2(1 + yn 2 ), n= =5
0.2
Hence,
x = 0.2,
y1 = y0 + 0.2(1 + y0 2 ) = 0 + 0.2(1 + 02 ) = 0.2
x = 0.4,
y2 = y1 + 0.2(1 + y1 2 ) = 0.1 + 0.2(1 + (0.2)2 ) = 0.0.408
4
x h = 0.1, yn h = 0.2, yn Extract(6D) h=0.2, h= 0.4,
Error Error
0.0 0.000000 0.000000 0.000000 0.000000 0.000000
0.1 0.100000 0.100335 0.000335
0.2 0.201000 0.200000 0.202710 0.001710 0.002710
0.3 0.305040 0.309336 0.004296
0.4 0.414345 0.408000 0.422793 0.008448 0.0147930
0.5 0.531513 0.546302 0.014789
0.6 0.659764 0.641293 0.684137 0.024373 0.042844
0.7 0.803293 0.842288 0.038996
0.8 0.967821 0.923544 1.029639 0.061818 0.106095
0.9 1.161488 1.260158 0.098670
1.0 1.396394 1.294131 1.557408 0.161014 0.263277
Figure 1.3: The two appromation curves obtained using Euler’s Method and exact solution
of example 1.1
In this figure 1.3, we see that the smaller step has led to a better approximation.
5
1.3 Drivation for Euler’s Method
We briefy introduced Euler’s Method with an example. In this section, we shall develop
Euler’s method by paying close attention to the derivation that uses Taylor’s formula from
calculus to approximate the solution to a first-order ODE at points that are a distance h
apart.
The Taylor series
h2
(2) y(x + h) = y(x) + hy ′ (x) + y”(x) + ...
2
This formula is the key idea that let us develop Euler’s method. Let us start by deriving
Euler’s method. For small h the higher powers h2 , h3 , ... in (2) are very small. Dropping all
of the gives the crude approximation.
= y(x) + hf (x, y)
Geometrically, this is an approximation of the curve of y(x) by a polygon whose first side is
tangent to this curve at x0 . (See Figure 1.2)
1
y(x + h) = y(x) + hy ′ (x) + h2 y”(η), x≥η ≥x+h
2
It shows that the error in each step or local error in the Euler Method is proportional to h2 ,
written O(h2 ). Now, over a fixed x interval, the number of steps is proportional to h1 . Hence
global error is proportional to h2 ( h1 ) = h. Hence, the Euler Method is called a first-order
method.
6
1.5 Improved Euler Method(Heun’s Method)
Figure 1.4: Heun’s Method for approximating the solution to the initial value problem
The method that we consider here is also called a predictor-corrector method. The idea
is to use the formula from Euler’s method to obtain a first approximation to the solution
y(xn+1 ). We denote this approximation by yn+1 , so that first the predictor
(3a)
∗
yn+1 = yn + hf (xn , yn )
which is an auxiliary value, and then we now improve (or correct) this approximation by one
more applying Euler’s Method.
∗ ). This gives the
We use the average of the solution curves through (xn , yn ) and (xn+1 , yn+1
corrector
(3b)
1 ∗
yn+1 = yn + h[f (xn , yn ) + f (xn+1 , yn+1 )]
2
In each step, we predict a value (3a) and then we correct it by (3b)
In summmary, the Improved Euler Method for approximating the solution to the initial value
problem
y ′ = f (x, y), y(x0 ) = y0
1
yn+1 = yn + [k1 + k2 ]
2
where
∗
yn+1 = yn + hf (xn , yn ), n = 0, 1, ...
7
1.6 Derivation for Heun’s Method
Recall First Order derivative of the given function f (x, y)
Using Chain rule,
δf δf dy δf δf
f ′ (xn , yn ) = + = + f
δx δy dx δx δy
Let
yn+1 = yn + (a1 k1 + a2 k2 )h
k1 = f (xn , yn )
k2 = f (xn + p1 , yn + q11 k1 h)
f ′ (xn , yn ) 2
yn+1 = yn + f (xn , yn )h + h + O(h3 )
2
δf δf h2
= yn + f (xn , yn )h + + f + O(h3 ) ........(ii)
δx δy 2
δf δf
k2 = f (xn + p1 , yn + q11 k1 h) = f (xn , yn ) + p1 h + q11 k1 h + O(h2 )
δx δy
8
Using this in eq(i), we generate
δf δf
yn+1 = yn + [a1 f (xn , yn ) + a2 (f (xn , yn ) + p1 h + q11 k1 h + O(h2 ))]h
δx δy
δf δf
= yn + [a1 f (xn , yn ) + a2 f (xn , yn )]h + [a2 p1 + a2 q11 f (xn , yn ) ]h2 + O(h3 ) ........(iii)
δx δy
a1 + a2 = 1
1
a2 p 1 =
2
1
a2 q11 =
2
4 unknowns and 3 equations. We choose one of them arbitrary and calculate the others. This
means there are many different 2nd Order Runge-Kutta Methods.
Select a2 = 12 ⇒ a1 = 12 , p1 = 1, q11 = 1
1 1
yn+1 = yn + ( k1 + k2 )h, k1 = f (xn , yn ), k2 = f (xn + h, yn + hk1 )
2 2
Example 1.6.1. Apply the improved Euler’s method with h = 0.2 to determine an approxi-
mation to the solution to the initial value problem
For h = 0.1,
n = 0, 1, ..., N − 1, N = 10
xn+1 = xn + h
1
yn+1 = yn + (k1 + k2 )
2
9
where
k1 = hf (xn , yn )
k2 = hf (xn+1 , yn + k1 )
k1 = 0.2(1 + yn2 )
k2 = 0.2(1 + (yn + k1 )2 )
When n = 0,
k1 = 0.2(1) = 0.2
= 0.204
When n = 1,
= 0.425163
10
n xn yn Exact(6D) Absolute Error
0 0.0 0.000000 0.000000 0.000000
1 0.2 0.204000 0.202710 0.001290
2 0.4 0.425163 0.422793 0.002369
3 0.6 0.686973 0.684137 0.002836
4 0.8 1.030472 1.029639 0.000834
5 1.0 1.544841 1.557408 0.012567
Table 1.2: The results of applying Improved Euler’s Method with h=0.2
11
Figure 1.6: Runge-Kutta Method for approximating the solution to the initial value problem
12
1
yn+1 = yn + (k1 + 2k2 + 2k3 + k4 )
6
where
k1 = hf (xn , yn )
1 1
k2 = hf (xn + h, yn + k1 )
2 2
1 1
k3 = hf (xn + h, yn + k2 )
2 2
k4 = hf (xn+1 , yn + k3 ) n = 0, 1, 2, ...
where knowing the value of y = yn at xn , we can find the value of y = yn at xn+1 and
h = xn+1 − xn .
Equation (i) is equated to finish five terms of Taylor Series
dy 1 d2 y 2 1 d3 y
yn+1 = yn + (xn+1 − xn ) + (x n+1 − x n ) + (xn+1 − xn )3 .....(ii)
dx 2! dx2 3! dx3
dy
We have, dx = f (x, y) and xn+1 − xn = h
1 ′ 1 1
yn+1 = yn + f (xn , yn )h + f (xn , yn )h2 + f ′′ (xn , yn )h3 + f ′′′ (xn , yn )h4 .....(iii)
2! 3! 4!
Based on equating equ(ii) and (iii), one of the popular solutions used is
1
yn+1 = yn + (k1 + 2k2 + 2k3 + k4 )h
6
13
where
k1 = f (xn , yn )
h 1
k2 = f (xn + , yn + k1 h)
2 2
h 1
k3 = f (xn + , yn + k2 h)
2 2
k4 = f (xn+1 + h, yn + k3 h)
Example 1.8.1. Apply the fourth-order Runge-Kutta method with h = 0.2 to determine an
approximation to the solution to the initial value problem below x = 1.
y′ = 1 + y2, y(0) = 0
f (xn , yn ) = 1 + yn2
k1 = 0.2(1 + yn2 )
k1 2
k2 = 0.2(1 + (yn + ) )
2
k2
k3 = 0.2(1 + (yn + )2 )
2
k3 2
k4 = 0.2(1 + (yn + ) )
2
14
when n=0,
k1 = 0.2(1) = 0.2
= 0.202707
15
n xn yn Exact(6D) Absolute Error
0 0.0 0.000000 0.000000 0.000000
1 0.2 0.202707 0.202710 0.000003
2 0.4 0.422789 0.422793 0.000004
3 0.6 0.684133 0.684137 0.000003
4 0.8 1.029637 1.029639 0.000002
5 1.0 1.557352 1.557408 0.000056
Table 1.3: The Results of applying Runge-Kutta Fourth Order Method with h=0.2
Figure 1.7: Runge-Kutta Method for approximating the solution to the initial value problem
16
Table 1.4: Comparsion of the Accuracy of the three Methods under Condideration
in the Case of IVP with h=0.2
Error
x y = tan(x) Euler Improved Euler Runge-Kutta
0.2 0.202710 0.002710 0.001290 0.000003
0.4 0.422793 0.014793 0.002360 0.000004
0.6 0.684137 0.042844 0.002836 0.000003
0.8 1.029639 0.106095 0.000834 0.000002
1.0 1.557408 0.263277 0.012567 0.000056
Figure 1.8: Comparing Exact Solution of y(x) with numerical method for h=0.2
17
(Solution: y = 1/[2.5 − S(x)] + 0.01x2 where S(x) is the Fresnel Integral.)
Solve (4) by Euler, improved Euler and RK methods for 0 ≤ x ≤ 5 with step h = 0.2. Com-
pare the errors for (x = 1, 3, 5) and graph solution curve of the ODE in (4).
whenn = 0,
f (x0 , y0 ) = f (0, 0.4) = 0
when n = 1,
f (x − 1, y1 ) = f (0.2, 0.4) = 0.01038
and so on.
k1 = hf (xn , yn )
k2 = hf (xn+1 , yn + k)
1
yn+1 = yn + (k1 + k2 )
2
18
when n=0,
k1 = hf (x0 , y0 ) = 0.2(0) = 0
k2 = hf (x1 , y0 + k) = 0.2f (0.2, 0.4)
= 0.2(0.01038)
1
y1 = y0 + (k1 + k2 )
2
1
= 0.4 + (0.2)(0.01038)
2
= 0.40104
and so on.
By using Runge-Kuffa 4th order method,
k1 = hf (xn , yn )
1 1
k2 = hf (xn + h, yn + k1 )
2 2
k3 = hf (xn + h, yn + k3 )
1
yn+1 = yn + (k1 + k2 + k3 + k4 )
6
19
when n=0,
h k1
k2 = hf (x0 + , y0 + ) = hf (0.1, 0.4)
2 2
= 0.2(0.00359) = 0.000718
h k2
k3 = hf (x0 + , y0 + ) = hf (0.1, 0.4004)
2 2
= 0.2(0.003595) = 0.00719
1
y1 = y0 + (k1 + 2k2 + 2k3 + k4 )
6
= 0.4 + 0.00083 = 0.40083
and so on.
Table 1.5: Comparsion of the Accuracy of the three Methods under Condideration
in the Case of IVP with h=0.2 at x = 1, 3, 5
Error
x Exact(5D) Euler Improved Euler Runge-Kutta
1.0 0.46668 0.02002 0.00045 0.00000
3.0 0.66896 0.06259 0.01182 0.00025
5.0 0.75788 0.05154 0.01040 0.00134
20
xn Exact (5D) Euler method Improved Euler Runge-kutta
0 0.4 0.4 0.4 0.4
0.2 0.40083 0.40000 0.40104 0.40083
0.4 0.40504 0.40208 0.40545 0.40504
0.6 0.41534 0.40879 0.41593 0.41535
0.8 0.43481 0.42275 0.43545 0.4348
1 0.46668 0.44666 0.46713 0.46668
1.2 0.51340 0.48275 0.51311 0.51343
1.4 0.57286 0.53104 0.57071 0.57281
1.6 0.63014 0.58505 0.62505 0.62993
1.8 0.65469 0.62583 0.64758 0.65487
2 0.62987 0.62611 0.62355 0.6299
2.2 0.57982 0.58212 0.57815 0.58009
2.4 0.54717 0.53441 0.5483 0.54725
2.6 0.55595 0.52129 0.55625 0.55587
2.8 0.60858 0.55058 0.60403 0.60841
3 0.66896 0.60637 0.65714 0.66921
3.2 0.66929 0.64035 0.65718 0.67007
3.4 0.63101 0.61102 0.62604 0.62929
3.6 0.62947 0.58315 0.62635 0.6287
3.8 0.68869 0.61333 0.67596 0.68821
4 0.73027 0.6705 0.70868 0.73044
4.2 0.70065 0.6715 0.69004 0.69914
4.4 0.70109 0.64244 0.69307 0.69996
4.6 0.76681 0.67972 0.7435 0.76627
4.8 0.78013 0.7305 0.75471 0.77975
5 0.75788 0.70634 0.74748 0.75654
Table 1.6
21
Figure 1.9: Comparing
22
Reference
23