Solving Linear Systems of ODEs with Matlab

Similar documents
The SIR Disease Model Trajectories and MatLab

Linear Systems of ODE: Nullclines, Eigenvector lines and trajectories

Linear Systems of ODE: Nullclines, Eigenvector lines and trajectories

Solving systems of ODEs with Matlab

Outline. Additional Nonlinear Systems. Abstract. Finding Equilibrium Points Numerically. Newton s Method

Runge - Kutta Methods for first and second order models

Exam 2 Study Guide: MATH 2080: Summer I 2016

Runge - Kutta Methods for first order models

Predator - Prey Model Trajectories and the nonlinear conservation law

Runge - Kutta Methods for first order models

Getting Started With The Predator - Prey Model: Nullclines

Newton s Cooling Model in Matlab and the Cooling Project!

Riemann Sums. Outline. James K. Peterson. September 15, Riemann Sums. Riemann Sums In MatLab

Matrix Solutions to Linear Systems of ODEs

Extreme Values and Positive/ Negative Definite Matrix Conditions

Riemann Integration. James K. Peterson. February 2, Department of Biological Sciences and Department of Mathematical Sciences Clemson University

Riemann Integration. Outline. James K. Peterson. February 2, Riemann Sums. Riemann Sums In MatLab. Graphing Riemann Sums

Lecture 5b: Starting Matlab

Project Two. James K. Peterson. March 26, Department of Biological Sciences and Department of Mathematical Sciences Clemson University

Math 148. Polynomial Graphs

Project Two. Outline. James K. Peterson. March 27, Cooling Models. Estimating the Cooling Rate k. Typical Cooling Project Matlab Session

AP Calculus AB Summer Assignment. Due Date: First day of school.

2D Plotting with Matlab

The First Derivative and Second Derivative Test

The First Derivative and Second Derivative Test

Uniform Convergence Examples

The Method of Undetermined Coefficients.

Geometric Series and the Ratio and Root Test

Matrices and Vectors

Integration by Parts Logarithms and More Riemann Sums!

Taylor Polynomials. James K. Peterson. Department of Biological Sciences and Department of Mathematical Sciences Clemson University

Lesson 4 Linear Functions and Applications

Numerical method for approximating the solution of an IVP. Euler Algorithm (the simplest approximation method)

Recitation 8: Graphs and Adjacency Matrices

Chapter 1: January 26 January 30

Qualitative Analysis of Tumor-Immune ODE System

More On Exponential Functions, Inverse Functions and Derivative Consequences

Regression and Covariance

MOL410/510 Problem Set 1 - Linear Algebra - Due Friday Sept. 30

Uniform Convergence Examples

Lecture 10: Powers of Matrices, Difference Equations

+ i. cos(t) + 2 sin(t) + c 2.

Qualitative Analysis of Tumor-Immune ODE System

HOMEWORK 3: Phase portraits for Mathmatical Models, Analysis and Simulation, Fall 2010 Report due Mon Oct 4, Maximum score 7.0 pts.

Fourier Series Code. James K. Peterson. April 9, Department of Biological Sciences and Department of Mathematical Sciences Clemson University

Slope Fields and Differential Equations. Copyright Cengage Learning. All rights reserved.

Lesson 5 Practice Problems

IM3 Unit 1 TEST - Working with Linear Relations SEP 2015

Why This Class? James K. Peterson. August 22, Department of Biological Sciences and Department of Mathematical Sciences Clemson University

Mathematical Induction Again

Geometric Series and the Ratio and Root Test

Mathematical Induction Again

Graded Project #1. Part 1. Explicit Runge Kutta methods. Goals Differential Equations FMN130 Gustaf Söderlind and Carmen Arévalo

Predator - Prey Model Trajectories are periodic

Differentiating Series of Functions

MINI LESSON. Lesson 2a Linear Functions and Applications

Solution: (a) Before opening the parachute, the differential equation is given by: dv dt. = v. v(0) = 0

Lower semicontinuous and Convex Functions

Consequences of Continuity

Mon Jan Improved acceleration models: linear and quadratic drag forces. Announcements: Warm-up Exercise:

Predator - Prey Model Trajectories are periodic

Linear Algebra review Powers of a diagonalizable matrix Spectral decomposition

WILEY. Differential Equations with MATLAB (Third Edition) Brian R. Hunt Ronald L. Lipsman John E. Osborn Jonathan M. Rosenberg

Calculus for the Life Sciences II Assignment 6 solutions. f(x, y) = 3π 3 cos 2x + 2 sin 3y

Numerical Solution of Differential Equations

2.1 Identifying Patterns

Motivation: Sparse matrices and numerical PDE's

1 Wyner PreCalculus Fall 2013

Advanced Protein Models again: adding regulation

Math 3301 Homework Set Points ( ) ( ) I ll leave it to you to verify that the eigenvalues and eigenvectors for this matrix are, ( ) ( ) ( ) ( )

1 The relation between a second order linear ode and a system of two rst order linear odes

Defining Exponential Functions and Exponential Derivatives and Integrals

Eigenvalue and Eigenvector Homework

Eigenvalues and Eigenvectors, More Direction Fields and Systems of ODEs

Math 331 Homework Assignment Chapter 7 Page 1 of 9

A 2. =... = c c N. 's arise from the three types of elementary row operations. If rref A = I its determinant is 1, and A = c 1

Mathematical Induction

Function Composition and Chain Rules

Linear Algebra review Powers of a diagonalizable matrix Spectral decomposition

20D - Homework Assignment 5

Numerical Linear Algebra

Differential Equations FMNN10 Graded Project #1 c G Söderlind 2017

Derivatives and the Product Rule

Consequences of Continuity

More Protein Synthesis and a Model for Protein Transcription Error Rates

PART 1 - CALCULATOR ACTIVE QUESTIONS

Practice Problems. 1. The age and weights of six cats are given in the following table:

Upper and Lower Bounds

Section 4.4 Z-Scores and the Empirical Rule

The Derivative of a Function

Mon Jan Improved acceleration models: linear and quadratic drag forces. Announcements: Warm-up Exercise:

Scientific Computing with Case Studies SIAM Press, Lecture Notes for Unit V Solution of

Numerical method for approximating the solution of an IVP. Euler Algorithm (the simplest approximation method)

Numerical Linear Algebra

dt 2 The Order of a differential equation is the order of the highest derivative that occurs in the equation. Example The differential equation

3.2 Quadratic Equations by Graphing

Lab 2 Worksheet. Problems. Problem 1: Geometry and Linear Equations

Lab 5: Nonlinear Systems

At right: Closeups of the graphs of. with WINDOW settings Xmin=-1, Xmax=1, Xscl=0.1, Ymin=-1, Ymax=1, Yscl=0.1

Eigenvalues & Eigenvectors

Transcription:

Solving Linear Systems of ODEs with Matlab James K. Peterson Department of Biological Sciences and Department of Mathematical Sciences Clemson University October 27, 2013 Outline Linear Systems Numerically An Attempt At An Automated Phase Plane Plot Further Automation! Eigenvalues in MatLab

Abstract This lecture is going to discuss solving linear systems of ODEs with MatLab We now turn our attention to solving systems of linear ODEs numerically. We will show you how to do it in two worked out problems. We then generate the plot of y versus x, the two lines representing the eigenvectors of the problem, the x 0 and the y 0 lines on the same plot. A typical MatLab session to solve x 3x + 4y, y x + 2y;, x(0) 1; y(0) 1 would look like this: 1 f @( t, x ) [ 3 x (1)+4 x (2); x (1)+2 x ( 2 ) ] ; E1 @( x ) 0. 2 5 x ; E2 @( x ) x ; xp @( x ) ( 3 / 4 ) x ; yp @( x ) 0. 5 x ; 6 T 1. 4 ; h. 0 3 ; x0 [ 1 ; 1 ] ; N c e i l (T/h ) ;

1 [ ht, r k ] FixedRK ( f, 0, x0, h, 4,N ) ; X r k ( 1, : ) ; Y r k ( 2, : ) ; xmin min (X ) ; xmax max (X ) ; 6 xtop max ( abs ( xmin ), abs (xmax ) ) ; ymin min (Y ) ; ymax max (Y ) ; ytop max ( abs ( ymin ), abs (ymax ) ) ; D max ( xtop, y t o p ) ; 11 s l i n s p a c e ( D, D, 2 0 1 ) ; p l o t ( s, E1 ( s ), -r, s, E2 ( s ), -m, s,... xp ( s ), -b, s, yp ( s ), -g,x, Y, -k ) ; x l a b e l ( x ) ; y l a b e l ( y ) ; 16 t i t l e ( Phase Plane for Linear System x -3 x +4 y, y - x +2 y, x (0) -1, y (0) 1 ) ; l e g e n d ( E1, E2, x 0, y 0, y vs x, Location, Best ) ; This generates this figure. This matches the qualitative analysis we do by hand, although when we do it by hand we usually plot a lot of trajectories.

Now let s annotate the code. 1 % Set up t h e dynamics f o r t h e model f @( t, x ) [ 3 x (1)+4 x (2); x (1)+2 x ( 2 ) ] ; % E i g e n v e c t o r 1 i s [ 1 ;. 2 5 ] so s l o p e i s.25 % s e t up a s t r a i g h t l i n e u s i n g t h i s s l o p e E1 @( x ) 0. 2 5 x ; 6 % Eigenvector 2 is [ 1 ;. 1 ] so slope is 1 % s e t up a s t r a i g h t l i n e u s i n g t h i s s l o p e E2 @( x ) x ; % t h e x 0 n u l l c l i n e i s 3x+4y 0 o r y3/4 x % s e t up a l i n e w i t h t h i s s l o p e 11 xp @( x ) ( 3 / 4 ) x ; % t h e y 0 n u l l c l i n e i s x+2y 0 o r y. 5 x % so s e t up a l i n e w i t h t h i s s l o p e yp @( x ) 0. 5 x ; % s e t t h e f i n a l time 16 T 1. 4 ; % s e t t h e s t e p s i z e h. 0 3 ; % s e t t h e IC y0 [ 1 ; 1 ] ; 21 % f i n d how many s t e p s we l l take N c e i l (T/h ) ; % F i n d N a p p r o x i m a t e RK 4 v a l u e s % s t o r e t h e t i m e s i n ht and t h e v a l u e s i n r k 3 % r k ( 1, : ) i s t h e f i r s t column which i s x % rk ( 2, : ) i s the second column which i s y [ ht, r k ] FixedRK ( f, 0, y0, h, 4,N ) ; % r k ( 1, : ) i s t h e f i r s t row which i s s e t to X % rk ( 2, : ) i s the second row which i s s e t to Y 8 X r k ( 1, : ) ; Y r k ( 2, : ) ; % t h e x v a l u e s r a n g e from xmin to xman xmin min (X ) ; xmax max (X ) ; 13 % f i n d max o f t h e i r a b s o l u t e v a l u e s % example : xmin 7, xmax 4 > xtop 7 xtop max ( abs ( xmin ), abs (xmax ) ) ; % t h e x v a l u e s r a n g e from xmin to xman ymin min (Y ) ; ymax max (Y ) ; 18 % f i n d max o f t h e i r a b s o l u t e v a l u e s % example : ymin 3, ymax 10 > ytop 10 ytop max ( abs ( ymin ), abs (ymax ) ) ; % f i n d max of xtop and ytop ; our example g i v e s D 10 23 D max ( xtop, y t o p ) ;

% The plot l i v e s in the box [ D,D] x [ D,D] 2 % s e t up a l i n s p a c e o f [ D,D] s l i n s p a c e ( D, D, 2 0 1 ) ; % p l o t t h e e i g e n v e c t o r l i n e s, t h e n u l l c l i n e s % and Y v s X % r i s red, m i s magenta, b i s blue, 7 % g i s g r e e n and k i s b l a c k p l o t ( s, E1 ( s ), -r, s, E2 ( s ), -m, s, xp ( s ), -b, s, yp ( s ), -g,x, Y, -k ) ; % s e t x and y l a b e l s x l a b e l ( x ) ; 12 y l a b e l ( y ) ; % s e t t i t l e t i t l e ( Phase Plane for Linear System x -3 x +4 y, y - x +2 y, x (0) -1, y (0) 1 ) ; % s e t l e g e n d 17 l e g e n d ( E1, E2, x 0, y 0, y vs x, Location, Best ) ; Example For the system below [ x ] [ y 4 9 1 6 ] [ ] [ ] [ x x(0), y y(0) ] 4 2 Find the characteristic equation Find the general solution Solve the IVP Solve the System Numerically Solution The eigenvalue and eigenvector portion of this solution has already been done earlier and so we only have to copy the results here.

Solution The characteristic equation is ( [ ] [ ]) 1 0 4 9 det r 0 1 1 6 with eigenvalues r 1 5 and r 2 3. 0 1. For eigenvalue r 1 5, we found the eigenvector to be [ ] 1. 1 2. For eigenvalue r 2 3, we found the eigenvector to be [ ] 1 1/9 Solution The general solution to our system is thus [ ] [ ] [ ] x 1 A e 5t 1 + B e 3t y 1 1/9 We solve the IVP by finding the A and B that will give the desired initial conditions. This gives [ ] [ ] [ ] 4 1 1 A + B 2 1 1/9 or 4 A + B 2 A + 1 9 B

Solution This is easily solved using elimination to give A 7/4 and B 9/4. The solution to the IVP is therefore [ ] x y 7 4 [ [ ] 1 e 5t + 9 [ 1 4 7/4 e 5t + 9/4 e 3t 7/4 e 5t 1/4 e 3t 1 1 9 ] ] e 3t We now have all the information needed to solve this numerically. The MatLab session for this problem is given next. Solution f @( t, x ) [ 4 x (1)+9 x (2); x (1) 6 x ( 2 ) ] ; 2 E1 @( x ) 1 x ; E2 @( x ) (1/9) x ; xp @( x ) (4/9) x ; yp @( x ) (1/6) x ; T 0. 4 ; 7 h. 0 1 ; x0 [ 4 ; 2 ] ; N c e i l (T/h ) ; [ ht, r k ] FixedRK ( f, 0, x0, h, 4,N ) ; X r k ( 1, : ) ; 12 Y r k ( 2, : ) ; xmin min (X ) ; xmax max (X ) ; xtop max ( abs ( xmin ), abs (xmax ) ) ; ymin min (Y ) ; 17 ymax max (Y ) ; ytop max ( abs ( ymin ), abs (ymax ) ) ; D max ( xtop, y t o p ) ;

Solution 1 x l i n s p a c e ( D, D, 2 0 1 ) ; p l o t ( x, E1 ( x ), -r, x, E2 ( x ), -m, x, xp ( x ), -b, x, yp ( x ), -c,x, Y, -k ) ; x l a b e l ( x ) ; y l a b e l ( y ) ; 6 t i t l e ( Phase Plane for Linear System x 4 x +9 y, y -x -6 y, x (0) 4, y (0) -2 ) ; l e g e n d ( E1, E2, x 0, y 0, y vs x, Location, Best ) ; Again this matches the kind of qualitative analysis we have done by hand, but for only one plot instead of the many trajectories we would normally sketch. We had to choose the final time T and the step size h by trial and error to generate the plot you see. If T is too large, the growth term in the solution generates x and y values that are too big and the trajectory just looks like it lies on top of the dominant eigenvector line. The figure we generate is given below.

Homework 62 For these models, Find the characteristic equation Find the general solution Solve the IVP Solve the System Numerically 62.1 [ x ] y [ ] x(0) y(0) [ 1 1 1 3/2 [ ] 5 2 ] [ ] x y Homework 62 62.3 [ x ] y [ ] x(0) y(0) [ 4 2 9 5 [ ] 1 2 ] [ ] x y 62.4 [ x ] y [ ] x(0) y(0) [ ] [ ] 3 7 x 4 6 y [ ] 2 3

Now let s try to generate a real phase plane portrait by automating the phase plane plots for a selection of initial conditions. 1 function AutoPhasePlanePlot ( fname, stepsize,... t i n i t, t f i n a l, r k o r d e r,... x b o x s i z e, y b o x s i z e,... xmin, xmax, ymin, ymax ) % fname i s t h e name o f t h e model dynamics 6 % s t e p s i z e i s t h e chosen s t e p s i z e % t i n i t i s t h e i n i t i a l time % t f i n a l i s t h e f i n a l time % r k o r d e r i s t h e RK o r d e r % we w i l l use i n i t i a l c o n d i t i o n s chosen from t h e box 11 % [ xmin, xmax ] x [ ymin, ymax ] % T h i s i s done u s i n g t h e l i n s p a c e command % so x b o x s i z e i s t h e number o f p o i n t s % i n the i n t e r v a l [ xmin, xmax ] % y b o x s i z e i s t h e number o f p o i n t s 16 % i n the i n t e r v a l [ ymin, ymax ] % u and v a r e t h e v e c t o r s we use to compute our I C s n c e i l ( ( t f i n a l t i n i t )/ s t e p s i z e ) ; u l i n s p a c e ( xmin, xmax, x b o x s i z e ) ; v l i n s p a c e ( ymin, ymax, y b o x s i z e ) ; % h o l d p l o t and c y c l e l i n e c o l o r s x l a b e l ( x ) ; y l a b e l ( y ) ; newplot ; 5 h o l d a l l ; f o r i 1: x b o x s i z e f o r j 1: y b o x s i z e x0 [ u ( i ) ; v ( j ) ] ; [ htime, rk, f r k ] FixedRK ( fname, t i n i t, x0,... 10 s t e p s i z e, r k o r d e r, n ) ; X r k ( 1, : ) ; Y r k ( 2, : ) ; p l o t (X, Y ) ; end 15 end t i t l e ( Phase Plane Plot ) ; hold o f f ;

There are some new elements here. We set up vectors u and v to construct our initial conditions from. Each initial condition is of the form (u i, v j) and we use that to set the initial condition x0 we pass into FixedRK() as usual We start by telling MatLab the plot we are going to build is a new one; so the previous plot should be erased. The command hold all then tells MatLab to keep all the plots we generate as well as the line colors and so forth until a hold off is encountered. So here we generate a bunch of plots and we then see them on the same plot at the end! A typical session usually requires a lot of trial and error. In fact, you should find the analysis by hand is actually more informative! As discussed, the AutoPhasePlanePlot.m script is used by filling in values for the inputs it needs. Again the script has these inputs: AutoPhasePlanePlot ( fname, stepsize, tinit, tfinal, rkorder,... x b o x s i z e, y b o x s i z e, xmin, xmax, ymin, ymax ) 3 % fname i s t h e name o f our dynamics f u n c t i o n % s t e p s i z e i s our c a l l, h e r e. 0 1 seems good % t i n i t is the starting time, here always 0 % t f i n a l i s hard to p i c k, h e r e % s ma ll seems best ;. 2. 6 8 % b e c a u s e one o f o ur e i g e n v a l u e s i s % p o s i t i v e and t h e s o l u t i o n % grows too f a s t % rkorder is the Runge Kutta order, here 4 % x b o x s i z e i s how many d i f f e r e n t x i n i t i a l v a l u e s 13 % we want, here 4 % y b o x s i z e i s how mnay d i f f e r e n t y i n i t i a l v a l u e s % we want, here 4 % xmin and xmax g i v e t h e i n t e r v a l we p i c k % t h e i n i t i a l x v a l u e s from ; 18 % here, they come from [.3,.3] % i n the l a s t attempt. % ymin and ymax g i v e t h e i n t e r v a l we p i c k % t h e i n i t i a l x v a l u e s from % here, they come from [.3,.3] 23 % i n the l a s t attempt.

Now some attempts for the model [ x ] y [ 4 1 8 5 ] [ ] x y We can then try a few phase plane plots. >> v e c f u n c @( t, y ) [ 4 y ( 1 ) y ( 2 ) ; 8 y ( 1 ) 5 y ( 2 ) ] ; >> A u t o P h a s e P l a n e P l o t ( v e c f u n c,. 1, 0, 1, 4, 4, 4, 1, 1, 1, 1 ) ; >> A u t o P h a s e P l a n e P l o t ( v e c f u n c,. 0 1, 0,. 1, 4, 4, 4, 1, 1, 1, 1 ) ; >> A u t o P h a s e P l a n e P l o t ( v e c f u n c,. 0 1, 0,. 4, 4, 4, 4, 1, 1, 1, 1 ) ; >> A u t o P h a s e P l a n e P l o t ( v e c f u n c,. 0 1, 0,. 6, 4, 4, 4, 1, 1, 1, 1 ) ; >> A u t o P h a s e P l a n e P l o t ( v e c f u n c,. 0 1, 0,. 6, 4, 4, 4, 1. 5, 1. 5, 1. 5, 1. 5 ) ; >> A u t o P h a s e P l a n e P l o t ( v e c f u n c,. 0 1, 0,. 4, 4, 4, 4,. 2,. 2,. 2,. 2 ) ; >> A u t o P h a s e P l a n e P l o t ( v e c f u n c,. 0 1, 0,. 4, 4, 4, 4,. 3,. 3,. 3,. 3 ) ; >> x l a b e l ( x a x i s ) ; >> y l a b e l ( y a x i s ) ; >> t i t l e ( Phase Plane Plot ) ; After a while, we get a plot we like

We will now discuss certain ways to compute eigenvalues and eigenvectors for a square matrix in MatLab. For a given A, we can compute its eigenvalues as follows: >> A [ 1 2 3 ; 4 5 6 ; 7 8 1] A 1 2 3 4 4 5 6 7 8 1 >> E e i g (A) E 0.3954 9 1 1. 8 1 6 1 6.4206 >> So we have found the eigenvalues of this small 3 3 matrix. Note, in general they are not returned in any sorted order like small to large. Bummer! To get the eigenvectors, we do this: >> [ V,D] e i g (A) V 0. 7 5 3 0 0.3054 0.2580 4 0.6525 0.7238 0.3770 0. 0 8 4 7 0.6187 0. 8 8 9 6 D 0.3954 0 0 0 1 1. 8 1 6 1 0 9 0 0 6.4206 So the eigenvalues here are the diagonal entries in D.

The eigenvalue/ eigenvector pairs are thus λ1 0.3954, V1 0.7530 0.6525 0.0847 0.3054 λ2 11.8161, V2 0.7238 0.6187 0.2580 λ3 6.4206, V3 0.3770 0.8896 MatLab s eigenvectors are returned having length one. That means take our usual eigenvector and divide it by its length. We have already solves linear models using MatLab tools. Now we will learn to do a bit more. We begin with a sample problem. Note to analyze a linear systems model, we can do everything by hand, we can then try to emulate the hand work, using computational tools. A sketch of the process is thus: 1. For the system below, first do the work by hand. [ ] [ ] [ ] [ ] [ ] x 4 1 x x(0) 3 y, 8 5 y y(0) 1 Find the characteristic equation Find the general solution Solve the IVP On the same x y graph, 1.1 draw the x 0 line, draw the y 0 line 1.2 draw the eigenvector one line, draw the eigenvector two line 1.3 divide the x y into four regions corresponding to the algebraic signs of x and y 1.4 draw the trajectories to create the phase plane portrait 2. Now do the graphical work with MatLab.

For the problem above, a typical MatLab session would be 1 vecfunc @( t, x ) [4 x (1) x (2);8 x (1) 5 x ( 2 ) ] ; % E1 i s [ 1 ; 8 ] so s l o p e i s 8 E1 @( x ) 8 x ; % E2 i s [ 1 ; 1 ] so s l o p e i s 1 E2 @( x ) x ; 6 % x 0 i s 4x y0 or y 4x xp @( x ) 4 x ; %y 0 i s 8x 5y0 so 5 y 8 x o r y 8/5 x yp @( x ) ( 8 / 5 ) x ; T 0. 4 ; 11 h. 0 1 ; x0 [ 3 ; 1 ] ; N c e i l (T/h ) ; [ ht, rk, f k ] FixedRK ( v e c f u n c, 0, x0, h, 4,N ) ; X r k ( 1, : ) ; 16 Y r k ( 2, : ) ; xmin min (X ) ; xmax max (X ) ; xtop max ( abs ( xmin ), abs (xmax ) ) ; ymin min (Y ) ; 21 ymax max (Y ) ; ytop max ( abs ( ymin ), abs (ymax ) ) ; D max ( xtop, y t o p ) ; x l i n s p a c e ( D, D, 2 0 1 ) ; 2 p l o t ( x, E1 ( x ), -r, x, E2 ( x ), -m, x, xp ( x ),... -b, x, yp ( x ), -c,x, Y, -k ) ; x l a b e l ( x ) ; y l a b e l ( y ) ; t i t l e ( Phase Plane for Linear System x 4x -y, 7 y 8 x -5 y, x (0) -3, y (0) 1 ) ; l e g e n d ( E1, E2, x 0, y 0, y vs x, Location, Best ) ; This is our old code. We have to find the eigenvalues, eigenvectors and nullclines by hand and type them into our session. Let s try to do it better.

This generates the plot below. Consider the new function below: 1 f u n c t i o n Approx A u t o P h a s e P l a n e P l o t L i n e a r M o d e l ( fname,... s t e p s i z e, t i n i t, t f i n a l, r k o r d e r, x b o x s i z e,.... y b o x s i z e, xmin, xmax, ymin, ymax, mag ) % % fname i s t h e name o f t h e model dynamics 6 % s t e p s i z e i s t h e chosen s t e p s i z e % t i n i t i s t h e i n i t i a l time % t f i n a l i s t h e f i n a l time % r k o r d e r i s t h e RK o r d e r % we w i l l use i n i t i a l c o n d i t i o n s chosen from t h e box 11 % [ xmin, xmax ] x [ ymin, ymax ] % T h i s i s done u s i n g t h e l i n s p a c e command % so x b o x s i z e i s t h e number o f p o i n t s % i n the i n t e r v a l [ xmin, xmax ] % and y b o x s i z e i s t h e number o f p o i n t s 16 % i n the i n t e r v a l [ ymin, ymax ] % mag i s r e l a t e d to the zoom in l e v e l of our plot. % n c e i l ( ( t f i n a l t i n i t )/ s t e p s i z e ) ;

1 % e x t r a c t a, b, c, and d u0 f e v a l ( fname, 0, [ 1 ; 0 ] ) ; a u0 ( 1 ) ; c u0 ( 2 ) ; v0 f e v a l ( fname, 0, [ 0 ; 1 ] ) ; 6 b v0 ( 1 ) ; d v0 ( 2 ) ; % now we can c o n s t r u c t A A [ a, b ; c, d ] ; % g e t e i g e n v a l u e s and e i g e n v e c t o r s 11 [V,D] e i g (A ) ; e v a l s d i a g (D ) ; % t h e f i r s t column o f V i s E1, second i s E2 E1 V ( :, 1 ) ; E2 V ( :, 2 ) ; 16 % The r i s e o v e r run o f E1 i s t h e s l o p e % The r i s e o v e r run o f E2 i s t h e s l o p e E 1 s l o p e E1 ( 2 ) / E1 ( 1 ) ; E 2 s l o p e E2 ( 2 ) / E2 ( 1 ) ; % d e f i n e t h e e i g e n v e c t o r l i n e s 21 E 1 l i n e @( x ) E 1 s l o p e x ; E 2 l i n e @( x ) E 2 s l o p e x ; % s e t u p t h e n u l l c l i n e l i n e s xp @( x ) (a/b ) x ; yp @( x ) (c /d ) x ; % c l e a r out any o l d p i c t u r e s c l f h o l d on % s e t u p x and y i n i t i a l c o n d i t i o n box 5 x i c l i n s p a c e ( xmin, xmax, x b o x s i z e ) ; y i c l i n s p a c e ( ymin, ymax, y b o x s i z e ) ; % find a l l the trajectories and store them Approx { } ; f o r i 1: x b o x s i z e 10 f o r j 1: y b o x s i z e x0 [ x i c ( i ) ; y i c ( j ) ] ; [ ht, r k ] FixedRK ( fname, 0, x0, s t e p s i z e, 4, n ) ; Approx { i, j } r k ; U Approx { i, j } ; 15 X U ( 1, : ) ; Y U ( 2, : ) ; % g e t t h e p l o t t i n g s q u a r e f o r each t r a j e c t o r y umin min (X ) ; umax max (X ) ; utop max ( abs ( umin ), abs ( umax ) ) ; vmin min (Y ) ; vmax max (Y ) ; 20 vtop max ( abs ( vmin ), abs (vmax ) ) ; D( i, j ) max ( utop, v t o p ) ; end end % get the largest square to put a l l the plots into 25 E max ( max (D) )

% s e t u p t h e x l i n s p a c e f o r t h e p l o t x l i n s p a c e ( E, E, 2 0 1 ) ; % s t a r t t h e h o l d h o l d on 5 % p l o t t h e e i g e n v e c t o r l i n e s and then t h e n u l l c l i n e s p l o t ( x, E 1 l i n e ( x ), -r, x, E 2 l i n e ( x ), -m, x, xp ( x ), -b, x, yp ( x ), -c ) ; % loop over a l l the ICS and get a l l trajectories f o r i 1: x b o x s i z e 10 f o r j 1: y b o x s i z e U Approx { i, j } ; X U ( 1, : ) ; Y U ( 2, : ) ; p l o t (X, Y, -k ) ; end 15 end % s e t l a b e l s and so f o r t h x l a b e l ( x ) ; y l a b e l ( y ) ; t i t l e ( Phase Plane ) ; l e g e n d ( E1, E2, x 0, y 0, y vs x, Location, BestOutside ) ; 20 % s e t zoom f o r p l o t u s i n g mag axis ([ E mag E mag E mag E mag ] ) ; % f i n i s h t h e h o l d hold o f f ; end In this function, we pass in vecfunc into the argument fname. We evaluate this function using the command feval(fname,0,[a time; an x]);. The linear model has a coefficient matrix A of the form [ ] a b c d and so u feval(fname,0,[1;0]); returns column one of A and v feval(fname,0,[0;1]); returns column two of A. This is how we can extract a, b, c and d for our linear model without having to type them in. With them we can define our A using A [a,b;c,d]; and then grab the eigenvalues and eigenvectors.

Example In use, for f @(t,x)[4*x(1)+9*x(2);-x(1)-6*x(2)] we can generate nice looking plots and zero in on a nice view with an appropriate use of the parameter max. The E we return is the size of the plotting square. Solution >> Approx A u t o P h a s e P l a n e P l o t L i n e a r M o d e l E ( f,. 0 1, 0,. 4 5, 4, 8, 8,. 5,. 5,. 5,. 5,. 2 ) ; E 4. 1 4 2 1 >> Approx A u t o P h a s e P l a n e P l o t L i n e a r M o d e l E ( f,. 0 1, 0,. 6 5, 4, 8, 8,. 5,. 5,. 5,. 5,. 1 ) ; E 7. 6 4 8 1 Solution >> Approx A u t o P h a s e P l a n e P l o t L i n e a r M o d e l E ( f,. 0 1, 0,. 6 5, 4, 1 2, 1 2, 1. 5, 1. 5, 1. 5, 1. 5,. 1 ) ; E 2 2. 9 4 4 3 >> Approx A u t o P h a s e P l a n e P l o t L i n e a r M o d e l E ( f,. 0 1, 0, 1. 6 5, 4, 1 2, 1 2, 1. 5, 1. 5, 1. 5, 1. 5,. 0 1 ) ; E 4 6 2. 3 8 3 3 >> Approx A u t o P h a s e P l a n e P l o t L i n e a r M o d e l E ( f,. 0 1, 0, 1. 6 5, 4, 1 2, 1 2, 1. 5, 1. 5, 1. 5, 1. 5,. 0 0 5 ) ; E 4 6 2. 3 8 3 3 >> Approx A u t o P h a s e P l a n e P l o t L i n e a r M o d e l E ( f,. 0 1, 0, 1. 6 5, 4, 6, 6, 1. 5, 1. 5, 1. 5, 1. 5,. 0 0 5 ) ; E 4 6 2. 3 8 3 3 >> Approx A u t o P h a s e P l a n e P l o t L i n e a r M o d e l E ( f,. 0 1, 0, 1. 6 5, 4, 6, 6, 1. 5, 1. 5, 1. 5, 1. 5,. 0 0 8 ) ; E 4 6 2. 3 8 3 3

The last command generates the plot below: Homework 63 For a given model Plot Many Trajectories Simultaneously Using MatLab: This part of the report is done in a word processor with appropriate comments, discussion etc. Show your MatLab code and sessions as well as plots with appropriate documentation. Choose Initial Conditions wisely: Choose a useful set of initial conditions to plot trajectories for by choosing xmin, xmax, ymin, ymax, and xboxsize, yboxsize appropriately. Choose the final time and step size: You ll also have to find the right final time and step size to use. Generate a nice phase plane plot: Work hard on generating a really nice phase plane plot by an appropriate use of the mag factor.

Homework 63 63.1 [ x ] y [ ] [ ] 1 1 x 1 3/2 y 63.2 [ x ] y [ ] [ ] 4 2 x 9 5 y Homework 63 63.3 [ x ] y [ ] [ ] 3 7 x 4 6 y 63.4 [ x ] y [ ] [ ] 1 5 x 5 1 y