U NIVERSITY OF THE W ITWATERSRAND, J OHANNESBURG
School of Computational and Applied Mathematics
Numerical Methods
Lab 5
Instructions
• Read all the instructions carefully.
• M ATLAB has a help file for every function if you get stuck.
• There are also numerous sources available on the internet, Google is your friend!
SECTION 1
Main Exercises
Consider the initial value problem
dy
= f (x, y),
dx
y(x 0 ) = y 0
(†)
which is to be solved using any of the following numerical scheme:
Euler Method : y k+1 = y k + h f (x k , y k )
!
"
Improved Euler Method: y k+1 = y k + h2 f (x k , y k ) + f (x k + h, y k + h f (x k , y k )
Midpoint Method : y k+1 = y k + h f (x k + h2 , y k + h2 f (x k , y k ))
Runge kutta Method (Order 4): y k+1 = y k + 16 (k 1 + 2k 2 + 2k 3 + k 4 ),
where k 1 = h f (x k , y k ) ,
k 2 = h f (x k + h2 , y k + k21 ) ,
k 3 = h f (x k + h2 , y k + k22 ),
k 4 = h f (x k + h, y k + k 3 )
Program Euler’s Method for solving initial value problems for an ordinary differential equation given
in (†). Your function should take as five inputs, the function given in equation (†) (passed as a
function handle), , the initial x value, , the initial y value, , the number of subintervals and
the final x value to be evaluated. The output should be x, a (N +1)→1 column vector and y, a (N +1)→1
column vector. The first row then would be the initial condition. The first line of your function should
look like this:
function [ x , y ] = Euler ( f , x0 , y0 ,N, x f )
1
Program Improved Euler’s Method (or Heun’s Method) for solving initial value problems for an
ordinary differential equation given in (†). Your function should take as five inputs, the function
given in equation (†) (passed as a function handle), , the initial x value, , the initial y value, ,
the number of subintervals and
the final x value to be evaluated. The output should be x, a
(N + 1) → 1 column vector and y, a (N + 1) → 1 column vector. The first row then would be the initial
condition. The first line of your function should look like this:
function [ x , y ] = Heun( f , x0 , y0 ,N, x f )
Program the Midpoint Method for solving initial value problems for an ordinary differential equation
given in (†). Your function should take as five inputs, the function given in equation (†) (passed as
a function handle), , the initial x value, , the initial y value, , the number of subintervals and
the final x value to be evaluated. The output should be x, a (N +1)→1 column vector and y, a (N +1)→1
column vector. The first row then would be the initial condition. The first line of your function should
look like this:
function [ x , y ] = Midpoint ( f , x0 , y0 ,N, x f )
Program the Runge-Kutta 4th order Method for solving initial value problems for an ordinary
differential equation given in (†). Your function should take as five inputs, the function given in
equation (†) (passed as a function handle),
, the initial x value,
, the initial y value, , the
number of subintervals and
the final x value to be evaluated. The output should be x, a (N + 1) → 1
column vector and y, a (N + 1) → 1 column vector. The first row then would be the initial condition.
The first line of your function should look like this:
function [ x , y ]= rk4 ( f , x0 , y0 ,N, x f )
————————————————————————————————————————
Program the Runge-Kutta 4th order method for solving an initial value problem for any system of n
linear first order ODEs. Your function should take as four inputs, the function (passed as a function
handle), a vector xspan = [
] where
and
are the initial and final x values, yω0, a row vector
of initial values for y and its derivation at x 0 and , the number of subintervals. The output should
be x, a (N + 1) → 1 column vector and y, an (N + 1) → n matrix with the first column representing the
y values, the second column, the y ↑ values, . . . , the last column, the y (n↓1) values. Note that y (n↓1)
means the (n ↓ 1)th derivative of y . The first row then would be the initial condition. The first line of
your function should look like this:
2
function [ x , y ]= RK4system ( ω
f , xspan , yω0 ,N)
NB: Your function must be able to return the desired output, given any system of n first order ordinary
differential equations, where n ↔ 2.
(Bonus Question)
Combine your functions for Euler’s method, Heun’s method and Runge-Kutta 4th order method in
the following way. Your function should take as inputs: f , the function handle of the function in
question, x0, the initial x-value, y0, the initial y value, h, the step size, xf, the final x value to be
evaluated and ytrue, the true solution vector at all x values. Your function must now return a matrix
errorMat. The first column of errorMat is the vector of domain points x = x 0 , x 1 , . . . , x f . The next three
columns contain the absolute error of the three methods at each x. That is, errorMat is a matrix of size
x f ↓x 0
(N +1)→4, where N = h and where columns 2 to 4 contains the absolute error of each method over
all x values in the domain. The first row will be the absolute error at x 0 . The second column should be
for Euler’s method, the third for Heun’s method and the last for 4th order Runge-Kutta method. The
first line of the function should look like:
function errorMat = ivpmethods ( f , x0 , y0 , h , xf , ytrue )
Suppose that you are to solve the first order problem
dy
4
= (1 ↓ x)y,
dx
3
y(0) = 0
(†)
For any differential equation in the form (†), e.g., y ↑ = f (x, y), we begin the numerical solution process
by defining the function f (x, y).
For single equations of the form y ↑ = f (x, y), we can define f (x, y) as an anonymous function.
For example, in equation (†), we can create a handle (given by variable fun) to the anonymous
function given by the right hand side of (†) via:
fun = @(x, y) (1 ↓ 4/3x)y.
The usage, in the case of Exercise 4 is
[ x , y ]= rk4 ( fun , x0 , y0 ,N, x f )
or by direct substitution,
[ x , y ]= rk4 (@( x , y ) (1 !4/3 x ) y , x0 , y0 ,N, x f )
Alternatively, we can define a function given by
function dy = func ( x , y )
dy=(1 !4/3x ) y
end
3
and then either create a handle via funchandle = @func and then use as
[ x , y ]= rk4 ( funchandle , x0 , y0 ,N, x f )
or directly
[ x , y ]= rk4 ( @func , x0 , y0 ,N, x f )
In the case of nth order ivp (n>2), which gets transformed to a system of n first orders, a similar
approach holds. Consider the 3rd order
d3y
d2y
+
y
= 0,
d x3
d x2
The ode is transformed to
y(0) = 0; y ↑ (0) = 0 y ↑↑ (0) = 1.
0↗x ↗5
dω
y
ω (x,ω
=F
y)
dx
where
ω (x,ω
F
y) =
y2
y3
↓y 1 y 3
ω (x,ω
Function F
y ) can be defined as an anonymous function via
fun = @(x, y) [y(2) y(3) ↓ y(1)y(3)].
The variable y, in the case of vector equation, is to be seen as a vector with components y(1), y(2), . . .
etc.
Alternatively, we can define a function given by
function dy = func ( x , y )
dy=[ y ( 2 ) ; y ( 3 ) ; ! y ( 1 ) y ( 3 ) ]
end
and then create a handle @func. The usage in Exercise 5 will be
function [ x , y ]= RK4system ( @func , xspan , yω0 ,N)
4