Runge - Kutta Methods for first order models James K. Peterson Department of Biological Sciences and Department of Mathematical Sciences Clemson University October 30, 2013 Outline Runge - Kutte Methods RK Methods: One Variable
Abstract This lecture discusses the Runge - Kutta Method for first order models. 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.
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. 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 dep 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 dep on time. So from now on, we write f=@(t,x) 3*x even if there is no time depence.
DoEulerTwo.m: the functions are now time depent. 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 find % 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 ; xhat ( 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) ) ; 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.
f u n c t i o n [ tnew, ynew, fnew ] = RKstep ( fname, tc, yc, fc, h, k ) % fname the name o f the r i g h t hand s i d e f u n c t i o n f ( t, y ) % yc approximate s o l u t i o n to y ( t ) = f (t, y ( t ) ) at t=t c % f c f ( tc, yc ) % h The time step, k i s RK order 1<= k <=4 % tnew t c+h, ynew i s approx s o l n at tnew, fnew i s f (tnew i s, ynew ) i f k==1 ynew = yc+k1 ; e l s e i f k==2 k2 = h f e v a l (fname, tc+(h /2), yc+(k1 /2) ) ; ynew = yc + k2 ; e l s e i f k==3 k2 = h f e v a l (fname, tc+(h /2), yc+(k1 /2) ) ; k3 = h f e v a l ( fname, tc+h, yc k1+2 k2 ) ; ynew = yc+(k1+4 k2+k3 ) / 6 ; e l s e i f k==4 k2 = h f e v a l (fname, tc+(h /2), yc+(k1 /2) ) ; k3 = h f e v a l (fname, tc+(h /2), yc+(k2 /2) ) ; k4 = h f e v a l ( fname, tc+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 ) ) ; tnew = t c+h ; fnew = f e v a l (fname, tnew, ynew ) ; 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 s t e p s to t f 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 ) = approximate s o l u t i o n at % t v a l s ( j ), 1 <= j <= n tc = t0 ; yc = y0 ; t v a l s = tc ; 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 tc ] ;
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 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 the 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.
Homework 59 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. Homework 59 Continued 59.1 h =.05 and the model is y =.07 y (75 y), y(0) = 5. 59.2 h =.06 and the model is u (t) =.13 u(t) (30 u(t)), u(0) = 45. 59.3 h =.4 and the model is x = 1.9x, x(0) = 6. 59.4 h =.5 and the model is x = 1.95x + 30, x(0) = 16.