MATLAB Ordinary Differential Equations – Part I Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University September.

Slides:



Advertisements
Similar presentations
Boyce/DiPrima 9th ed, Ch 2.4: Differences Between Linear and Nonlinear Equations Elementary Differential Equations and Boundary Value Problems, 9th edition,
Advertisements

Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Coordinatate systems are used to assign numeric values to locations with respect to a particular frame of reference commonly referred to as the origin.
Lecture 4.
Differential Equations Math Review with Matlab: Finding Solutions to Differential Equations S. Awad, Ph.D. M. Corless, M.S.E.E. D. Cinpinski E.C.E. Department.
SOLUTION OF STATE EQUATION
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 3.
Introduction to C Programming
1.2 Row Reduction and Echelon Forms
Linear Equations in Linear Algebra
By Hrishikesh Gadre Session II Department of Mechanical Engineering Louisiana State University Engineering Equation Solver Tutorials.
Ch 5.2: Series Solutions Near an Ordinary Point, Part I
Transfer Functions Convenient representation of a linear, dynamic model. A transfer function (TF) relates one input and one output: The following terminology.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Transfer Functions Convenient representation of a linear, dynamic model. A transfer function (TF) relates one input and one output: The following terminology.
MATLAB Cell Arrays Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University.
Introduction to C Programming
Ordinary Differential Equations S.-Y. Leu Sept. 21,28, 2005.
Differential Equations
Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers Daniel Baur ETH Zurich, Institut.
Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.
Module 1 Introduction to Ordinary Differential Equations Mr Peter Bier.
Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers Daniel Baur ETH Zurich, Institut.
Lecture 2 Differential equations
SOLUTION FOR THE BOUNDARY LAYER ON A FLAT PLATE
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 8 Linear Algebraic Equations PowerPoint to accompany Copyright © 2010.
Chapter 7 In chapter 6, we noted that an important attribute of inductors and capacitors is their ability to store energy In this chapter, we are going.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.
Dr. Hatim Dirar Department of Physics, College of Science Imam Mohamad Ibn Saud Islamic University.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
Linear Equations in Linear Algebra
1 1.7 © 2016 Pearson Education, Inc. Linear Equations in Linear Algebra LINEAR INDEPENDENCE.
Sheng-Fang Huang. 4.0 Basics of Matrices and Vectors Most of our linear systems will consist of two ODEs in two unknown functions y 1 (t), y 2 (t),
Copyright © 2010 Pearson Education, Inc. All rights reserved Sec
Math 3120 Differential Equations with Boundary Value Problems
Differential Equations. Definition A differential equation is an equation involving derivatives of an unknown function and possibly the function itself.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Solving Linear Systems of Equations
Chapter 7 In chapter 6, we noted that an important attribute of inductors and capacitors is their ability to store energy In this chapter, we are going.
In section 11.9, we were able to find power series representations for a certain restricted class of functions. Here, we investigate more general problems.
Chapter 8 Integration Techniques. 8.1 Integration by Parts.
Beginning Programming for Engineers
Ordinary Differential Equations (ODEs) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers Michael Sokolov ETH Zurich, Institut.
Trees Example More than one variable. The residual plot suggests that the linear model is satisfactory. The R squared value seems quite low though,
Ch 9.2: Autonomous Systems and Stability In this section we draw together and expand on geometrical ideas introduced in Section 2.5 for certain first order.
ME451 Kinematics and Dynamics of Machine Systems Numerical Integration. Stiffness: Implicit Methods. October 30, 2013 Radu Serban University of Wisconsin-Madison.
Advanced Engineering Mathematics, 7 th Edition Peter V. O’Neil © 2012 Cengage Learning Engineering. All Rights Reserved. CHAPTER 4 Series Solutions.
Lecture 6 - Single Variable Problems & Systems of Equations CVEN 302 June 14, 2002.
Ch 1.1: Basic Mathematical Models; Direction Fields Differential equations are equations containing derivatives. The following are examples of physical.
Copyright © Cengage Learning. All rights reserved. 16 Vector Calculus.
ERT 210/4 Process Control & Dynamics DYNAMIC BEHAVIOR OF PROCESSES :
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
ACSL, POSTECH1 MATLAB 입문 CHAPTER 8 Numerical Calculus and Differential Equations.
Sec 232: The coefficient tableau 2ed order RK method Expand all f around (x,y) using Taylor expansion in 2d and then choose constants such that the equation.
1 1.2 Linear Equations in Linear Algebra Row Reduction and Echelon Forms © 2016 Pearson Education, Ltd.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Part 3 - Chapter 8 Linear Algebraic Equations and Matrices.
1 Week 3 First-order ordinary differential equations (ODE) 1.Basic definitions 2.Separable ODEs 3.ODEs reducible to separable form 4.Linear first-order.
Transfer Functions Convenient representation of a linear, dynamic model. A transfer function (TF) relates one input and one output: The following terminology.
Modeling and Simulation Dr. Mohammad Kilani
Advanced Engineering Mathematics 6th Edition, Concise Edition
Sec:25.3 RUNGE-KUTTA METHODS.
Linear Equations in Linear Algebra
Sec:5.4 RUNGE-KUTTA METHODS.
Chapter 7 In chapter 6, we noted that an important attribute of inductors and capacitors is their ability to store energy In this chapter, we are going.
Linear Equations in Linear Algebra
ECE 576 POWER SYSTEM DYNAMICS AND STABILITY
Presentation transcript:

MATLAB Ordinary Differential Equations – Part I Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University September 2013

MATLAB Ordinary Differential Equations – Part I © Greg Reese. All rights reserved 2

ODE 3 Ordinary Differential Equation (ODE) Differential Equation – an equation with an unknown function and some of its derivatives Ordinary – the function depends on one variable only

ODE 4 An ODE is linear if y and its derivatives occur only with an exponent of 0 or 1 Linear – y'' +5y' + 7 = 0 – y''' = 40y'' – x 3 y ''' + y ' + cos x = 0 Nonlinear – y'' = -ky 2 – yy ' = 1

ODE 5 Ordinary Differential Equation (ODE) Independent variable often denoted as t and referred to as "time" Unknown function often denoted as y(t) The derivative of y with respect to t is denoted as y', the second derivative as y'', and so on

ODE 6 The degree of the highest derivative in a differential equation is that equation's order, e.g., y' = ky first order y'' + 2y' + 3y = sin(t) second order y''' +2y'' + 2y + 2 =40 third order All of MATLAB's ODE solvers take only first order systems of equations. Not a severe restriction though

ODE 7 MATLAB has many ODE solvers. They have names of the form odennzz() where nn is a two digit number having to do with some characteristics of the algorithm zz is 0, 1, or 2 letters denoting the type of equation the function should be used for –s = stiff –i = implicit –t = moderately stiff –tb = stiff

ODE 8 MATLAB's ODE functions only solve first order ODEs, and specifically, these kind: Explicit ODE y ' = f(t,y) Linearly implicit ODE M(t,y)y ' = f(t,y) – M(t,y) is a matrix Fully implicit ODE f(t,y,y') = 0 – ode15i() only In all of above, y and y ' are vectors

ODE 9 Since y and y ' are vectors, the equation y ' = f(t,y) looks like this: Sometimes called the "state-space" form

ODE 10 Requirement to be in state-space form not as limiting as it appears Can always convert any linear (in y) ODE to state-space form Can convert many other ODEs also A lot of work in solving ODEs is in conversion to right form. So let's give it a try!

State-space form 11 STEP 1 If the highest derivative is n, define y 1, y 2, … y n as in the example

State-space form 12 STEP 2 From can see that so write

State-space form 13 STEP 3 Solve original equation for highest derivative and substitute y k+1 for y (k) on right side Remember

State-space form 14 STEP 4 Since we have Substitute y ' 3 for y ''' on left side

State-space form 15 STEP 5 Write in form STEP 5

State-space form 16 STEP 6 Write system of equations as MATLAB function that accepts a scalar (time) and one column vector (y-values at the given time) and returns one column vector (y ' -values at the given time).

function yPrime = mySystem( t, y ) yPrime = zeros( 3, 1 ); yPrime(1) = y(2); yPrime(2) = y(3); yPrime(3) = -8*y(1)+6*y(2)+2*y(3)+7; Must be column vector! State-space form 17 can use any names

State-space form 18 Van der Pol equation Describes a type of nonconservative oscillator with nonlinear damping Discovered in work with electrical circuits Used in biology as model for action potentials in neurons Used in seismology to model the two plates in geological fault μ is scalar for strength of damping

State-space form 19 Try It Convert Van der Pol equation to state-space form Step 1 – rename y and y ' as y 1 and y 2 Step 2 – combine derivative of top eqn with bottom eqn to get

State-space form 20 Try It STEP 3 Solve original equation for highest derivative (y'') and substitute y 1 for y and y 2 for y' on right side

State-space form 21 Try It Step 4 Derivative of bottom equation of gives y 2 ' = y'' Substitute y 2 ' for y'' on the left side of to get

State-space form 22 Try It Step 5 Combine and into state-space form

State-space form 23 Try It STEP 6 Write system of equations as MATLAB function called vanderpol1 that contains scalar μ, accepts scalar t (time) and one vector y (y-values at t), and returns one vector yPrime (y ’ -values at t) function yPrime = vanderpol1( t, y ) mu = 1.0; yPrime(1,1) = y(2); yPrime(2,1) = mu*( 1-y(1)*y(1) )*y(2) - y(1);

State-space form 24 Try It STEP 7 Ta-dah! Ready to solve. Well, almost... PROBLEM In general, there are many functions that satisfy a given ODE. Need additional information to get specific solution.

State-space form 25 Questions?

Initial value problem 26 Two common ways to specify the extra information needed to produce only one solution of differential equation. Let's start with one way. It's called the Initial Value Problem

Initial value problem 27 In initial value problem, solution of interest satisfies a specific initial condition, i.e., y = y 0 at initial time t 0 For state-space form, initial value problem is y ' = f(t,y) and y(t 0 ) = y 0 If f(t,y) is "sufficiently smooth", problem has exactly one solution. Generally, no analytic expression so must solve numerically

Initial value problem 28 Some kind of differential equations are stiff problems. Will discuss stiffness later. Right now, let's work on nonstiff problems

Initial value problem 29 MATLAB has three solvers for nonstiff equations ode45 –A one-step solver – in computing, needs only solution at immediately preceding time point –Best function to apply as a "first try" for most problems. ode23 –A one-step solver –May be more efficient than ode45() at crude tolerances and in presence of mild stiffness ode113 –A multistep solver – needs solutions at several preceding time points to compute current solution –May be more efficient than ode45() at stringent tolerances and when ODE function is particularly expensive to evaluate

Initial value problem 30 All MATLAB ODE functions (except ode15i ) have same calling syntax: [T,Y] = solver(odefun,tspan,y0) [T,Y] = solver(odefun,tspan,y0,options) [T,Y,TE,YE,IE] = solver(odefun,tspan,y0,options) sol = solver(odefun,[t0 tf],y0...) Same syntax makes it very easy to try different solvers – just change solver name

Initial value problem 31 [T,Y] = solver(odefun,tspan,y0) [T,Y] = solver(odefun,tspan,y0,options) odefun – a function handle that evaluates the right side of the differential equations tspan – a vector specifying the interval of integration, [t0,tf]. The solver imposes the initial conditions at tspan(1), and integrates from tspan(1) to tspan(2) y0 – A vector of initial conditions options – Structure of optional parameters that change the default integration

Initial value problem 32 Outputs of ODE solvers [T,Y] = solver(odefun,tspan,y0) [T,Y] = solver(odefun,tspan,y0,options) [T,Y,TE,YE,IE] = solver(odefun,tspan,y0,options) T – column vector of time points Y – solution array –One row is solution of all elements of y at time given in T in same row –One column is solution of one element of y for all times in T –Y(m,n) is value of y n at time t m Will look at other outputs later

Initial value problem 33 Try It STEP 7 Now we're ready to solve. Recap: solve Van der Pol eqn. with μ=1, time = [ 0 20 ] and initial conditions y(1)=2, y(2)=0. Plot both components of y versus t.

Initial value problem 34 Try It STEP 7 >> [ t y ] = [ 0 20 ], [ 2 0 ]' ); >> whos t y Name Size Bytes Class Attributes t 237x double y 237x double make column vector

Initial value problem 35 Try It STEP 7 >> plot( t, y(:,1), 'r', t, y(:,2), 'b' ) >> legend( 'y_1(t)', 'y_2(t)' )

Initial value problem 36 Try It Rewrite vanderpol1.m as vanderpol2.m. Make the new version of the function accept μ as the third argument. Run vanderpol2 as an anonymous function, using the same initial conditions and value of μ as before.

Initial value problem 37 Try It function yPrime = vanderpol2( t, y, mu ) yPrime(1,1) = y(2); yPrime(2,1) = mu*( 1-y(1)*y(1) )*y(2) - y(1);

Initial value problem 38 Try It STEP 7 >> [ t y ] = [ 0 20 ], [ 2 0 ]' ); >> plot( t, y(:,1), 'r', t, y(:,2), 'b' ) >> legend( 'y_1(t)', 'y_2(t)' )

Initial value problem 39 Try It Let's try another initial value problem – a mass on a spring.

Initial value problem 40 Newton: F gravity - F spring - F oil = m∙a m∙g – k(s+y) – c y ' = m y'' m∙y '' + c∙y' + k∙y = m∙g – k∙s Can show that m∙g = k∙s, so m∙y'' + c∙y' + k∙y = 0 or m m k k k y=0 y0y0 y(t) Oil C C s

Initial value problem 41 STEP 1 Define STEP 1

Initial value problem 42 STEP 2 From write STEP 2

Initial value problem 43 STEP 3 Solve original equation for highest derivative (y'') and substitute y 1 for y and y 2 for y' on right side STEP 3

Initial value problem 44 STEP 4 From before so y 2 ' = y'' Substitute y 2 ' for y'' on the left side STEP 4

Initial value problem 45 STEP 5 Write in form STEP 5

Initial value problem 46 STEP 6 Write system of equations as MATLAB function that accepts a scalar t (time) and one vector (y-values at t) and returns one vector (y ’ -values at t), as shown on next slide

Initial value problem 47 STEP 6 function yPrime = massInOil( t, y ) c = ?; k = 4; m = 1; % yPrime must be a column vector yPrime = zeros( 2, 1 ); yPrime(1) = y(2); yPrime(2) = -k*y(1)/m - c*y(2)/m;

Initial value problem 48 STEP 7 Suppose oil is thick. Qualitatively, what will behavior of mass be? function yPrime = massInOil( t, y ) c = ?; k = 4; m = 1; % yPrime must be a column vector yPrime = zeros( 2, 1 ); yPrime(1) = y(2); yPrime(2) = -k*y(1)/m - c*y(2)/m; function yPrime = massInOil( t, y ) c = 5; k = 4; m = 1; % yPrime must be a column vector yPrime = zeros( 2, 1 ); yPrime(1) = y(2); yPrime(2) = -k*y(1)/m - c*y(2)/m; What parameter setting of c would effect this?

Initial value problem 49 STEP 7 c = 5 >> [t y] = [0 10], [1 0] ); >> plot(t,y(:,1)) Solution for first 10 seconds y(0) = 1 y'(0) = 0

Initial value problem 50 TIP If you see a lot of repetitive output in the command window when you run ode45(), you probably left the semicolon off the end of a line in your function Example k = 4

Initial value problem 51 STEP 7 Suppose oil is thin. Qualitatively, what will behavior of mass be? function yPrime = massInOil( t, y ) c = ?; k = 4; m = 1; % yPrime must be a column vector yPrime = zeros( 2, 1 ); yPrime(1) = y(2); yPrime(2) = -k*y(1)/m - c*y(2)/m; function yPrime = massInOil( t, y ) c = 1; k = 4; m = 1; % yPrime must be a column vector yPrime = zeros( 2, 1 ); yPrime(1) = y(2); yPrime(2) = -k*y(1)/m - c*y(2)/m; What parameter setting of c would effect this?

Initial value problem 52 STEP 7 c = 1 >> [t y] = [0 10], [1 0] ); >> plot(t,y(:,1))

Initial value problem 53 STEP 7 Suppose we drain the tube, so there’s no oil in it. Qualitatively, what will behavior of mass be? function yPrime = massInOil( t, y ) c = ?; k = 4; m = 1; % yPrime must be a column vector yPrime = zeros( 2, 1 ); yPrime(1) = y(2); yPrime(2) = -k*y(1)/m - c*y(2)/m; function yPrime = massInOil( t, y ) c = 0; k = 4; m = 1; % yPrime must be a column vector yPrime = zeros( 2, 1 ); yPrime(1) = y(2); yPrime(2) = -k*y(1)/m - c*y(2)/m; What parameter setting of c would effect this?

Initial value problem 54 STEP 7 c = 0 >> [t y] = [0 10], [1 0] ); >> plot(t,y(:,1))

Initial value problems 55 Questions so far?

Stiff equations 56 "Stiffness is a subtle, difficult, and important concept in the numerical solution of ordinary differential equations." - Cleve Moler, inventor of MATLAB

Stiff equations 57 Three definitions of stiffness A problem is stiff if the solution being sought varies slowly, but there are nearby solutions that vary rapidly, so the numerical method must take small steps to obtain satisfactory results. A property of some differential equations in which some states change very rapidly while others may change very slowly. For a stiff problem, solutions can change on a time scale that is very short compared to the interval of integration, but the solution of interest changes on a much longer time scale.

Stiff equations 58 Stiffness Depends on the differential equation, initial conditions, and numerical solution method Stiffness is an efficiency issue –If you don't care how much computation time a problem takes, you don't need to worry about stiffness – Nonstiff numerical methods can solve stiff problems – they just take a long time to do it

Stiff equations 59 Let's go back to vanderpol2(), the function we used to solve the Van der Pol equation. Let's solve it again, but with μ=1000 and t = [0 3000]. Use ode15s() and on two separate plots graph y 1 (t) vs. t and y 2 (t) vs. t

Stiff equations 60 Try It >> [ t y ] = 3000],[2 0]'); >> plot( t, y(:,1) ) >> title( 'y_1(t) vs. t' ) >> figure >> plot( t, y(:,2) ) >> title( 'y_2(t) vs. t' )

Stiff equations 61 Try It Note that y 1 (t) action occurs over about 800 time units but on same scale, y 2 (t) action is instantaneous

Stiff equations 62 Can find out how much work a solver did [t y] = solver( fun, tSpan, y0, options ) To make solver display processing statistics, set "Stats" to "on" in options by using function odeset() For more options, type help odeset

Stiff equations 63 Try It Run previous solution but print out statistics >> options = odeset( 'Stats', 'on' ); >> [ t y ] = [ ], [ 2 0 ]', options ); 591 successful steps 225 failed attempts 1883 function evaluations 45 partial derivatives 289 LU decompositions 1747 solutions of linear systems

Stiff equations 64 Try It Unfortunately, didn't show elapsed time. Run it again with tic and toc as shown: >> tic,... [ t y ] = [ ], [ 2 0 ]', options );, toc 591 successful steps 225 failed attempts 1883 function evaluations 45 partial derivatives 289 LU decompositions 1747 solutions of linear systems Elapsed time is seconds.

Stiff equations 65 Try It (not really) Repeat previous command but use ode45 instead. >> tic, [ t y ] = [ ],... [ 2 0 ]', options );, toc e+006 successful steps failed attempts e+007 function evaluations Elapsed time is seconds. = 45 minutes!

Stiff equations 66 Try It Comparison ode45ode15s Successful steps1,684, Failed attempts112, Function evaluations10,778, Elapsed time (seconds)

Stiff equations 67 Questions?

Linearly implicit equations 68 Up to now have only dealt with equations that can be put into state- space form. Another kind of equation is the linearly implicit ODE: M(t,y)y ' = f(t,y) where M(t,y) is a matrix. This is a generalization of state-space formulation

Linearly implicit equations 69 Consider the following linear, implicit system with initial conditions y 1 (0) = y 2 (0) = 0 Find the solution over [ 0 10 ] and plot both state variables on the same graph

Linearly implicit equations 70 Example Write in matrix form as or where Note – in this example, neither M nor f depend explicitly on t

Linearly implicit equations 71 Example Write a function for M(t,y) that takes a (scalar) t, and a vector y, the values of y at that time t, and returns the matrix M(t,y) Then set the “Mass” property to a handle to that function

Linearly implicit equations 72 Example >> massFun [ sin(y(1)) cos(y(2));... -cos(y(2)) sin(y(1)) ]; >> options = odeset( 'Mass', massFun ); >> f 1-y(1); -y(2) ]; >> [t,y] = ode45( f, [0 10], [0;0], options );

Linearly implicit equations 73 Example >> plot(t,y) >> legend( 'y_1(t)', 'y_2(t)' )

Linearly implicit equations 74 Questions?

Events 75 Usually run a simulation from a start time to a stop time. Sometimes would rather stop when something happens than when solver reaches a certain time. For example Equations no longer valid at event – Force of gravity between two bodies varies inversely as square of distance. As two bodies get very close, force becomes infinite

Events 76 No longer interested in solution – If computing fall of parachutist, none but the sickest among us cares what happens after she hits the ground Only want to see a few cycles of a periodic solution

Events 77 MATLAB's ODE solvers have a facility for detecting and acting on events. You create a function and pass a handle to it as part of the options passed to the solver. The solver will then give you information about any events that occur and act on them, e.g., stop solving.

Events 78 The function's form is [value,isterminal,direction] = events(t,y) value, isterminal, and direction are vectors for which the ith element corresponds to the ith event value(i) is the value of the ith event function. Want to make it zero isterminal(i) = 1 if the integration is to terminate at a zero of this event function, otherwise, 0

Events 79 [value,isterminal,direction] = events(t,y) direction(i) = 0 if all zeros are to be located (the default), +1 if only zeros where the event function is increasing, and -1 if only zeros where the event function is decreasing

Events 80 If you specify an events function call the solver as [T,Y,TE,YE,IE] =... solver(odefun,tspan,y0,options) The three additional outputs are: TE - column vector of times at which events occur YE - solution values corresponding to these times IE - indexes into the vector returned by the events function. The values indicate which event the solver detected. If no events detected, TE, YE, IE will be empty

Events 81 Try It A crude model for a falling body is y '' = -1 + (y ' ) 2 with y(0)=1, y ' (0)=0 When does the body splatter, i.e., for what t is y(t) = 0 ? y(t) y(1) y(0)

Events 82 Try It Convert y '' = -1 + (y ' ) 2 to state-space form:

Events 83 Try It Write as function for ODE and save in falling_body.m function yPrime = falling_body( t,y ) yPrime(1,1) = y(2); yPrime(2,1) = y(2)*y(2) – 1;

Events 84 Try It Want the solver to 1.stop 2.whenever y(t) = 0 ( y 1 (t) = 0 ) 3.from either direction Thus the event function should be function [ value,isterminal,direction ] = splat( t,y ) isterminal = 1; % stop value = y(1); % when y(1) = 0 direction = 0; % approached from either direction

Events 85 Try It Save the function in splat.m, then >> options = odeset( ); >> [ t y te ye ie ] = [0 inf], [1 0]', options ); >> plot( t, y(:,1) ) integrate to infinity to see if this sucker really stops

Events 86 Try It Cool! Stopped at y=-1.08x10 -14, which is pretty close to zero. The splat took place at t=1.657

Events 87 Try It When did solver stop? >> te te = What were the values of the y vector when it stopped? >> ye ye = Which event occured? >> ie ie = 1

Multiple Events 88 Try It Let’s also make a note (but not stop solving) of when the parachutist’s heart rate goes above 200 beats per minute (bpm) 1.Don’t stop 2.Whenever heart-beat rate goes above 200 bpm 3.In increasing direction (goes above)

Multiple Events 89 Try It Assume heart beats given by heartbeats(t) = *t Save a copy of splat.m as splat2.m and rewrite as

Multiple Events 90 Try It function [ value, isterminal, direction ] = splat2( t,y ) % first event isterminal(1) = 1; % stop value(1) = y(1); % when y(1) = 0 direction(1) = 0; % approached from either direction % second event isterminal(2) = 0; % don't stop heartBeats = * t; % beats per minute (bpm) value(2) = heartBeats - 200; % bpm over or under 200 % only trigger when value(2) crosses zero while increasing direction(2) = +1;

Multiple Events 91 Try It >> options = odeset( ); >> [ t y te ye ie ] = [0 inf], [1 0]', options ); >> ie ie = 2 % event 2 is heart beats > % event 1 is hitting ground >> te te = % heart beats hit 200 at 1 sec % lands after seconds >> ye ye =

Events 92 Questions?

93 The End

Linearly implicit equations 94 Consider the following linear, implicit system with initial conditions y 1 (0) = y 2 (0) = 0 Find the solution over [ 0 10 ] and plot both state variables on the same graph

Linearly implicit equations 95 Example Write in matrix form as or where

Linearly implicit equations 96 Example If A(y) is nonsingular, can take inverse and system becomes which is state-space form. Can now solve as before. If A(y) is singular, solver will stop with error when attempts to take inverse.

Linearly implicit equations 97 Example >> sin(y(1)) cos(y(2));... -cos(y(2)) sin(y(1)) ] \ [ 1-y(1) -y(2) ]'; >> [ t y ] = ode45( f, [0 10], [ 0; 0 ] ); >> plot( t, y ) >> legend( 'y_1(t)',... 'y_2(t)' )