lab12 - Chapter 12 Solving High-order ODEs using Matlab...

This preview shows pages 1–5. Sign up to view the full content.

Chapter 12 Solving High-order ODEs using Matlab 12.1 Solving ODEs using ODE45() Previously, you learned how to write an m-ﬁle that used Euler’s method to solve a single 1st order diﬀerential equation. In this section, you will learn how to use Matlab’s own functionality to solve 1st order and sets of 1st order diﬀerential equations. Matlab actually has lots of functions to solve ODEs. The most popular method is ODE45 , which uses a robust algorithm that strategically combines 4th order and 5th order Runga-Kutta solutions to determine an accurate approximation of a wide variety of common diﬀerential equations. There are some basic steps to follow when solving ODEs, that are outlined in the following sections. 110

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CHAPTER 12. SOLVING HIGH-ORDER ODES USING MATLAB 111 12.1.1 Deﬁning an ODE Function First we must deﬁne the ODE function in its own m-ﬁle. For example, consider the following m-ﬁle (fun1.m) for a ﬁrst-order diﬀerential equation y 0 = - 2 ty 2 . function yprime = fun1(t,y) yprime = -2 * t * y^2; If you have a system of ODEs, you must deﬁne the function as a vector. If you have two 1st order equations: ˙ y 1 = y 2 ˙ y 2 = y 1 - y 2 then you would create and m-ﬁle (fun2.m) that looks like: function yprime = fun2(t,y) yprime = [ y(2) ; y(1)-y(2) ]; 12.1.2 Solving a single ODE Function using ODE45() The syntax for ODE45 is: [T,Y] = ODE45( ODEFun, Tspan, y_0) where T is the independent variable (often time in physical systems), Y is the dependent variable or vector of independent variables. Tspan = [timeStart, timeStop] is the span of time of interest. Finally, y 0 is the initial conditions for the system. Remember, you can use any variable names you like as long as you are consistent! Consider the following example: ˙ x ( t ) = sin( xt ) x (0) = 1 0 t 10 Create the following .m ﬁle called ex1.m:
CHAPTER 12. SOLVING HIGH-ORDER ODES USING MATLAB 112 function xprime=ex1(t,x) xprime=sin(t*x); Create an m-ﬁle that contains the following commands: [t,x] = ode45(’ex1’,[0 10], 1); plot(t,x); grid Run the m-ﬁle and you should see the result: Problem 12.1 Recreate the above ﬁgure except use a time span from 0 to 15 seconds. Turn in your ﬁles and a copy of the ﬁgure. 12.1.3 Solving Sets of ODEs using ODE45() When solving systems of ODEs, we use vectors to organize the system. Consider the system: ~ y = " y 1 y 2 # where y 1 (0) = 5 . 1 y 2 (0) = 6 . 7 ˙ ~ y = " ˙ y 1 ˙ y 2 # = " cos( y 2 ) + sin( t ) sin( y 1 ) - cos( t ) # If we are interested in state of the system after 10 seconds, we could have the two m-ﬁles:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CHAPTER 12. SOLVING HIGH-ORDER ODES USING MATLAB 113 function yprime=ex2(t,y) yprime=[ cos(y(2))+sin(t); sin(y(1))-cos(t) ]; Now y and yprime are both column vectors, so our main m-ﬁle may look like. tspan=[0, 10]; y0=[5.1, 6.7]; [t, y]=ode45(’ex2’, tspan, y0); plot(t, y); Problem 12.2 After creating these two m-ﬁles, turn in the plot created when you call the main m-ﬁle.
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/14/2011 for the course ME 218 taught by Professor Unknown during the Fall '08 term at University of Texas at Austin.

Page1 / 10

lab12 - Chapter 12 Solving High-order ODEs using Matlab...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online