You are on page 1of 14

# MatLab Programming – Algorithms to Solve Differential Equations

y(x)

2 1 3

4 5

x1

x2

x

x3

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. MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].

Massachusetts Institute of Technology. Spring 2007.053J Dynamics and Control I.mit.003J/1.edu). .Revisit the task of recovering the motion of a dynamical system from its equation of motion Consider the simplest 1st order system: & + kx = 0 bx What does this system corresponds to? The solution of this system can of course be obtained analytically but also simply numerically by a single integration Cite as: Peter So. MIT OpenCourseWare (http://ocw. Downloaded on [DD Month YYYY]. course materials for 2.

MIT OpenCourseWare (http://ocw.edu). Downloaded on [DD Month YYYY]. Massachusetts Institute of Technology.Limitation of Simple Integration: Quad Simple integration is very limited and does not solve a large class of dynamic problems.mit. Spring 2007. course materials for 2. .053J Dynamics and Control I.003J/1. As examples: by’ θ1 θ2 Coupled multiple degree of freedom system mg Falling ball – 2nd order Cite as: Peter So.

course materials for 2.2 start y(t) 1 0.053J Dynamics and Control I. Downloaded on [DD Month YYYY] . vy0=5 1.t=t+dt.mit. y=0.4 X=0.8*dt.dt=0. People uses it only if they don’t know any better. Massachusetts Institute of Technology. y0=0. vx=5.1 0.2 0 1 2 3 x(t) 4 5 6 yes Y=<0 no Actually.edu).t=0. output x. MIT OpenCourseWare (http://ocw.vy=5. this simple approach Has a name – it is called Euler Method In general.vy=vy-9. t end Cite as: Peter So. you should NEVER ever use Euler Method.003J/1.y=vy*dt.6 0.How did we solve this class of problems? We use a very simple straight forward approach of doing numerical integration: Projectile trajectory with x0=0. vx0=5.8 0.4 1. y=vx*dt.2 0 -0. Spring 2007.

The General Numerical Problem of Solving Ordinary Differential Equations (ODEs) y (n) = f (y ( n −1) .edu). Downloaded on [DD Month YYYY]. Spring 2007. t ) Note that y does not have to be a scaler but can be a vector as in the case for multiple degrees of freedom systems y = ( y1 .mit. y ' . MIT OpenCourseWare (http://ocw. y2 . .053J Dynamics and Control I.L. y. Massachusetts Institute of Technology. L ym ) Cite as: Peter So. course materials for 2.003J/1.

Downloaded on [DD Month YYYY]. course materials for 2. Massachusetts Institute of Technology. MIT OpenCourseWare (http://ocw.edu).003J/1.Converting higher order differential equation to a system of first order differential equation Consider probably the most important case: y ' ' = f (t ) y '+ g (t ) y + h(t ) This can be readily converted to a system of first order differential equations y2 = y ' y1 ' = y2 y2 ' = f (t ) y2 + g (t ) y1 + h(t ) Cite as: Peter So. Spring 2007. y1 = y .mit.053J Dynamics and Control I.

MIT OpenCourseWare (http://ocw. y1 . y ' . t ) The problem of solving all higher order ordinary differential equation is thus reduced to solving a system of linear differential equations Cite as: Peter So. t ) y1 = y.edu). L . y.L . Spring 2007. yn = y ( n −1) .mit. L . y2 = y ' . Downloaded on [DD Month YYYY].General equivalence between higher order differential equation and a system of first order equations y ( n ) = f ( y ( n −1) .053J Dynamics and Control I. yn −1 = y yn ' = f ( yn .003J/1. Massachusetts Institute of Technology. y2 . ( n−2) . course materials for 2.

Downloaded on [DD Month YYYY]. yn (0) = yn 0 Cite as: Peter So. t ) i = 1L n Euler Method says: yi ( jΔt ) = yi (( j − 1)Δt ) + f i ( y1 (( j − 1)Δt ). y2 (0) = y20 . L .053J Dynamics and Control I. Spring 2007.mit. the system of equations look like: yi ' (t ) = f i ( y1 .Solving linear first order differential equation by Euler Method In general. ( j − 1)Δt )Δt i = 1L n This equation can be solved if we have the initial conditions: y1 (0) = y10 .edu). course materials for 2. yn . MIT OpenCourseWare (http://ocw.003J/1. Massachusetts Institute of Technology. y2 . . L.L. yn (( j − 1)Δt ).

edu). Downloaded on [DD Month YYYY]. Spring 2007. yn (( j − 1)Δt ). MIT OpenCourseWare (http://ocw.L. ( j − 1)Δt )Δt + O(Δt 2 ) i = 1L n Correction is only one less order then the correction term.053J Dynamics and Control I.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 yi ( jΔt ) = yi (( j − 1)Δt ) + f i ( y1 (( j − 1)Δt ).003J/1. . course materials for 2. Massachusetts Institute of Technology. How do we get better accuracy? Cite as: Peter So.mit.

In Numerical Recipes in C: The Art of Scientific Computing. Teukolsky.edu). In Numerical Recipes in C: The Art of Scientific Computing. y(x) 1 x1 x2 x x3 y(x) 2 1 3 4 5 2nd order Runge-Kutta x1 x2 x3 Adapted from Figure 16. W. 2nd Ed. W. 1992. Press. A. course materials for 2. H.003J/1. W. S. Figure by MIT OCW. Downloaded on [DD Month YYYY].Schematically. Figure by MIT OCW. A. UK: Cambridge University Press. H. Flannery.053J Dynamics and Control I.1. 711. S. Cambridge. Vetterling. 1992. ISBN: 9780521431088. x From Numerical Recipe in C Cite as: Peter So. T. Cambridge.mit. Massachusetts Institute of Technology. UK: Cambridge University Press. p. ISBN: 9780521431088. and B. Spring 2007. Flannery. W. . p. P.1. P. Teukolsky.1. Press. 2nd Ed. MIT OpenCourseWare (http://ocw. 711. T. the differences between Euler and 2nd order Runge-Kutta are fairly clear 2 Euler Method Adapted from Figure 16.1. and B. Vetterling.

( j − 1 / 2)Δt )Δt yi ( jΔt ) = yi (( j − 1)Δt ) + k 2i i = 1L n Because of symmetry. MIT OpenCourseWare (http://ocw. Evaluate the slope at the mid-point to estimate the next value of yi.mit. this method is good to O(Δt3) This is called the 2nd order Runge-Kutta method.edu).A working ODE solver – Runge-Kutta Method Estimate where the mid-point for yi is first. k1i = f i ( y1 (( j − 1)Δt ). Spring 2007.003J/1. yn (( j − 1)Δt + k1n / 2). Downloaded on [DD Month YYYY]. Cite as: Peter So. . yn (( j − 1)Δt ). Massachusetts Institute of Technology. ( j − 1)Δt )Δt k 2i = f i ( y1 (( j − 1)Δt ) + k11 / 2. Then. course materials for 2. L.053J Dynamics and Control I.L.

The most commonly used one is the 4th order Runge-Kutta method k1i = f i ( y1 (( j − 1)Δt ).mit.003J/1. ( j − 1 / 2)Δt )Δt k 4i = f i ( y1 (( j − 1)Δt ) + k 31 L . L.Higher Order Runge-Kutta Method Just like Simpson method can be extended to higher order estimate. yn (( j − 1)Δt ). ( j − 1 / 2)Δt )Δt k 3i = f i ( y1 (( j − 1)Δt ) + k 21 / 2. MIT OpenCourseWare (http://ocw. Downloaded on [DD Month YYYY]. yn (( j − 1)Δt + k 3n ). ( j − 1)Δt )Δt k 2i = f i ( y1 (( j − 1)Δt ) + k11 / 2. Spring 2007. yn (( j − 1)Δt + k1n / 2). course materials for 2. L .edu).053J Dynamics and Control I.L. . yn (( j − 1)Δt + k 2 n / 2). jΔt )Δt yi ( jΔt ) = yi (( j − 1)Δt ) + k1i / 6 + k 2i / 3 + k 3i / 3 + k 4i / 6 + O (Δt 5 ) i = 1L n Runge-Kutta methods are implemented in MatLab as ODE23 and ODE45 functions Cite as: Peter So. Runge-Kutta also has straightforward Higher order analog. Massachusetts Institute of Technology.

mit. MIT OpenCourseWare (http://ocw. Spring 2007.053J Dynamics and Control I.003J/1. Figure by MIT OCW. Massachusetts Institute of Technology. course materials for 2.edu). Cite as: Peter So. .51y1 y2 y1(0) = 0 y2(0) = 1 y3(0) = 1 Adapted from MATLAB Help Sections. Downloaded on [DD Month YYYY].Using MatLab to solve a system of differential equations Consider solving the following system of ODE: y'1 = y2 y3 y'2 = -y1 y3 y'3 = -0.

3).'. % a column vector dy(1) = y(2) * y(3).'.1).Y(:.edu). course materials for 2.'-'. Downloaded on [DD Month YYYY].[0 1 1]).003J/1. dy(2) = -y(1) * y(3).053J Dynamics and Control I.T.1). .mit. dy(3) = -0. (2) Call ODE45 or ODE23 using the function handle [T.y) dy = zeros(3.Y] = ode45(@system.'-.') Cite as: Peter So.Y(:. MIT OpenCourseWare (http://ocw. Massachusetts Institute of Technology.2). (3) Plot result plot(T.Y(:.51 * y(1) * y(2).[0 12].Using MatLab to solve a system of differential equations (1) First define the system of ODEs as a function: function dy = system(t.T. Spring 2007.