0% found this document useful (0 votes)
20 views

Diff Matlab C

Uploaded by

Attila Nagy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
20 views

Diff Matlab C

Uploaded by

Attila Nagy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 515
Ordinary and Partial Differential Equation Routines in C, C++, Fortran, Java®, Maple®, and MATLAB® Copyright 2004 by Chapman & Hal¥CRC Ordinary and Partial Differential Equation Routines in C, C++, Fortran. lava’. Manle® and Matr ‘pe SUL GUL, Java) PA) CG trai HJ. Lee and W.E. Schiesser CHAPMAN & HALL/CRC ACRE Pros Campany Boca Raton London NewYork Washington, D.C. Copyright 2004 by Chapman & Hal¥CRC vt epee ade of Sim MS, Mapes a reise wader of Waeroo Map, Is [MATLAB ss opted dem of Te MathWorks Ie. Ba po nrc, ee ctc cae ‘Tl $08-647-7000 ra ‘Web: wirwartiworkscom hipvwwmathnots som Library of Congress Cataloging in Publication Data tse, HL. (yn a) (Oninary and paral dierent equation routines in C. C+, Fortran, Java, Maple, and MATLAB / HJ Lee and WE. Sebiessex pcm. Includes bibliographical references and index. SDH 1-38498-423-1 AK papery 1, Diffrenalequatons-—Data processing. 2, Difercial equations, Partal—Data pressing | Sohieseee WF Tie (QA371,SD37L48 2003 SIS.3570285—de2? 2008085809 “This hook conti information obtained rom authentic and high regarded sourecs, Repent material {s quoted with permission, and sources are inicaled. A wide variety of references ave sted. Reasonable efflonts have been made to publish relish data and information, but the autor an the publisher cannot assume resporsiility forthe validity ofall materials forthe consequences of thee ws. [either this hook nor any part may be reproduced or transmit in any form or by any means, elect ‘or mechanical, including photocopying. ncroflming, and recrding. or by ny formation storage or reueval system, without prior pemission in writing from the publisher. ‘The consent of CRC Press LLC doesnot extend to copying for general distribution, for promotion, for creating new works, o foe resale. Specie permission must he obaine in writing fom CRC Press LLC for such copying Diget all inaiies 9 CRC Press LLC. 2000 N.W. Corporate Blvd. Boca Raton, Floida 3431 ‘Trademark Notice: Product or corporate names may be trademarks or regiterel trademarks, and sod only foe ionisation and explanation, without att to inking, Visit the CRC Press Web site at www.crepress.com_ (© 2004 by Chapman & HalUCRC No claim w original US. Goverment works Inurional Staind Hox Namib L38486-45-1 Liteary of Congress Crd Nombor 2003088809 Printed on aci-ree paper Copycat 2004 by Chapman & HalliCRC Preface Initial value ordinary differential equations (ODEs) and partial differential equations (PDEs) are among the most widely used forms of mathematics in science and engineering. However, insights from ODE/PDE-based models are realized only when solutions to the equations are produced with accept- able accuracy and with reasonable ettort. ‘Most ODE/PDE models are complicated enough (e.g,, sets of simultane- ‘ous nontinvar equations) tw precude analytical metiwods of suiutiony insivadi, numerical methods must be used, which is the central topic of this book. The calculation of a numerical solution usually requires that well established numerical integration algorithms are implemented in quality li- brary routines. The library routines in turn can be coded (programmed) in a variety of programming languages. Typically. fora scientist or engineer with an ODE/PDE- based mathematical model, finding routines written ina famil- iar language can be a demanding requirement, and perhaps even impossible (if such routines do not exist), The purpose of this book, therefore, is to provide a set of ODE/PDE in- tegration routines written in six widely accepted and used languages. Our intention is to facilitate ODE/PDE-based analysis by using the library rou Lines to compute reliable numerical solutions to the ODE/PDE system of interest However, the integration of ODE/PDEs is a large subject, and to keep this, discussion to reasonable length. we have limited the selection of algorithms and the associated routines. Specifically. we concentrate on explicit (nonstiff) Runge Kutta (RK) embedded pairs. Within this setting, we have provided integrators that are both fixed step and variable step; the latter accept a user- specified error tolerance and attempt to compute a solution to this required accuracy. The discussion of ODE integration includes truncation error moni- toring and control, hand p rennement, stability and stitiess, andl explicit and. implicit algoritims. Extensions to stiff systems are also discussed and illus- uated Huough an ODE application, however, 4 detailed presentation of stiff (implicit) algorithms and associated software in six languages was judged impractical for a book of reasonable length Further, we have illustrated the application af the ODF integration ronitines to PDEs through the method of lines (MOL). Briefly, the spatial (boundary value) derivatives of the PDEs are approximated algebraically, typically by finite differences (FDs); the resulting system of initial-value ODEs is then solved numerically by one of the ODE routines. Copy 2004 by Chapman & HalVCRE ‘Thus, we have attempted to provide the reader with a set of computational tools for the convenient solution of ODE/PDE models when programming, in any ofthesix languages. The discussion is introductory with limited math- ematical details. Rather, we rely on numerical results to illustrate some basic mathematical properties, and we avoid detailed mathematical analysis (e.g, revit an pout), which utay aot really provide muuch assistance iat Ue actual calculation of numerical solutions to ODE/PDE problems. Instead, we have attempted to provide useful computational tools in the form ofsoftware. The use ofthe softwares illustrated through asmall number of ODE/PDE applications: in each case, the complete code is first presented, and then its components are discussed in detail, with particular reference to the concepts of integration, eg,, stability, error monitoring, and control. Since the algorithms and the associated software have limitations (as do all algorithms and software), we have tried to point out these limitations, and make suggestions for additional methods that could be effective ‘Also, We have intentionally avoided using features specific to a particular language, 2, sparse utilities, object-oriented progranuiiins. Rath, we lave ‘emphasized the commonality of the programming in the six languages, and thereby illustrate how scientific computation can be done in any of the lan- ‘guages. Of course, language-specific features can be aclded to the source code that is provided ‘We hope this format will allow the reader to understand the basic elements of ODE/PDE integration, and then proceed expeditiously toanumerical solu- tion of the ODE/PDE system of interest. The applications discussed in detail, two in ODEs and two in PDEs, can be used as a starting point (ie, as tem= plates) for the development of a spectrum of new applications. ‘We welcome comments and questions about how we might be of assis- Lance (directed 10 west leh iigh.edu) Information for acquiring Qgratis) all he source code in this book is available from hitp://wwwlchigh.edu/~wesl/ weal.himl. Additional information about the book and software is available from the CRC Press Wob site, http:/ /www.crepress.com. Dr. Fred Chapman provided expert assistance with the Maple program- ming. We note with sadness the passing of Jaeson Lee, father of H. J. Lee, during the completion of H. J. Lee's graduate studies at Lehigh University. H. J. Lee W. E. Schiesser Bethlehem, PA CConyrisht 2004 by Chapman & HallCRC Contents 1. Same Basies of ODE Integration 1.1 General Initial Value ODE Problem 1.2 Origin of ODE Integrators in the Taylor Series 13 The Runge Kutta Method 1A Accuracy of RK Methods 1.5 Embedded RK Algorithms 1.6 Library ODE Integrators L7 Stability of RK Methods 2 Solution of a 11 ODE System 2.1 Programming in MATLAB 2.2. Programming in C 23 Programming in C++ 24 Programming in Fortran 2.5. Programming in Java 2.6 Programming in Maple 3. Solution of a 2x2 ODE System 3.41 Programming in MATLAB 3.2. Programming in C 3.3. Programming in C++ 3.4 Programming in Fortran 3.5. Programming in Java 3.6. Programming in Maple 4. Solution of a Linear PDE 4.1 Programming in MATLAB 4.2. Programming in C 43. Programming in C++ 44 Programming in Fortran 45. Programming in Java 4.6 Programming in Maple Solution of a Nonlinear PDE 5.1 Programming in MATLAB 52 Programming in C 5.3. Programming in C++ 5.4 Programming in Fortran Copyeht 2004 by Chapman & HalvCRC 55. Programming in Java 5.6. Programming in Maple AppendixA Embedded Runge Kutta Pairs AppendixB Integrals from ODEs Appendix Stiff ODE Integration Cl The BDF Formulas Applied to the 2:2 ODE System €.2 MATLAB Program for the Solution of the 2x2 ODE System CA MATLAB Program for the Solution of the 2x2 ODE System Using ode23s and ode15s AppendixD Alternative Forms of ODEs AppendixE Spatial p Refinement AppondixE Testing ODE/PDE Codec CConyrisht 2004 by Chapman & HallCRC 1 Some Basics of ODE Integration The central topic of this book is the programming and use of a set of li brary routines for the numerical solution (integration) of systems of initial value ordinary differential equations (ODES). We start by reviewing some of the basic concepts of ODEs, including methods of integration, that are the mathematical foundation for an understanding of the ODL integration routines. 1.1 General Initial Value ODE Problem The general problem fora single initial-value ODE is simply stated as FY.D, —— yllo) (1.0.2) where 1 — independent variable fly. 1) = derivative function, fy = initial value of the independent variable si = initial value of the dependent variable Equations 1.1 and 1.2 will be termed a 1x1 problem (one equation in one un- known). The solution of this 1x1 problem is the dependent variable asa function of the mdependent variable, y(t) (this function when substituted into Equations 1.1 and 1.2 satisfies these equations). This solution may be a mathematical function, termed an analytical solution. Copyeight 2004 by Chapman & HalvCRC ‘To illustrate these ideas, we consider the 1x1 problem, from Braun! (which will be discussed subsequently in more detail) Wo) = asya) where 4 and @ are positive constants. Equation 13 is termed a first-order, linear, ordinary differential equation wit uriable coefficients. These terms are explained below. Explanation Tyualion 19hasa devivativedy/dt — fy hey Ordinary Equation 1.3 has only one independent variable, f, 30 that the derivative dy/dt is a total or ordinary derivative Firstonder The highestrder derivative i first order (dy/dt is first order) Linear _yand its derivative dy/dt are tothe first power; thus, ‘Equation 1.3 is also termed first degree (do not contuse loner and degree) Variable coefficient The coefficient ¢~ isa function of the independent variahle, # GFit were a finetian af the dependent variable, v, Equation 1.3 would be nonlinear or not first degree) ‘The analytical solution to Equations 1.3 and 1.4 is from Braun:' p. 8) = werpl 20 ~ expt-at)), 0) = w (3) where exp(x) = e*. Equation 1.5 is easily verified as the solution to Equations these equations: ‘Substitution of Equation 15 Equations 13 and 14 woo(ha - eo) (3) (expla y(-0) =rwee S-eapatn Jean dey te wonr( 2- ex oD) 4 4 pn 4) werr(20-exp-at0)) = wie) w thus confirming Equation 1.5 satisfies Equations 1.3 and 14. Copyeigh 2004 by Chapman & HallCRC As an example of a muxn problem (1t ODEs in 1 unknowns), we will also subsequently consider in detail the 2x2 system 8 ayant $= (1.6) dy Te Az + Aza 000) = Yn The solution to Equations 1.6 is again the dependent variables, 41, yp, as a function of the independent variable, t, Since Equations 1.6 are linear, constant coefficient ODEs, their solution is easily derived, e.g., by assuming exponential funetionsin’ orby the Laplace transform Iweassume exponential functions wn) = cet : . a7 yolt) = ere where c;,¢». and J. are constants to be determined. substitution of Equations 1,7 in Equations 1.6 gives eRe = ayere | azexe™ eqhe™ = ancye™ + argcre™ Cancellation of e* gives assuming exponential sol ODEs) system of algebraic equations (this is the reason, ons works in the case of linear, constant coefficient er sane terre (18) anses + tz = A)e2 Tyuations 1.8 ate the 2:2 case of Ue Hinew ulgebruic eigeneulue proven (A-aDe=0 as) ty ayo an A Loo oro ood Copyeight 2004 by Chapman & HalvCRC « 0 0 0 °o n= 2 for Equations 18, and we use a bold faced symbol for a matrix or a vector, ‘The preceding matrices and vectors are A. nxn coefficient matrix I nxn identity matrix ¢ nx1 solution vector 0 nxl zero vector ‘The reader should confirm that the matrices and vectors in Fquation 1.9 have the correct dimensions for all of the indicated operations (e.g.. matrix addi- tions, matrix-vector multiples). Note that Equation 1.9 is a linear, homogeneous algebraic system (homoge- neousmeans that the right-hand side (RHS) isthe zero vector). Thus, Equation 19, or its 2x2 counterpart, Equations 1.8, will haow nontrivial solutions (c + 0) ifand only if i) the determinant of the cooficient matrix is zero, 1.6, [A -al| 19) Equation 1.10 is the chacteristic equation for Equation 1.9 (note that it is a scalar equation). The values of 3 that catisfy Equation 1.10 are the eigenvalues of Equation 1.9. For the 2x? problem of Equations 1.8, Equation 1.10 is Jon-a me Joan anal aun) (ay = 2am = 4) = arr Equation 1.11 is the characteristic equation or claracteristic polynomial tor Equations 1.8; note that since Equations 1.8 are a 2x2 near homogeneous algebraic system, the characteristic equation (Equation 1.11) isa second-order Copyeigh 2004 by Chapman & HallCRC polynomial, Similarly, since Equation 1.9 is a nvm linear homogeneous alge- braic system, its characteristic equation is a nth-order polynomial. Equation 1.11 can be factored by the quadratic formula (an +ax)2 + ander — arm =0 de (a2) Thus, ao expected, the 282 aystem of Equations 1.8 hao two eigenvalue, In general, the nn algebraic system, Equation 19, will have m eigonwal- utes Iya, «sy (which may be Teal oF complex conjugates, distinct or repeated). Since Equations 1.6 are linear constant coefficient ODEs, their general so- lution will be a linear combination of exponential functions, one for each eigenvalue ® (113) We = cre + eye! Equations 1.13 have tour constants which occur in pairs, one pair tor each eigenvalue. Thus, the pair [ex cai]? is the eigenvector for eigenvalue 1 while [er eza]" is the eigenvector for eigenvalue 22. In general, the 1x1 system of Equation 1.9 will have a 1x1 eigenvector for each of its 1 eigenvalues. Note that the naming convention for any constant in an eigenvector, ¢, is the ith constant for the jth eigenvalue, We can restate the two eigenvectors for Equation 1.13 (or Equations 1.8) as a). (el, uy Finally, the four constants in eigenvectors (Equations 1.14) are related through the initial conditions of Equations 1.6 and either of Equations 1.8 tho — e048 eget (1.13) soo = ene? + ene? ‘To simplify the analysis somewhat, we consider the special case @) = dz = =A, 2) = 0) = b, Where a and ? are constants. Then from Equation 1.12, _ tat VOPR _ Dada = 2 na kb ~ (a —b), +b) (1.16) Copyeight 2004 by Chapman & HalvCRC From the first of Equations 1.8 for A = Ai (ass = Ayden +ayerr =0 ( are constants to be determined, Ary trom Equation 1.51¢ 18 expanied mn a Laylor series in two variables, ba = fy uaf (ys Wile tuaioie = [Flu t+ ful ner fyi, tM + flys faal] W+ OUP) 1.32) ‘Substituting Equations 1.31b and 1.32 in Equation 1.31a gives Me = MEL er GI + CLS Wer 1 > Soler bee J yer tHe + fiir taht + OW?) WH (Cs Fea) FY, HE cal fli a f(b) + fil, taal + OU) 33) Note that Equation 1.33 is a polynomial in increasing powers of I; ie., it has the form of a Taylor series. Thus, if we expand yj... in a Taylor series around 4, we will obtain a polynomial of the same form, Le, in increasing powers oft dy yl ony wa Sra Sey +O) AF yt) = + flu, toh + +00") (134) a where we have used dy /dt = f(y, t),Le., the ODE we wish to integrate nu- merically. To match Equations 1.33 and 1.34, term-by-term (with like powers of i), we need to have [df (y,t)/dtl(#?/2!) in Equation 1.34 in the form of ule fa f (Yi i) + Joly fa an Equation 1.53, If chain-ruie differentiation is applied to d f(y, f)/dt afc b) dt fultie nt + filet) = Fale FC AY + FuMiet) (135) Copyeigh 2004 by Chapman & HallCRC Substitution of Equation 1.35 in Equation 1.34 gives Yr = M+ fi LF [Fold fF Ui t+ flys tO] a +.OUP) (1.36) We can now equate coefficients of like powers of i in Equations 1,33 and 1.36 Tquation 1.35 n (ea + e2) fet FH Weal alr taa Fw, A+ Fl Fa] [falc td Feb + fant] ‘Thus, we conchide toa 1.37) ext, — 1/2 as) ‘This is a system of two equations in three unknowns or constants (1, €2, 2); thus, one constant can be selected aubituarily (Ihere are actually an infinite number of second-order RK methods, depending on the arbitrary choice of ‘one of the constants in Equations 1.37). Here is one choice: Choose a =12 Other constants cj = 1/2 (1.38) % and the resulting second order RK method is ith: a = y beaks + eake = yb hy = swt Ky = FY Haak ty bd, Haat = Flys + Flu tlt A+ which is the modified Euler method, Equations 1.27, 1.28, and 1.29. For the choice Choose Other constants ¢; 0 (220) 4/2 the resulting second-order RK method is Meer = i berks + cxky = yt ke (1.40a) ke = fli + ark (yi, tb + ankle = Flu + 2D Fm, tl + 20h = fu + APD th + /2,IO (ase) Copyeight 2004 by Chapman & Hal¥CRC ‘Stepping formula: (dy) dons Naat Pug! 2: Exaet solution (0: Numerical solution ee Ta y wo - Mo xt) FIGURE Midpoint method. whichis the mudpoont method ilustrated in Figure 1.3. Asthename suggests, an Euler step is used to computea predicted value of the solution at the midpoint between points i and i + 1 according to Equation 1.40e. The corresponding midpoint derivative (ky of Equation 1.40) is then. from i to i +1 (according to Equation 1-403). Another choice of the constants in Faiation 1.97 is (Ieerles? p84) .cd toadvance the solution Choose & Other constants c) (at) and theretore vies — + ADE + B/D (1423) ky = fui tole (1.420) ko = Fly + 2/30k + 2/3 (820) ‘The third-order RK formulas are derived in the samme way, but the par- tial differentiation is more complicated. Thus, we just state the beginning Copyeigh 2004 by Chapman & HallCRC ‘equations and the final result (Iserles,? p. 40). The third order stepping for- mulais ist = Merk + cake + caks (1.43a) The RK constants are ky = Few, tyhe (1.436) k= fy back bake (1.43) Fy + Baki +s — bake, taste (48a) Four algebraic equations define he six constants 61,2, 3-at2rus ly (b= tained by matching the stepping formula, Equation 1:43a, with the Taylor series up to and including the term (a®y,/d)(h/3!) atota=t (1.430) ext | ests — 1/2 (38) cx0§ + ean = 1/3 (143g) estas ~ Deda = 1/6 (43h) ‘To illustrate the use of Equations 1.43e to 1.43h, we can take ¢2 and from Equation 1.43e, c) = 1 — 3 — 3 = 4. From Equation 1436 & (3/8)ag + G/B)a3 = 1/2 45. From Equation 113g, (3/8)(4/3 — as)? + (3/84; ras = # (by the quadratic formula). Thus 2 2 and from Equation 3/8) 2/3 — by)2/3 = 1/6 ‘This particular third-order Nustrom method (Iserles? p, 40) is therefore ist = i + (2/B)ky + G/B)k2 + B/8)ks (14a) ky = Fito (1.44) ho = fy + 23h, + A/S (te) ks = FY + 2/3dk2, 6 + 2/3) (44a) Wenext consider some MALLAB code which implements the Euler method. of Equation 1.28, the modified Euler method of Equations 1,30, the second- order RK of Equations 142, and the third-order RK of Equations 144 Copyeight 2004 by Chapman & Hal¥CRC ‘The objectives to investigate the accuracy of these RK methods in computing solutions to an ODE test problem. 1.4 Accuracy of RK Methods We start with the numerical solution of a single ODE, Equation 1.3, subject, (o initial condition Equation 1.4, by the Luler and modified Euler methods, Equation 1.28 and Equations 1.20. The analytical solution, Equation 1.5, can. bbe used to calculate the exact errors in the numerical solutions Equation 1.3 models the growth of tumors, and this important application is first described in the words of Braun’ (the dependent variable in Equation 1.3 is changed from “y” to “V" corresponding to Braun’s notation where V denotes tumor volume) It has been observed experimentally that “tree living” dividing cells, such as bacteria cells, grow al a sate proportional (@ the volume of the dividing cols at that moment, Let V(t) denote the volume of the divi cellgat time f. Then, voy a {or some positive constant 4. The solution of Equation 145is, vin = Mew (146) where Vp is the volume of dividing cells atthe initial time f. Thus, free living dividing cells grew exponentially with time One important conse quence of Equation 1.46 is that the volume of the cells keeps doubling every time interval of length In2/2. On the other hand, solid tumors do not grow exponentially with time. ‘As the tumor becomes larger, the doubling time of the toral tumor vol- ‘ume continuously increases. Various researchers have shown that the data for many solid tumors is fitted remarkably well, over almost 2 1000-fold increase in tumor volume. by the equation (previously Equation 15) ve wool Ma ont -») on where exp(x) —e", and d.and a are positive constants. that the tumor grows more and more slowly with the passage of time. and that it ultimately approaches the limiting volume Ve". Medical researchers havelong been concerned with explaining this deviation from simple exponential growth. A great deal ofinsight into this problem canbe gained by finding a differential equation satisfied by V(t) Differentiating Copyeigh 2004 by Chapman & HallCRC Equation 147 gives eo © sea abenit-en-a)) (148) (formerly Equation 1.3). ‘Two conilicting theories have been advanced tor the dynamics of tumor growth. They corsesporul lo the two attauigenents WY nomy MY aey eo) AV gw . ey ssn) of differential Equation 1.48, According to the first theory, the retarding ettect of tumor growti 1s due to an increase in the mean generation time Of the cells, without ¢ change in the proportion of xeproducnng cells. As fime goes on, the reproducing cella mature, of age, and thas divide more slowiy. This theory corresponds tothe bracketing of Equation 1.48a The bracketing of Equation 1.48b suggests the mean generation time of the dividing cells remains constant, and the retardation of growth is due to a Joss in reproductive cells inthe tumor. Une possible explana- {ign for this is thal @ nevrotic region develops in te center of the tuto “This necrosis appears at a ertical sce fora particular type of tumor, and thereafter the necrotic “core” increases rapidly as the total tumor mass increases. According to this theory, a necrotic core develops because in many tumors the supply ofblood, and thus of oxygen and nutrients, i al- most completely contined to the surtace ofthe tumor and a short distance Upewall it As te amor yoows, te supply of oxygen to the venti wore by diffusion becomes more and more difficult, resulting inthe formation of a necrotic core. ‘We can note the following interesting ideas about this problem: 1¢ Fquation 1 AR is a linear, variable coefficient ONE; it can ale he consid cred to have a variable eigenvalue. ‘* The application of mathematical analysis to tumor dynamics apparently started with a “solution” to an ODE. ie, Fquation 1.47 ‘+ To gain improved insight into tumor dynamics, the question was posed “ig there an OE corresponding, to Equation 1.47?” ‘+ Once an ODE was found (Equation 1.48), it helped explain why the solution, Equation 1.47, represents tumor dynamics so well shegis tion model, then using the solution to explain the performance of the problem eystom A MATLAB program that implements the solution of Equation 1.48 using the Euler and modified Euler methods, Equations 1.28 and 1.30, follows: Copyeight 2004 by Chapman & Hal¥CRC Program 1.1 Tumor model of eqs. (1.47), (1.48) Model parameters yori. lanbda=1.0; alpha-2.0; & Step through eases for neas % Integration step § Variablea for ODE integration te-20.9 § Initial condition vi=vo; v2=V0; % Print heading qprincz(*\n\nn = $6.32\n',n); Eprints ( oe ve va erevi coevi % Continue integration while t<0.999¥tE s ¥ Take nsteps integration steps for isl:nsteps s % Store solution at base point © RK constant k1 xa1+Lambda¥exp (-alpha*t) *v1*h; Copyeigh 2004 by Chapman & HallCRC K12=Lambda*exp (-alpha*t) *v2*h; % RK constant k2 22=Lambda*exp (-alpha*t) *v2*h; & RK step na (361249) /2 bth: end % Print solutions and error Ve=Votexp | (Lanbda/aipha) *{1.0-exp(-alpha*t)}); errvi-vi-Ve; errvieve-ver estvi-v2-V1; sprint (1§5.1£09,4£69.4£415.10£015.10£89.4£825.10£\n", Ve, V1, emrvi, ectv1,va,exev2); % Continue integration end Next case end Program 1.1 MATLAB program for the integration of Equation 1.48 by the modified Euler method of Equations 1.28 and 1.30 ‘We can note the following points about Program 1.1: ‘* The initial condition and the parameters of Equation 1.48are first defined (note that % defines a comment in MATLAB): gration steps Ir — 1.0, 0.1, 0.01, 0.001 : ¥ step through cases. for ncase-1:4 Copyeight 2004 by Chapman & Hal¥CRC & Integration step 0 jnsteps=1 end 1 jmsteps-10 ;end 01 ;nsteps-100 ena vulsnsceps=Luvu;ena For each hi, the corresponding, number of integration steps is nsteps Thus, the product (i\(rsteps) = 1 unit in t for each output from the program; ie., the output from the program is at ! = 0,1,2,..., 10. '¢ For each case, the initial and final values of fare defined, ie, 1 = 0. tf 10, and the initial condition, V(O) = VO is set to start the solution: s $ Variables for ODE integration crei0.0; 205 . § Initial condition vievo; va-vo Two initial conditions are set, one for the Euler solution, computed as VL, and one for the modified Euler solution, V2 (subsequently, we will program the solution vector, in this case [V1 ¥2]", as a one-dimensional (GD) array). ‘* A heading indicating the integration step, ft, and the two numerical solutions is then displayed. “...” indicates a line is to be continued on the next line. (Note. .. does not work ina character string delineated by single quotes, so the character string in the second fprinif statement has bbeen placed on two lines in order to fit within the available page width; to exocrite this program, the charactor string showild he rettimned to one line.) 5 § Print heading rprantt(*\n\nn = $6.3t\n",n); Eprinct(. © ve va exevi estvi va ereva\a!) ‘* A:whileloop then computes the solution until the final time, is reached: : % Continue integration waile t<0.999¢te Copyeigh 2004 by Chapman & HallCRC Of course, at the beginning of the execution, | = 0 so the while loop continues. ‘© nnsleps Euler and modified Euler steps are then taken: ape integra! ensteps mn etepe % Store solution at base point At each point along the solution (point ), the solution is stored for sub- sequent use in the numerical integration, ‘The first RK constant. fs. is then computed for each dependent variable in [V1 V2)]" according to Equation 1.27a: . & RK conetant ket ‘ambda*exp(-alphatt) *vith: ambda*exp(-alpha*t) *v2*h; Note that wehave used the RHS of the ODE, Equation 1.18, in computing ky is ky for VI, and kaa is ky for V2. Subsequently, the RK constants will be programmed as 1D arrays, e... (1) kx (2)]" ‘* Thesolution is then advanced from the base point according to Equation 198 4 4 RK conatant 12 vi-vibseat; voevahwiet 9; tetbehs k22elambda*exp (-alphatt) *V2*h; ‘The second RK constant, kz for V2, is then computed according to Equa- fion 17h. At the came time, the independent variahlo, #, fc advanced * The modified Euler solution, V2,is then computed according to Equation 41.20: : = RK wLep ‘The advance of the independent variable, ¢, was done previously and is therefore redundant; itis done again just to emphasize the advance in Copyeight 2004 by Chapman & Hal¥CRC for the modified Euler method. The end statement ends the loop of nsteps steps, starting with for iat nateps ‘+ The exact solution, Ve, is computed from Equation 147, The exact error iin the Duler sulution, c17 V1, atnd in the uiodified Euler sulution, err ¥2, are then computed. Finally, the difference in the two solutions, ¢stV1 — V2 V1, is computed as an estimate of the error in V1. The independent variable, [, the two dependent variables, V1, V2, and the three errors, errV1., errV2, estV1. are then displayed. © Print colutions and exrore Ve=vo+exp ( (Lambda/alpha) *(1.0-exp(-alphatt))) 7 prrvievi-va errv2=v2-ve estV1=V2-V1; Eprinté (145.1£%9.4ft9.a£%15,10£%15.10£%9.4£ #15.10£\n",...t,Ve,V1, errvi, estV1, V2, errv2) ; The ontpnt from the fprintf statement is considered subsequently. ‘+ The wltile loop is then terminated, followed by the end of the for loop that sets case : % Continue integration end § Next case end We now consider the output from this program listed below (reformatted slightly to fit on a printed page): n= 1,000 . ve vm erent earvt 1.0 1,8816 2.0000 0.1184036125 -0.1321205588 2.0 2.3742 2.7358 0.3615489626 -0.3514091013 3.0 2.5863 3.1060 0.5197432882 -0.4929227741 4.0 2.6689 3.2606 0.5916944683 -0.5573912375 5.0 2.7000 3.3204 06203353910 -0.5830821148 6.0 2.7126 3.3427 0.6321823526 -0.5928173392 7.0 2.7158 3.3510 0.6352171850 -0.5954380611 8.0 2.7174 3.3541 0.6367070277 -0.5977754104 Copyeigh 2004 by Chapman & HallCRC 9.0 2.7179 20.0 2.7182 3 3. 3552 3556 mogatied Euler method a.ee16 2.3742 2 5RKa 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 eeriee n= 0.200 Euler method ve eee 3742 5863 5589 -7000 :7116 71se 74 179 -7182 nodified Euler ve 8816 5262 7000 -7116 Copyeih 2004 by Chapman & HalvCRC v2 9679 2042 aa 7033 nn 27499 17586 7563 7568 Lipre v2 8816 5962 -7007 27122 6372559081 6374579380 exev2 o1z7169464 0102298612 269908749 0343032307 0372532762 0383660134 0387791239 0389316092 0389877746 vsyuusaaed errvi 0178364041 -0433341041 0575343031 os3se08e94 -0659265619 soesa06aaa1 oevaaa4a39 DR7282KRER 0672969439 -0673132386 errvz 0000409693 -000s222502, pnna25na7> -0006583152 0006707429 -0 -0 -0 -0 -0 +0 -0 -0 5982681335, 5984494919 estvi 0178773733 0430037365 0569959440 0629558472 0652682467 0661356702 neess70926 nR65786310 0666192852 0666353503 158 :7174 .7278 7182 = 0.020 Euler method ve ees 3742 15863 6689 =7000 crite .7158 7374 modified Euler ve seis 3742 -3863 16609 7000 m6 mse -7174 2.7179 7182 a 3. a 7 8. 9. ° Copyeigh 2004 by Chapman & HallCRC 7165 780 7186 n188 v1 eas 3786 5921 6754 7067 vies 7226 yaaa method vi eels 3742 9863 6690 7009 m6 7188 nm 7180 782 vy agai 2.3747 0006753402 0006770348 0006776587 0006778883 errv1 oo1e696826 0044269942 0058291952 0064227494 0066525021, vue /s0119 0067705073 0067622704 067266019 errv2 000000647 0000038793 0000060332 0000069239 cooce726a9 nnnnn73099 0000074392 -0000074566 0000074629 0000074653 eri oo01878608 -0008436596 estvl -0.0018697473 -0.0044231149, -0.0058231620, 00064158254 -0.0066452372 -u.vue/si2.yy -0.0067630680 0.0067748129, -0.0067792209 eatvt -0.0001878611 -0.0004436202, 5863 76689 +7000 7116 758 aren 7179 -7182 nodified Euler ve 8816 3742 5863 7000 7.7126 .7179 7182 5a68 16696 +7007 7122 27265 2 ae0 7186 .7288 method v2 8816 3742 5863 +7000 1.7216 nase .7179 17182 0005836997 0006429440 0006658719 0006744643 -0008776463 0006792528 0006794118 ercv2 0000000003 0000000394 0000000610 0000000734 0000000747 009000782 -0000000754 0000000754 -0.0005836386 -0.0006428744 -0,0006657985 -0.0006743896 -0.0006775727 -v.uuue 787495 -0.0006791774 0,.0006752364 We can note the following points about thio output ‘© Considering first the output for the Euler method at, ve I T8816 oa 18816 om 1886 0.001__1.8816 We can note the following points for this output: ve 2.0000 19995 185 1.8818 envi oT1eHORID S.0178064081 .ONTRKOKRN 0,0001878608 estv Vivestvi ‘O.1321205588 1.8679 0.0178773% AomisKo7472 RATA O.0001878611 1.8816 — The exact error, errV1. decreases linearly with integration step. I. For example, when his decreased from 0.01 to 0.001, errV1 decreases, from 0,0018696826 to 0,0001878608. Roughly speaking, as thedecimal point in lt moves one place, the decimal point in errV1 moves one place. However, this is true only when / becomes small (so that higher-order termsin the underlying Layior series become negligibly small). — Thus, the error in the Euler method 1s proportional to Copyeight 2004 by Chapman & Hal¥CRC eri Ch where C isa constant. The Euler method is therefore termed first order inn h or first order correct or of order h, which is usually designated as errV1 = Oh where “O” denotes “of order.” — The estimated error, es!V1 is also first order in hr (note again, that ash is decreased by a factor of 1/10, estV1 decteases by a factor of 1/10). Furthermore, the estimated error, es!V/1, approaches the exact error, errVforsmall h. Thisis an important point since the estimated error can be computed roithont knovong the exact solution; mother words, we can estimate the error in the numerical solution without knowing the exact solution. The estimated error,estV1 — V2~ V1is the same as ¢; given by Equation 1.26b and discussed in words following Equations 1.26. «al solution, V1, the coszected solution (in the last column) is much clozer to the exact solution, Ve. Thus, the cotimated error cannot only bbe used to judge the accuracy of the numerical solution, and thereby ‘used to decrease /t if necessary to meet a specified error tolerance (see again Equation 1.26b and the subsequent discussion), but the estimated error can be used as « correction for the numerical solution to obtain a more accurate solution. We will make use of these impor- tant features of the estimated error in the subsequent routines that automatically adjust the step, i, to achieve a specified accuracy. + Considering next the output for the modified Euler method at ve va enV I Tasi6 18679 —0.0137160464 01 1.8816 1.8816 —0,0000409693, 0.01 1881618816 —0,0000000647 O01 _Lssie _Lasie _—9.0000000003, We can note the following points for this output: — Theexact error for the modified Euler method. errV2. is substantially smaller than the error for the Euler method, errV1 and est V1. This is to be expected since the modified Euler method includes the second derivative term in the Taylor series, (¢?y/d!?)(i?/2!, while the Euler ‘method includes only the first derivative term, (dy/d1)(I/1), — In other words, the exact error, errV2, decreases much faster with ft than does err. The order of this decrease is difficult to assess from. the solution at f = 1. For example, when itis decreased trom 0.1 to 0.01, the number ot zeros aiter the decimal point increases from four (-0.000040968) to seven (—0.0000000647) (or roughly, a decrease of 1/1000). But when lr decreases from 0.01 to 0.001, the number of zeros Copyeigh 2004 by Chapman & HallCRC after the decimal point only increases from seven (—0.0000000647) to nine (—0.0000000003) (or roughly, a decrease of 1/100). Thus, is the order of the modified Euler method O(h?) or OU)? ‘+ Wecome toa somewhat different conclusion if we consider the modified Euler solution at f = 10: ve env2 T 7is2 27572 00390084461 01 27182-27188 0.0006778883 fo 271227182 annnon746s3 oo 27182 27182 _o.0000000754 We can note the following points for this output: = Thosrenr, rr¥/?, nw appears tahe sorand ardor Rar example, when f is rechuced from 0.1 te 0.01, the error decreases from (.00405778883 to 0.0000074653, a decrease of approximately 1/100. Similarly, when It is reduced from 0.01 to 0.001, the error decreases from 00000074653, to 0,0000000754, again a decrease of approximately 1/100. Thus, we can conclude that at least for this numerical output at ¢ = 10, the ‘modified Euler method appears to be second order correct, 1, orrV2.— 00) ‘We shall generally find this to be the case (the modified Euler method is second order), although, clearly, there can be exceptions (ie, the output at t = 1). ‘* Finally, we can come to some additional conclusions when comparing the output for the Euler and modified Euler methods: — Generally, for bath methods, the accuracy of the numerical solutions canbe improved by decreasing h. This process is termed h refinement, and is an important procedure in ODE: library integration routines, xe, decreasing i! to improve the solution accuracy. — Anerrorinthenumerical solution, in this case estV1, canbe estimated by subtracting the solutions from two methods of different orders, ie, estvi V1. This estimated error can then be used to adjust 10 achieve a solution of prescribed awcuracy (see Eyuations 1.26) ‘This proceduire of subtracting solutions of different order is termed p refinement since generally the order of the approximations is stated in terms of a variable "p", ie, error = Oth?) In the present case, p = 1 for the Euler method (it 1s first order correct), and p = 2for the modified Euler method (itis second order correct). Thus, by using the 7’ refinement of increasing p from 1 to 2, Copyeight 2004 by Chapman & Hal¥CRC we can estimate the error in the numerical solution (without having, to know the exact solution), and thereby make some adjustments in hr to achieve a specified accuracy. — Theintegration errors we have been considering are called truncation errorssince they result from truncation of the underlying Taylor series (after (dy/dt)(t/1}) and (déy/dt*) (4/2) for the Euler and modified Euler methods, respectively). — The preceding analysis and conclusions are based on a sufficiently small value of i that the higher-order terms (in/) in the Taylor series {atter the pomt of truncation) are negligibly small, — We have not produced a rigorous proof of (li) and O(h*) for the Eu- Jer method and modified Euler method. Kather, all of the preceding analysis was through the use ofa single, linear ODE, Equation 1.4. Thus, we Canmvi comiude thai these under wniitions axe generatiy trac (for any systcm of ODES). Fortunately, they have been observed to be approximately correct for many ODE oystems, both linear and nonlinear — Higher order RK algorithms that fit more of the terms of the underly ing Taylor series are available (consider the third-order RK method fof Equations 1.44) ‘The preceding error analysis can he applied ta them in the same way. and we will now consider again the results, for the numerical solution of Equation 1.48. In other words, we can consider hand p refinement for higher-order RK methods. — The higher order of the modified Euler method, O(i), relative to the Euler method, O(h), was achieved through additional compu- tation. Specifically, in the preceding MATLAB program, the Euler ‘method required only one derioutive evaluation (use of Equation 1.48) foreach step along the solution, while the modified Euler method re- quited two derivative evaluations for each step along the solution. In other words, wepay 4 “womputational prive” uf additional derivative evaluations when using higher-order methods (that fit more of the underlying Taylor series). However, this additional computation is usually well worth doing (consider the substantially more accurate solution of Fquation 1.48 when using the modified Fuler method relative to the Euler method. and how much more quickly the er- ror dropped off with decreasing hy i, OU") vs. Olt) ). Generally, an increase in the order of the method of one (e.g., Oth) to O12) requires one additional derivative evaluation for order up to and in- cluding four; beyond fourth order, increasing the order of accuracy by one will require more than one additional derivattve evaiuation (we shall observe this for a fiflirorder RK method to be discussed subsequently). — Inall ofthe preceding discussion, we have assumed that the solution toanODEsystemcanbe represented by a Taylorseries(ora truncated Copyeigh 2004 by Chapman & HallCRC Taylor series), which is basically a polynomial in ht. Of course, this does not have to be the case, but we are assuming, that in using numerical ODE integration algorithms, for sufficiently small the Taylor series approximation of the solution is sutticiently accurate for the given ODE application. — The RK method is particularly attractive since it can be formulated formcreasing orders (more termsin the laylor series) withouthaving, to differentiate the differential equation to proctuce the higher-order derivatives requited in the Taylor series. Thus, all we have to do in the programming of an ODE system is numerically evaluate the derivatives defined by the ODEs. — As we shall sce in subsequent examples, the RK method can be ap- plied to the mv problem (1 ODEs in 1 unknowns) as easily as we ag " [Procedure far the solition of systems of ONEs of virtually any ardor (nxn) and complexity (which is why it is so widely used). In other ‘words, the RK algorithms (as well as other well-established integra- tion algorithms) are a powerful tool in the use of ODEs in science and engineering; we shall see that the same is also true for PDEs. a We now conclude this gection hy considering the errors in the numerical solution of Equation 1.48 witha (2. 3) RK pair G.e.. O¢h®) and OU in analogy ‘with the (1,2) pair of the Euler and modified Euler methods), and thena (4, 5) pair (O(i*) and O(:>)). This error analysis will establish that the expected order conditions are realized and also will provide two higher RK pairs that we can then put into library ODE integration routines. ‘Ihe (2, 5) pair we considered previously (Equations 1.42 and 1.44) 1s coded in the following, program. Here we have switched back from the dependent variable V used previously in Equation 1.48 to the more commonly used y in Equation 1.3. Also, y2 is the solution of Equation 1.3 using the second-order RK of Equations 1.42 while y3 is the solution ssing the third-order RK of Equations 1 4 Tumor model of eqs. (1.47), (2.48) lor age (1a), (14), (1.8)) Model parameters alpha=1.0; = step through cases for ncase=1:4 Copyeight 2004 by Chapman & Hal¥CRC % Integration step )h=1.0 jnstep: yend )heO.1 jnsteps=10 ;end )n=0.01 ;nsteps=100 ;end )hev, vuinsteps=10vu;end + § Variablea for ODE integration e£-20.0; reo 0; s Initial condition 0; y3ey0; + % Print heading Eprancz("\n\nh = $e. 3f\n",n); Eprinet(... oe ye ¥2 erry? coty2 ye * % Continue integration while te0.999*rf * ¥ Take nsteps integration steps for i=i:nsteps ® % Store solution at base point RK constant k1 x12=Lambda*exp (-alpha*t) *y2*h; k13+Lambda*exp (-alpha*t) *y3*h; © RK constant k2 -y2b+ (2.0/3.0) *k12; -y3D+ (2.0/3.0) "13; beth +(2.0/3.0) +h; “Lanbdavexp (-alphate) *y34 % RK integration x3 y3=y3b+ (2.0/3.0) *k23; Copyeigh 2004 by Chapman & HallCRC b +(2.0/3.0)*h; k33-lambda*exp (-alpha*t) *y3* ’ SRK step 2+(1.0/4.0) *k12+ (3.0/4.0) *K22; S+(1.0/4.0) *KL5+ (5.0/8.0) kes (5.0/8.0) 755; betben, ond : & Print aniutions and errara ye=y0*exp { (lambda /alpha) * (1.0-exp(-alpha*t))): ervy2-y2-yer erry3=y3-ye; esty2=y3-y2; fprinté ('$5.1£89.4£89.4f815.10£815.10f89.4£815.10f\n", tyye,y2,erry2,esty2,y3,erry3); § continue integration ond A Next case end Program 1.2 Program for the integration of Equation 1.18by the RK (2, 3) pair of Equations, La2and 144 Program 1.2 closely parallels Program 1.1. The only essential difference is, the coding of the RK (2, 3) pair of Equations 1.42 and 1.44 in place of the RK 1, 2) pair of Equations 1.28 and 1.29. We can note the following points about Program 1.2: ‘© Initial condition (Fqu numerical solutions: tion 1.4) is again sot for y? and y3 ta start the + + The integration proceeds with the outer wile loop (that eventually reaches the final time, If), and an inner for loop that takes risteps RK ie stored at the base point for subsequent use in the RK formulas: : ¥ Continue integration while to0.999ete Copyeight 2004 by Chapman & Hal¥CRC s § Take nsteps integration steps for i=l:nsteps 5 % Store solution at pase point yebsye; y2b=y3; tbe “Ihe RK constant fy 1s computed for each dependent variable by using Equation 1.3 (612 for the kof y2 and 13 for the ki of y3): * ¥ RK constant kt k12-Lambdatexp (-alpha*t) +24 k13eLanbdatexp (-alphatt) +y3+h; ‘© The solution is then advanced from the base point using a 2 weighting applied to k; and hr (in accordance with Equations 1.42 and 1.44): ® & RK constant k2 2+ (2.0/3.0) *K12:; Be (2.0/3.0) *k13; b +(2.0/3.0) *h; Lambda*exp(-alpha*t) *y2*h; This advance of the dependent and independent variables sets the stage for the calculation of ks (again, using Equation 1.3). ‘* kevis computed for 43 (it is not required for 12): . % nk integra: ya-yabe (2.0/3.0) #223; b +(2.0/3.0) +h. Lambda*exp(-alpha*t) *y3" tions can be advanced to the next point using the stepping formulae: = RK step yaey2br (1.0/4.0) 12+ (3.0/4.0) *h22; ena Note that the stepping formula for y2 does not include ks, The end! state- ment concludes the for loop that is executed steps times. Copyeigh 2004 by Chapman & HallCRC ‘© The solutions, y2 and 1, and associated errors are then displayed: + % Print solutions and errors ye=y0¥exp( (lambda/alpha) *(1.0-exp(-alpha*t))); erry2-y2-ye; erry3=y3-yer esty2-y2-y2; Eprints (145.1689, 1689. 46825. 206825.20889.46 215.108\n',..-t.ye,¥7 eety2.y2,errva); ‘+ Finally, the while loop 1s concluded, followed by the for loop that sets the values of i, and the initial and final values of f: + Continue integration ond Next case end # Note that Equation 1.3 was used twice to compute Ar and A2 for y2 (lwo derivative evaluations), and Equation 1.3 was used three times to com pute k;, kz, and ky for y3 (three derivative evaluations). This again illus- trates the additional computation required, in this case, the calculation of ks, to achieve higher-order results (OUP) rather than Ov), This improved accuracy is evident in the following output from Program 1.2, ‘The output from Program 1.2 is listed below (again, with some minor for- matting to fit on a printed page): h= 1.000 second order RK 2.9 2.8816 1.9918 9.0102750113 -9.0185221389 > 9.742 9 1995 9 259890307 = 925990429 3.0 2.5863 2.6170 0,0307095187 -0. 0408693758 4.0 2.6689 2.7014 00324302424 -0. 0425724266 5.0 2.7000 2.7330 00330043494 -0.. 0431262652 6.0 2.7116 2.7448 0,0332064307 -0. 0433188969 7.0 2.7158 2.7491 00332794779 -0. 0433881911 8.0 2.7174 2.7507 0.0333061722 -0.0434134670 9.0 2.7179 2.7513 0,0333159682 -0.0434227360 20.0 2.7182 2.7515 0.0333195687 -0.0434261419 Copyeight 2004 by Chapman & Hal¥CRC ‘Third order RK ye sais <3742 soues 6609 +7000 nus nea .7174 n79 7182 n= 0.100 Second order RK ye 3742 75863 16689 7000 -7116 nse na 7273 :7182 ‘Third order RK ye sais 3742 -5863, 5689 +7000 -71se m8 -7178 7182 Copyeigh 2004 by Chapman & HallCRC y: 732 3642 diet 6508 6299 7014 087 7073 7078 7081 ¥: 3748 5869 6696 7007 722 7165 ye. nes 7198 ¥: aeis 3742 5362 6689 7000 vase a8 779 7181 -0 -0. svluidyes 12 -0 -0 -0 -0. -0 -0. -0 -0. -0. -0 -0. -0 -0. erry3 0083471276 0099760125 o1or423842 -0101219158 o101124662 nra97997739 0101072948 0101067678 0101065733 erey? 0005660244 -0006477190, 0006733478 0006819708 -0006850226 0006861284 006865329 -000s866e24 0006867360 erry3 0000090358 0000102699 0000104074 ovvo103865 0000103698 0000103577 panoinas6a 0000103560 0000103558 “0. -0. -0. -0 -0 -0 -0. 0006970938 esty? 0005762943 0006581264 0006837363 0006923405 -0006953838 0006964862 006958894 0006970374 h= 0.010 Second Order RK ye 8810 3742 +5863 e600 ana0 .7116 vise -7174 27179 7182 Third order RK ye 3742 -5863 +6689 7000 -7116 nse na .7273 7102 n= 0.002 Second order RK ye -8816 3742 3863 7000 m6 7158 :7174 Copyeih 2004 by Chapman & HalvCRC ye 3742 15862 6690 7000 7116 738 .7174 17180 71e2 y 3742 15863 6689 7000 - 7216 7158 na .7273 17202 yi 8816 3742 5863 7000 m6 2.7158 .7174 -0. -0 -0 -0 -0 -0 -0. 0000000095 erry2 wouuuss ry 000062016 000070634 0000072355 pnaao7a975 0000074601 0000074720 0000074763 0000074779 0000074785 erey2 0000000096 0000000096 000000096 0000000096 -0000000096 0000000095 000000095 2000000095 erry? -0000000362 ovv000626 000000713 0000000749 pnaanna7s> -0000000754 0000000754 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 esty2 uovuussse> 000062232 0000070731 0000072452 onnag7aa7 0000074697 0000074815 0000074859 0000074875 0000074880 esty2 0000000362 ovvv000826 0000000733 2000000749 0000000754 0000000754 9.0 2.7179 2.7179 0.0000000754 -0.0000000754 10.0 2.7182 2.7182 0.0000000754 -0.0000000754 ‘mara order RK ® ye y erzy3 1.0 2.8816 1,816 0.0000000000 2.0 2.2742 2.3742 0.0000000000 40 9.886% 9 886% 4 NaNonaaonA 4.0 2.6689 2.6689 4.0000000000 5.0 2.7000 2.7000 0.0000000000 6.0 2.7116 2.7116 0.0000000000 7.0 2.7158 2.7158 0.0000000000 8.0 2.7174 2.7174 —0.0000000000 9.0 2.7179 2.7179 9.0000000000 qv.u Z.fge 2, /1ve — u.vuuuvuuuve This output closely parallels the previous output for the (1, 2) RK pair. Here are some details. * Considering the output for the second-order RK at 7 ve wR erry esty2 1 18816 1.8918 00101750113 ~0,0185221389 1 1188168819 0.0003179977_—0,9003270335 0.01 1.8816 1.8816 0000035779 —0,0000035865 fool 18816 T8816 —a9000N00%62 —_—o,00n0N0036? = The O01?) behavior of erry2 is clear. i. for ht = 0.1.0.01, 0.001 the corresponding values of erry2 are 0.0003179977, 0.0000035779, _0.0000000362 00 that for each reduction in it by 1/10, crry2 is reduced by a factor ‘oF 1/100 (two more zeros are added after the decimal point), — The same is truc for the estimated error, erty2 (computed as the difference y3 — 2), ie, for ht — 0.1, 0.01, 0.001 the corresponding values of osty? are —0,0003270335, —0,0000035865, —0.0000000362 so that two more zerus are added after the deci 1/10 reduction in I. I point for each, Copyeigh 2004 by Chapman & HallCRC — The estimated error, es1y2is in close agreement with the exact error, erry2, for small — Thus, adding esty2 as a correction to y2 will bring the corrected 2 into closer agreement with the exact solution, ye. In other words, esty2 canbe used to determine whether iis small enough to achieve a prescribed accuracy, and once an acceptable tis thereby selected, esty2can be added to 2 to improve the numerical solution (all with~ ‘out knowledge of the exact solution). ‘ The corresponding output for the third order RK at ¢ = 1 is ee emmys TY _Issie 18732 —o.008347i276 Gol L88i6 8816 —0.0000000085 0.001 18816 8816 20000000000 — Again, the third order behavior is clear. For fi = 1,0.1, 0.01, 0.001, the corresponding exact errors are 00083471276, —0.0000090358, —0,0000000085, _ 9,0000000000 80 a 1/10 reduction in h results in a 1/1000 reduction in err 3. — Infact, since for mostscientificand engineering applications of ODES, five figure accuracy of the numerical solutions is usually adequate, the last two values of erry3 (for It = 0.01, 0.001) can be considered excessively small (these errors are much less than five significant fig- ‘ures compared to the exact solution ye = 1.8816). Inother words, ft = 0.01, 0.001 are excessively sittall, This is ant important point. While MATLAB produced all of the numerical output (for — 1, 0.1, 0.01, 0.001) in the order of a second or two for this modest 1x1 problem, for large systems of ODFs, using an execessively small H will merely result in long computer run times with no significant improvement in the accuracy of the solution. Thus, library routines for integrat- ing ODEs increase as well as decrease it to produce solutions close to the specified error tolerance (and not far below the specified error because of excessively small hi). We shall subsequently consider this feature of reducing and increasing i to stay close to the specified error tolerance in the ifbrary routines. — Stated in another way, the preceding solutions tor = 1,0.1, 001, (0.001 forthe mterval <1 < tf(= 10) required 10/1, 10/0.1, 10/001, 10/0.001 steps, respectively. 10/0.1=100 steps were adequate (because of the accuracy of the third-order RK), while 10/0.01 — 1000 Copyeight 2004 by Chapman & Hal¥CRC ‘and 10/0.001 = 10000 steps produced excessive accuracy. However, 10/1 = 10 steps were inadequate as might be expected. * Inconclusion, the effectiveness of higher order algorithms,e.., the third- order RK, in reducing the error in the numerical solution of ODEs is clearly evident from this example. To conclude this section, we consider a widely used RK (1,5) pair, the Runge Kutta Fehlberg (RKF) method (Isorles,? p. 84): ky = fy toh (1.49) ke = fy thy i+ hh (1.495) ky = Fy + B/32)k: + O/32)ke, fe + B/8)HVh (1.49) ky = f(y + (1932/2197 )ky — (7200/2197 )ky + (7296/2197), + C2/A3)Iyh (49a) ks = f(y + 499/216); — Bky + (3680/513)is — (B45/4104)ky, hhh (1.49e) ke = fly — (8/27 ky 4 Aha — ABA /2565 dks + (1859/4104, (110k, + PDI (499, A O(h*) stepping formula is then Yio = i + Q5/AGKy + (1408/2565)ko + (Z197/AIOHAY— A/S)ks (1.49y) and a Uti) stepping formula is (with the same k terms) sist = w+ (16/315)Ky + (6656/12825)k3 + (28561 /56430) ks (9/50 )ks + (2/55) (49h) An error estimate can then be obtained by subtracting Equation 1.49g from Equation 149h: 6 tsa thas asin Note that six derivative evaluations are required (k; through ks), even though, the final result from Equation 1.49h is only O(h*) (the number of derivative evaluations will, in general, be equal to or greater than the order of the final stepping formula), The stepping, formulas of Equations 1.49h and 1.4¥4g match the Laylor series up to and including the terins (dy /dt)(/4) and Fy dE) /3)), respec tively, as demonstrated by the following Program 1.3. Copyeigh 2004 by Chapman & HallCRC Program 1.3 Tumor model of eqs. (1.47), (1.48) (or eqs. (1.3), (1.4), (1.5) Model parameters vora.05 lambda=2.0; alpha-2.0; & Step Phrangh cases for ncase=1:4 & Integration step +0 jnsteps=1 jend imsteps=10 jend it (ncase==4) he. vu1nsteps=10u;end \ Voriablee for ODE integration ee-30 ¥ Initial condition va=v0; v5=V0; = Print neading qprintr(*\n\nn = $6.3f\n',0); Eprints (. roe ve ve ereve cotva % Continue integration while t<0.999¥tf Take nsteps integration steps for i=1:nsteps ’ = Store solution at base poinc vapeva; vb-ts 2 Copyeih 2004 by Chapman & HalvCRC RK constant kl ambda*exp (-alpha*t) *V4*h; ambda¥exp (-alpha*t) *V5*h; % RK constant K2 vaevaptu. 25714; Vo-vabe0.25*K15; te eb10.25¢h; k2d-Lambdatexp (-alphatt) *v4+h; 3e28=1 ambdavoxn (-alphase) #us+h; © RK constant 12 Wa=vab+(3.0/32.0)*k14. +(9.0/32.0) #24; VSeVSb+(3.0/32.0)#k15 +(9.0/32.0)*K25; ce cpt (5.0/8.0) "hy x34=Lambda*exp(-alpha*t) *va+h; K35-Lambda*exp (-alphat) *V5+h; 4% RK conatant ke VasVabs (1932.0/2197.0) *k14 = (7200.0/2197.0) #k24.. +(7296.0/2197.0) *k34; VS=VSb+ (1932.0/2197.0) #15 = (7200.0/2187.0) #25 417296 .0/2187.0) *k35; te eb+(12.0/13.0)"; wad=Lambda*exp(-alpha*t) *va+h; k4S-Lambdatcxp( alphatt) *v5+h; & RK conetant ks Vaevabs( 439.0/ 216.0) *k14 -C 8.0 yeK24 +(3680.0/ 513.0) *k34 = ( 845.0/4104.0) #44; VW5=v5b+( 439.0/ 216.0) *K15.. -( 8.0 )#K25 +(3880.0/ 513.0) 735 = ( 845.0/4104.0) #45; “Lambda vexp (-alphatt) *vael kes8-]ambdatexp (-alphatr) #vseh % RK constant k6 Copyeigh 2004 by Chapman & HallCRC 4b-( 8.0/ 27.0) #14 +( 2.0 )K24 = (3544.0/2565.0) ¥K34 +(1859.0/4104.0) #44 ( 11.0/ 40.0) «54; pb-( 8.U/ 2Y.U) TKD +( 2.0 yeK25, (3544.0/2565.0) +35 + (2959.0/4204.0) #kas =( 11 Of 40 oy ¥eRs; fe stb+0.5th: k65=lambda*exp(-alpha*t) *V5¥h; ’ ‘ 25.0/ 216.0)*k14 1408.0/2565.0) #34. 2197.0/4104.0) "kaa. 3.0/5.0) *k54; vs-vsbi( 16.0/ 135.0) +ki5. 1( 6656.0/22025.0) +295 4(9a861 0/56430_0) #145, -(9,0/ 50.0) *k55 +( 2.0/5.0) *k65; t =tb+h; end : = Print solutions ana erzore Vve=vorexp ( (1ambaa/aipna) * (1.0-exp(-alpna*t))); ereVS-V5 Ve; Fprintf (15 1f80 af, 4F875.10F815 10F8a AFEIS.1OF\n! t.Ve,V4,errv4,estv4,V5,errV5) ; % Continue integration end & Next case ena Program 1.3 Program for the integration of Equation 148 by the RKF43 method of Equations 1.49 Program 1.3 closely parallels Programs 1.1 and 1.2. Therefore, we consider only the essential difference, the evaluation of the RK constants, ky to ky Copyeight 2004 by Chapman & Hal¥CRC % Store solution at base point vost; 4% RK constant ki ke14-Lambda*exp (-alpha+t) +y4*hy k15~Lambdatexp (-alphatt) *y5*h; % RK constant k2 yaayab+0.25*k14; yS-ySb+0.25*k15; tb+0.25*h; ambda*exp(-alpha*t) +y4*h; ambda¥exp (-alpna*t) *y5*h; k2 \ RK constant k3 yanyabs (3.0/32.0) +24. 1(9.0/22.9)*%24, ysayshs (30/39 0) 4k15, +(9.0/32.0) +25: t= tb+ (3.0/8.0) *h; k34=Lambda*exp(-alpha*t) *y4*h; k35=Lambda*exp (-alphatt) *y5*h; % RK constant Ké yasyabs (1932.0/2197.0) "14 = (7200.0/2137.0) *k24 +(7296.0/2197.0) +34; sbi (2922.0/2197.0) #15 = (7700.0 /9197..0) #425 +(7296.0/2197.0) #35; t= tb+(12.0/13.0) #h; k44=Lambda*exp (-alphatt) *y4*h; k45=1ambda*exp (-alpha*t) *y5*h; % RK constant k5 yasyap+( 439.0/ 216.0) "k14 -( 8.0 yak. e45.0/4204.0) #244; 4200/2160) #K15 (8.0 ys. +(3680.0/ 513.0) ¥K35.. Copyeigh 2004 by Chapman & HallCRC = ( 845.0/4104.0) #45; ebeh; 54=1ambda*exp (-alpha*t) *ya*h; 55=Lambda*exp (-alpha*t) y5*h; : ¥ KK constant ko yasyab-(8.0/ 27.0) +K14 HC 2.0 y+ka4 = (3544.0/2565.0) #34 4(7a89 p/aina 0) eka =( 11.0/ 40.0)4K54: yS=ySb-(8.0/ 27.0) #kI5, + 2.0 )4k25... = (3544.0/2565.0) #435. +(1859.0/4104.0) #45 =( 11,0/ 40.0) ¥K55; seotu.otny 65=Lambda*exp (-alpha*t) *y5*h; ‘ © BE ctep 28.af 716. 0)#k14 +( 1408.0/2565.0) *k34 +( 2197.0/4104.0) #44 1.0/5.0) *k54; 16.0/ 135.0) *k15 +( 6656 .0/12825.0) *k35.. +(28561.0/56430.0) #45, =( 3.0/ 50.0) *K55 +( 2.0/5.0) *k65; ft stbehy ona Not much explanation is required for this code since it follows directly trom Equations 1.49a to 1.49), We can note the following points: * Clearly there is a substantial degree of repetitive coding that could be streamlined through the use of 1D arrays (particularly in the calculation Of to ke). ‘+ The O(h") and OU) solutionsare computed independently, and we will next observe that they can be combined. * The code isa mixture of problem-specificcoding, ie, using Equations 1.3 and 1.4, and general coding, i., Equations 1.49a to 1.491. The separation of the code into problem-specific and general coding would facilitate the application of the (4, 5) pair to other problems; we will see how this can be done; ie, we are headed toward the development of general library routines Copyeight 2004 by Chapman & Hal¥CRC ‘The output from Program 1.3is listed below (again, reformatted slightly to fit ona printed page): 1.000 Fourth order method e ye ya ereyt eotya 1.0 1.8816 1.8814 -9.0001703891 9. onn0660329 2 2.9742 9.3780 =a nan2aKRRer a pnnin27992 3.0 2.5863 2.5860 -0.0002711657 _0.0001138050, 4.0 2.6689 2.6687 -0.0002799191 0.0001183957 5.0 2.7000 2.6997 -0.0002821506 0.0001202510 6.0 2.7116 2.7113 -0.0002843446 —0.0001209619 7.0 2.7158 2.7155 -0.00028478¢8 0.0001212276 Bu 2.7178 2.7171 -u.v00zvavae9 0. v0uIz15259 9.0 2.7179 2.7177 -0.0002850065 0. 0001213621, 10.0 2.7102 2.7179 -0.0002050205 © 0.0003213755 Fifth order method t ye ys erryS 1.0 1.8816 1.8815 -0.0001043662 2.0 2.3742 2.3741 -0.0001437920 3.0 2.5863 2.5861 -0.0001573607 4.0 2.6689 2.6688 -0.0001615234 5.9 2.7000 2.6999 -0. 0001628996 6.0 2.7126 2.7114 -0.0001633627 7.0 2.7150 2.7156 9.9002629572 2.0 2.7174 2.9172 -9.0001626210 9.0 2.7179 2.7178 -0.0001636464 10.0 2.7182 2.7180 -0.0001636530 b= 0.100 Fourth order metnoa u re vs cuays eeiys 1.0 2,0026 12,0016 9.9000000232 +0. e000000235 2.9 2.2742 2.2742 -9.000000019@ 9. 0000000292 3.0 2.5863 2.5863 -0.0000000218 a. 0000000212 4.0 2.6689 2.6689 -0.0000000226 0.0000000220 Copyeigh 2004 by Chapman & HallCRC 2.7000 2.7116 2.7158 2.7174 2.7179 erie 7000 .7216 17258 m4 7278 see igth order method ve e016 3742 5863 6689 +7000 site -7158 7.7174 7179 n= 0.010 ys aai6 13742 15863 6629 +7000 aera 17258 .7274 Fourth order method ya 8816 13742 see 7000 -7216 .7158 74 .7178 7182 Copyeih 2004 by Chapman & HalvCRC -0 -0 -0 -0 -0. <0 -0 -0. -0 -0 -0. -0 0000000229 0000000230 0000000231 0000000231 000000232 vouuvvuest erry5 0000000003 0000000005 oad000006 000000006 000000006 2000000006 000000006 ecaacoacc6 errys 0000000000 0000000000 ecadcoagce 000000000 -0000000000 0000000000 0000000000 000000000 +0000000000 -0000000000 000000000 0000000223 0000000224 0000000224 0000000225 9000000225 escys 9000000000 0000000000 pecasea900 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 3.0 2.5863 2.5863 0.0000000000 4.0 2.6689 2.6689 0. 0000000000 5.0 2.7000 2.7000 0.0000000000 6.0 2.7116 2.7116 0.0000000000 7.0 2.7158 2.7158 9.0000000000 Bu 2.4/4 4.7174 u.vuuuvuuuve 9.0 2.7179 2.7173 0.0000000000 10.0 2.7182 2.7182 0.0000000000 b= 0.001 Fourth order method t ye ya erzy4 esty4 1.0 1.8816 1.8816 9.0000000000 0. 0000000000 2.0 4.8/42 4.5142 U.vuuuvuuuvy —_v-vuuuvJuUU 3.0 2.5863 2.5863 9.0000000000 —_9.0000000000 4.0 2.6689 2.6689 0.0000000000 0. 0000000000 5.9 2.7000 2.7000 9.000e009000 @. 0000000000 7.0 2.7158 2.7158 0.0000000000 9. 0000000000 8.0 2.7174 2.7174 0.0000000000 0. 0000000000 9.0 2.7179 2.7179 0.0000000000 0. 0000000000 10.0 2.7182 2.7182 0.0000000000 —0.0000000000 Firth oraer methoa © ye ys creys 2.9 2.8026 2.9916 90000000000 2.9 9.3747 7.3742 9 annonnanna 3.0 2.5863 2.5863 0.0000000000 4.0 2.6689 2.6689 0.0000000000 5.0 2.7000 2.7000 0.0000000000 6.0 2.7116 2.7116 0.0000000000 7.0 2.7158 2.7158 0.0000000000 8.0 2.7174 2.7174 _0.0000000000 v.09 2.7178 2.7173 0.0000000000 10.0 2.7182 2.7182 9.0000000000 his outputis relatively easy to discuss since there are alot of zeros! Specit- ically, Copyeigh 2004 by Chapman & HallCRC © Att_= 1, with l = 1, which corresponds to a total of 10/1 = 10steps, the Uh) method computed a solution accurate to at least four figures! 1.0 1.8816 1.8815 —0.0001043662 With 100 steps (ht = 0.1) the error is only ~0.0000000003 at 1.0 1.8816 1.8816 -0.0000000003, ‘= The Oli) behavior of the fourth order method is evident (at least to a degree). Att = 1, we we ory esiya T Tassie T8814 —U,OD0L70S91_.UODUEENE oo T8816 T8816 —-BaNNHANHAAN—BANNoNANANN 0.001__1.8816 1.8816 _0.0000000000 __0.0000000000 Approximately four zeros are added to the exact and estimated errors when Iris reduced from 1 to 0.1. When four more zeros (between ft ~ 0.1 and fr = 0101) are added, the errar drops below (0000000000 corre sponding to the %15.10f format of the forintf statement in Program 1.3. Clearly, we can conclude that It = 0.01, 0.001 are excessively small for most practical applications in science and engineering. * The O¢h®) behavior of the fifth-order method is evident (also, to a de- gree). Att =1, ve e emmys 7 Tssie Lesi5 _ 0,0001043660 a1 TaRI6 R816 —A.annaNNNAN 001 1.8816 1.8816 0.000000, G.o01 18816 18816 _0.0000000000 At least five zeros are added to the exact error when hr is reduced from 1 to 0.1, When five more zeros (between ht = 0.1 and ft = 0.01) are added, the error drops far below 0.0000000000 (presumably) corresponding to the %15.10f format of the fprintf statement in Program 1.3. Again, we can conclude that i = 0.01, 0.001 are excessively small for most practical applications, and a hibrary routine would be far more efficient it hited the reduction in itt somewhere in the range U.i = i? = i rather than allow fr to drup much below 0.1 ‘© We can conclude the additional effort to compute the KK constants ki to Ais probably worthwhile since far larger steps (Ii) canbe used to achieve Copyeight 2004 by Chapman & Hal¥CRC a solution ofa given accuracy than when using lower-order methods (the (1, 2) pair or even the (2, 3) pair). Finally, we can consider why the various RK algorithms have the orders we have observed (heyond just observing that the higher-order methods fit more of the terms in the underlying Taylor series). For example. why is the first-order RK (Euler’s method) Oi)? ‘The first-order RK method includes the (dy/dt)(lt/) term in the Taylor se- ries, but excludes through truncation of the Taylor series the term (d?y/d'?) x (12/2) and higher-order terms. If the second order term is considered, the principal source of the integration error for the Euler method, which is true for small it for which the higher-order terms are negligibie, then it would seem that the Euler method is second order (because of the 1? in (@? yl?) (28). However, this sccond derivative term is the local or one step error, that is, computing a numerical solution using, foreample, Programs 1 1to1 3, many stepsare taken, and we are primarily interested in the falal or glohal error after many steps (thisis the error that we actually observe in the numerical solution toan ODE system, and which we want to control at some acceptable level). ‘Wecan analyze the relationship between the local error and the global error in the following way. If we assume that the error in using the Euler method. 1s due to just the second derivative term: Pyle = eo then the local or one step error is Uh). If we integrate over a series of steps of length hi from t = a tot = b using 1 steps, that is we can then estimate the total or global error ae global error = (one step error)(number of steps) ay IP (b ay (0 dior = ib (Heat) a (et), so that the global erroris OW/t) as we observed. Note that thisisan approximate analysis based on two assumptions: or 1, All of the loval error is contained in just the one tern (Wy fe?) 2. 2. The derivative d?yj/d¢* 1s essentially constant over the interval a < {Sb (or we can use sume appropriate average value of this second derivative). Copyeigh 2004 by Chapman & HallCRC A more rigorous analysis to show that the Euler method is O(lt) globally is rather involved. Also, generally for the higher-order methods, the global error will be one order in h lower than the one step error, so, for example, the previous (4, 5) pair is 0¢h°) — O(H*) locally, but Oth") — OH?) globally (again, this can be established in a nonnigorous way for a general interval f = w to 1 = as we did for the Buler method). We now consider the streamlining of the programming as mentioned pre~ viously for the (A, 5) pair. 1.5 Embedded RK Algorithms We first note the interesting property of the RKF (4. 5) pair that the RK con- stants k; toks given by Equations 1.49a to 1.49e are the same forboth the O(l) and O(h*) stepping formulas of Equations 1.49g and 1.49h (ks is required for only the O(/) method of Equation 1.49h). Thus, we can consider the O(i*) method of Equation 1.49g to be embedded in the Q(i) method of Equation 1.49h. Thishasan important implication: k; tos need becalclated oy once for Dott methods (rather than for eact method as in Prograsm 1.3). With this iddea in mind, the only difference between the two methods is the calculation of iy forthe (15) method of Equation 119g, and the selection ofa base point forthe next step (either the OU") or the QU") base point—we will select the latter) This same feature appears in the (1. 2) pair of Equations 1.28 and 1.29; the Euler method is embedded in the modified Euler method. with the common k, of Equation 1.27a, Similarly, for the (2,3) pair, the second-order method of Equation 1.42a is embedded in the third-order method of Equation 14a, with the common kr of Equation 1.42b (or Equation 144b) and the common ky of Equation 1.42c (or Equation 1.44¢). ‘The embedding of the (1, 2) pair is illustrated by the following Program. 1.4, which is a smail revision of Program 1: Program 1.4 = Step through cases for nease=1:4 Copyeight 2004 by Chapman & Hal¥CRC % Integration step send pend he0.01 imsteps=100 ;end )n=0.002 ;nsteps=1000;end 4 variables for ODE integration e£-10.0; 0.0; * % Initial condition yeey0; ‘ % Print heading fprint£(*\n\nh = $6.3£\n!,h); tprantt ( as ye ya erry. estyi y2 erry2\n") while ten ooastf ‘ 4 Take neteps integration steps for isi:nsteps % % Store solution at base point yoay2i whet; ‘ SRK conatant ie Lanbbdatoxp (-alphateb) *y2¥h, . : constant 2 -yb+k1; beh; ‘Lambda*exp (-alpha*t) *y2*h; * ¥ step LyD+KL; syst (R1+K2) /2.07 Print solutions and errors ye=y0¥exp ( (lambda/alpha) * (1.0-exp(-alpha*t))); Copyeigh 2004 by Chapman & HallCRC erryleyi-ye; erry2=y2-yer fprinté (°$5.1f89. 4f89.4f%15.10£815.10£%9.4£%15.10f\n", tyye,yl,erryl,esty, y2,erry2) ; ¥ continue integration end & Next case ena Progeam 14 Program for the integration of Equation 1.48 by the embedded ((1, 2) pair) modified Euler method of Equations 1.28 and 1.29 We can note the following pomts about Program 14: ‘© The essential differences between Programs 1-1 and 1.2 are in the way that the RK constants are computed and used. In particular, while keep- ing in mind that y1 is the O(h) (Euler method) and y2 is the O(i*) (modified Euler method), the base point is selected as the running value ot y2: * % Slore wulution al base podal yo-y2 eb. where the initial value of y2 was set previously as an initial condition. ‘* ky and k are then calculated (according to Equations 1.27a and 1.27b): % RK constant kL ki=sLambda*exp (-alpha*th) *y2*h; sh: k2eLambda*exp(~alpha*t) *y2*h; ‘¢ The first- and second-order stepping formulas are then used (according Copyeight 2004 by Chapman & Hal¥CRC Note in this code that: = The estimated err in yl, eoty1, is computed by p refinement (sub traction of the O(f} solution from the O() solution). = The same value of ky is used for both the first- and second-order stepping formulas (making use of the embedding of the (2,3) pair, uc, the firot order method ie embedded in the second orders method) = The end statement terminates Ue for loop of ssteps of length f Otherwise the programming is essentially the same as in Program 1.1. The ‘output from Program 1.4 is listed below (formatted to fit on a page): b= 1.000 First order method ye 1.0 2.8816 2.0000 9.11@4026125 -9.1321205588 2. 9.7742 9.8880 0 IRNRDIGKKA -0.-176R4710RI 3.0 2.5863 2.7070 0.1207761366 -0.0939556225 4.0 2.6689 2.7432 0.0742305143 -0.0399272836 5.0 2.7000 2.7528 0.0527351769 -0.0154819007 6.0 2.7116 2.7557 0.0441724614 -0.0058064480 7.0 2.7158 2.7567 0.0409303986 -0.0021512746 8.0 2.7174 2.7571 0.0397250855 -0.0007934762 9.0 2.7179 2.7572 0.0392799583 -0.0002921837 10.0 2.7182 2.7573 0.0391159724 -0.0001075263 Second order method t ye erry? 1.0 1.8816 1 00137169464 2.0 2.3742 2 0.0101398613, 3.0 2.5863 2 00268205142 4.0 2.6689 2 0.0343032307 5.0 2.7000 2 0.0372532762 70 3. 2 0.0397793239 eo 2 2 9.0299216002 oa 2 2 0.03a9877746 10.0 2. 2 0.0390084461 Copyeigh 2004 by Chapman & HallCRC h= 0.100 First order method ye -8810 3742 +5863 e600 ana0 .7116 vise -7174 27179 7182 vi 3760 15074 e608 a0 7223 7165 .7281 17186 na8 Second order method ye 3742 -5863 +6689 7000 -7116 nse na .7273 1.7102 n= 0.010 y 23745 15868 6696 7007 .7122 7165 780 7286 17288 First order method ye -8816 3742 3863 7000 m6 7158 :7174 Copyeih 2004 by Chapman & HalvCRC yi 8816 3742 +5863 = 7000 m6 -7158 .7174 erry1 vvziuruaye 0017594212 0011768161 0008767242 na78a9K0s -0007059944 o006ee3s24 0006818277 0006794227 0006785373 erey? 0003303677 0005383591 0006250422 0006583152 -0006707429, 0006753402 0006770348 -0006776587 0006778003, erryl -0000217778 0000178106 000121768 0000081729 9naa077991 -0000075636 000075024 -0 -0 -0 -0 -0 -0 -0 -0 <0 -0 <0 <0 -0 esty1 vuiaryyyt 0014290935 0006384570 0002516919 onnngaaasa 0000352515 0000130223 0000047929 0000017640 0000006491 estyl 0000218425 0000139313 0000061436 2000009079 0000001243 0000000458 9.0 2.7179 10.0 2.7182 2 2 7180 782 Second order metnoa ye a.ear6 2.2742 > saKa 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.rase = 0.902 ¥: aeis 2742 aKa 6690 7000 ns 7158 na74 7180 vase First order method ye sais -3742 5863 5689 -7000 .7126 7180 m8 n79 -7182 y! aeis 3742 5263 6689 7000 vas vase a8 7179 mea Second order method ye 8016 -5962 -7000 .7226 Copyeigh 2004 by Chapman & HallCRC yi esis 5262 7900 m6 0000074798 0000074715 erzy2 0000000647 0000028793 nanongara> 0000069239 0000072649 0000073922 0000074392, 0000074566 0000074629 svuuvv7aeos erryl 0000002185 -0000001784 0000001222 ovvoo00960 -0000000824 0000000780 codecaa764 0000000756 -0000000755 erry2 0000000003 -0000000620 0000000734 0000000747 -0 -0. -0 -0 -0 -0. 0000000024 -0 -0 0000000169 000000062 estyl 0000002187 0000001390, 000000612 000000240 2000000030 2000000022 000000002 0000000002, 7.0 2.7158 2.7158 0,0000000751 8.0 2.7174 2.7174 0,0000000753 9.0 2.7179 2.7179 0.0000000754 10.0 2.7182 2.7182 0. 0000000754 ‘We can note the following points about this output: ‘+ The first order method appears to be higher than O(). For example, at 1 = 1, the output is i we a emi eatul 7 T8B16 2.0000 01184036125 —0.1321208588 01 8816 1.8837 0021070298 —0,0021479991 oot 18816 0.0000217778 —0,0000218125 0.001 18516 _ 0.0000002185, In fact, the first-order method appears to be second order correct! For ex- ample, reducing Jr from 0.1 to 0.01 reduces the exact error from .0977107079R to 0.0000717778 (two zer0s are added after the decimal point). The reason for this is that the second-order solution. u2. is used as the base point for the next step along, the solution, i.e, + © etore eolution at base point yhey? tb=t: Tu state this in other words, y1 is corrected by styl before going un ty the next point. For example, at = 1 for ht = 0.1, y+ ylest 8837 —0.0021479991 = 1.8816 = y2 Thisisan important point discussed previously asStep 4 in the algorithm atter Equation 1.26c. In other words, m a hibrary ODE integrator, which automatically adjusts the step ir, the estimated error estyl will generally be computed (© determine if the step itis sinall enough to satisfy a specified error tolerance. When It becomes small enough to mect the ‘error criterion, the estimated error can be acided as a correction before faking the nevt stop along tha ealution In thie eaca (the (1,2) pair), this in effect increases the accuracy of the solution from O(h) to OU) as we observed in the preceding output from Program 1.4 ‘© This error correction could be programmed in a sii equivalent, way (see Equations 1.30) yhtly dfifferent, but Copyeight 2004 by Chapman & Hal¥CRC

You might also like