{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# 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-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

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 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:
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:

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-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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}