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

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

View Full Document Right Arrow Icon
Chapter 12 Solving High-order ODEs using Matlab 12.1 Solving ODEs using ODE45() Previously, you learned how to write an m-file that used Euler’s method to solve a single 1st order differential equation. In this section, you will learn how to use Matlab’s own functionality to solve 1st order and sets of 1st order differential 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 differential equations. There are some basic steps to follow when solving ODEs, that are outlined in the following sections. 110
Background image of page 1

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

View Full DocumentRight Arrow Icon
CHAPTER 12. SOLVING HIGH-ORDER ODES USING MATLAB 111 12.1.1 Defining an ODE Function First we must define the ODE function in its own m-file. For example, consider the following m-file (fun1.m) for a first-order differential 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 define 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-file (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 file called ex1.m:
Background image of page 2
CHAPTER 12. SOLVING HIGH-ORDER ODES USING MATLAB 112 function xprime=ex1(t,x) xprime=sin(t*x); Create an m-file that contains the following commands: [t,x] = ode45(’ex1’,[0 10], 1); plot(t,x); grid Run the m-file and you should see the result: Problem 12.1 Recreate the above figure except use a time span from 0 to 15 seconds. Turn in your files and a copy of the figure. 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-files:
Background image of page 3

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

View Full DocumentRight Arrow Icon
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-file 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-files, turn in the plot created when you call the main m-file.
Background image of page 4
Image of page 5
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 Right Arrow Icon
Ask a homework question - tutors are online