MA/CS 371 - Lab 9
Comparing Three Different Methods on an ODE
Section 1: Introduction
In this lab you will use Euler's Method, the second-order Runge-Kutta
method, and the classical fourth-order Runge-Kutta method to calculate
approximate solutions to a given ODE.
If you have not already done so, create a directory for this lab and
copy the lab 9 files into it. If you start off in your home directory you
can do the following:
cp ~cs371/lab9/* .
and (as always) don't forget to type that last period.
Section 2: Topics for Lecture
No new MATLAB tricks are required for this lab so there will be no
Section 3: The ODE
The ODE we will be solving in this lab is:
y' = 2y - 2t + 1
y(0) = 2
Normally we use an ODE solver when we can't solve the equation
analytically. In this case, since we're testing various methods,
we've chosen a function whose answer is known. In this case, you
should check that the function
y = t + 2*exp(-2*t)
is the correct solution.
I have supplied the one-line function func.m which you can call
y_prime = func(y,t)
so that when you write your ODE solvers you can have them call
func.m rather than have a hard coded function inside them.
Section 4: The Three Methods
Write three MATLAB functions: euler_meth.m, rk2.m, and
rk4.m. Each should take four input parameters: the left and
right endpoints of the interval, the number of steps, and the value
of the function at the left endpoint. Each function should
output a vector of approximate function values at the indicated
points. So for example after the call
values = euler_meth(0,2,21,0);
the output variable "values" should be a 21-element row vector with
the approximate function values at t = [0 0.1 0.2 ... 2].
Pseudocode for euler_meth is available on page 328 of the text and
rk4 is on pp. 339-340. The text does not include pseudocode
for the second-order Runge-Kutta method but you should be able to
work it out from the description on pp. 337-338 and by similarity with
the other programs.
NOTE: You will have to restructure the pseudocode a little bit in
order to return the answer as a vector.
Section 5: Running your programs
Write a script file ode_methods.m which will do the following:
- Run each ODE solver (euler_meth, rk2, and rk4) on the function in
the range [0,2] with 5, 21, and 101 points (4, 20, and 100 intervals).
- Produce a table showing how many flops each method requires
with each number of intervals.
- Produce three graphs - one for the 5-point case, one for the
21-point case, and one for the 101-point case - where each graph
Make sure that each graph has an appropriate title.
- the true solution as a solid line
- the euler_meth solution as a dotted line (plot symbol ":")
- the rk2 solution as a dashed line (plot symbol "--")
- the rk4 solution as a dot-dash line (plot symbol "-.")
Section 6: Turning In Your Work
When your euler_meth.m, rk2.m, rk4.m, and
ode_methods.m files are ready, type
These programs will be due by 5:45pm on Thursday, April 18.