Runge - Kutta Methods for first and second order models

Similar documents
Runge - Kutta Methods for first order models

Runge - Kutta Methods for first order models

Solving systems of ODEs with Matlab

Solving Linear Systems of ODEs with Matlab

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

Uniform Convergence Examples

Uniform Convergence Examples

Advanced Protein Models again: adding regulation

Linear Systems of ODE: Nullclines, Eigenvector lines and trajectories

Predator - Prey Model Trajectories and the nonlinear conservation law

Extreme Values and Positive/ Negative Definite Matrix Conditions

Getting Started With The Predator - Prey Model: Nullclines

Section 7.4 Runge-Kutta Methods

Derivatives and the Product Rule

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

Linear Systems of ODE: Nullclines, Eigenvector lines and trajectories

The Method of Undetermined Coefficients.

Project One: C Bump functions

Predator - Prey Model Trajectories are periodic

Outline Calculus for the Life Sciences II. Pollution in a Lake 1. Introduction. Lecture Notes Numerical Methods for Differential Equations

Newton s Cooling Model in Matlab and the Cooling Project!

ODE Homework 1. Due Wed. 19 August 2009; At the beginning of the class

Predator - Prey Model Trajectories are periodic

Variation of Parameters

Lecture 5b: Starting Matlab

Calculus for the Life Sciences

Complex Numbers. Outline. James K. Peterson. September 19, Complex Numbers. Complex Number Calculations. Complex Functions

Complex Numbers. James K. Peterson. September 19, Department of Biological Sciences and Department of Mathematical Sciences Clemson University

Linear algebra and differential equations (Math 54): Lecture 20

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

Lecture 10: Powers of Matrices, Difference Equations

Numerical Methods for ODEs. Lectures for PSU Summer Programs Xiantao Li

Physics 299: Computational Physics II Project II

Solutions to Homework 3

Worksheet # 2: Higher Order Linear ODEs (SOLUTIONS)

Integration by Parts Logarithms and More Riemann Sums!

Defining Exponential Functions and Exponential Derivatives and Integrals

Consequences of Continuity

EXAMPLE OF ONE-STEP METHOD

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

Lösning: Tenta Numerical Analysis för D, L. FMN011,

Math 308 Week 8 Solutions

Ordinary Differential Equations. Monday, October 10, 11

2. Determine whether the following pair of functions are linearly dependent, or linearly independent:

Numerical Methods for Ordinary Differential Equations

An Overly Simplified and Brief Review of Differential Equation Solution Methods. 1. Some Common Exact Solution Methods for Differential Equations

The First Derivative and Second Derivative Test

Fourier Sin and Cos Series and Least Squares Convergence

The First Derivative and Second Derivative Test

Numerical Optimization

What if the characteristic equation has a double root?

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

More On Exponential Functions, Inverse Functions and Derivative Consequences

Ordinary Differential Equations (ODEs)

Derivatives in 2D. Outline. James K. Peterson. November 9, Derivatives in 2D! Chain Rule

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

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

Geometric Series and the Ratio and Root Test

Hölder s and Minkowski s Inequality

Consequences of Continuity

Modeling and Experimentation: Compound Pendulum

Matrix Solutions to Linear Systems of ODEs

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

Multistep Methods for IVPs. t 0 < t < T

Hölder s and Minkowski s Inequality

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

Convergence of Sequences

The Existence of the Riemann Integral

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

Dirchlet s Function and Limit and Continuity Arguments

Sin, Cos and All That

The SIR Disease Model Trajectories and MatLab

Physics 584 Computational Methods

Unit 2: Solving Scalar Equations. Notes prepared by: Amos Ron, Yunpeng Li, Mark Cowlishaw, Steve Wright Instructor: Steve Wright

INTRODUCTION TO COMPUTER METHODS FOR O.D.E.

Differentiating Series of Functions

Power Series Solutions for Ordinary Differential Equations

MATH10001 Mathematical Workshop Difference Equations part 2 Non-linear difference equations

MATH10001 Mathematical Workshop Graph Fitting Project part 2

Power Series Solutions of Ordinary Differential Equations

Regression and Covariance

Mathematical Induction Again

Mathematical Induction Again

An Introduction to Numerical Methods for Differential Equations. Janet Peterson

Lecture 10 Polynomial interpolation

24, B = 59 24, A = 55

First Derivative Test

δ Substituting into the differential equation gives: x i+1 x i δ f(t i,x i ) (3)

Function Composition and Chain Rules

Sin, Cos and All That

Upper and Lower Bounds

CPSC 540: Machine Learning

CS1210 Lecture 23 March 8, 2019

Objectives: Review open, closed, and mixed intervals, and begin discussion of graphing points in the xyplane. Interval notation

Notes on numerical solution of differential equations

Conjugate Gradient (CG) Method

Convergence of Sequences

Simple ODE Solvers - Derivation

Geometric Series and the Ratio and Root Test

x = x y and y = x + y.

Transcription:

Runge - Kutta Methods for first and second order models James K. Peterson Department of Biological Sciences and Department of Mathematical Sciences Clemson University October 3, 2013

Outline 1 Runge - Kutte Methods 2 RK Methods: One Variable 3 RK Methods: Two Variables

Abstract This lecture discusses the Runge - Kutta Method for first and second order models.

Runge - Kutte Methods Runge - Kutta methods are based on more sophisticated ways of approximating the solution to y = f (t, y). These methods use multiple function evaluations at different time points around a given t to approximate y(t ). In more advanced classes, we can show this technique generates a sequence {ŷ n } starting at y 0 using the following recursion equation: ŷ n+1 = ŷ n + h F o (t n, ŷ n, h, f ) ŷ 0 = y 0 where h is the step size we use and F o is a fairly complicated function of the previous approximate solution, the step size and the right hand side function f. The Runge - Kutta methods are available for various choices of the superscript o which is called the order of the method.

Runge - Kutte Methods We will not discuss F o here, as it is best to do that in a more advanced class. We know that for Order One: Local error is h 2 and this method is the Euler Method. The global error then goes down linearly with h. Order Two: Local error is h 3, global is h 2. If the global error for a given stepsize h is E, halving the stepsize to h/2 gives a new global error E/4. The global error goes down quadratically. Order Three: Local error is h 4, global is h 3. If the global error for a given stepsize h is E, then halving the stepsize to h/2 gives a new global error E/8. The global error goes down as a cubic power. Order Four: Local error is h 5, global is h 4. If the global error for a given stepsize h is E, then halving the stepsize to h/2 gives a new global error of E/16.

Runge - Kutte Methods The basic code to implement the Runge-Kutta methods is broken into two pieces. The first one, RKstep.m implements the evaluation of the next approximation solution at point (t n, ŷ n ) given the old approximation at (t n 1, ŷ n 1 ). We then loop through all the steps to get to the chosen final time using the code in FixedRK.m. The details of these algorithms are beyond the scope of this text; however, they arise from carefully combined multiple tangent line approximations at each step. In the RK code, we want the dynamic functions to depend on time also. Previously, we used dynamics like f = @(x) 3*x and we expected the dynamics functions to have that form in DoEuler. However, we want more complicated dynamics now so we will define our dynamics as if they depend on time. So from now on, we write f=@(t,x) 3*x even if there is no time dependence.

Runge - Kutte Methods DoEulerTwo.m: the functions are now time dependent. We have to change euler also. f u n c t i o n [ xhat, x, e ] = DoEulerTwo ( f, t r u e, N, x0, h ) % N = number of approximations to f i n d % f = t h e model dynamics f u n c t i o n o f ( t, x ) % t r u e = t h e t r u e s o l u t i o n % x0 = t h e i n i t i a l c o n d i t i o n % h = t h e s t e p s i z e % e u l e r = @( t, x, h ) x + f ( t, x ) h ; x h a t ( 1 ) = x0 ; % i n i t i a l i z e x, x h a t and e x ( 1 ) = x0 ; e ( 1 ) = 0 ; f o r i =1:N x h a t ( i +1) = e u l e r ( i h, x h a t ( i ), h ) ; x ( i +1) = t r u e ( i h ) ; e ( i +1) = abs ( x ( i +1) x h a t ( i +1) ) ; end end

Runge - Kutte Methods RKstep.m uses the new dynamics functions. In it you see the lines like feval(fname,t,x) which means take the function fname passed in as an argument and evaluate it as the pair (t,x). Hence, fname(t,x) is the same as f(t,x). RKstep.m does all the heavy lifting. It manages all of the multiple tangent line calculations that Runge - Kutta needs at each step. We loop through all the steps to get to the chosen final time using the code in FixedRK.m.

Runge - Kutte Methods f u n c t i o n [ tnew, ynew, fnew ] = RKstep ( fname, tc, yc, fc, h, k ) % fname t h e name o f t h e r i g h t hand s i d e f u n c t i o n f ( t, y ) % yc a p p r o x i m a t e s o l u t i o n to y ( t ) = f ( t, y ( t ) ) a t t=t c % f c f ( tc, yc ) % h The time step, k i s RK order 1<= k <=4 % tnew i s t c+h, ynew i s a p p r o x s o l n a t tnew, fnew i s f ( tnew, ynew ) i f k==1 k1 = h f c ; ynew = yc+k1 ; e l s e i f k==2 k1 = h f c ; k2 = h f e v a l ( fname, t c +(h /2), yc+(k1 /2) ) ; ynew = yc + k2 ; e l s e i f k==3 k1 = h f c ; k2 = h f e v a l ( fname, t c +(h /2), yc+(k1 /2) ) ; k3 = h f e v a l ( fname, t c+h, yc k1+2 k2 ) ; ynew = yc+(k1+4 k2+k3 ) / 6 ; e l s e i f k==4 k1 = h f c ; k2 = h f e v a l ( fname, t c +(h /2), yc+(k1 /2) ) ; k3 = h f e v a l ( fname, t c +(h /2), yc+(k2 /2) ) ; k4 = h f e v a l ( fname, t c+h, yc+k3 ) ; ynew = yc+(k1+2 k2+2 k3+k4 ) / 6 ; e l s e d i s p ( s p r i n t f ( The RK method %2d o r d e r i s not a l l o w e d!, k ) ) ; end tnew = tc+h ; fnew = f e v a l ( fname, tnew, ynew ) ; end

Runge - Kutte Methods f u n c t i o n [ t v a l s, y v a l s ] = FixedRK ( fname, t0, y0, h, k, n ) % G i v e s a p p r o x i m a t e s o l u t i o n to % y ( t ) = f ( t, y ( t ) ) % y ( t0 ) = y0 % u s i n g a kth o r d e r RK method % t0 i n i t i a l time % y0 i n i t i a l s t a t e % h s t e p s i z e % k RK order 1<= k <= 4 % n Number o f s t e p s to t a k e % t v a l s time v a l u e s o f form % t v a l s ( j ) = t0 + ( j 1) h, 1 <= j <= n % y v a l s a p p r o x i m a t e s o l u t i o n % y v a l s ( : j ) = a p p r o x i m a t e s o l u t i o n a t % t v a l s ( j ), 1 <= j <= n tc = t0 ; yc = y0 ; t v a l s = t c ; y v a l s = yc ; f c = f e v a l ( fname, tc, yc ) ; f o r j =1:n 1 [ tc, yc, f c ] = RKstep ( fname, tc, yc, fc, h, k ) ; y v a l s = [ y v a l s yc ] ; t v a l s = [ t v a l s t c ] ; end end

RK Methods: One Variable Example: solve a model using all four RK orders and do a plot >> f = @( t, x ).5 x. (60 x ) ; >> t r u e = @( t ) 6 0. / ( 1 + (60/20 1) exp (.5 60 t ) ) ; >> T =. 6 ; >> time = l i n s p a c e ( 0,T, 3 1 ) ; >> h1 =. 0 6 ; >> N1 = c e i l (T/ h1 ) ; >> [ htime1, x h a t 1 ] = FixedRK ( f, 0, 2 0,. 0 6, 1, N1) ; >> [ htime2, x h a t 2 ] = FixedRK ( f, 0, 2 0,. 0 6, 2, N1) ; >> [ htime3, x h a t 3 ] = FixedRK ( f, 0, 2 0,. 0 6, 3, N1) ; >> [ htime4, x h a t 4 ] = FixedRK ( f, 0, 2 0,. 0 6, 4, N1) ; % t h e... a t t h e end o f t h e l i n e a l l o w s us to c o n t i n u e % a l o n g l i n e to t h e s t a r t o f t h e n e x t l i n e >> p l o t ( time, t r u e ( time ), htime1, xhat1,, htime2, xhat2, o,... htime3, xhat3, +, htime4, xhat4,. ) ; >> x l a b e l ( Time ) ; >> y l a b e l ( x ) ; % We want to use t h e d e r i v a t i v e symbol x so % s i n c e Matlab t r e a t as t h e s t a r t and s t o p o f the l a b e l % we w r i t e. That way Matlab w i l l t r e a t s as a s i n g l e quote % t h a t i s our d i f f e r e n t i a t i o n symbol >> t i t l e ( RK A p p r o x i m a t i o n s to x =. 5 x(60 x ), x ( 0 ) 2 0 ) ; % N o t i c e we c o n t i n u e t h i s l i n e too >> l e g e n d ( True, RK 1, h =. 0 6, RK 2, h =. 0 6, RK 3, h =. 0 6,... RK 4, h =. 0 6, L o c a t i o n, Best ) ; This generates the figure in the next slide: note RK Order 4 does a great job even with a large h.

RK Methods: One Variable

RK Methods: One Variable Homework 45 You are now ready to solve a few on your own. For these problems, (i): Write the dynamics function code as f = @(t,x) etc. (ii): Write the true function code as usual true = @(t) etc. (iii): Solve the problem with all four Runga - Kutte Methods for the given step size h and appropriate time interval. Plot all four Runga - Kutte approximate solutions and the true solution on the same plot. (iv): Do all of this in a word doc as usual.

RK Methods: One Variable Homework 45 Continued 45.1 h =.05 and the model is y =.07 y (75 y), y(0) = 5. 45.2 h =.06 and the model is u (t) =.13 u(t) (30 u(t)), u(0) = 45. 45.3 h =.4 and the model is x = 1.9x, x(0) = 6. 45.4 h =.5 and the model is x = 1.95x + 30, x(0) = 16.

RK Methods: Two Variables We can convert a second order linear model to a system easily. Here is an example. Example Convert to a matrix - vector system u (t) + 4.0 u (t) 5.0 u(t) = 0; u(0) = 1.0; u (0) = +1.0 Solution Let x be given by x(t) = [ ] x1 (t) x 2 (t) = [ ] u(t) u (t)

RK Methods: Two Variables Solution Solution continued: x 1(t) = u (t) = x 2 (t), x 2(t) = u (t) = 5u(t) (4)u (t) = 5x 1 (t) 4x 2 (t). Thus x (t) = [ ] x 1 (t) x 2 (t) = [ ] [ ] 0 1 x1 (t) 5 4 x 2 (t) Also, note that x(0) = [ ] x1 (0) x 2 (0) = [ ] u(0) u (0) = [ ] 1 1 = x(0).

RK Methods: Two Variables The conversion process also tells us the dynamics of the new systems model. We can convert a second order linear model to a system easily. Example Find the systems dynamics for the model u (t) + 4.0 u (t) 5.0 u(t) = 0; u(0) = 1.0; u (0) = +1.0 Solution Let x be given by x(t) = [ ] x1 (t) x 2 (t) = [ ] u(t) u (t)

RK Methods: Two Variables Solution Solution continued: x 1(t) = u (t) = x 2 (t), x 2(t) = u (t) = 5u(t) (4)u (t) = 5x 1 (t) 4x 2 (t). Thus, the new dynamics is f given by [ ] x f(x 1, x 2 ) = 2 5x 1 4x 2

RK Methods: Two Variables Note we can code the dynamics in Matlab as follows: The model u (t) + 4.0 u (t) 5.0 u(t) = 0; u(0) = 1.0; u (0) = +1.0 in Matlab becomes >> f = @( x ) [ x ( 2 ) ; 5 x ( 1 ) 4 x ( 2 ) ] ; Note, x can be a number or a vector as the function has been coded to accept either. We can now apply Runge - Kutta methods to solve second order models.

RK Methods: Two Variables Consider the problem ay + by + cy = g, y(0) = y 0, y (0) = y 1 y + (b/a)y + (c/a)y = (1/a)g, y(0) = y 0, y (0) = y 1 This converts to the system x 1 x 2 = x 2 = (c/a)x 1 (b/a)x 2 + (1/a)g And letting A = (c/a), B = (b/a) and C = (1/a) we have x 1 x 2 = x 2 = Ax 1 + Bx 2 + Cg. We ll use this in the code below.

RK Methods: Two Variables Consider the problem y (t) + 4.0 y (t) 5.0 y(t) = 0; y(0) = 1.0; y (0) = +1.0 This has characteristic equation r 2 + 4r 5 = 0 with roots r 1 = 5 and r 2 = 1. Hence, the general solution is x(t) = Ae 5t + Be t. The initial conditions give A + B = 1 5A + B = 1 It is straightforward to see that A = 1/3 and B = 2/3. Then we solve the system using MatLab with this session:

RK Methods: Two Variables % d e f i n e t h e dynamics f o r y + 4 y 5 y = 0 >> a = 1 ; b = 4 ; c = 5; >> B = (b/a ) ; A = (c /a ) ; C = 1/ a ; >> g = @( t ) 0. 0 ; >> f = @( t, y ) [ y ( 2 ) ; A y ( 1 ) + B y ( 2 ) + C g ( t ) ] ; % d e f i n e t h e t r u e s o l u t i o n >> t r u e= @( t ) [ ( 1. 0 / 3. 0 ) exp( 5 t ) (2.0/3.0) exp ( t ) ;... ( 5. 0 / 3. 0 ) exp( 5 t ) (2.0/3.0) exp ( t ) ] ; >> y0 = [ 1 ; 1 ] ; >> h =. 2 ; >> T = 3 ; >> time = l i n s p a c e ( 0,T, 1 0 1 ) ; >> N = c e i l (T/h ) ; >> [ htime1, r k a p p r o x 1 ] = FixedRK ( f, 0, y0, h, 1,N) ; >> y h a t 1 = r k a p p r o x 1 ( 1, : ) ; >> [ htime2, r k a p p r o x 2 ] = FixedRK ( f, 0, y0, h, 2,N) ; >> y h a t 2 = r k a p p r o x 2 ( 1, : ) ; >> [ htime3, r k a p p r o x 3 ] = FixedRK ( f, 0, y0, h, 3,N) ; >> y h a t 3 = r k a p p r o x 3 ( 1, : ) ; >> [ htime4, r k a p p r o x 4 ] = FixedRK ( f, 0, y0, h, 4,N) ; >> y h a t 4 = r k a p p r o x 4 ( 1, : ) ; >> p l o t ( time, t r u e ( time ), htime1, yhat1, o, htime2, yhat2,,... htime3, yhat3, +, htime4, yhat4, ) ; >> x l a b e l ( Time ) ; >> y l a b e l ( y ) ; >> t i t l e ( S o l u t i o n to x + 4x 5 x = 0, x ( 0 ) = 1, x ( 0 ) = 1 on [ 0, 3 ] ) ; >> l e g e n d ( True, RK1, RK2, RK3, RK4, L o c a t i o n, Best ) ;

RK Methods: Two Variables This generates the plot

RK Methods: Two Variables Now add an external input. We don t know true solution anymore. % d e f i n e t h e dynamics f o r y + 4 y 5 y = 10 s i n (5 t ) eˆ{.03 t } >> a = 1 ; b = 4 ; c = 5; >> B = (b/a ) ; A = (c /a ) ; C = 1/ a ; >> g = @( t ) 10 s i n (5 t ). exp (.03 t ) ; >> f = @( t, y ) [ y ( 2 ) ; A y ( 1 ) + B y ( 2 ) + C g ( t ) ] ; >> y0 = [ 1 ; 1 ] ; >> h =. 2 ; >> T = 3 ; >> N = c e i l (T/h ) ; >> [ htime1, r k a p p r o x 1 ] = FixedRK ( f, 0, y0, h, 1,N) ; >> y h a t 1 = r k a p p r o x 1 ( 1, : ) ; >> [ htime2, r k a p p r o x 2 ] = FixedRK ( f, 0, y0, h, 2,N) ; >> y h a t 2 = r k a p p r o x 2 ( 1, : ) ; >> [ htime3, r k a p p r o x 3 ] = FixedRK ( f, 0, y0, h, 3,N) ; >> y h a t 3 = r k a p p r o x 3 ( 1, : ) ; >> [ htime4, r k a p p r o x 4 ] = FixedRK ( f, 0, y0, h, 4,N) ; >> y h a t 4 = r k a p p r o x 4 ( 1, : ) ; >> p l o t ( htime1, yhat1, o, htime2, yhat2,,... htime3, yhat3, +, htime4, yhat4, ) ; >> x l a b e l ( Time ) ; >> y l a b e l ( Approx y ) ; >> t i t l e ( S o l u t i o n to x + 4x 5 x = 10 s i n (5 t ) eˆ{.03 t }, x ( 0 ) = 1, x ( 0 ) = 1 on [ 0, 3 ] ) ; >> l e g e n d ( RK1, RK2, RK3, RK4, L o c a t i o n, Best ) ;

RK Methods: Two Variables Note the plot is not quite smooth enough because of the coarseness of h. Big question is are we getting closer to the true solution?

RK Methods: Two Variables Homework 46 On all of these problems, for the given h and final time T, find the true solution generate the plot of the true solution and all four RK order plots as we have done in the example. Write this up with attached plots in word. 46.1 For h =.3 and T = 4. u (t) + u (t) 2 u(t) = 0; u(0) = 1; u (0) = 2 46.2 For h =.4 and T = 3. x (t) + 6 x (t) + 9 x(t) = 0; x(0) = 1; x (0) = 2 46.3 For h =.2 and T = 5. y (t) + 4 y (t) + 13 y(t) = 0; y(0) = 1; y (0) = 2

RK Methods: Two Variables Homework 47 On all of these problems, for the given stepsize h and final time T, generate all four RK order plots as we have done in the example. Write this up with attached plots in word. 47.1 For h =.4 and T = 3. 2 u (t) + 4 u (t) 3 u(t) = exp( 2t) cos(3t + 5) 47.2 For h =.2 and T = 4. u(0) = 2 u (0) = 3 u (t) 2 u (t) + 13 u(t) = exp( 5t) sin(3t 2 + 5) u(0) = 12 u (0) = 6