Professional Documents
Culture Documents
Ordinary Differential
Equations – Part I
3
ODE
An ODE is linear if y and its derivatives
occur only with an exponent of 0 or 1
• Linear
– y'' +5y' + 7 = 0
– y''' = 40y'' - 100
– x3y''' + y' + cos x = 0
• Nonlinear
– y'' = -ky2
– yy' = 1
4
ODE
5
ODE
The degree of the highest derivative in a
differential equation is that equation's
order, e.g.,
• y' = ky first order
• y'' + 2y' + 3y = sin(t) second order
•y''' +2y'' + 2y + 2 =40 third order
All of MATLAB's ODE solvers take only
first order systems of equations. Not a
severe restriction though
6
ODE
MATLAB has many ODE solvers. They
have names of the form odennzz()
where
• nn is a two digit number having to do with
some characteristics of the algorithm
• zz is 0, 1, or 2 letters denoting the type of
equation the function should be used for
– s = stiff
– i = implicit
– t = moderately stiff
– tb = stiff
7
ODE
MATLAB's ODE functions only solve first
order ODEs, and specifically, these kind:
• Explicit ODE y' = f(t,y)
• Linearly implicit ODE M(t,y)y' = f(t,y)
– M(t,y) is a matrix
• Fully implicit ODE f(t,y,y') = 0
– ode15i() only
In all of above, y and y' are vectors
8
ODE
Since y and y' are vectors, the equation
y' = f(t,y) looks like this:
y1 f1 t , y1 , y2 , , ym
y2 f 2 t , y1 , y2 , , ym
y3 f 3 t , y1 , y2 , , ym
ym f m t , y1 , y2 , , ym
y f t , y , y , , y
• Can always convert any y f t , y , y , , y
2 2 1 2 m
3 3 1 2 m
linear (in y) ODE to
state-space form y f t , y , y , , y
m m 1 2 m
10
State-space form
STEP 1
If the highest derivative is n, define
y1, y2, … yn as in the example
y 2 y 6 y 8 y 7
y1 y
y2 y
y3 y
11
State-space form
STEP 2
y1 y
From y2 y can see that
y3 y
y1 y y2 , y2 y y3
y1 y2
so write
y2 y3
12
State-space form
STEP 3
Solve original equation for highest
derivative and substitute yk+1 for y(k)
on right side y 2 y 6 y 8 y 7
Remember
y 2 y 6 y 8 y 7
y1 y
y2 y y 2 y3 6 y2 8 y1 7
y3 y
y 8 y1 6 y2 2 y3 7
13
State-space form
STEP 4
y1 y
Since y2 y we have y3 y
y3 y
y1 y2 y3 8 y1 6 y2 2 y3 7
y2 y3
y3 f t , y1 , y2 , y3 y1 y2
y2 y3
y3 8 y1 6 y2 2 y3 7
15
State-space form
STEP 6
Write system of equations as
MATLAB function that accepts a
scalar (time) and one column vector
(y-values at the given time) and
returns one column vector (y'-values
at the given time).
16
State-space form
y1 y2
y2 y3
y3 8 y1 6 y2 2 y3 7
can use any names
17
State-space form
''
y 1 y y y 02
'
1 y y y
' 2
to get y2 1 2 1
21
State-space form
Try It
Step 5
Combine y1 y2 and y2 ' 1 y12 y2 y1
into state-space form y1 y2
y2 f t , y1 , y2
y y2
1
'
2
y (1 y1 ) y2 y1
'
2 22
State-space form
Try It
y y2
1
'
STEP 6 ' 2
y (1 y1 ) y2 y1
2
PROBLEM
In general, there are many functions that
satisfy a given ODE. Need additional
information to get specific solution.
24
State-space form
Questions?
25
Initial value problem
Two common ways to specify the extra
information needed to produce only one
solution of differential equation. Let's start
with one way. It's called the
26
Initial value problem
In initial value problem, solution of interest
satisfies a specific initial condition, i.e.,
y = y0 at initial time t0
For state-space form, initial value problem is
y' = f(t,y) and y(t0) = y0
28
Initial value problem
MATLAB has three solvers for nonstiff equations
ode45
– A one-step solver – in computing , needs only solution at immediately
preceding time point
– Best function to apply as a "first try" for most problems.
ode23
– A one-step solver
– May be more efficient than ode45() at crude tolerances and in
presence of mild stiffness
ode113
– A multistep solver – needs solutions at several preceding time points to
compute current solution
– May be more efficient than ode45() at stringent tolerances and when
ODE function is particularly expensive to evaluate
29
Initial value problem
All MATLAB ODE functions (except
ode15i) have same calling syntax:
[T,Y] = solver(odefun,tspan,y0)
[T,Y] = solver(odefun,tspan,y0,options)
[T,Y,TE,YE,IE] =
solver(odefun,tspan,y0,options)
sol = solver(odefun,[t0 tf],y0...)
Try It
STEP 7
Now we're ready to solve.
Recap: solve Van der Pol eqn. with μ=1,
time = [ 0 20 ] and initial conditions y(1)=2,
y(2)=0 . Plot both components of y versus t.
33
Initial value problem
STEP 7
>> [ t y ] = ode45( @vanderpol1, [ 0 20 ], [ 2 0 ]' );
>> whos t y
Name Size Bytes Class
Attributes
t 237x1 1896 double
34
Initial value problem
Try It
STEP 7
>> plot( t, y(:,1), 'r', t, y(:,2), 'b' )
>> legend( 'y_1(t)', 'y_2(t)' )
3
y1(t)
y2(t)
2
-1
-2
35
-3
0 2 4 6 8 10 12 14 16 18 20
Initial value problem
Try It
Rewrite vanderpol1.m as
vanderpol2.m . Make the new version
of the function accept μ as the third
argument. Run vanderpol2 as an
anonymous function, using the same
initial conditions and value of μ as
before.
36
Initial value problem
y y2
1
'
Try It ' 2
y (1 y1 ) y2 y1
2
function yPrime = vanderpol2( t, y, mu )
yPrime(1,1) = y(2);
yPrime(2,1) = mu*( 1-y(1)*y(1) )*y(2) - y(1);
37
Initial value problem
Try It
STEP 7
>> [ t y ] = ode45( @(t,y)vanderpol2(t,y,1),
[ 0 20 ], [ 2 0 ]' );
>> plot( t, y(:,1), 'r', t, y(:,2), 'b' )
>> legend( 'y_1(t)', 'y_2(t)' )
3
y 1(t)
y 2(t)
2
-1
-2
38
-3
0 2 4 6 8 10 12 14 16 18 20
Initial value problem
Try It
Let's try another initial value problem – a
mass on a spring.
39
Initial value problem
Newton:
Fgravity - Fspring - Foil = m∙a
k k k m∙g – k(s+y) – c y' = m y''
m∙y'' + c∙y' + k∙y = m∙g – k∙s
Can show that m∙g = k∙s,
s so
m
y=0 C m∙y'' + c∙y' + k∙y = 0
C or
m y0 c k
y y y 0
m m
Oil Oil y ( 0) 1
y(t) y(0) 0 40
Initial value problem
STEP 1 STEP 1
Define c k
y
y y0
m m
y1 y
y2 y y1 y
y2 y
41
Initial value problem
STEP 2 STEP 2
From
y1 y
y2 y
write
y1 y2 y1 y2
42
Initial value problem
STEP 3 STEP 3
Solve original c k
y y y 0
equation for highest m m
derivative (y'') and
substitute y1 for y y
c k
y y
m m
and y2 for y' on right
c k
side y y2 y1
m m
k c
y y1 y2
m m
43
Initial value problem
STEP 4 STEP 4
From before
k c
y1 y y y1 y2
m m
y2 y
so y2' = y''
k c
Substitute y2 for
' y2 y1 y2
m m
y'' on the left side
44
Initial value problem
STEP 5 STEP 5
Write in form y1 y2 y2
k c
y1 y2
m m
y1 y2
y1 y2
y2 f t , y1 , y2 k c
y2 y1 y2
m m
45
Initial value problem
STEP 6
Write system of equations as MATLAB
function that accepts a scalar t (time)
and one vector (y-values at t) and
returns one vector (y’-values at t), as
shown on next slide
46
Initial value problem
STEP 6 y1 y2
k c
y2 y1 y2
m m
y(0) = 1 y'(0) = 0
49
Initial value problem
TIP
If you see a lot of repetitive output in
the command window when you run
ode45(), you probably left the
semicolon off the end of a line in
your function
Example k = 4
k = 4
k = 4
k = 4 50
Initial value problem
STEP 7
Suppose oil is thin. Qualitatively,
what will behavior of mass be?
What parameter setting of c would
effect this?
function yPrime = massInOil( t, y )
c = 1;
?;
k = 4;
m = 1;
52
STEP 7 Initial value problem
Suppose we drain the tube, so there’s no oil in
it. Qualitatively, what will behavior of mass be?
54
Initial value problems
Questions so far?
55
Stiff equations
56
Stiff equations
Three definitions of stiffness
• A problem is stiff if the solution being sought varies slowly,
but there are nearby solutions that vary rapidly, so the
numerical method must take small steps to obtain
satisfactory results.
• A property of some differential equations in which some
states change very rapidly while others may change very
slowly.
• For a stiff problem, solutions can change on a time scale
that is very short compared to the interval of integration,
but the solution of interest changes on a much longer time
scale.
57
Stiff equations
Stiffness
• Depends on the differential equation, initial
conditions, and numerical solution method
• Stiffness is an efficiency issue
– If you don't care how much computation time a
problem takes, you don't need to worry about
stiffness
– Nonstiff numerical methods can solve stiff
problems – they just take a long time to do it
58
Stiff equations
y y2
1
'
' 2
y (1 y1 ) y2 y1
2
Try It
>> [ t y ] = ode15s( ...
@(t,y)vanderpol2(t,y,1000),[0 3000],[2 0]');
>> plot( t, y(:,1) )
>> title( 'y_1(t) vs. t' )
>> figure
>> plot( t, y(:,2) )
>> title( 'y_2(t) vs. t' )
60
Stiff equations
Try It y 1(t) vs. t y 2(t) vs. t
2 1500
1.5
1000
1
0.5 500
0
0
-0.5
-1 -500
-1.5
-1000
-2
-2.5
0 500 1000 1500 2000 2500 3000 -1500
0 500 1000 1500 2000 2500 3000
63
Stiff equations
Try It
Unfortunately, didn't show elapsed time. Run it
again with tic and toc as shown:
>> tic,...
[ t y ] = ode15s( @(t,y)vanderpol2(t,y,1000),...
[ 0 3000 ], [ 2 0 ]', options );, toc
591 successful steps
225 failed attempts
1883 function evaluations
45 partial derivatives
289 LU decompositions
1747 solutions of linear systems
Elapsed time is 0.286233 seconds.
64
Stiff equations
Try It (not really)
Repeat previous command but use ode45
instead.
>> tic, [ t y ] = ode45( ...
@(t,y)vanderpol2(t,y,1000), [ 0 3000 ], ...
[ 2 0 ]', options );, toc
1.68411e+006 successful steps
112240 failed attempts
1.07781e+007 function evaluations
Elapsed time is 2705.705528 seconds.
= 45 minutes!
65
Stiff equations
Try It
Comparison
ode45 ode15s
66
Stiff equations
Questions?
67
Linearly implicit equations
Up to now have only dealt with
equations that can be put into state-
space form. Another kind of equation
is the linearly implicit ODE:
M(t,y)y' = f(t,y)
where M(t,y) is a matrix. This is a
generalization of state-space
formulation
68
Linearly implicit equations
sin( y1 ) cos( y 2 y1
) '
1 y1
form as cos( y ) sin( y ) y ' y
2 1 2 2
or M (t , y ) y f (t , y )
'
71
Linearly implicit equations
Example
sin( y1 ) cos( y2 ) y1' 1 y1
M (t , y ) y f (t , y )
'
M (t , y ) , y ' ,
'
f (t , y )
cos( y2 ) sin( y1 ) y2 y2
72
Linearly implicit equations
Example
>> plot(t,y)
>> legend( 'y_1(t)', 'y_2(t)' )
1.2
y 1(t)
1 y 2(t)
0.8
0.6
0.4
0.2
73
-0.2
0 1 2 3 4 5 6 7 8 9 10
Linearly implicit equations
Questions?
74
Events
Usually run a simulation from a start time
to a stop time. Sometimes would rather
stop when something happens than
when solver reaches a certain time. For
example
• Equations no longer valid at event
– Force of gravity between two bodies
varies inversely as square of distance. As
two bodies get very close, force becomes
infinite
75
Events
76
Events
77
Events
The function's form is
[value,isterminal,direction] = events(t,y)
value, isterminal, and direction are
vectors for which the ith element corresponds
to the ith event
• value(i) is the value of the ith event
function. Want to make it zero
• isterminal(i) = 1 if the integration is to
terminate at a zero of this event function,
otherwise, 0
78
Events
[value,isterminal,direction] = events(t,y)
• direction(i) = 0 if all zeros are to be
located (the default), +1 if only zeros where
the event function is increasing, and -1 if
only zeros where the event function is
decreasing
79
Events
If you specify an events function call the solver
as
[T,Y,TE,YE,IE] = ...
solver(odefun,tspan,y0,options)
The three additional outputs are:
• TE - column vector of times at which events occur
• YE - solution values corresponding to these times
• IE - indexes into the vector returned by the events
function. The values indicate which event the solver
detected.
If no events detected, TE, YE, IE will be empty
80
Events
Try It
A crude model for a falling body is
y'' = -1 + (y')2 with y(0)=1, y'(0)=0
y(0)
81
Events
Try It
Convert y = -1 + (y )
'' ' 2 y '' 1 ( y ' ) 2
to state-
y1 y, y2 y '
space form:
y1' y2 , y2' y ''
y1' y2
y2' 1 y22
y1' y2
' 2
y2 y2 1 82
Events y '' 1 ( y ' ) 2
y1' y2
' 2
Try It y2 y2 1
83
Events
Try It y1' y2
Want the solver to ' 2
y2 y2 1
1. stop y1 y, y2 y '
2. whenever y(t) = 0 ( y1(t) = 0 )
3. from either direction
Thus the event function should be
function [ value,isterminal,direction ] = splat( t,y )
isterminal = 1; % stop
value = y(1); % when y(1) = 0
direction = 0; % approached from either direction
84
Events y1' y2
' 2
y2 y2 1
y1 y, y2 y '
Try It
Save the function in splat.m, then
>> options = odeset( 'events', @splat );
>> [ t y te ye ie ] = ode45( ...
@falling_body, [0 inf], [1 0]', options );
>> plot( t, y(:,1) )
integrate to infinity to see
if this sucker really stops
85
Events
1.2
0.8
Try It
0.6
y=-1.08x10-14, 0.2
which is pretty
X: 1.657
Y: -1.082e-014
0
86
Events
1.2
0.8
Try It
0.6
0.4
>> te 0
Y: -1.082e-014
te = 1.6575 -0.2
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8
Try It
Let’s also make a note (but not stop solving) of
when the parachutist’s heart rate goes above
200 beats per minute (bpm)
1. Don’t stop
2. Whenever heart-beat rate goes above 200 bpm
3. In increasing direction (goes above)
88
Multiple Events
Try It
Assume heart beats given by
heartbeats(t) = 100 + 100*t
Save a copy of splat.m as splat2.m and
rewrite as
89
Multiple Events
Try It
function [ value, isterminal, direction ] = splat2( t,y )
% first event
isterminal(1) = 1; % stop
value(1) = y(1); % when y(1) = 0
direction(1) = 0; % approached from either direction
% second event
isterminal(2) = 0; % don't stop
heartBeats = 100 + 100 * t; % beats per minute (bpm)
value(2) = heartBeats - 200; % bpm over or under 200
% only trigger when value(2) crosses zero while increasing
direction(2) = +1;
90
Multiple Events
Try It
>> options = odeset( 'events', @splat2 );
>> [ t y te ye ie ] = ode45( ...
@falling_body, [0 inf], [1 0]', options );
>> ie
ie = 2 % event 2 is heart beats > 200
1 % event 1 is hitting ground
>> te
te = 1.0000 % heart beats hit 200 at 1 sec
1.6575 % lands after 1.6575 seconds
>> ye
ye = 0.5663 -0.7617
-0.0000 -0.9300
91
Events
Questions?
92
The
End 93
Linearly implicit equations
Consider the following linear, implicit
system sin( y1 ) y1' cos( y2 ) y2' y1 1
cos( y2 ) y1' sin( y1 ) y2' y2 0
with initial conditions y1(0) = y2(0) = 0
Find the solution over [ 0 10 ] and
plot both state variables on the same
graph
94
Linearly implicit equations
Example
sin( y1 ) y1' cos( y2 ) y2' y1 1
Write cos( y2 ) y1' sin( y1 ) y2' y2 0 in matrix
or A( y ) y b( y )
'
'y_2(t)' ) 0.8
y 1(t)
y 2(t)
0.6
0.4
0.2
-0.2
0 1 2 3 4 5 6 7 8 9 10
97