Matlab Methods For Ode
Matlab Methods For Ode
Matlab Methods For Ode
P. Howard
Fall 2003
Contents
1 Solving Ordinary Dierential Equations in MATLAB
1.1
1.2
1.1.1
1.1.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1.3
Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2.1
1.2.2
1.2.3
1.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.4
1.5
1.6
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
1.7
Laplace Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
2.1
Parameters in ODE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
2.2
13
2.3
Event Location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
14
1.1
y 0 (x) = xy.
We can use MATLAB's built-in
dsolve().
(1.1)
given below.
> >y = dsolve('Dy = y*x','x')
y = C1*exp(1/2*x^2)
Notice in particular that MATLAB uses capital D to indicate the derivative and requires that the entire
equation appear in single quotes. MATLAB takes
be explicitly specied as the independent variable. Alternatively, if you are going to use the same equation
eqn1.
y(1) = 1,
use
y,
y(x)
vectorize().
symbolic object ).
eval(),
which evaluates or executes text strings that constitute valid MATLAB commands. Hence, we can use
> >x = linspace(0,1,20);
> >z = eval(vectorize(y));
> >plot(x,z)
Suppose we would like to evaluate our solution
workspace and then evaluate
y(x)
at the point
x = 2.
as
in the
y:
> >x=2;
> >eval(y)
On the other hand, we may want to nd the value of
for which
strcat().
solve(),
First, we
We have,
> >solve(strcat(char(y),'=7'))
y(0) = 0, y 0 (0) = 1.
(1.2)
1.1.3 Systems
Suppose we want to solve and plot solutions to the system of three ordinary dierential equations
x0 (t) =
y 0 (t) =
x(t) + 2y(t)
x(t) + z(t)
z(t)
(1.3)
+5z(t).
First, to nd a general solution, we proceed as in Section 1.1.1, except with each equation now braced in its
own pair of (single) quotation marks:
> >[x,y,z]=dsolve('Dx=x+2*y-z','Dy=x+z','Dz=4*x-4*y+5*z')
x =
2*C1*exp(2*t)-2*C1*exp(t)-C2*exp(3*t)+2*C2*exp(2*t)-1/2*C3*exp(3*t)+1/2*C3*exp(t)
y =
2*C1*exp(t)-C1*exp(2*t)+C2*exp(3*t)-C2*exp(2*t)+1/2*C3*exp(3*t)-1/2*C3*exp(t)
z =
-4*C1*exp(2*t)+4*C1*exp(t)+4*C2*exp(3*t)-4*C2*exp(2*t)-C3*exp(t)+2*C3*exp(3*t)
(If you use MATLAB to check your work, keep in mind that its choice of constants C1, C2, and C3 probably
won't correspond with your own. For example, you might have
of exp(t) in the expression for
C = 2C1 + 1/2C3,
are assigned.) Notice that since no independent variable was specied, MATLAB used its default,
t.
For an
example in which the independent variable is specied, see Section 1.1.1. To solve an initial value problem,
we simply dene a set of initial values and add them at the end of our
x(0) = 1, y(0) = 2,
and
z(0) = 3.
dsolve()
We have, then,
> >inits='x(0)=1,y(0)=2,z(0)=3';
> >[x,y,z]=dsolve('Dx=x+2*y-z','Dy=x+z','Dz=4*x-4*y+5*z',inits)
x =
6*exp(2*t)-5/2*exp(t)-5/2*exp(3*t)
y =
5/2*exp(t)-3*exp(2*t)+5/2*exp(3*t)
z =
-12*exp(2*t)+5*exp(t)+10*exp(3*t)
Finally, plotting this solution can be accomplished as in Section 6.1.2.
> >t=linspace(0,.5,25);
> >xx=eval(vectorize(x));
> >yy=eval(vectorize(y));
> >zz=eval(vectorize(z));
> >plot(t, xx, t, yy, t, zz)
The gure resulting from these commands is included as Figure 1.1.
25
20
15
10
0.1
0.2
0.3
0.4
0.5
1.2
MATLAB has a staggering array of tools for numerically solving ordinary dierential equations. We will
focus on the main two, the built-in functions
1
as the derivative of y .
y 0 (x) = xy2 + y .
First,
y 0 (x).
rstode.m
x and y
A script for solving the ODE and plotting its solutions now takes the following form:
> >xspan = [0,.5];
> >y0 = 1;
> >[x,y]=ode23('rstode',xspan,y0);
> >x
x =
0
0.0500
0.1000
0.1500
0.2000
0.2500
0.3000
0.3500
0.4000
0.4500
0.5000
1 Actually,
for an equation this simple, we don't have to work as hard as we're going to work here, but I'm giving you an
> >y
y =
1.0000
1.0526
1.1111
1.1765
1.2500
1.3333
1.4286
1.5384
1.6666
1.8181
1.9999
> >plot(x,y)
Notice that
xspan
is the domain of
plot(x,y).
Finally, observe that it is not the dierential equation itself that goes in the function
ode23,
but rather the derivatives of the dierential equation, which MATLAB assumes to be a rst order system.
y1 (x) = y(x)
y20 (x)
yprime
y1
y2
is a column vector, as is evident from the semicolon following the rst appearance of
MATLAB input and output for solving this ODE is given below.
> >xspan = [0,.5];
> >y0 = [1;0];
> >[x,y]=ode23('secondode',xspan,y0);
> >[x,y]
ans =
0
1.0000
0.0001
1.0000
-0.0001
0.0005
1.0000
-0.0005
0.0025
1.0000
-0.0025
y(2).
The
0.0124
0.9999
-0.0118
0.0296
0.9996
-0.0263
0.0531
0.9988
-0.0433
0.0827
0.9972
-0.0605
0.1185
0.9948
-0.0765
0.1613
0.9912
-0.0904
0.2113
0.9864
-0.1016
0.2613
0.9811
-0.1092
0.3113
0.9755
-0.1143
0.3613
0.9697
-0.1179
0.4113
0.9637
-0.1205
0.4613
0.9576
-0.1227
0.5000
0.9529
-0.1241
In the nal expression above, the rst column tabulates x values, while the second and third columns tabulate
y1 and y2 (y(1) and (y(2))), or y and y 0 . Recall from Section 5 on matrices that for the matrix y(m, n),
x.
y1
versus
y2
we use
y2 )
and 17
th row, 1st
To get, for instance, the 4th entry of the vector y(1), type y(4,1)4
y1 ,
use
y(:, 1),
plot(y(:,1),y(:,2)).
every
equations, which have some properties of equations arising in atmospherics, and whose solution has long
served as an example for chaotic behavior. We have
dx
= x + y
dt
dy
= y xz
dt
dz
= bz + xy
dt
where for the purposes of this example, we will take
y(0) = 8,
and
z(0) = 27.
(1.4)
(1.5)
br,
= 10, b = 8/3,
(1.6)
and
r = 28,
as well as
x(0) = 8,
20
y(t)
10
10
20
30
20
15
10
10
15
20
x(t)
1.3
direction eld
dy
= f (x, y).
dx
f (x, y) is complicated, it may be impossible to nd a solution y(x) explicitly. At each point (x, y), however,
y(x): by the denition of derivative, the slope is simply f (x, y).
To plot a direction eld, we simply go to each point (x, y) in the xy plane (actually, a discrete grid of points)
If
and draw in the slope of the tangent line at this point. We will require a few new MATLAB commands to
do this, so let's go through them one at a time. The rst is
meshgrid().
The command
o
vector v = (x, y). For example, quiver(0,0,1,1) begins at the point (0, 0) and draws an arrow inclined 45
y
from the horizontal. Recalling that the vector v = (x, y) has slope
x , we make the useful observation that
dy
the command quiver(x ,y ,dx, dy) begins at the point (x, y) and draws a vector with slope
dx . Fortunately,
the last two commands we'll need are signicantly easier to master. The command
the dimensions of the matrix A, while
ones(m,n)
size(A)
simply returns
dx=ones(size(dy))
f (x, y)
dy
=
.
dx
1
7
2.5
1.5
0.5
0.5
1.5
2.5
3
In this case,
dy = f (x, y) = x + sin(y)
and
y 0 (x) = x + sin(y).
dx = 1.
Looking at Figure 1.3, we observe that each arrow has a dierent length. Since we're only really concerned
with direction here, we might make all vectors unit length. For this, we use
> >[x,y]=meshgrid(-3:.3:3,-2:.3:2);
> >dy = x+sin(y);
> >dx = ones(size(dy));
> >dyu = dy./sqrt(dx.^2+dy.^2);
> >dxu = dx./sqrt(dx.^2+dy.^2);
> >quiver(x,y,dxu,dyu)
which produces Figure 1.4.
2.5
1.5
0.5
0.5
1.5
2.5
3
y 0 (x) = x + sin(y).
1.4
dy
= x2 y.
dx
Proceeding almost exactly as above, we have
> >[x,y]=meshgrid(-3:.5:3,-3:.5:3);
> >dy=x.^2-y;
> >dx=ones(size(dy));
> >dyu=dy./sqrt(dy.^2+dx.^2);
> >dxu=dx./sqrt(dy.^2+dx.^2);
> >quiver(x,y,dxu,dyu)
We obtain Figure 1.5.
4
3
4
1.5
y 0 (x) = x2 y .
phase diagrams,
dierential equations. A rst order system of ordinary dierential equations is called autonomous if it can
be written in the form
dx
dt
dy
dt
= f (x, y)
= g(x, y).
That is, if the independent variablet, heredoes not explicitly appear on the right-hand side.
Such
systems are often too dicult to analyze exactly; however, we can obtain a great deal of information from
dy
dx
the following clever trick. Dividing
dt by dt , we have (forgoing even so much as a nod toward rigor)
dy
=
dx
or the
phase equation
dy
dt
dx
dt
g(x, y)
,
f (x, y)
g(x, y)
dy
=
.
dx
f (x, y)
9
versus
as phase diagrams.
The standard introductory example to phase diagrams involves the equations of a pendulum.
represents the angle with which the pendulum is displaced from the vertical at time
0
its angular velocity (that is, y(t) = x (t)), then Newton's second law asserts that
dx
= y
dt
dy
= gl
dt
where
g = 9.81m/s2
t,
and
y(t)
If
x(t)
represents
sin(x),
is the length
of the pendulum (which we will take to be 1). (See Problem 7 in Section 4.10 of [NSS] for a discussion of
the derivation of this equation.) Our phase plane equation becomes
g
sin(x)
dy
=l
,
dx
y
from which we observe immediately that
y=0
(1.7)
Following our remarks from Section 6.3, we can have MATLAB sketch in a direction eld for (1.7). The
following commands suce (though, for reasons discussed below, the resulting gure is disappointing):
> >[x,y]=meshgrid(-5:.4:5,-10:.51:10);
> >dy = -9.81*sin(x)./y;
> >dx = ones(size(dy));
> >dxu=dx./sqrt(dx.^2+dy.^2);
> >dyu=dy./sqrt(dx.^2+dy.^2);
> >quiver(x,y,dxu,dyu)
Notice that I've chosen my grid for
the slope for
y=0
so that
for which
sin(x)
is 0,
is innite, corresponding with a vertical line. Unfortunately, this simple analysis doesn't
yield quite as much information as we would like. The main problem is that our arrows all point toward the
right, as though the independent variable
do). But what we really want here is to know what direction the solutions are moving as
For this, we will have to go back to our original system of dierential equations.
As usual, we will take up and right to be our positive directions and left and down to be our negative
directions. To determine the direction in which
x(t)
change with respect to time, or its derivative. Glancing at our equations, we see that
y > 0,
then
x(t)
x is increasing, we don't need to change anything (recall that our problem was that
x to be increasing everywhere). If it's decreasing, that is, if y < 0, then we must change the sign
dx and dy , to turn the vector around. The upshot of all this is that we cannot lump the expression
dx
dy.
Which is to say,
dx
2 The following
sign(), which returns a 1 for each matrix entry that is positive, a -1 for each
dx and
matrix entry that is negative, and a 0 for each matrix entry that is 0. In order to insure that the
10
15
10
10
15
6
dy
sign(x).^2
and
(x, y),
both
sign(y).^2,
and
dy
and
dx.
Observe that we can recover quite a bit of information from Figure 1.6. For example, notice that the
arrows form circles around the point
(0, 0).
the larger the circle, the greater the swing. We call the point
notice that the equilibrium point
physically?)
A nal word: the expression
(, 0)
(, 0)
and
phase diagram
point. Alternatively,
the pendulum.
1.6
As a second example, let's consider the phase diagram arising in the case of the LotkaVolterra predatorprey
model
x0 (t) =
ax(t) bx(t)y(t)
x(t)
y(t)
wildcat, for example). For this system, the phase plane equation becomes
ry + cxy
dy
=
,
dx
ax bxy
which is too dicult to solve exactlyat least by routine methods. For the purposes of this example, we
will take
a = r = 2, b = c = 1.
x=0
and
y = 2.
The
2 Recall
(a, b)
is
(a, b):
11
4.5
3.5
2.5
1.5
0.5
0.5
0.5
1.5
2.5
3.5
4.5
1.7
Laplace Transforms
One of the most useful tools in mathematics is the Laplace transform. MATLAB has built-in routines for
computing both Laplace transforms and inverse Laplace transforms. For example, to compute the Laplace
2
transform of f (t) = t , type simply
> >syms t;
> >laplace(t^2)
In order to invert, say,
F (s) =
1
1+s , type
> >syms s;
> >ilaplace(1/(1+s))
2.1
Parameters in ODE
It is often the case that we have some parameter in our dierential equation that needs to be determined
during the course of our analysis.
For example, in the ballistics project, you will need to consider the
coecient of air resistance, b, for an object projected through the air. For the case of linear air resistance, this
can be accomplished in a straightforward manner from an exact solution
y(t)
data. For the case of nonlinear air resistance, however, an exact solution is cumbersome, and we are best
served by carrying out the analysis directly from the ODE. As an example, let's study how this approach
could be carried out in the case of linear air resistance.
First, let's recall what we're after. We are given that when dropped from a height
projectile takes
t = .95s
y(0) = 4.06
m, the
d2 y
dt2
= g b dy
dt ,
y(0) = 4.06,
12
y 0 (0) = 0,
(2.1)
we obtain
y(.95) = 0.
y(.95)
as a function of
global b,
M-le to the next, and to the MATLAB command line as well. Notice that for script M-les, all variables
are global by denitionrunning a script M-le is equivalent to typing the commands one after the other
into the command line. Not so for function M-les. We now dene a second function
linheight.m :
function height=linheight(bb);
%LINHEIGHT: Solves ODE for linear air resistance
global b;
b = bb;
tspan = [0 .95];
y0 = [4.06 0];
[t,z]=ode23('linsys', tspan, y0);
height = z(length(t)); %Extracts z at t = .95 (nal value)
With these two M-les thus dened, we nd
simply by typing
> >fzero('linheight',1)
Though this is the most complicated combination of les we've seen, the only new command in
t,
2.2
so that
z(length(t))
is the value of
at
length(V),
For various reasons of arguable merit most introductory courses on ordinary dierential equations focus
primarily on initial value problems (IVP's).
boundary value problems (BVP's). Again, let's look to the model of linear air resistance for an example. In
the Ballistics project, we are told that when red straight up from a height of
2.13 seconds to hit the groundwhich is to say,
problem
y(2.13) = 0.
y(0) = .39m,
dy
d2 y
y(0) = .39,
y(2.13) = 0,
= g b ,
dt2
dt
points t = 0 and t = 2.13 as the endpoints, or boundaries,
(2.2)
of the domain we are
interested in. We could solve this particular equation by obtaining a general solution for the ODE and using
y(0)
and
y(2.13)
to evaluate the constants of integration (which is what you should have done for the linear
air resistance part of the project), but the point here is to study how we could solve (2.2) with MATLAB.
The rst step in this analysis is again to write (2.2) as a rst order system, and to write the right hand
side of this system as a function le. Fortunately, this information has already been stored as
Section 2.1 (you need only alter it by inserting the value you found in Section 2.1 for
boundary conditions as a very simple M-le:
function res=bc(ya,yb)
%BC: Evaluates the residue of the boundary condition
res=[ya(1)-.39;yb(1)];
13
b).
linsys.m
from
residue
we mean the left hand side of the boundary condition after it has been set
2.8036
4.4619
5.4160
5.7133
5.3985
4.5127
11.2370
8.1086
5.1918
2.4722
-0.0636
-2.4279
-4.6324
Columns 8 through 10
3.0947
1.1803
-6.6878
-8.6043
Observe that
sol.y
0
-9.5491
contains values of
and
y0
at each
t-value
bvpinit.
One
v,
with
value you might nd interesting above is 11.2370, from the rst column. This is the initial velocity,
which the projectile is launched, assuming linear damping.
2.3
Event Location
A MATLAB tool that will save us a lot of work in the nonlinear part of the ballistics project is
event location.
Typically, the ODE solver in MATLAB terminates after solving the ODE over a specied domain of the
independent variable (xspan=[0,.5] above, where the independent variable is
x).
In applications, however,
we often would like to stop the solution at a particular value of the dependent variable (for example, when
a pendulum reaches the peak of its arc, or when a population crosses some threshhold value). For example,
in the ballistics project, we are interested in nding the value of
y(t) = 0when
x(t)
for which
the dart strikes the ground. In order to give you the idea of how event location works, let's
work through the case you did by hand: linear damping. Here, you have the system of equations,
y 00 (t) = g by 0 (t);
x00 (t) =
bx0 (t);
where now
y(0) = .18
(2.3)
(2.4)
indicates that the object has been red from a platform .18 meters o the ground,
is the angle of inclination with which the object was red, and
was launched. Suppose our goal is to nd the distance the object travels prior to hitting the ground. As
usual, the rst step consists in writing (2.4) as a rst order system. We can do this with the substitutions
z1 = y , z2 = x, z3 = y 0 , and z4 = x0 , which gives
z10 = z3
z20 = z4
z30 = g
z40 = bz4
bz3
The event we will be looking for is the object's hitting the ground, or
y(0) 6= 0,
y(t) = 0
y(t) = 0.
a bit more general, however, we will also specify the direction we would like the object to be goingdown.
14
As usual, we begin by writing the right-hand side of our system as a MATLAB function M-le, in this
case
linproj.m.
events
function that species what event we want MATLAB to look for and
linevent.m,
y(t) = 0).
the line
lookfor=z(1)
y(t) = 1,
z(1) = 0
(that is,
we would use
We can now solve the ODE up until the time our projectile strikes the ground with the following commands
issued in the Command Window:
> >options=odeset('Events',@linevent)
> >z0=[.18;0;5;10];
> >[t,z,te,ze,ie]=ode45(@linode,[0 10],z0,options)
Here,
z0 is a vector of initial data (though the velocities don't represent any particular angle).
The command
oder45() returns a vector of times t, a matrix of dependent variables z , the time at which the event occurred,
te, the values of z when the event occurred, ze, and nally the index when the event occurred, ie.
15
Index
boundary value problems, 13
direction elds, 7
dsolve, 1
eval, 2
event location, 14
global, 13
inverse laplace, 12
laplace, 12
Laplace transforms, 12
length(), 13
meshgrid, 7
ode, 4
ones(), 7
parameters in ODE, 12
phase diagrams, 9
quiver, 7
sign, 10
size(), 7
vectorize(), 2
16