MATH2071: LAB 1 (B) : Using Matlab ODE Solvers
MATH2071: LAB 1 (B) : Using Matlab ODE Solvers
Introduction Exercise 1
Matlab ODE solvers Exercise 2
ODE systems Exercise 3
Goodwins economic model Exercise 4
Higher Order ODEs Exercise 5
Phase Plane Plots Exercise 6
Sti systems Exercise 7
Roundo errors Exercise 8
1 Introduction
This version of the rst lab is intended only for students who have
already taken Math 2070.
There are two versions of the rst lab. This version introduces the Matlab ODE solvers and is intended for
students who took Math 2070. If you have not already taken Math 2070, please see Lab 1(a). That version
of the rst lab introduces the Matlab environment and programming language, and presents the general
format of the work you need to hand in.
This lab is concerned with solution of ordinary dierential equations (ODEs) using a Matlab function
for the solution. You will see it applied rst to a simple scalar equation, then to a system of equations, and
then to a higher order equation converted into a system. In later labs, you will be writing your own ODE
solver routines so you can understand the underlying theory.
A very simple ordinary dierential equation (ODE) is the explicit scalar rst-order initial value problem:
y
= f(x, y)
y(x
0
) = y
0
.
Here y
or y
(4)
The third assumption is that the relative rates of change of productivity and population are both con-
stants. Productivity is the average value of goods produced by employees
productivity =
GNP
numberEmployed
and population was used above in the denition of y
2
. Thus
dproductivity/dt
productivity
= (5)
dpopulation/dt
population
= (6)
A simple consequence of (5) is that
dGNP/dt
GNP
=
dnumberEmployed/dt
numberEmployed
+ . (7)
The fourth assumption is that the relative change in wages is a function of employment.
dwages/dt
wages
= + y
2
. (8)
A consequence of the derivative of denition of employment rate, e, (7), (4) and (6) is one of the two
dierential equations for Goodwins model
dy
2
/dt
y
2
=
1 y
1
. (9)
A consequence of the derivative of the denition of wage share, y
1
, is the other equation for Goodwins
model
dy
1
/dt
y
1
=
dwages/dt
wages
+
dnumberEmployed/dt
numberEmployed
dGNP/dt
GNP
= + y
2
(10)
In summary, Goodwins model can be written
dy
1
dt
= ( + )y
1
+ y
1
y
2
(11)
dy
2
dt
= (
1
)y
2
y
1
y
2
(12)
where the Greek letters signify positive constants.
4
Exercise 2:
(a) Copy the following code to a Matlab function m-le called goodwin ode.m, that returns the
derivative function as a column vector:
function yprime = goodwin_ode ( x, y )
% comments
% your name and the date
ALPHA =0.4;
BETA =0.9;
DELTA =0.05;
NU =0.01;
RHO =5.0;
yprime=zeros(2,1);
yprime(1)=-(DELTA+ALPHA)*???+BETA*y(1)*y(2);
yprime(2)=(1/RHO-DELTA-NU)*???-y(1)*y(2)/RHO;
(b) Add comments just below the signature to indicate the calling and return sequences and to provide
a summary of what the function does.
(c) Replace the symbols ??? with the proper quantities so goodwin ode implements Goodwins model
equations.
(d) What is the role of the line
yprime=zeros(2,1);
What would happen if it were left out?
(e) Note that the variable x, corresponding to time, is unused. Nonetheless, it must be present in the
signature.
(f) Now use ode45 to integrate from x = 0 to x = 100 starting from y(1)=0.5, y(2)=0.5;
yInit = [ 0.5
0.5 ];
ode45 ( goodwin_ode, [ 0.0, 100.0 ], yInit );
(g) Why do you get two curves? Please include a copy of this plot with your summary.
(h) Open a blank plot window with the command figure(2). Solve the same system, but this time
use ode15s. Your solution should look the same, but you should see a slightly dierent distribution
of circles indicating a dierent set of step sizes.
5 Higher Order ODEs
The previous examples are for rst order equations and systems. What about higher order dierential
equations, ones that include derivatives such as y
+ 4z
+ 3z
+ 2z
+ z = sin(x)
with initial conditions
z
(
0) = 0, z
(0) = 10, z
(0) = 20, z
(0) = 30.
5
The rst step is to dene new variables
y
1
= z
y
2
= z
y
3
= z
y
4
= z
(Note that the original equation is fourth order and there are four new variables dened. With these new
variables, the original scalar equation can be written as a system of equations
y
1
= y
2
y
2
= y
3
y
3
= y
4
y
4
= (sin x y
1
2y
2
3y
3
4y
4
)/5
Exercise 3: A pendulum swings through a circular arc. At any time, the angle (x) the pendulum
makes with the downward vertical is described by Newtons law (F = ma):
+ 1.5 = 0
with initial conditions
(x
0
) = 1
(x
0
) = 0
(a) Rewrite this as a rst order system. (Hint: set y
1
= and y
2
=
= 0
= 32
where denotes distance along the ground, and denotes height. The independent variable, x, is time.
The initial conditions are
(0) = 0
y
3
=
y
4
=
(a) Write a function m-le called cannon ode.m to implement the system. Be sure to include com-
ments and to have the function return a column vector.
(b) Use ode45 to integrate the equations of motion and determine, by trial and error, a value of so
that the cannon ball hits the wall. You can determine success by checking that 0 10 when
= 300. Watch out: the condition is on , not x. You can nd the rst step beyond = 300 using
the Matlab find statement. The rst index, k for which
k
300 is k=min(find(y(:,1)>=300)).
Alternatively, you can plot the balloons trajectory and see where it hits the ground or the wall.
You can plot the trajectory (, ) with
plot ( y(:,1), y(:,3) )
You might nd the axis([xmin,xmax,ymin,ymax]) command in Matlab useful to zoom in on the
target, or you can use the magnifying icon on the plot window. (Click on the magnifying glass
and then use the mouse to outline a square by clicking on the upper left and dragging. Warning:
this might cause Matlab to crash on the computers in GSCC.)
Include the le cannon ode.m and the value of alpha you used when you turn in this exercise.
6 Phase Plane Plots
In many problems, a system is dened by a second order ODE whose right hand side does not depend on
time. Such a system is called autonomous. The behavior of the solution doesnt depend on when you
start the problem, only on the initial condition. Most of the above examples are autonomous. Especially for
these kind of problems, it can be useful to look at the solution on a special phase plane plot. This simply
means that instead of plotting x versus the solution y, we plot y versus y
on the vertical axis, on the horizontal axis) you get a circle. Of course, once you go around the
circle once, the autonomous nature of the system guarantees that you will continue going around forever, so
the periodic behavior is highlighted. If you add a little damping to the pendulum equation, the phase plane
plot becomes a slow spiral into the center.
Exercise 5: An equation for motion of a pendulum including a frictional term proportional to velocity
is
+ .2
+ 1.5 = 0.
(a) Copy your le pendulum ode.m to decay ode.m and modify it to include the frictional term .2
.
(b) Use ode45 to solve the resulting ODE for x between 0 and 20, starting from the same initial
condition as in Exercise 3 above, [1;0].
(c) Plot it in phase space ( along the horizontal axis and
+ a(z
2
1)z
+ z = 0 (13)
For positive a, it can be seen by analogy with decay ode.m and grow ode.m that solutions z to (13) will
grow when z < 1 and shrink when z > 1. The result is a non-linear oscillator. Physical systems that behave
somewhat as a van der Pol oscillator include electrical circuits with semiconductor devices such as tunnel
diodes in them (see this web page from Duke, and some biological systems, such as the beating of a heart,
or the ring of neurons.
Exercise 7: This exercise illustrates phase plane plots using the van der Pol ODE.
(a) Rewrite the van der Pol equation as a rst-order system, and place the code in a function m-le
named vanderpol ode.m. Use the value a = 1. Be sure you place comments where they belong.
(b) Use ode45 to solve the system of ODEs for x between x=0 to x=20, starting from an initial
condition of yInitial = [0; 0.25] (notice the semicolon, making it a column vector). Make a
phase plane plot from your solution.
(c) Now, on the same plot (use the Matlab command hold on), plot the solution starting from the
initial condition [0;3]. Send me the plot and your guesses about:
What would the plot look like if you started at other points close to the ones you already
did?
What would the plot look like if you started very near the origin?
What would the plot look like if you started exactly on the origin?
7 Sti systems
ODEs can be either sti or non-sti. We will look at sti ODEs again later in the term, but it is instructive
to look at one practical method for distinguishing sti from non-sti ODEs. Basically, you try both non-sti
and sti solvers and note which is faster.
Take my word that the IVP
y
=1000(sinx y) (14)
y(0) =1
can be regarded as a sti system.
8
Exercise 8:
(a) Implement the right side of the above IVP, Equation (14) in a function m-le named stiff ode.m.
You may want to start from ex1 ode.m.
(b) Integrate it from x=0 to x=15, starting from y=1 using ode45. Measure the time it takes with the
commands
tic;[x,y]=ode45(stiff_ode,[0,15],1);toc
How long does it take? How many steps did it take (length(x)-1)?
(c) Use the following command to plot your result, using a blue line.
plot(x,y,b)
(d) Repeat the above steps using ode15s. Again, how much time does it take (should be much
smaller)? How many steps does it take (length(x)-1)? Plot the results in red on the same frame
as above. You should see the lines almost on top of one another, so the solutions are essentially
the same. Please include this plot with your summary.
8 Roundo errors
Last term you saw some eects of roundo errors. Later in this term you will look at roundo errors again.
Right now, though, is a good time to look at how some roundo errors come about.
In the exercise below we will have occasion to use a special matrix called the Frank matrix. Row k of
the n n Frank matrix has the formula:
A
k,j
=
_
_
_
0 for j < k 2
n + 1 k for j = k 1
n + 1 j for j k
The Frank matrix for n = 5 looks like:
_
_
_
_
_
_
5 4 3 2 1
4 4 3 2 1
0 3 3 2 1
0 0 2 2 1
0 0 0 1 1
_
_
_
_
_
_
The determinant of the Frank matrix is 1, but is dicult to compute numerically. This matrix has a special
form called Hessenberg form wherein all elements below the rst subdiagonal are zero. Matlab provides
the Frank matrix in its gallery of matrices, gallery(frank,n), but we will use an m-le frank.m.
The inverse of the Frank matrix also consists of integer entries and an m-le for it can be downloaded as
frank inv.m. You can nd more information about the Frank matrix from the Matrix Market, and the
references therein.
Exercise 9: Lets look carefully at the Frank matrix and its inverse. For convenience, dene A to be
the Frank matrix of order 6, and Ainv its inverse, computed using frank and frank inv. Similarly,
let B and Binv be the Frank matrix of order 24 and its inverse. Do not use the Matlab inv function
for this exercise! You know that both A*Ainv and B*Binv should equal the identity matrices of order
6 and 24 respectively. Lets just look at the top left entry. Compute
A(1,:)*Ainv(:,1)= __________
B(1,:)*Binv(:,1)= __________
Both of these answers should equal 1. The rst does and the second does not. To see what goes right,
compute the terms:
9
A(1,6)*Ainv(6,1)= __________
A(1,5)*Ainv(5,1)= __________
A(1,4)*Ainv(4,1)= __________
A(1,3)*Ainv(3,1)= __________
A(1,2)*Ainv(2,1)= __________
A(1,1)*Ainv(1,1)= __________
sum = __________
Note that the signs alternate, so that when you add them up, each term tends to cancel part of the
preceeding term. Now, to see what goes wrong, compute the terms:
B(1,24)*Binv(24,1)= __________
B(1,23)*Binv(23,1)= __________
B(1,22)*Binv(22,1)= __________
B(1,21)*Binv(21,1)= __________
B(1,20)*Binv(20,1)= __________
B(1,16)*Binv(16,1)= __________
B(1,11)*Binv(11,1)= __________
B(1,6) *Binv(6,1) = __________
B(1,1) *Binv(1,1) = __________
You can see what happens to the sum. The rst few terms are huge compared with the nal sum of
1. Matlab uses 64-bit oating point numbers, so you can only rely on the rst thirteen or fourteen
signicant digits in numbers like B(1,24)*Binv(24,1). Further, they are of opposing signs so that
there is extensive cancellation. There simply are not enough bits in the calculation to get anything
like the correct answer. (Remark: It would not have been productive to compute each of the products
B(1,k)*Binv(k,1) for each k, so I had you do the ve largest and then sampled the rest. I chose to
sample the terms with an odd-sized interval between adjacent terms. Had I chosen an even interval
say every other termthe alternating sign pattern would have been obscured. When you are sampling
errors or residuals for any reason, never take every other term!)
10