8/14/2019 lab7_8_ode
1/14
MatLab Programming Algorithms to Solve
Differential Equations
MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].
x1 x2 x3
x
y(x)1
23 5
4
Adapted from Figure 16.1.2. In Numerical Recipes in C:The Art of Scientific Computing.
2nd Ed. W. H. Press, S. A. Teukolsky, W. T. Vetterling, and B. P. Flannery.
Cambridge, UK: Cambridge University Press, 1992. p. 711. ISBN: 9780521431088. Figure by MIT OCW.
Cite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007.
8/14/2019 lab7_8_ode
2/14
Revisit the task of recovering the motion of a
dynamical system from its equation of motion
Consider the simplest 1st order system:
0=+ kxxb&
What does this system corresponds to?
The solution of this system can of course be
obtained analytically but also simply numerically
by a single integrationCite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007.MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].
8/14/2019 lab7_8_ode
3/14
8/14/2019 lab7_8_ode
4/14
How did we solve this class of problems?We use a very simple straight forward approach ofdoing numerical integration:
0 1 2 3 4 5 6-0.2
0
0.2
0.4
0.6
0.8
1
1.2
1.4
x(t)
y(t)
Projectile trajectory with x0=0, y0=0, vx0=5, vy0=5
X=0; y=0; vx=5;vy=5;t=0;dt=0.1
Y=
8/14/2019 lab7_8_ode
5/14
The General Numerical Problem of Solving
Ordinary Differential Equations (ODEs)
),,',,( )1()( tyyyfy nn L=
Note that y does not have to be a scaler
but can be a vector as in the case formultiple degrees of freedom systems
),,( 21 myyyy L=
Cite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007.MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].
8/14/2019 lab7_8_ode
6/14
Converting higher order differential equation to
a system of first order differential equation
Consider probably the most important case:
)()(')('' thytgytfy ++=
This can be readily converted to a system
of first order differential equations
)()()('
'
'
122
21
12
thytgytfy
yy
yyyy
++=
=
==
Cite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007.MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].
8/14/2019 lab7_8_ode
7/14
General equivalence between higher order differential
equation and a system of first order equations
),,,,('
;,;';
),,',,(
12
)1()2(121
)1()(
tyyyfy
yyyyyyyy
tyyyfy
nn
nn
nn
nn
L
L
L
=
====
=
The problem of solving all higher order ordinary
differential equation is thus reduced to solving a systemof linear differential equations
Cite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007.MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].
S l i li fi t d diff ti l ti
8/14/2019 lab7_8_ode
8/14
Solving linear first order differential equationby Euler Method
nityyyfty nii LL 1),,,,()(' 21 ==
In general, the system of equations look like:
Euler Method says:
ni
ttjtjytjyftjytjy niii
L
L
1
))1(),)1((,),)1((())1(()( 1
=
+=
This equation can be solved if we have theinitial conditions:
0202101 )0(,,)0(,)0( nn yyyyyy === L
Cite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007.MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].
f ?
8/14/2019 lab7_8_ode
9/14
What is the accuracy of the Euler Method?
Euler method is equivalent to taking the
1st order Taylor series expansion for yi(t);
it is unsymmetric and uses only the derivative
information at the start of the time step
ni
tOttjtjytjyftjytjy niii
L
L
1
)())1(),)1((,),)1((())1(()( 21
=
++=
Correction is only one less order then the correction term.
How do we get better accuracy?
Cite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007.MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].
S h ti ll th diff b t
8/14/2019 lab7_8_ode
10/14
Schematically, the differences between
Euler and 2nd order Runge-Kutta are fairly clear
Euler Method
2nd order
Runge-Kutta
From Numerical Recipe in CCite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007.MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].
x1 x3x2
x
y(x) 1
2
x1 x3x2
x
y(x)
4
5312
Adapted from Figure 16.1.1. InNumerical Recipes in
: The Art of Scientific Computing.
nd Ed. W. H. Press, S. A. Teukolsky, W. T. Vetterling,nd B. P. Flannery. Cambridge, UK: Cambridge
niversity Press, 1992. p. 711. ISBN: 9780521431088.
igure by MIT OCW.
C
2a
U
F
Adapted from Figure 16.1.1. InNumerical Recipes inC: The Art of Scientific Computing.
2nd Ed. W. H. Press, S. A. Teukolsky, W. T. Vetterling
and B. P. Flannery. Cambridge, UK: Cambridge
niversity Press, 1992. p. 711. ISBN: 9780521431088.
Figure by MIT OCW.
U
A ki ODE l R K tt M th d
8/14/2019 lab7_8_ode
11/14
A working ODE solver Runge-Kutta Method
ni
ktjytjy
ttjktjyktjyfk
ttjtjytjyfk
iii
nnii
nii
L
L
L
1
2))1(()(
))2/1(),2/1)1((,,2/1))1(((2
))1(),)1((,),)1(((1
11
1
=
+=
++=
=
Estimate where the mid-point for yi is first. Then,
Evaluate the slope at the mid-point to estimate
the next value of yi.
Because of symmetry, this method is good to
O(t3)
This is called the 2nd order Runge-Kutta method.Cite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007.MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].
Hi h O d R K tt M th d
8/14/2019 lab7_8_ode
12/14
Higher Order Runge-Kutta Method
Just like Simpson method can be extended to higher
order estimate, Runge-Kutta also has straightforwardHigher order analog. The most commonly used
one is the 4th order Runge-Kutta method
ni
tOkkkktjytjy
ttjktjyktjyfk
ttjktjyktjyfk
ttjktjyktjyfk
ttjtjytjyfk
iiiiii
nnii
nnii
nnii
nii
L
L
L
L
L
1
)(6/43/33/26/1))1(()(
)),3)1((,3))1(((4
))2/1(),2/2)1((,,2/2))1(((3
))2/1(),2/1)1((,,2/1))1(((2
))1(),)1((,),)1(((1
5
11
11
11
1
=
+++++=
++=
++=
++=
=
Runge-Kutta methods are implemented in MatLabas ODE23 and ODE45 functionsCite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007.MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].
U i M tL b t l t f diff ti l ti
8/14/2019 lab7_8_ode
13/14
Using MatLab to solve a system of differential equations
Consider solving the following system of ODE:
Cite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007.MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].
y'1= y2y3
y'2= -y1y3
y'3= -0.51y1y2
y1(0) = 0
y2(0) = 1
y3(0) = 1
Adapted from MATLAB Help Sections. Figure by MIT OCW.
Using MatLab to solve a system of differential equations
8/14/2019 lab7_8_ode
14/14
Using MatLab to solve a system of differential equations
(1) First define the system of ODEs as a function:function dy = system(t,y)
dy = zeros(3,1); % a column vector
dy(1) = y(2) * y(3);
dy(2) = -y(1) * y(3);
dy(3) = -0.51 * y(1) * y(2);
(2) Call ODE45 or ODE23 using the function handle
[T,Y] = ode45(@system,[0 12],[0 1 1]);
(3) Plot result
plot(T,Y(:,1),'-',T,Y(:,2),'-.',T,Y(:,3),'.')Cite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007.MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].