0% found this document useful (0 votes)
121 views10 pages

MATH2071: LAB 8: Explicit ODE Methods

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

MATH2071: LAB 8: Explicit ODE methods

Introduction Exercise 1
Euler’s method review Exercise 2
The Euler Halfstep (RK2) Method Exercise 3
Runge-Kutta Methods Exercise 4
The Midpoint Method Exercise 5
Adams-Bashforth Methods Exercise 6
Exercise 7

1 Introduction
In this lab we consider solution methods for ordinary differential equations (ODEs). We will be looking at
two classes of methods that excel when the equations are smooth and derivatives are not too large. This lab
will take two class sessions. If you print this lab, you may prefer to use the pdf version.
The lab begins with an introduction to Euler’s method for ODEs. Euler’s method is the simplest approach
to computing a numerical solution of an initial value problem. However, it has about the lowest possible
accuracy. If we wish to compute very accurate solutions, or solutions that are accurate over a long interval,
then Euler’s method requires a large number of small steps. Since most of our problems seem to be computed
“instantly,” you may not realize what a problem this can become when solving a “real” differential equation.
Applications of ODEs are divided between ones with space as the independent variable and ones with
time as the independent variable. We will use x as independent variable consistently. Sometimes it will be
interpreted as a space variable (x-axis) and sometimes as time.
A number of methods have been developed in the effort to get solutions that are more accurate, less
expensive, or more resistant to instabilities in the problem data. Typically, these methods belong to “families”
of increasing order of accuracy, with Euler’s method (or some relative) often being the member of the lowest
order.
In this lab, we will look at “explicit” methods, that is, methods defined by an explicit formula for yk+1 ,
the approximate solution at the next time step, in terms of quantities derivable from previous time step data.
In a later lab, we will address “implicit” methods that require the solution of an equation in order to find
yk+1 . We will consider the Runge-Kutta and the Adams-Bashforth families of methods. We will talk about
some of the problems of implementing the higher order versions of these methods. We will try to compare
the accuracy of different methods applied to the same problem, and using the same number of steps.
Runge-Kutta methods are “single-step” methods while Adams-Bashforth methods are “multistep” meth-
ods. Multistep methods require information from several preceding steps in order to find yk+1 and are a
little more difficult to use. Nonetheless, both single and multistep methods have been very successful and
there are very reliable Matlab routines (and libraries for other languages) available to solve ODEs using both
types of methods.

2 Euler’s method
A very simple ordinary differential equation (ODE) is the explicit scalar first-order initial value problem:

y0 = f (x, y)
y(x0 ) = y0 .

Here y 0 is shorthand for the derivative dy/dx. The equation is explicit because y 0 can be written explicitly as
a function of x and y. It is scalar because we assume that y(x) is a scalar quantity. It is first-order because

1
the highest derivative that appears is the first derivative y 0 . It is an initial value problem (IVP) because we
are given the value of the solution at some time or location x0 and are asked to produce a formula for the
solution at later times.
An analytic solution of an ODE is a formula y(x), that we can evaluate, differentiate, or analyze in any
way we want. Analytic solutions can only be determined for a small class of ODE’s.
A “numerical solution” of an ODE is simply a table of abscissæ and approximate values (xk , yk ) that
approximate the value of an analytic solution. This table is usually accompanied by some rule for inter-
polating solution values between the abscissæ. With rare exceptions, a numerical solution is always wrong;
the important question is, how wrong is it? One way to pose this question is to determine how close the
computed values (xk , yk ) are to the analytic solution, which we might write as (xk , y(xk )).
The simplest method for producing a numerical solution of an ODE is known as Euler’s method. Given
a solution value (xk , yk ), we estimate the solution at the next abscissa by:

yk+1 = yk + hy 0 (xk , yk ).

(The step size is denoted h here. Sometimes it is denoted dx.) We can take as many steps as we want with
this method, using the approximate answer from one step as the starting point for the next step.
Matlab note: In the following function, the name of the function that evaluates y 0 is arbitrary. Recall
that if you do not know the actual name of a function, but it is contained in a Matlab variable (I often use
the variable name “f”) then you can evaluate the function using the Matlab function “feval.” Supposing you
have a Matlab function m-file named my ode.m and its signature line looks like
function yprime=my_ode(x,y)

and suppose this name is contained in the variable f in your m-file. Then you can call the function using
the syntax

yprime=feval(f,x,y);
Some students have difficulty distinguishing when to enclose names in quotes and mistakenly use the
syntax
feval(’f’,x,y) % WRONG! WRONG! WRONG!
This syntax tells Matlab to use the function whose name is f and that can be found in the file named f.m.
In contrast, the correct syntax, feval(f,x,y), tells Matlab to find the function whose name is the value of
the variable f. These are very different things.
Typically, Euler’s method will be applied to systems of ODEs rather than a single ODE. This is because
higher order ODEs can be written as systems of first order ODEs. The following Matlab function m-file
implements Euler’s method for a system of ODEs.
function [ x, y ] = euler ( f, xRange, yInitial, numSteps )
% [ x, y ] = euler ( f, xRange, yInitial, numSteps ) uses
% Euler’s explicit method to solve a system of first-order ODEs
% y’=f(x,y).
% f = name of an m-file with signature
% yprime = f(x,y)
% to compute the right side of the ODE as a row vector
% xRange = [x1,x2] where the solution is sought on x1<=x<=x2
% yInitial = column vector of initial values for y at x1
% numSteps = number of equally-sized steps to take from x1 to x2
% x = row vector of values of x
% y = matrix whose k-th row is the approximate solution at x(k).

2
x=zeros(numSteps+1,1);
x(1) = xRange(1);
h = ( xRange(2) - xRange(1) ) / numSteps;
y(1,:) = transpose(yInitial);
for k = 1 : numSteps
x(k+1) = x(k) + h;
y(k+1,:) = y(k,:) + h * transpose(feval ( f, x(k), y(k,:) ));
end
Warning: In the above code, the initial value is a column vector, and the function that gets called returns a
column vector; however, the values are returned in the rows of the matrix m! The function transpose is used
instead of a prime because a prime without a dot means “Hermitian” or “adjoint” or “conjugate-transpose,”
but only a true transpose is needed here. The .’ operator could have been used, but is harder to read.
In the following exercise, you will use euler.m to find the solution of the initial value problem
y0 = −y + 2x
y(0) = 1
The exact solution of this IVP is y = 3e−x + 2x − 2.
Exercise 1:
(a) If you have not done so already, copy (use cut-and-paste) the above code into a file named euler.m
or download euler.m.
(b) Copy the following code into a Matlab m-file called expm ode.m.
function yprime = expm_ode ( x, y )
% yprime = expm_ode ( x, y ) is the right side function for
% the ODE y’=-y+2*x
% x is the independent variable
% y is the dependent variable
% yprime is y’

yprime = -y+2*x;
(c) Now you can use Euler’s method to march from y=yInit at x=0:
yInit = 1.0;
[ x, y ] = euler ( ’expm_ode’, [ 0.0, 2.0 ], yInit, numSteps );
for each of the values of numSteps in the table below. Use at least four significant figures when
you record your numbers, and you can use the first line as a check on the correctness of the code.
In addition, compute the error as the difference between your approximate solution and the exact
solution at x=2, y=3*exp(-2)+2, and compute the ratios of the error for each value of nstep
divided by the error for the succeeding value of nstep. As the number of steps increases, your
errors should become smaller and the ratios should tend to a limit.
Euler’s explicit method
numSteps Stepsize Euler Error Ratio

10 0.2 2.32212254 8.3883e-02 __________


20 0.1 __________ __________ _________
40 0.05 __________ __________ _________
80 0.025 __________ __________ _________
160 0.0125 __________ __________ _________
320 0.00625 __________ __________

3
(d) You know the error is O(hp ) for some p. There is a simple way to estimate the value of p by
successively halving h. If the error were exactly Chp , then by solving twice, once using h and
the second time using h/2 and taking the ratio of the errors, you would get Chp /(C(h/2)p ) = 2p .
Since the error is only O(hp ), the ratio is only approximately 2p .
Based on the ratios in the table, estimate the order of accuracy of the method, i.e., estimate the
exponent p in the error estimate Chp , where h is the step size. p is an integer in this case.

3 The Euler Halfstep (RK2) Method


The “Euler halfstep” or “RK2” method is a variation of Euler’s method. It is the second-simplest of a family
of methods called “Runge-Kutta” methods. As part of each step of the method, an auxiliary solution, one
that we don’t really care about, is computed halfway, using Euler’s method:
xa = xk + h/2
ya = yk + 0.5hf (xk , yk ) (1)
The derivative function is evaluated at this point, and used to take a full step from the original point:
xk+1 = xk + h;
yk+1 = yk + hf (xa , ya ) (2)
Although this method uses Euler’s method, it ends up having a higher order of convergence. Loosely
speaking, the initial half-step provides additional information: an estimate of the derivative in the middle of
the next step. This estimate is presumably a better estimate of the overall derivative than the value at the
left end point. The per-step error is O(h3 ) and, since there are O(1/h) steps to reach the end of the range,
O(h2 ) overall. Keep in mind that we do not regard the auxiliary points as being part of the solution. We
throw them away, and make no claim about their accuracy. It is only the whole-step points that we want.
In the following exercise we compare the results of RK2 with Euler.
Exercise 2:
(a) Write a Matlab function m-file named rk2.m that implements the Euler halfstep (RK2) method
sketched above in Equations (1) and (2). Keep the same calling parameters and results as for
euler.m above. Keeping these the same will make it easy to compare different methods. The
following model for the file is based on the euler.m file with the addition of the variables xa and
ya representing the auxiliary variables xa and ya in Equation (1). Add comments to this outline,
including explanations of all the variables in the signature line, and fill in expressions where ???
have been left.
function [ x, y ] = rk2 ( f, xRange, yInitial, numSteps )
% comments including the signature, meanings of variables,
% math methods, your name and the date

x=zeros(numSteps+1,1);
x(1) = xRange(1);
h = ( xRange(2) - xRange(1) ) / numSteps;
y(1,:) = transpose(yInitial);
for k = 1 : numSteps
xa = ??? ;
ya = ??? ;
x(k+1) = x(k) + h;
y(k+1,:) = y(k,:) + h * transpose(feval( ??? ));
end

4
(b) Use this file to compute the numerical solution of the model ODE for the exponential, expm ode.m,
from Exercise 1, from x = 0.0 to x = 2.0, and with the same initial value as in Exercise 1, but
using Euler’s halfstep method, RK2, with stepsizes below. For each case, record the value of the
numerical solution at x = 2.0; the error, that is, the difference between the numerical solution
and the true solution at the end point x=2 (y=3*exp(-2)+2); and, the ratios of the error for
each value of numSteps divided by the error for the succeeding value of numSteps. Use at least 4
significant digits when you record values.

RK2
numSteps Stepsize RK2 Error Ratio

10 0.2 2.41234409 6.3382e-03 _________


20 0.1 __________ __________ __________
40 0.05 __________ __________ __________
80 0.025 __________ __________ __________
160 0.0125 __________ __________ __________
320 0.00625 __________ __________
(c) Based on the ratios in the table, estimate the order of accuracy of the method, i.e., estimate the
exponent p in the error estimate Chp . p is an integer in this case.
(d) Compare errors from Euler’s method (Exercise 1) and Euler’s halfstep method for this problem.
You should clearly see that Euler’s halfstep method (RK2) converges much faster than Euler’s
method.
Euler
numSteps Stepsize Error RK2 Error

10 0.2 __________ __________


20 0.1 __________ __________
40 0.05 __________ __________
80 0.025 __________ __________
160 0.0125 __________ __________
320 0.00625 __________ __________
(e) Based on the above table, roughly how many steps does Euler require to achieve the accuracy
that RK2 has for numSteps=10?
(f) You have already found the the error for Euler’s method is approximately CE hpE and the error for
RK2 is approximately CRK2 hpRK2 . Based on one or both of these estimates, roughly how many
steps would Euler require to achieve the accuracy That RK2 has for numSteps=320? Explain your
reasoning.
Be sure to include a copy of your rk2.m file when you send me your summary.

4 Runge-Kutta Methods
The idea in Euler’s halfstep method is to “sample the water” between where we are and where we are going.
This gives us a much better idea of what f is doing, and where our new value of y ought to be. Euler’s
method (“RK1”) and Euler’s halfstep method (“RK2”) are the junior members of a family of ODE solving
methods known as “Runge-Kutta” methods.
To develop a higher order Runge-Kutta method, we sample the derivative function f at even more
“auxiliary points” between our last computed solution and the next one. These points are not considered

5
part of the solution curve; they are just a computational aid. The formulas tend to get complicated, but let
me describe the next one, at least.
The third order Runge Kutta method “RK3,” given x, y and a stepsize h, computes two intermediate
points:

xa = xk + .5h
ya = yk + .5hf (xk , yk ) (3)
xb = xk + h
yb = yk + h(2f (xa , ya ) − f (xk , yk ))

and then estimates the solution as:

xk+1 = xk + h
yk+1 = yk + h(f (xk , yk ) + 4.0f (xa , ya ) + f (xb , yb ))/6.0 (4)

The global accuracy of this method is O(h3 ), and so we say it has “order” 3 method. Higher order Runge
Kutta methods have been computed, with those of order 4 and 5 the most popular.

Exercise 3:
(a) Write a Matlab function m-file called rk3.m with the signature
function [ x, y ] = rk3 ( f, xRange, yInitial, numSteps )
% comments including the signature, meanings of variables,
% math methods, your name and the date

that implements the above algorithm. You can use rk2.m as a model.
(b) Repeat the numerical experiment in Exercise 2 (using expm ode) and fill in the following table.
Use the first line of the table to confirm that you have written the code correctly.
RK3
numSteps Stepsize RK3 Error Ratio
10 0.2 2.40568816 3.1769e-04 __________
20 0.1 __________ __________ __________
40 0.05 __________ __________ __________
80 0.025 __________ __________ __________
160 0.0125 __________ __________ __________
320 0.00625 __________ __________
(c) Based on the ratios in the table, estimate the order of accuracy of the method, i.e., estimate the
exponent p in the error estimate Chp . p is an integer in this case.

Exercise 4: You have not tested your code for systems of equations yet. In this exercise you will do
so by solving the “system”

y10 = −y1 + 2x
y20 = −y2 + 2x. (5)

You can see that this “system” is really expm ode twice, so you can check your results against your
earlier solution.
(a) Write a function m-file named system ode.m to implement 5.

6
(b) Solve the system on the interval [0,2] using rk3 with 40 steps. Call the solution ysystem. What
is ysystem(end,:)? If the two values are different, you have an error somewhere.
(c) Solve using expm ode on the interval [0,2] using rk3 with 40 steps. Call the solution yscalar.
What is yscalar(end)? If this value is different from those of ysystem(end,:), you have an
error somewhere.
Debugging hint: Check that yscalar(1) and ysystem(1,:) also disagree. Use format long
so you can see all decimal places. Using the debugger, look at xa, ya, xb, yb, x(2) and y(2,:)
and compare them with the respective results from expm ode. The earliest difference you see
comes from an incorrect line of code.
(d) Use the following code to compare the solutions for all x values.
norm(ysystem(:,1)-yscalar) % should be roundoff or zero
norm(ysystem(:,2)-yscalar) % should be roundoff or zero

Exercise 5:
The equation describing the motion of a pendulum can be described by the single dependent variable θ
representing the angle the pendulum makes with the vertical. The coefficients of the equation depend
on the length of the pendulum, the mass of the bob, and the gravitational constant. Assuming a
coefficient value of 2.5, the equation is
θ00 + 1.25θ = 0
and one possible set of initial conditions is

θ(x0 ) = 1
θ0 (x0 ) = 0

This second order equation can be written as a system

y10 = y2
y20 = −2.5y1
y1 (0) = 1
y2 (0) = 0.

(Recall that this transformation is accomplished by the change of variables y1 = θ and y2 = θ0 .)


(a) Write a function m-file named pendulum ode.m with signature
function yprime = pendulum_ode(x,y)
% comments including the signature, meanings of variables,
% math methods, your name and the date
Be sure to put comments after the signature line and wherever else they are needed. Be sure that
you return a column vector.
(b) Compare the solutions using the first order Euler method, and the third order RK3 method, over
the interval 0 to 25, with initial condition y=[1;0], and using 100 steps for each method. Fill in
the following table.
x Euler RK3
0.00 1.00 0.00 1.00 0.00
10.00 __________ __________ __________ __________
20.00 __________ __________ __________ __________
25.00 __________ __________ __________ __________

7
(c) Note that conservation of energy guarantees that θ should stay between -1 and 1. Compare the
plots of the two solutions. Can you see anything in the plot of the Euler’s method solution that
might indicate that it is wrong, aside from conservation of energy? You do not have to send me
a copy of the plot.
(d) Solve the ODE using Euler’s method again, but use 10,000 steps instead of 100. Plot both the
refined Euler solution and the original RK3 solution on the same plot and include it with your
summary. Your plots should illustrate the fact that refining the mesh helps Euler, but it is still
inaccurate compared with RK3. Please include this plot with your summary.
Message: Euler’s method is not very accurate, even on nice problems. Our solution curve from Euler’s
method looks smooth, but it’s completely incorrect! You must not accept a solution just because it
“looks nice.”

5 Stability
You have seen a considerable amount of theory about stability of methods for ODEs, and you will see more
in the future. Explicit methods generally are conditionally stable, and require that the step size be smaller
than some critical value in order to converge. It is interesting to see what happens when this stability
limit is approached and exceeded. In the following exercise you will drive Euler’s method and Runge-Kutta
third-order methods unstable using the expm ode function in a previous exercise. You should observe very
similar behavior of the two methods. This behavior, in fact, is similar to that of most conditionally stable
methods.

Exercise 6: Each part of this exercise results in a plot. Please include each of the plots with your
summary.
(a) Use euler to solve the ODE using expm ode, using numSteps=20 and starting from y=1 over the
interval [0,10]. This solution is well within the stable regime. Plot this case.
(b) Same, but over the intervals [0,20], [0,25], [0,30], [0,35], and [0,40]. Continue using numSteps=20.
The consequence of increasing the interval length is to increase the step size. Plot these five solu-
tions on the same plot. You should observe increasing “plus-minus” oscillations in the solutions.
(c) Same, but over the interval [0,45]. This solution “explodes” in a “plus-minus” fashion. Plot this
case.
(d) Use rk3 to solve the ODE using expm ode, using numSteps=20, and starting from y=1 over the
intervals [0,35], [0,40], [0,45], and [0,50]. Plot these on the same plot. The first of these is stable.
(e) Same, but over the interval [0,55]. This solution “explodes” in a “plus-minus” fashion. Plot it.

The message you should get from the previous exercise is that you can observe poor solution behavior
when you are near the stability boundary. The “poor behavior” appears as a “plus-minus” oscillation that can
shrink, grow, or remain of constant amplitude (unlikely, but possible). It can be tempting to accept solutions
with small “plus-minus” oscillations that die out, but it is dangerous, especially in nonlinear problems, where
the oscillations can cause the solution to move to a nearby curve with different initial conditions that has
very different qualitative behavior from the desired solution.

6 Adams-Bashforth Methods
Like Runge-Kutta methods, Adams-Bashforth methods want to estimate the behavior of the solution curve,
but instead of evaluating the derivative function at new points close to the next solution value, they look
at the derivative at old solution values and use interpolation ideas, along with the current solution and

8
derivative, to estimate the new solution. This way they don’t compute solutions at auxiliary points and then
throw the auxiliary values away. The savings can result in increased efficiency.
Looked at in this way, the Euler method is the first order Adams-Bashforth method, using no old points
at all, just the current solution and derivative. The second order method, which we’ll call “AB2,” adds the
derivative at the previous point into the interpolation mix. We might write the formula this way:
yk+1 = yk + h(3f (xk , yk ) − f (xk−1 , yk−1 ))/2
The AB2 method requires derivative values at two previous points, but we only have one. If we simply
used an Euler step, we would pick up a relatively large error on the first step, which would pollute all our
results. In order to get a reasonable starting value, we should use the RK2 method, whose per-step error is
order O(h3 ), the same as the AB2 method.
The following is a complete version of Matlab code for the Adams-Bashforth second-order method.
function [ x, y ] = ab2 ( f, xRange, yInitial, numSteps )
% [ x, y ] = ab2 ( f, xRange, yInitial, numSteps ) uses
% Adams-Bashforth second-order method to solve a system
% of first-order ODEs y’=f(x,y).
% f = name of an m-file with signature
% yprime = f(x,y)
% to compute the right side of the ODE as a row vector
%
% xRange = [x1,x2] where the solution is sought on x1<=x<=x2
% yInitial = column vector of initial values for y at x1
% numSteps = number of equally-sized steps to take from x1 to x2
% x = column vector of values of x
% y = matrix whose k-th row is the approximate solution at x(k).

x=zeros(numSteps+1,1);
x(1) = xRange(1);
h = ( xRange(2) - xRange(1) ) / numSteps;
y(1,:) = transpose(yInitial);
yprime(1,:) = transpose(feval ( f, x(1), y(1,:) ));

k = 1;
xhalf = x(k) + 0.5 * h;
yhalf = y(k,:) + 0.5 * h * yprime(k,:);
yprime1 = transpose(feval ( f, xhalf, yhalf ));

x(k+1) = x(k) + h;
y(k+1,:) = y(k,:) + h * yprime1;
yprime(k+1,:) = transpose(feval ( f, x(k+1), y(k+1,:) ));

for k = 2 : numSteps
x(k+1) = x(k) + h;
y(k+1,:) = y(k,:) + ...
h * ( 3.0 * yprime(k,:) - yprime(k-1,:) ) / 2.0;
if k<numSteps
yprime(k+1,:) = transpose(feval ( f, x(k+1), y(k+1,:) ));
end
end
Exercise 7:

9
(a) Copy the code to a file called ab2.m or download ab2.m.
(b) Take a minute to look over this code and see if you can understand what is happening. Insert the
following two comment lines into the code in the correct locations:
% The Adams-Bashforth algorithm starts here
% The Runge-Kutta algorithm starts here
Be sure to include a copy of the commented code in your summary.
(c) The temporary variable yprime has been introduced here but was not needed in the Euler, RK2
or RK3 methods. What is its use here? (No more than two sentences.)
(d) If numSteps is 100, then exactly how many times will we call the derivative function f?
(e) Use ab2 to compute the numerical solution of the ODE for the exponential (expm ode) from x =
0.0 to x = 2.0, starting at y=1 with step sizes of 0.2, 0.1, 0.05, 0.025, 0.0125 and 0.00625. For
each case, record the value of the numerical solution at x = 2.0, and the error, and the ratios of
the errors. The first line of the table can be used to verify that your code is operating correctly.
AB2
numSteps Stepsize AB2(x=2) Error(x=2) Ratio

10 0.2 2.42020989 1.4204e-02 __________


20 0.1 __________ __________ __________
40 0.05 __________ __________ __________
80 0.025 __________ __________ __________
160 0.0125 __________ __________ __________
320 0.00625 __________ __________
(f) Based on the ratios in the table, estimate the order of accuracy of the method, i.e., estimate the
exponent p in the error estimate Chp . p is an integer in this case.

Adams-Bashforth methods try to squeeze information out of old solution points. For problems where the
solution is smooth, these methods can be highly accurate and efficient. Think of efficiency in terms of how
many times we evaluate the derivative function. To compute numSteps new solution points, we only have
to compute roughly numSteps derivative values, no matter what the order of the method (remember that
the method saves derivative values at old points). By contrast, a third order Runge-Kutta method would
take roughly 3*numSteps derivative values. In comparison with the Runge Kutta method, however, the old
solution points are significantly further away from the new solution point, so the data is less reliable and a
little “out of date.” So Adams-Bashforth methods are often unable to handle a solution curve which changes
its behavior over a short interval or has a discontinuity in its derivative. It’s important to be aware of this
tradeoff between efficiency and reliability!

10

You might also like