Ordinary and Partial Differential Equation Routines in C, C++, Fortran, Java®, Maple®, and MATLAB® H.J. Lee and W.E. Schiesser
CHAPMAN & HALL/CRC A CRC Press Company Boca Raton London New York Washington, D.C.
Copyright 2004 by Chapman & Hall/CRC
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 acceptable accuracy and with reasonable effort. Most ODE/PDE models are complicated enough (e.g., sets of simultaneous nonlinear equations) to preclude analytical methods of solution; instead, numerical methods must be used, which is the central topic of this book. The calculation of a numerical solution usually requires that wellestablished numerical integration algorithms are implemented in quality library routines. The library routines in turn can be coded (programmed) in a variety of programming languages. Typically, for a scientist or engineer with an ODE/PDE- based mathematical model, finding routines written in a familiar 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 integration routines written in six widely accepted and used languages. Our intention is to facilitate ODE/PDE-based analysis by using the library routines 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 userspecified error tolerance and attempt to compute a solution to this required accuracy. The discussion of ODE integration includes truncation error monitoring and control, h and p refinement, stability and stiffness, and explicit and implicit algorithms. Extensions to stiff systems are also discussed and illustrated through an ODE application; however, a 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 of the ODE integration routines 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.
Copyright 2004 by Chapman & Hall/CRC
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 of the six languages. The discussion is introductory with limited mathematical details. Rather, we rely on numerical results to illustrate some basic mathematical properties, and we avoid detailed mathematical analysis (e.g., theorems and proofs), which may not really provide much assistance in the actual calculation of numerical solutions to ODE/PDE problems. Instead, we have attempted to provide useful computational tools in the form of software. The use of the software is illustrated through a small 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, e.g., 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, e.g., sparse utilities, object-oriented programming. Rather, we have emphasized the commonality of the programming in the six languages, and thereby illustrate how scientific computation can be done in any of the languages. Of course, language-specific features can be added 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 to a numerical solution 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 (i.e., as templates) for the development of a spectrum of new applications. We welcome comments and questions about how we might be of assistance (directed to
[email protected]). Information for acquiring (gratis) all the source code in this book is available from http://www.lehigh.edu/˜ wes1/ wes1.html. Additional information about the book and software is available from the CRC Press Web site, http://www.crcpress.com. Dr. Fred Chapman provided expert assistance with the Maple programming. 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
Copyright 2004 by Chapman & Hall/CRC
Contents
1 Some Basics of ODE Integration 1.1 General Initial Value ODE Problem 1.2 Origin of ODE Integrators in the Taylor Series 1.3 The Runge Kutta Method 1.4 Accuracy of RK Methods 1.5 Embedded RK Algorithms 1.6 Library ODE Integrators 1.7 Stability of RK Methods 2 Solution of a 1x1 ODE System 2.1 Programming in MATLAB 2.2 Programming in C 2.3 Programming in C++ 2.4 Programming in Fortran 2.5 Programming in Java 2.6 Programming in Maple 3 Solution of a 2x2 ODE System 3.1 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 4.3 Programming in C++ 4.4 Programming in Fortran 4.5 Programming in Java 4.6 Programming in Maple 5 Solution of a Nonlinear PDE 5.1 Programming in MATLAB 5.2 Programming in C 5.3 Programming in C++ 5.4 Programming in Fortran
Copyright 2004 by Chapman & Hall/CRC
5.5 Programming in Java 5.6 Programming in Maple Appendix A Embedded Runge Kutta Pairs Appendix B Integrals from ODEs Appendix C Stiff ODE Integration C.1 The BDF Formulas Applied to the 2x2 ODE System C.2 MATLAB Program for the Solution of the 2x2 ODE System C.3 MATLAB Program for the Solution of the 2x2 ODE System Using ode23s and ode15s Appendix D Alternative Forms of ODEs Appendix E Spatial p Refinement Appendix F Testing ODE/PDE Codes
Copyright 2004 by Chapman & Hall/CRC
1 Some Basics of ODE Integration
The central topic of this book is the programming and use of a set of library 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 ODE integration routines.
1.1
General Initial Value ODE Problem
The general problem for a single initial-value ODE is simply stated as dy = f (y, t), dt where
y= t= f (y, t) = t0 = y0 =
y(t0 ) = y0
(1.1)(1.2)
dependent variable independent variable derivative function initial value of the independent variable initial value of the dependent variable
Equations 1.1 and 1.2 will be termed a 1x1 problem (one equation in one unknown). The solution of this 1x1 problem is the dependent variable as a function of the independent 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.
Copyright 2004 by Chapman & Hall/CRC
To illustrate these ideas, we consider the 1x1 problem, from Braun1 (which will be discussed subsequently in more detail) dy y(t0 ) = y0 (1.3)(1.4) = λe −αt y, dt where λ and α are positive constants. Equation 1.3 is termed a first-order, linear, ordinary differential equation with variable coefficients. These terms are explained below. Term Differential equation Ordinary First-order Linear
Variable coefficient
Explanation Equation 1.3 has a derivative dy/dt = f (y, t) = λe −αt y Equation 1.3 has only one independent variable, t, so that the derivative dy/dt is a total or ordinary derivative The highest-order derivative is first order (dy/dt is first order) y and its derivative dy/dt are to the first power; thus, Equation 1.3 is also termed first degree (do not confuse order and degree) The coefficient e −αt is a function of the independent variable, t (if it were a function of the dependent variable, y, Equation 1.3 would be nonlinear or not first degree)
The analytical solution to Equations 1.3 and 1.4 is from Braun:1 λ y(t) = y0 exp (1 − exp(−αt)) , y(0) = y0 α
(1.5)
where exp(x) = e x . Equation 1.5 is easily verified as the solution to Equations 1.3 and 1.4 by substitution in these equations: Terms in Equations 1.3 and 1.4 dy dt
−λe −αt y = 0 y(0)
Substitution of Equation 1.5 in Equations 1.3 and 1.4
λ λ (1 − exp(−αt)) (−exp(−αt))(−α) α α λ (1 − exp(−αt)) (exp(−αt)) = λy0 exp α λ −λe −αt y0 exp (1 − exp(−αt)) α = 0
y0 exp
λ (1 − exp(−α(0))) y0 exp α
thus confirming Equation 1.5 satisfies Equations 1.3 and 1.4.
Copyright 2004 by Chapman & Hall/CRC
= y0 (e 0 ) = y0
As an example of a nxn problem (n ODEs in n unknowns), we will also subsequently consider in detail the 2x2 system dy1 = a 11 y1 + a 12 y2 dt
y1 (0) = y10
dy2 = a 21 y1 + a 22 y2 dt
y2 (0) = y20
(1.6)
The solution to Equations 1.6 is again the dependent variables, y1 , y2 , 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 functions in t or by the Laplace transform. If we assume exponential functions y1 (t) = c 1 e λt y2 (t) = c 2 e λt
(1.7)
where c 1, c 2 , and λ are constants to be determined, substitution of Equations 1.7 in Equations 1.6 gives c 1 λe λt = a 11 c 1 e λt + a 12 c 2 e λt c 2 λe λt = a 21 c 1 e λt + a 22 c 2 e λt Cancellation of e λt gives a system of algebraic equations (this is the reason assuming exponential solutions works in the case of linear, constant coefficient ODEs) c 1 λ = a 11 c 1 + a 12 c 2 c 2 λ = a 21 c 1 + a 22 c 2 or
(a 11 − λ)c 1 + a 12 c 2 = 0 a 21 c 1 + (a 22 − λ)c 2 = 0
(1.8)
Equations 1.8 are the 2x2 case of the linear algebraic eigenvalue problem (A − λI)c = 0 where
a 11 a 21 A= . ..
a 12 a 22
a n1
a n2
1 0 I= . ..
0 1
0
0
··· ··· .. .
· · · a nn
··· 0 ··· 0 .. .. . . ··· 1
Copyright 2004 by Chapman & Hall/CRC
a 1n a 2n .. .
(1.9)
c1 c2 c= . .. cn 0 0 0= . .. 0 n = 2 for Equations 1.8, 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 c nx1 solution vector 0 nx1 zero vector The reader should confirm that the matrices and vectors in Equation 1.9 have the correct dimensions for all of the indicated operations (e.g., matrix additions, matrix-vector multiples). Note that Equation 1.9 is a linear, homogeneous algebraic system (homogeneous means that the right-hand side (RHS) is the zero vector). Thus, Equation 1.9, or its 2x2 counterpart, Equations 1.8, will have nontrivial solutions (c = 0) if and only if (iff) the determinant of the coefficient matrix is zero, i.e., |A − λI| = 0
(1.10)
Equation 1.10 is the characteristic equation for Equation 1.9 (note that it is a scalar equation). The values of λ that satisfy Equation 1.10 are the eigenvalues of Equation 1.9. For the 2x2 problem of Equations 1.8, Equation 1.10 is a 11 − λ a 12 =0 a 21 a 22 − λ or (a 11 − λ)(a 22 − λ) − a 21 a 12 = 0
(1.11)
Equation 1.11 is the characteristic equation or characteristic polynomial for Equations 1.8; note that since Equations 1.8 are a 2x2 linear homogeneous algebraic system, the characteristic equation (Equation 1.11) is a second-order
Copyright 2004 by Chapman & Hall/CRC
polynomial. Similarly, since Equation 1.9 is a nxn linear homogeneous algebraic system, its characteristic equation is a nth-order polynomial. Equation 1.11 can be factored by the quadratic formula λ2 − (a 11 + a 22 )λ + a 11 a 22 − a 21 a 12 = 0
(a 11 + a 22 ) ± (a 11 + a 22 )2 − 4(a 11 a 22 − a 21 a 12 ) λ1 , λ2 = 2
(1.12)
Thus, as expected, the 2x2 system of Equations 1.8 has two eigenvalues. In general, the nxn algebraic system, Equation 1.9, will have n eigenvalues, λ1 , λ2 , . . . , λn (which may be real or complex conjugates, distinct or repeated). Since Equations 1.6 are linear constant coefficient ODEs, their general solution will be a linear combination of exponential functions, one for each eigenvalue y1 = c 11 e λ1 t + c 12 e λ2 t y2 = c 21 e λ1 t + c 22 e λ2 t
(1.13)
Equations 1.13 have four constants which occur in pairs, one pair for each eigenvalue. Thus, the pair [c 11 c 21 ]T is the eigenvector for eigenvalue λ1 while [c 12 c 22 ]T is the eigenvector for eigenvalue λ2 . In general, the nxn system of Equation 1.9 will have a nx1 eigenvector for each of its n eigenvalues. Note that the naming convention for any constant in an eigenvector, c i j , is the ith constant for the jth eigenvalue. We can restate the two eigenvectors for Equation 1.13 (or Equations 1.8) as
c 11 c 21
,
λ1
c 12 c 22
(1.14) λ2
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 y10 = c 11 e λ1 0 + c 12 e λ2 0 y20 = c 21 e λ1 0 + c 22 e λ2 0
(1.15)
To simplify the analysis somewhat, we consider the special case a 11 = a 22 = −a , a 21 = a 12 = b, where a and b are constants. Then from Equation 1.12, λ1 , λ2 =
−2a ±
Copyright 2004 by Chapman & Hall/CRC
(2a )2 − 4(a 2 − b 2 ) = −a ± b = −(a − b), −(a + b) (1.16) 2
From the first of Equations 1.8 for λ = λ1 (a 11 − λ1 )c 11 + a 12 c 21 = 0 or (−a + (a − b))c 11 + bc 21 = 0 c 11 = c 21 Similarly, for λ = λ2 (a 11 − λ2 )c 12 + a 12 c 22 = 0 or (−a + (a + b))c 12 + bc 22 = 0 c 12 = −c 22 Substitution of these results in Equations 1.15 gives y10 = c 11 − c 22 y20 = c 11 + c 22 or y10 + y20 = c 21 2 y20 − y10 = −c 12 = 2
c 11 = c 22
Finally, the solution from Equations 1.13 is y1 =
y10 + y20 λ1 t y20 − y10 λ2 t e − e 2 2
y10 + y20 λ1 t y20 − y10 λ2 t e + e y2 = 2 2
(1.17)
Equations 1.17 can easily be checked by substitution in Equations 1.6 (with a 11 = a 22 = −a , a 21 = a 12 = b) and application of the initial conditions at t = 0:
Copyright 2004 by Chapman & Hall/CRC
y10 + y20 λ1 t y20 − y10 λ2 t e − λ2 e 2 2 y10 + y20 λ1 t y20 − y10 λ2 t = −(a − b) e + (a + b) e 2 2
y + y y − y 10 20 λ1 t 20 10 λ2 t e − e +a 2 2
y + y20 − y10 λ2 t y20 λ1 t 10 −b e + e 2 2 = 0 y10 + y20 λ1 t y20 − y10 λ2 t λ1 e + λ2 e 2 2 y10 + y20 λ1 t y20 − y10 λ2 t e − (a + b) e = −(a − b) 2 2
y + y y20 − y10 λ2 t 10 20 λ1 t e + e +a 2 2
y + y y − y 10 20 λ1 t 20 10 λ2 t e − e −b 2 2 = 0
dy1 dt
λ1
+a y1 −by2 = 0 dy2 dt
+a y2 −by1 = 0
For the initial conditions of Equations 1.6 y10 + y20 λ1 0 y20 − y10 λ2 0 e − e = y10 2 2 y10 + y20 λ1 0 y20 − y10 λ2 0 y20 = e + e = y20 2 2 y10 =
as required. The ODE problems of Equations 1.3, 1.4, and 1.6 along with their analytical solutions, Equations 1.5 and 1.17, will be used subsequently to demonstrate the use of the ODE integration routines and to evaluate the computed solutions. Since these problems are quite modest (1x1 and 2x2, respectively), we will also subsequently consider two problems with substantially more ODEs. At the same time, these ODE systems will be considered as approximations to partial differential equations (PDEs); in other words, we will use systems of ODEs for the solution of PDEs.
1.2
Origin of ODE Integrators in the Taylor Series
In contrast to the analytical solutions presented previously (Equations 1.5 and 1.17), the numerical solutions we will compute are ordered pairs of numbers. For example, in the case of Equation 1.3, we start from the pair (t0 , y0 ) (the initial condition of Equation 1.3) and proceed to compute paired values (ti , yi ) where i = 1, 2, . . . is an index indicating a position or point along the solution.
Copyright 2004 by Chapman & Hall/CRC
Taylor series (Equation 1.18), in this case, to Equation 1.19. In other words, εi is the truncation error for Euler’s method, Equation 1.19. We could logically argue that the truncation error could be reduced (for a given h) by including more terms in the Taylor, e.g., the second derivative term (d 2 yi /dt 2 )(h 2 /2!). Although this is technically true, there is a practical problem. For the general ODE, Equation 1.1, we have only the first derivative available dyi = f (yi , ti ) dt The question then in using the second derivative term of the Taylor series is “How do we obtain the second derivative, d 2 yi /dt 2 ?”. One answer would be to differentiate the ODE, i.e., d2 y d f (y, t) ∂ f dy ∂ f ∂f ∂f d dy = = + = f + (1.20) = dt 2 dt dt dt ∂ y dt ∂t ∂y ∂t Then we can substitute Equation 1.20 in Equation 1.18: yi+1 = yi + f i h +
∂f ∂f f + ∂y ∂t
i
h2 2!
(1.21)
where again subscript “i” means evaluated at point i. As an example of the application of Equation 1.21, consider the model ODE dy = f (y, t) = λy dt
(1.22)
where λ is a constant. Then
f i = λyi ∂f ∂f f + = λ (λyi ) ∂y ∂t i
(note: ∂ f /∂t = 0 since f = λy does not depend on t) and substitution in Equation 1.21 gives yi+1 = yi + λyi h + λ (λyi )
h2 = yi (1 + λh + (λh)2 /2!) 2!
yi (1+λh +(λh)2 /2!) is the Taylor series of yi e λh up to and including the h 2 term, but yi e λh is the analytical solution to Equation 1.22 with the initial condition y(ti ) = yi for the integration step, h = ti+1 − ti . Thus, as stated previously, Equation 1.21 fits the Taylor series of the analytical solution to Equation 1.22 up to and including the (d 2 yi /dt 2 )(h 2 /2!) term. Of course, we could, in principle, continue this process of including additional terms in the Taylor series, e.g., using the derivative of the second
Copyright 2004 by Chapman & Hall/CRC
derivative to arrive at the third derivative, etc. Clearly, however, the method quickly becomes cumbersome (and this is for only one ODE, Equation 1.1). Application of this Taylor series method to systems of ODEs involves a lot of differentiation. (Would we want to apply it to a system of 100 or 1000 ODEs? We think not.) Ideally, we would like to have a higher-order ODE integration method (higher than the first-order Euler method) without having to take derivatives of the ODEs. Although this may seem like an impossibility, it can in fact be done by the Runge Kutta (RK) method. In other words, the RK method can be used to fit the numerical ODE solution exactly to an arbitrary number of terms in the underlying Taylor series without having to differentiate the ODE. We will investigate the RK method, which is the basis for the ODE integration routines described in this book. The other important characteristic of a numerical integration algorithm (in addition to not having to differentiate the ODE) is a way of estimating the truncation error, ε, so that the integration step, h, can be adjusted to achieve a solution with a prescribed accuracy. This may also seem like an impossibility since it would appear that in order to compute ε we need to know the exact (analytical) solution. But if the exact solution is known, there would be no need to calculate the numerical solution. The answer to this apparent contradiction is the fact that we will calculate an estimate of the truncation error (and not the exact truncation error which would imply that we know the exact solution). To see how this might be done, consider computing a truncation error estimate for the Euler method. Again, we return to the Taylor series (which is the mathematical tool for most of the numerical analysis of ODE integration). Now we will expand the first derivative dy/dt dyi+1 dyi d 2 yi h = + + ··· dt dt dt 2 1!
(1.23)
d 2 yi /dt 2 is the second derivative we require in Equation 1.18. If the Taylor series in Equation 1.23 is truncated after the h term, we can solve for this second derivative dyi dyi+1 − d 2 yi dt (1.24) = dt dt 2 h Equation 1.24 seems logical, i.e., the second derivative is a finite difference (FD) approximation of the first derivative. Note that Equation 1.24 has the important property that we can compute the second derivative without having to differentiate the ODE; rather, all we have to do is use the ODE twice, at grid points i and i + 1. Thus, the previous differentiation of Equation 1.20 is avoided. However, note also that Equation 1.24 gives only an approximation for the second derivative since it results from truncating the Taylor series of Equation 1.23. Fortunately, the approximation of Equation 1.24 will generally
Copyright 2004 by Chapman & Hall/CRC
is too large and causes the large overshoot of the numerical solution above the exact solution (and thus, a relatively large value of εi ), the averaging of the derivatives at i and i + 1 in Figure 1.2 reduces this overshoot (and the p truncation error is reduced from εi to εic ). Equation 1.25 can be rearranged into a more useful form. If we assume that the truncation error of Euler’s method, εi , is due mainly to the second derivative term (d 2 yi /dt 2 )(h 2 /2!) of Equation 1.18 (which will be the case if the higher-order terms in Equation 1.18 are negligibly small), then d 2 yi h 2 εi = = dt 2 2!
dyi dyi+1 − 2 dt dt h = dyi+1 − dyi h h 2! dt dt 2!
and Equation 1.25 can be written as a two-step algorithm: p
yi+1 = yi + εi =
dyi h dt p
dyi+1 dyi − dt dt
c = yi + yi+1
(1.26a)
h 2!
dyi p h + εi = yi+1 + εi dt
(1.26b) (1.26c) p
With a little algebra, we can easily show that yi+1 of Equation 1.25 and yi+1 of Equation 1.26c are the same. While Equation 1.25 and Equations 1.26c are mathematically equivalent, Equations 1.26 have an advantage when used in a computer program. Specifically, an algorithm that automatically adjusts h to achieve a prescribed accuracy, tol, can be programmed in the following steps: p
1. Compute yi+1 by the Euler method, Equation 1.26a. The superscript p in this case denotes a predicted value. 2. Compute the estimated error, εi , from Equation 1.26b. Note that p p dyi+1 /dt = f (yi+1 , ti+1 ), where ti+1 = ti + h. 3. Pose the question is εi < tol? If no, reduce h and return to 1. If yes, continue to 4. p c 4. Add εi from 3 to yi+1 to obtain yi+1 according to Equation 1.26c. The superscript c denotes a corrected value. 5. Increment i, advance ti to ti+1 by adding h, go to 1. to take the next step along the solution. The algorithm of Equations 1.26 is termed a predictor-corrector method, which we will subsequently discuss in terms of a computer program. To conclude this introductory discussion of integration algorithms, we introduce the RK notation. If we define k1 and k2 (termed Runge Kutta constants
Copyright 2004 by Chapman & Hall/CRC
although they are not constant, but rather, vary along the solution) as k1 = f (yi , ti )h
(1.27a)
k2 = f (yi + k1 , ti + h)h
(1.27b)
the Euler method of Equation 1.19 can be written as (keep in mind dy/dt = f (y, t)) yi+1 = yi + k1
(1.28)
and the modified Euler method of Equation 1.25 can be written yi+1 = yi +
k1 + k2 2
(1.29)
(the reader should confirm that Equation 1.25 and Equation 1.29 are the same). Also, the modified Euler method written in terms of an explicit error estimate, Equations 1.26, can be conveniently written in RK notation: p
yi+1 = yi + k1 εi =
(1.30a)
(k2 − k1 ) 2
c = yi + k1 + yi+1
(1.30b) (k2 − k1 ) (k1 + k2 ) = yi + 2 2
(1.30c)
However, the RK method is much more than just a convenient system of notation. As stated earlier, it is a method for fitting the underlying Taylor series of the ODE solution to any number of terms without having to differentiate the ODE (it requires only the first derivative in dy/dt = f (y, t) as we observe in Equations 1.29 and 1.30). We next explore this important feature of the RK method, which is the mathematical foundation of the ODE integrators to be discussed subsequently.
1.3
The Runge Kutta Method
The RK method consists of a series of algorithms of increasing order. There is only one first order RK method, the Euler method, which fits the underlying Taylor series of the solution up to and including the first derivative term, as indicated by Equation 1.19. The second-order RK method is actually a family of second-order methods; a particular member of this family is selected by choosing an arbitrary constant in the general second-order RK formulas. The origin of these formulas is
Copyright 2004 by Chapman & Hall/CRC
illustrated by the following development (based on the idea that the secondorder RK method fits the Taylor series up to and including the second derivative term, (d 2 yi /dt 2 )(h 2 /2!)). We start the analysis with a general RK stepping formula of the form yi+1 = yi + c 1 k1 + c 2 k2
(1.31a)
where k1 and k2 are RK “constants” of the form k1 = f (yi , ti )h
(1.31b)
k2 = f (yi + a 2 k1 (yi , ti ), ti + a 2 h)h = f (yi + a 2 f (yi , ti )h, ti + a 2 h)h
(1.31c)
and c 1 , c 2 and a 2 are constants to be determined. If k2 from Equation 1.31c is expanded in a Taylor series in two variables, k2 = f (yi + a 2 f (yi , ti )h, ti + a 2 h)h = f (yi , ti ) + f y (yi , ti )a 2 f (yi , ti )h + f t (yi , ti )a 2 h h + O(h 3 )
(1.32)
Substituting Equations 1.31b and 1.32 in Equation 1.31a gives yi+1 = yi + c 1 f (yi , ti )h + c 2 [ f (yi , ti ) + f y (yi , ti )a 2 f (yi , ti )h + f t (yi , ti )a 2 h]h + O(h 3 ) = yi + (c 1 + c 2 ) f (yi , ti )h + c 2 [ f y (yi , ti )a 2 f (yi , ti ) + f t (yi , ti )a 2 ]h 2 + O(h 3 )
(1.33)
Note that Equation 1.33 is a polynomial in increasing powers of h; i.e., it has the form of a Taylor series. Thus, if we expand yi+1 in a Taylor series around yi , we will obtain a polynomial of the same form, i.e., in increasing powers of h dyi d 2 yi h 2 h+ + O(h 3 ) dt dt 2 2! d f (yi , ti ) h 2 = yi + f (yi , ti )h + + O(h 3 ) dt 2!
yi+1 = yi +
(1.34)
where we have used dyi /dt = f (yi , ti ), i.e., the ODE we wish to integrate numerically. To match Equations 1.33 and 1.34, term-by-term (with like powers of h), we need to have [d f (yi , ti )/dt](h 2 /2!) in Equation 1.34 in the form of f y (yi , ti )a 2 f (yi , ti ) + f t (yi , ti )a 2 in Equation 1.33. If chain-rule differentiation is applied to d f (yi , ti )/dt d f (yi , ti ) dyi = f y (yi , ti ) + f t (yi , ti ) = f y (yi , ti ) f (yi , ti ) + f t (yi , ti ) dt dt
Copyright 2004 by Chapman & Hall/CRC
(1.35)
Substitution of Equation 1.35 in Equation 1.34 gives h2 yi+1 = yi + f (yi , ti )h + f y (yi , ti ) f (yi , ti ) + f t (yi , ti ) (1.36) + O(h 3 ) 2! We can now equate coefficients of like powers of h in Equations 1.33 and 1.36 Power of h
Equation 1.33
Equation 1.36
h0 h1
yi (c 1 + c 2 ) f (yi , ti )
h2
c 2 f y (yi , ti )a 2 f (yi , ti ) + f t (yi , ti )a 2
Thus, we conclude
yi f (yi , ti )
1
f y (yi , ti ) f (yi , ti ) + f t (yi , ti )
c1 + c2 = 1 c 2 a 2 = 1/2
2!
(1.37)
This is a system of two equations in three unknowns or constants (c 1 , c 2 , a 2 ); thus, one constant can be selected arbitrarily (there 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 c 2 = 1/2 Other constants c 1 = 1/2 a2 = 1
(1.38)
and the resulting second-order RK method is yi+1 = yi + c 1 k1 + c 2 k2 = yi +
k1 + k2 2
k1 = f (yi , ti )h k2 = f (yi + a 2 k1 (yi , ti ), ti + a 2 h)h = f (yi + f (yi , ti )h, ti + h)h which is the modified Euler method, Equations 1.27, 1.28, and 1.29. For the choice c2 = 1 Other constants c 1 = 0 Choose
(1.39)
a 2 = 1/2 the resulting second-order RK method is yi+1 = yi + c 1 k1 + c 2 k2 = yi + k2 k1 = f (yi , ti )h
(1.40a) (1.40b)
k2 = f (yi + a 2 k1 (yi , ti ), ti + a 2 h)h = f (yi + (1/2) f (yi , ti )h, ti + (1/2)h)h = f (yi + (1/2)k1 , ti + (1/2)h)h
Copyright 2004 by Chapman & Hall/CRC
(1.40c)
equations and the final result (Iserles,2 p. 40). The third order stepping formula is yi+1 = yi + c 1 k1 + c 2 k2 + c 3 k3 (1.43a) The RK constants are k1 = f (yi , ti )h
(1.43b)
k2 = f (yi + a 2 k1 , ti + a 2 h)h
(1.43c)
k3 = f (yi + b 3 k1 + (a 3 − b 3 )k2 , ti + a 3 h)h
(1.43d)
Four algebraic equations define the six constants c 1 , c 2 , c 3 , a 2 , a 3 , b 3 (obtained by matching the stepping formula, Equation 1.43a, with the Taylor series up to and including the term (d 3 yi /dt 3 )(h 3 /3!) c1 + c2 + c3 = 1
(1.43e)
c 2 a 2 + c 3 a 3 = 1/2
(1.43f)
c 2 a 22 + c 3 a 32 = 1/3
(1.43g)
c 3 (a 3 − b 3 )a 2 = 1/6
(1.43h)
To illustrate the use of Equations 1.43e to 1.43h, we can take c 2 = c 3 = 38 , and from Equation 1.43e, c 1 = 1 − 38 − 38 = 28 . From Equation 1.43f (3/8)a 2 + (3/8)a 3 = 1/2 or a 2 =
4 3
− a 3 . From Equation 1.43g, (3/8)(4/3 − a 3 )2 + (3/8)a 32 = 1/3
or a 3 = 1.43h,
2 3
(by the quadratic formula). Thus, a 2 = 43 − 23 = 23 , and from Equation (3/8)(2/3 − b 3 )2/3 = 1/6
or b 3 = 0. This particular third-order Nystrom method (Iserles,2 p. 40) is therefore yi+1 = yi + (2/8)k1 + (3/8)k2 + (3/8)k3
(1.44a)
k1 = f (yi , ti )h
(1.44b)
k2 = f (yi + (2/3)k1 , ti + (2/3)h)h
(1.44c)
k3 = f (yi + (2/3)k2 , ti + (2/3)h)h
(1.44d)
We next consider some MATLAB code which implements the Euler method of Equation 1.28, the modified Euler method of Equations 1.30, the secondorder RK of Equations 1.42, and the third-order RK of Equations 1.44.
Copyright 2004 by Chapman & Hall/CRC
The objective is 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 to initial condition Equation 1.4, by the Euler and modified Euler methods, Equation 1.28 and Equations 1.30. The analytical solution, Equation 1.5, can be 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 Braun1 (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 “free living” dividing cells, such as bacteria cells, grow at a rate proportional to the volume of the dividing cells at that moment. Let V(t) denote the volume of the dividing cells at time t. Then, dV = λV (1.45) dt for some positive constant λ. The solution of Equation 1.45 is V(t) = V0 e λ(t−t0 )
(1.46)
where V0 is the volume of dividing cells at the initial time t0 . Thus, free living dividing cells grow exponentially with time. One important consequence of Equation 1.46 is that the volume of the cells keeps doubling every time interval of length ln 2/λ. On the other hand, solid tumors do not grow exponentially with time. As the tumor becomes larger, the doubling time of the total tumor volume continuously increases. Various researchers have shown that the data for many solid tumors is fitted remarkably well, over almost a 1000-fold increase in tumor volume, by the equation (previously Equation 1.5)
V(t) = V0 exp
λ (1 − exp(−αt)) α
(1.47)
where exp(x) = e x , and λ and α are positive constants. Equation 1.47 is usually referred to as a Gompertzjan relation. It says that the tumor grows more and more slowly with the passage of time, and that it ultimately approaches the limiting volume V0 e λ/α . Medical researchers have long been concerned with explaining this deviation from simple exponential growth. A great deal of insight into this problem can be gained by finding a differential equation satisfied by V(t). Differentiating
Copyright 2004 by Chapman & Hall/CRC
Equation 1.47 gives
dV λ = V0 λ exp(−αt) exp (1 − exp(−αt)) dt α
= λe −αt V
(1.48)
(formerly Equation 1.3). Two conflicting theories have been advanced for the dynamics of tumor growth. They correspond to the two arrangements dV = (λe −αt )V dt dV = λ(e −αt )V dt
(1.48a) (1.48b)
of differential Equation 1.48. According to the first theory, the retarding effect of tumor growth is due to an increase in the mean generation time of the cells, without a change in the proportion of reproducing cells. As time goes on, the reproducing cells mature, or age, and thus divide more slowly. This theory corresponds to the 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 loss in reproductive cells in the tumor. One possible explanation for this is that a necrotic region develops in the center of the tumor. This necrosis appears at a critical size for a 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 of blood, and thus of oxygen and nutrients, is almost completely confined to the surface of the tumor and a short distance beneath it. As the tumor grows, the supply of oxygen to the central core by diffusion becomes more and more difficult, resulting in the formation of a necrotic core.
We can note the following interesting ideas about this problem: • Equation 1.48 is a linear, variable coefficient ODE; it can also be considered to have a variable eigenvalue. • The application of mathematical analysis to tumor dynamics apparently started with a “solution” to an ODE, i.e., Equation 1.47. • To gain improved insight into tumor dynamics, the question was posed “Is there an ODE 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. • This is a reversal of the usual process of starting with a differential equation model, then using the solution to explain the performance of the problem system. 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:
Copyright 2004 by Chapman & Hall/CRC
% % Program 1.1 % Tumor model of eqs. (1.47), (1.48) % % Model parameters V0=1.0; lambda=1.0; alpha=1.0; % % Step through cases for ncase=1:4 % % Integration step if(ncase==1)h=1.0 ;nsteps=1 ;end if(ncase==2)h=0.1 ;nsteps=10 ;end if(ncase==3)h=0.01 ;nsteps=100 ;end if(ncase==4)h=0.001;nsteps=1000;end % % Variables for ODE integration tf=10.0; t=0.0; % % Initial condition V1=V0; V2=V0; % % Print heading fprintf('\n\nh = %6.3f\n',h); fprintf(... ' t Ve V1 errV1 V2 errV2\n') % % Continue integration while t<0.999*tf % % Take nsteps integration steps for i=1:nsteps % % Store solution at base point V1b=V1; V2b=V2; tb=t; % % RK constant k1 k11=lambda*exp(-alpha*t)*V1*h;
Copyright 2004 by Chapman & Hall/CRC
estV1
k12=lambda*exp(-alpha*t)*V2*h; % %
% %
RK constant k2 V1=V1b+k11; V2=V2b+k12; t=tb+h; k22=lambda*exp(-alpha*t)*V2*h; RK step V2=V2b+(k12+k22)/2.0; t=tb+h; end
% % Print solutions and errors Ve=V0*exp((lambda/alpha)*(1.0-exp(-alpha*t))); errV1=V1-Ve; errV2=V2-Ve; estV1=V2-V1; fprintf('%5.1f%9.4f%9.4f%15.10f%15.10f%9.4f%15.10f\n',... t,Ve,V1,errV1,estV1,V2,errV2); % % 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.48 are first defined (note that % defines a comment in MATLAB): % % Model parameters V0=1.0; lambda=1.0; alpha=1.0;
• The program then steps through four cases corresponding to the integration steps h = 1.0, 0.1, 0.01, 0.001: % % Step through cases for ncase=1:4
Copyright 2004 by Chapman & Hall/CRC
% % Integration step if(ncase==1)h=1.0 ;nsteps=1 ;end if(ncase==2)h=0.1 ;nsteps=10 ;end if(ncase==3)h=0.01 ;nsteps=100 ;end if(ncase==4)h=0.001;nsteps=1000;end
For each h, the corresponding number of integration steps is nste ps. Thus, the product (h)(nste ps) = 1 unit in t for each output from the program; i.e., the output from the program is at t = 0, 1, 2, . . . , 10. • For each case, the initial and final values of t are defined, i.e., t = 0, t f = 10, and the initial condition, V(0) = V0 is set to start the solution: % % Variables for ODE integration tf=10.0; t=0.0; % % Initial condition V1=V0; V2=V0;
Two initial conditions are set, one for the Euler solution, computed as V1, and one for the modified Euler solution, V2 (subsequently, we will program the solution vector, in this case [V1 V2]T , as a one-dimensional (1D) array). • A heading indicating the integration step, h, and the two numerical solutions is then displayed. “. . . ” indicates a line is to be continued on the next line. (Note: . . . does not work in a character string delineated by single quotes, so the character string in the second fprintf statement has been placed on two lines in order to fit within the available page width; to execute this program, the character string should be returned to one line.) % % Print heading fprintf('\n\nh = %6.3f\n',h); fprintf(... ' t Ve V1 V2
errV1 errV2\n')
estV1
• A while loop then computes the solution until the final time, t f , is reached: % % Continue integration while t<0.999*tf
Copyright 2004 by Chapman & Hall/CRC
Of course, at the beginning of the execution, t = 0 so the while loop continues. • nsteps Euler and modified Euler steps are then taken: % % Take nsteps integration steps for i=1:nsteps % % Store solution at base point V1b=V1; V2b=V2; tb=t;
At each point along the solution (point i), the solution is stored for subsequent use in the numerical integration. • The first RK constant, k1 , is then computed for each dependent variable in [V1 V2)]T according to Equation 1.27a: % %
RK constant k1 k11=lambda*exp(-alpha*t)*V1*h; k12=lambda*exp(-alpha*t)*V2*h;
Note that we have used the RHS of the ODE, Equation 1.48, in computing k1 . k11 is k1 for V1, and k12 is k1 for V2. Subsequently, the RK constants will be programmed as 1D arrays, e.g., [k1 (1) k1 (2)]T . • The solution is then advanced from the base point according to Equation 1.28: % %
RK constant k2 V1=V1b+k11; V2=V2b+k12; t=tb+h; k22=lambda*exp(-alpha*t)*V2*h;
The second RK constant, k2 for V2, is then computed according to Equation 1.27b. At the same time, the independent variable, t, is advanced. • The modified Euler solution, V2, is then computed according to Equation 1.29: % %
RK step V2=V2b+(k12+k22)/2.0; t=tb+h; end
The advance of the independent variable, t, was done previously and is therefore redundant; it is done again just to emphasize the advance in t
Copyright 2004 by Chapman & Hall/CRC
for the modified Euler method. The end statement ends the loop of nsteps steps, starting with for i=1:nsteps
• The exact solution, Ve, is computed from Equation 1.47. The exact error in the Euler solution, errV1, and in the modified Euler solution, errV2, are then computed. Finally, the difference in the two solutions, estV1 = V2 − V1, is computed as an estimate of the error in V1. The independent variable, t, the two dependent variables, V1, V2, and the three errors, errV1, errV2, estV1, are then displayed. % % Print solutions and errors Ve=V0*exp((lambda/alpha)*(1.0-exp(-alpha*t))); errV1=V1-Ve; errV2=V2-Ve; estV1=V2-V1; fprintf('%5.1f%9.4f%9.4f%15.10f%15.10f%9.4f %15.10f\n',...t,Ve,V1,errV1,estV1,V2,errV2);
The output from the fprintf statement is considered subsequently. • The while loop is then terminated, followed by the end of the for loop that sets ncase: % % Continue integration end % % Next case end
We now consider the output from this program listed below (reformatted slightly to fit on a printed page): h =
1.000
Euler method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0
Ve 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174
Copyright 2004 by Chapman & Hall/CRC
V1 2.0000 2.7358 3.1060 3.2606 3.3204 3.3427 3.3510 3.3541
errV1 0.1184036125 0.3615489626 0.5197432882 0.5916944683 0.6203353910 0.6311833526 0.6352171850 0.6367070277
estV1 -0.1321205588 -0.3514091013 -0.4929227741 -0.5573912375 -0.5830821148 -0.5928173392 -0.5964380611 -0.5977754184
9.0 10.0
2.7179 2.7182
3.3552 3.3556
0.6372559081 0.6374579380
-0.5982681335 -0.5984494919
modified Euler method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
h =
Ve 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
V2 1.8679 2.3843 2.6131 2.7033 2.7373 2.7499 2.7546 2.7563 2.7569 2.7572
errV2 -0.0137169464 0.0101398613 0.0268205142 0.0343032307 0.0372532762 0.0383660134 0.0387791239 0.0389316092 0.0389877746 0.0390084461
V1 1.8994 2.4175 2.6438 2.7325 2.7660 2.7784 2.7829 2.7846 2.7852 2.7855
errV1 0.0178364041 0.0433341041 0.0575343031 0.0635808894 0.0659265619 0.0668064211 0.0671324218 0.0672526658 0.0672969439 0.0673132386
0.100
Euler method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
Ve 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
modified Euler method t 1.0 2.0 3.0 4.0 5.0 6.0
Ve 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116
Copyright 2004 by Chapman & Hall/CRC
V2 1.8816 2.3745 2.5868 2.6696 2.7007 2.7122
errV2 -0.0000409693 0.0003303677 0.0005383591 0.0006250422 0.0006583152 0.0006707429
estV1 -0.0178773733 -0.0430037365 -0.0569959440 -0.0629558472 -0.0652682467 -0.0661356782 -0.0664570816 -0.0665756310 -0.0666192852 -0.0666353503
7.0 8.0 9.0 10.0
h =
2.7158 2.7174 2.7179 2.7182
2.7165 2.7180 2.7186 2.7188
0.0006753402 0.0006770348 0.0006776587 0.0006778883
V1 1.8835 2.3786 2.5921 2.6754 2.7067 2.7183 2.7226 2.7242 2.7247 2.7249
errV1 0.0018696826 0.0044269942 0.0058291952 0.0064227494 0.0066525021 0.0067386119 0.0067705073 0.0067822704 0.0067866019 0.0067881959
0.010
Euler method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
Ve 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
estV1 -0.0018697473 -0.0044231149 -0.0058231620 -0.0064158254 -0.0066452372 -0.0067312197 -0.0067630680 -0.0067748139 -0.0067791389 -0.0067807306
modified Euler method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
h =
Ve 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
V2 1.8816 2.3742 2.5863 2.6690 2.7000 2.7116 2.7158 2.7174 2.7180 2.7182
errV2 -0.0000000647 0.0000038793 0.0000060332 0.0000069239 0.0000072649 0.0000073922 0.0000074392 0.0000074566 0.0000074629 0.0000074653
V1 1.8818 2.3747
errV1 0.0001878608 0.0004436596
0.001
Euler method t 1.0 2.0
Ve 1.8816 2.3742
Copyright 2004 by Chapman & Hall/CRC
estV1 -0.0001878611 -0.0004436202
3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
2.5868 2.6696 2.7007 2.7122 2.7165 2.7180 2.7186 2.7188
0.0005836997 0.0006429444 0.0006658719 0.0006744643 0.0006776469 0.0006788206 0.0006792528 0.0006794118
-0.0005836386 -0.0006428744 -0.0006657985 -0.0006743896 -0.0006775717 -0.0006787453 -0.0006791774 -0.0006793364
modified Euler method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
Ve 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
V2 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
errV2 -0.0000000003 0.0000000394 0.0000000610 0.0000000700 0.0000000734 0.0000000747 0.0000000751 0.0000000753 0.0000000754 0.0000000754
We can note the following points about this output: • Considering first the output for the Euler method at t = 1: h
Ve
V1
errV1
estV1
V1 + estV1
1 0.1 0.01 0.001
1.8816 1.8816 1.8816 1.8816
2.0000 1.8994 1.8835 1.8818
0.1184036125 0.0178364041 0.0018696826 0.0001878608
−0.1321205588 −0.0178773733 −0.0018697473 −0.0001878611
1.8679 1.8815 1.8816 1.8816
We can note the following points for this output: — The exact error, errV1, decreases linearly with integration step, h. For example, when h is decreased from 0.01 to 0.001, errV1 decreases from 0.0018696826 to 0.0001878608. Roughly speaking, as the decimal point in h moves one place, the decimal point in errV1 moves one place. However, this is true only when h becomes small (so that higher-order terms in the underlying Taylor series become negligibly small). — Thus, the error in the Euler method is proportional to h errV1 = Ch 1
Copyright 2004 by Chapman & Hall/CRC
where C is a constant. The Euler method is therefore termed first order in h or first order correct or of order h, which is usually designated as errV1 = O(h) where “O” denotes “of order.” — The estimated error, estV1 is also first order in h (note again, that as h is decreased by a factor of 1/10, estV1 decreases by a factor of 1/10). Furthermore, the estimated error, estV1, approaches the exact error, errV1 for small h. This is an important point since the estimated error can be computed without knowing the exact solution; in other words, we can estimate the error in the numerical solution without knowing the exact solution. The estimated error, estV1 = V2 − V1 is the same as εi given by Equation 1.26b and discussed in words following Equations 1.26. — If the estimated error, estV1, is added as a correction to the numerical solution, V1, the corrected solution (in the last column) is much closer to the exact solution, Ve. Thus, the estimated error can not only be used to judge the accuracy of the numerical solution, and thereby used to decrease h 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 a correction for the numerical solution to obtain a more accurate solution. We will make use of these important features of the estimated error in the subsequent routines that automatically adjust the step, h, to achieve a specified accuracy. • Considering next the output for the modified Euler method at t = 1: h
Ve
V2
errV2
1 0.1 0.01 0.001
1.8816 1.8816 1.8816 1.8816
1.8679 1.8816 1.8816 1.8816
−0.0137169464 −0.0000409693 −0.0000000647 −0.0000000003
We can note the following points for this output: — The exact error for the modified Euler method, errV2, is substantially smaller than the error for the Euler method, errV1 and estV1. This is to be expected since the modified Euler method includes the second derivative term in the Taylor series, (d 2 y/dt 2 )(h 2 /2!), while the Euler method includes only the first derivative term, (dy/dt)(h/1!). — In other words, the exact error, errV2, decreases much faster with h than does errV1. The order of this decrease is difficult to assess from the solution at t = 1. For example, when h is decreased from 0.1 to 0.01, the number of zeros after the decimal point increases from four (−0.000040969) to seven (−0.0000000647) (or roughly, a decrease of 1/1000). But when h decreases from 0.01 to 0.001, the number of zeros
Copyright 2004 by Chapman & Hall/CRC
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 2 ) or O(h 3 )? • We come to a somewhat different conclusion if we consider the modified Euler solution at t = 10: h
Ve
V2
errV2
1 0.1 0.01 0.001
2.7182 2.7182 2.7182 2.7182
2.7572 2.7188 2.7182 2.7182
0.0390084461 0.0006778883 0.0000074653 0.0000000754
We can note the following points for this output: — The error, errV2, now appears to be second order. For example, when h is reduced from 0.1 to 0.01, the error decreases from 0.0006778883 to 0.0000074653, a decrease of approximately 1/100. Similarly, when h is reduced from 0.01 to 0.001, the error decreases from 0.0000074653 to 0.0000000754, again a decrease of approximately 1/100. Thus, we can conclude that at least for this numerical output at t = 10, the modified Euler method appears to be second order correct, i.e., errV2 = O(h 2 ) We shall generally find this to be the case (the modified Euler method is second order), although, clearly, there can be exceptions (i.e., 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 both methods, the accuracy of the numerical solutions can be improved by decreasing h. This process is termed h refinement, and is an important procedure in ODE library integration routines, i.e., decreasing h to improve the solution accuracy. — An error in the numerical solution, in this case estV1, can be estimated by subtracting the solutions from two methods of different orders, i.e., estV1 = V2 − V1. This estimated error can then be used to adjust h to achieve a solution of prescribed accuracy (see Equations 1.26). This procedure 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”, i.e., error = O(h p ) In the present case, p = 1 for the Euler method (it is first order correct), and p = 2 for the modified Euler method (it is second order correct). Thus, by using the p refinement of increasing p from 1 to 2,
Copyright 2004 by Chapman & Hall/CRC
we can estimate the error in the numerical solution (without having to know the exact solution), and thereby make some adjustments in h to achieve a specified accuracy. — The integration errors we have been considering are called truncation errors since they result from truncation of the underlying Taylor series (after (dy/dt)(h/1!) and (d 2 y/dt 2 )(h 2 /2!) for the Euler and modified Euler methods, respectively). — The preceding analysis and conclusions are based on a sufficiently small value of h that the higher-order terms (in h) in the Taylor series (after the point of truncation) are negligibly small. — We have not produced a rigorous proof of O(h) and O(h 2 ) for the Euler method and modified Euler method. Rather, all of the preceding analysis was through the use of a single, linear ODE, Equation 1.48. Thus, we cannot conclude that these order conditions are generally true (for any system of ODEs). Fortunately, they have been observed to be approximately correct for many ODE systems, both linear and nonlinear. — Higher-order RK algorithms that fit more of the terms of the underlying Taylor series are available (consider the third-order RK method of Equations 1.44). The preceding error analysis can be applied to 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 h and p refinement for higher-order RK methods. — The higher order of the modified Euler method, O(h 2 ), relative to the Euler method, O(h), was achieved through additional computation. Specifically, in the preceding MATLAB program, the Euler method required only one derivative evaluation (use of Equation 1.48) for each step along the solution, while the modified Euler method required two derivative evaluations for each step along the solution. In other words, we pay a “computational price” of 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 Equation 1.48 when using the modified Euler method relative to the Euler method, and how much more quickly the error dropped off with decreasing h, i.e., O(h 2 ) vs. O(h) ). Generally, an increase in the order of the method of one (e.g., O(h) to O(h 2 )) requires one additional derivative evaluation for order up to and including four; beyond fourth order, increasing the order of accuracy by one will require more than one additional derivative evaluation (we shall observe this for a fifth-order RK method to be discussed subsequently). — In all of the preceding discussion, we have assumed that the solution to an ODE system can be represented by a Taylor series (or a truncated
Copyright 2004 by Chapman & Hall/CRC
Taylor series), which is basically a polynomial in h. Of course, this does not have to be the case, but we are assuming that in using numerical ODE integration algorithms, for sufficiently small h, the Taylor series approximation of the solution is sufficiently accurate for the given ODE application. — The RK method is particularly attractive since it can be formulated for increasing orders (more terms in the Taylor series) without having to differentiate the differential equation to produce the higher-order derivatives required 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 see in subsequent examples, the RK method can be applied to the nxn problem (n ODEs in n unknowns) as easily as we applied it to the 1x1 problem of Equation 1.48. Thus, it is a general procedure for the solution of systems of ODEs of virtually any order (nxn) and complexity (which is why it is so widely used). In other words, the RK algorithms (as well as other well-established integration 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. We now conclude this section by considering the errors in the numerical solution of Equation 1.48 with a (2, 3) RK pair (i.e., O(h 2 ) and O(h 3 ) in analogy with the (1, 2) pair of the Euler and modified Euler methods), and then a (4, 5) pair (O(h 4 ) and O(h 5 )). 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. The (2, 3) pair we considered previously (Equations 1.42 and 1.44) is 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 using the third-order RK of Equations 1.44. % % % % % %
Program 1.2 Tumor model of eqs. (1.47), (1.48) (or eqs. (1.3), (1.4), (1.5)) Model parameters y0=1.0; lambda=1.0; alpha=1.0;
% % Step through cases for ncase=1:4
Copyright 2004 by Chapman & Hall/CRC
% % Integration step if(ncase==1)h=1.0 ;nsteps=1 ;end if(ncase==2)h=0.1 ;nsteps=10 ;end if(ncase==3)h=0.01 ;nsteps=100 ;end if(ncase==4)h=0.001;nsteps=1000;end % % Variables for ODE integration tf=10.0; t=0.0; % % Initial condition y2=y0; y3=y0; % % Print heading fprintf('\n\nh = %6.3f\n',h); fprintf(... ' t ye y2 erry2 y3 erry3\n') % % Continue integration while t<0.999*tf % % Take nsteps integration steps for i=1:nsteps % % Store solution at base point y2b=y2; y3b=y3; tb=t; % % RK constant k1 k12=lambda*exp(-alpha*t)*y2*h; k13=lambda*exp(-alpha*t)*y3*h; % % RK constant k2 y2=y2b+(2.0/3.0)*k12; y3=y3b+(2.0/3.0)*k13; t=tb +(2.0/3.0)*h; k22=lambda*exp(-alpha*t)*y2*h; k23=lambda*exp(-alpha*t)*y3*h; % % RK integration K3 y3=y3b+(2.0/3.0)*k23;
Copyright 2004 by Chapman & Hall/CRC
esty2
t=tb +(2.0/3.0)*h; k33=lambda*exp(-alpha*t)*y3*h; % %
RK step y2=y2b+(1.0/4.0)*k12+(3.0/4.0)*k22; y3=y3b+(1.0/4.0)*k13+(3.0/8.0)*k23+(3.0/8.0)*k33; t=tb+h; end
% % Print solutions and errors ye=y0*exp((lambda/alpha)*(1.0-exp(-alpha*t))); erry2=y2-ye; erry3=y3-ye; esty2=y3-y2; fprintf('%5.1f%9.4f%9.4f%15.10f%15.10f%9.4f%15.10f\n',... t,ye,y2,erry2,esty2,y3,erry3); % % Continue integration end % % Next case end
Program 1.2 Program for the integration of Equation 1.48 by the RK (2, 3) pair of Equations 1.42 and 1.44 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 (Equation 1.4) is again set for y2 and y3 to start the numerical solutions: % % Initial condition y2=y0; y3=y0;
• The integration proceeds with the outer while loop (that eventually reaches the final time, t f ), and an inner for loop that takes nsteps RK steps for each output. For each pass through the inner loop, the solution is stored at the base point for subsequent use in the RK formulas: % % Continue integration while t<0.999*tf
Copyright 2004 by Chapman & Hall/CRC
% % Take nsteps integration steps for i=1:nsteps % % Store solution at base point y2b=y2; y3b=y3; tb=t;
• The RK constant k1 is computed for each dependent variable by using Equation 1.3 (k12 for the k1 of y2 and k13 for the k1 of y3): % %
RK constant k1 k12=lambda*exp(-alpha*t)*y2*h; k13=lambda*exp(-alpha*t)*y3*h;
• The solution is then advanced from the base point using a 23 weighting applied to k1 and h (in accordance with Equations 1.42 and 1.44): % %
RK constant k2 y2=y2b+(2.0/3.0)*k12; y3=y3b+(2.0/3.0)*k13; t=tb +(2.0/3.0)*h; k22=lambda*exp(-alpha*t)*y2*h; k23=lambda*exp(-alpha*t)*y3*h;
This advance of the dependent and independent variables sets the stage for the calculation of k2 (again, using Equation 1.3). • k3 is computed for y3 (it is not required for y2): % %
RK integration K3 y3=y3b+(2.0/3.0)*k23; t=tb +(2.0/3.0)*h; k33=lambda*exp(-alpha*t)*y3*h;
• All the required RK constants have now been computed, and the solutions can be advanced to the next point using the stepping formulas: % %
RK step y2=y2b+(1.0/4.0)*k12+(3.0/4.0)*k22; y3=y3b+(1.0/4.0)*k13+(3.0/8.0)*k23+(3.0/8.0)*k33; t=tb+h; end
Note that the stepping formula for y2 does not include k3 . The end statement concludes the for loop that is executed nsteps times.
Copyright 2004 by Chapman & Hall/CRC
• The solutions, y2 and y3, 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-ye; esty2=y3-y2; fprintf('%5.1f%9.4f%9.4f%15.10f%15.10f%9.4f %15.10f\n',...t,ye,y2,erry2,esty2,y3,erry3);
• Finally, the while loop is concluded, followed by the for loop that sets the values of h, and the initial and final values of t: % % Continue integration end % % Next case end
• Note that Equation 1.3 was used twice to compute k1 and k2 for y2 (two derivative evaluations), and Equation 1.3 was used three times to compute k1 , k2 , and k3 for y3 (three derivative evaluations). This again illustrates the additional computation required, in this case, the calculation of k3 , to achieve higher-order results (O(h 3 ) rather than O(h 2 )). 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 formatting to fit on a printed page): h =
1.000
Second order RK t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
Copyright 2004 by Chapman & Hall/CRC
y2 1.8918 2.3995 2.6170 2.7014 2.7330 2.7448 2.7491 2.7507 2.7513 2.7515
erry2 0.0101750113 0.0252529307 0.0307095187 0.0324302424 0.0330043494 0.0332064307 0.0332794779 0.0333061722 0.0333159682 0.0333195687
esty2 -0.0185221389 -0.0352289432 -0.0408693758 -0.0425724266 -0.0431262652 -0.0433188969 -0.0433881911 -0.0434134670 -0.0434227360 -0.0434261419
Third order RK t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
h =
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y3 1.8732 2.3642 2.5761 2.6588 2.6899 2.7014 2.7057 2.7073 2.7078 2.7081
erry3 -0.0083471276 -0.0099760125 -0.0101598572 -0.0101421842 -0.0101219158 -0.0101124662 -0.0101087132 -0.0101072948 -0.0101067678 -0.0101065733
y2 1.8819 2.3748 2.5869 2.6696 2.7007 2.7122 2.7165 2.7181 2.7186 2.7188
erry2 0.0003179977 0.0005660244 0.0006477190 0.0006733478 0.0006819708 0.0006850226 0.0006861284 0.0006865329 0.0006866814 0.0006867360
y3 1.8816 2.3742 2.5862 2.6689 2.7000 2.7115 2.7158 2.7174 2.7179 2.7181
erry3 -0.0000090358 -0.0000102699 -0.0000104074 -0.0000103885 -0.0000103698 -0.0000103611 -0.0000103577 -0.0000103564 -0.0000103560 -0.0000103558
0.100
Second order RK t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
Third order RK t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
Copyright 2004 by Chapman & Hall/CRC
esty2 -0.0003270335 -0.0005762943 -0.0006581264 -0.0006837363 -0.0006923405 -0.0006953838 -0.0006964862 -0.0006968894 -0.0006970374 -0.0006970918
h =
0.010
Second Order RK t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y2 1.8816 2.3742 2.5863 2.6690 2.7000 2.7116 2.7158 2.7174 2.7180 2.7182
erry2 0.0000035779 0.0000062016 0.0000070634 0.0000073355 0.0000074275 0.0000074601 0.0000074720 0.0000074763 0.0000074779 0.0000074785
y3 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry3 -0.0000000085 -0.0000000096 -0.0000000096 -0.0000000096 -0.0000000096 -0.0000000096 -0.0000000095 -0.0000000095 -0.0000000095 -0.0000000095
esty2 -0.0000035865 -0.0000062112 -0.0000070731 -0.0000073451 -0.0000074371 -0.0000074697 -0.0000074815 -0.0000074859 -0.0000074875 -0.0000074880
Third order RK t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
h =
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
0.001
Second order RK t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174
Copyright 2004 by Chapman & Hall/CRC
y2 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174
erry2 0.0000000362 0.0000000626 0.0000000713 0.0000000740 0.0000000749 0.0000000752 0.0000000754 0.0000000754
esty2 -0.0000000362 -0.0000000626 -0.0000000713 -0.0000000740 -0.0000000749 -0.0000000753 -0.0000000754 -0.0000000754
9.0 10.0
2.7179 2.7182
2.7179 2.7182
0.0000000754 0.0000000754
y3 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry3 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
-0.0000000754 -0.0000000754
Third order RK t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
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 t = 1:
h
ye
y2
1 0.1 0.01 0.001
1.8816 1.8816 1.8816 1.8816
1.8918 1.8819 1.8816 1.8816
erry2 0.0101750113 0.0003179977 0.0000035779 0.0000000362
esty2 −0.0185221389 −0.0003270335 −0.0000035865 −0.0000000362
— The O(h 2 ) behavior of err y2 is clear, i.e., for h = 0.1, 0.01, 0.001 the corresponding values of err y2 are 0.0003179977,
0.0000035779,
0.0000000362
so that for each reduction in h by 1/10, err y2 is reduced by a factor of 1/100 (two more zeros are added after the decimal point). — The same is true for the estimated error, er ty2 (computed as the difference y3 − y2), i.e., for h = 0.1, 0.01, 0.001 the corresponding values of esty2 are −0.0003270335,
−0.0000035865,
−0.0000000362
so that two more zeros are added after the decimal point for each 1/10 reduction in h.
Copyright 2004 by Chapman & Hall/CRC
— The estimated error, esty2 is in close agreement with the exact error, err y2, for small h. — Thus, adding esty2 as a correction to y2 will bring the corrected y2 into closer agreement with the exact solution, ye. In other words, esty2 can be used to determine whether h is small enough to achieve a prescribed accuracy, and once an acceptable h is thereby selected, esty2 can be added to y2 to improve the numerical solution (all without knowledge of the exact solution). • The corresponding output for the third order RK at t = 1 is
h
ye
y3
erry3
1 0.1 0.01 0.001
1.8816 1.8816 1.8816 1.8816
1.8732 1.8816 1.8816 1.8816
−0.0083471276 −0.0000090358 −0.0000000085 0.0000000000
— Again, the third order behavior is clear. For h = 1, 0.1, 0.01, 0.001, the corresponding exact errors are −0.0083471276,
−0.0000090358,
−0.0000000085,
0.0000000000
so a 1/10 reduction in h results in a 1/1000 reduction in err y3. — In fact, since for most scientific and engineering applications of ODEs, five figure accuracy of the numerical solutions is usually adequate, the last two values of err y3 (for h = 0.01, 0.001) can be considered excessively small (these errors are much less than five significant figures compared to the exact solution ye = 1.8816). In other words, h = 0.01, 0.001 are excessively small. This is an important point. While MATLAB produced all of the numerical output (for h = 1, 0.1, 0.01, 0.001) in the order of a second or two for this modest 1x1 problem, for large systems of ODEs, 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 integrating ODEs increase h 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 h). We shall subsequently consider this feature of reducing and increasing h to stay close to the specified error tolerance in the library routines. — Stated in another way, the preceding solutions for h = 1, 0.1, 0.01, 0.001 for the interval 0 ≤ t ≤ t f (= 10) required 10/1, 10/0.1, 10/0.01, 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
Copyright 2004 by Chapman & Hall/CRC
and 10/0.001 = 10000 steps produced excessive accuracy. However, 10/1 = 10 steps were inadequate as might be expected. • In conclusion, the effectiveness of higher order algorithms, e.g., the thirdorder 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 (4, 5) pair, the Runge Kutta Fehlberg (RKF) method (Iserles,2 p. 84): k1 = f (yi , ti )h
(1.49a)
k2 = f (yi + k1 /4, ti + h/4)h
(1.49b)
k3 = f (yi + (3/32)k1 + (9/32)k2 , ti + (3/8)h)h
(1.49c)
k4 = f (yi + (1932/2197)k1 − (7200/2197)k2 + (7296/2197)k3 , ti + (12/13)h)h
(1.49d)
k5 = f (yi + (439/216)k1 − 8k2 + (3680/513)k3 − (845/4104)k4 , ti + h)h
(1.49e)
k6 = f (yi − (8/27)k1 + 2k2 − (3544/2565)k3 + (1859/4104)k4 −(11/40)k5 , ti + (1/2)h)h
(1.49f)
A O(h 4 ) stepping formula is then y4,i+1 = yi + (25/216)k1 + (1408/2565)k3 + (2197/4104)k4 − (1/5)k5
(1.49g)
and a O(h 5 ) stepping formula is (with the same k terms) y5,i+1 = yi + (16/315)k1 + (6656/12825)k3 + (28561/56430)k4 −(9/50)k5 + (2/55)k6 (1.49h) An error estimate can then be obtained by subtracting Equation 1.49g from Equation 1.49h: i = yi+1,5 − yi+1,4
(1.49i)
Note that six derivative evaluations are required (k1 through k6 ), even though the final result from Equation 1.49h is only O(h 5 ) (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.49g match the Taylor series up to and including the terms (d 4 yi /dt 4 )(h 4 /4!) and (d 5 yi /dt 5 )(h 5 /5!), respectively, as demonstrated by the following Program 1.3.
Copyright 2004 by Chapman & Hall/CRC
% % % % %
Program 1.3 Tumor model of eqs. (1.47), (1.48) (or eqs. (1.3), (1.4), (1.5)) Model parameters V0=1.0; lambda=1.0; alpha=1.0;
% % Step through cases for ncase=1:4 % % Integration step if(ncase==1)h=1.0 ;nsteps=1 ;end if(ncase==2)h=0.1 ;nsteps=10 ;end if(ncase==3)h=0.01 ;nsteps=100 ;end if(ncase==4)h=0.001;nsteps=1000;end % % Variables for ODE integration tf=10.0; t=0.0; % % Initial condition V4=V0; V5=V0; % % Print heading fprintf('\n\nh = %6.3f\n',h); fprintf(... ' t Ve V4 errV4 V5 errV5\n') % % Continue integration while t<0.999*tf % % Take nsteps integration steps for i=1:nsteps % % Store solution at base point V4b=V4; V5b=V5; tb=t; %
Copyright 2004 by Chapman & Hall/CRC
estV4
%
% %
% %
% %
% %
% %
RK constant k1 k14=lambda*exp(-alpha*t)*V4*h; k15=lambda*exp(-alpha*t)*V5*h; RK constant k2 V4=V4b+0.25*k14; V5=V5b+0.25*k15; t= tb+0.25*h; k24=lambda*exp(-alpha*t)*V4*h; k25=lambda*exp(-alpha*t)*V5*h; RK constant k3 V4=V4b+(3.0/32.0)*k14... +(9.0/32.0)*k24; V5=V5b+(3.0/32.0)*k15... +(9.0/32.0)*k25; t= tb+(3.0/8.0)*h; k34=lambda*exp(-alpha*t)*V4*h; k35=lambda*exp(-alpha*t)*V5*h; RK constant k4 V4=V4b+(1932.0/2197.0)*k14... -(7200.0/2197.0)*k24... +(7296.0/2197.0)*k34; V5=V5b+(1932.0/2197.0)*k15... -(7200.0/2197.0)*k25... +(7296.0/2197.0)*k35; t= tb+(12.0/13.0)*h; k44=lambda*exp(-alpha*t)*V4*h; k45=lambda*exp(-alpha*t)*V5*h; RK constant k5 V4=V4b+( 439.0/ 216.0)*k14... -( 8.0 )*k24... +(3680.0/ 513.0)*k34... -( 845.0/4104.0)*k44; V5=V5b+( 439.0/ 216.0)*k15... -( 8.0 )*k25... +(3680.0/ 513.0)*k35... -( 845.0/4104.0)*k45; t= tb+h; k54=lambda*exp(-alpha*t)*V4*h; k55=lambda*exp(-alpha*t)*V5*h; RK constant k6
Copyright 2004 by Chapman & Hall/CRC
V4=V4b-( 8.0/ 27.0)*k14... +( 2.0 )*k24... -(3544.0/2565.0)*k34... +(1859.0/4104.0)*k44... -( 11.0/ 40.0)*k54; V5=V5b-( 8.0/ 27.0)*k15... +( 2.0 )*k25... -(3544.0/2565.0)*k35... +(1859.0/4104.0)*k45... -( 11.0/ 40.0)*k55; t =tb+0.5*h; k65=lambda*exp(-alpha*t)*V5*h; % %
RK step V4=V4b+( 25.0/ 216.0)*k14... +( 1408.0/2565.0)*k34... +( 2197.0/4104.0)*k44... -( 1.0/ 5.0)*k54; V5=V5b+( 16.0/ 135.0)*k15... +( 6656.0/12825.0)*k35... +(28561.0/56430.0)*k45... -( 9.0/ 50.0)*k55... +( 2.0/ 55.0)*k65; t =tb+h; end
% % Print solutions and errors Ve=V0*exp((lambda/alpha)*(1.0-exp(-alpha*t))); errV4=V4-Ve; errV5=V5-Ve; estV4=V5-V4; fprintf('%5.1f%9.4f%9.4f%15.10f%15.10f%9.4f%15.10f\n',... t,Ve,V4,errV4,estV4,V5,errV5); % % Continue integration end % % Next case end
Program 1.3 Program for the integration of Equation 1.48 by the RKF45 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, k1 to k6 :
Copyright 2004 by Chapman & Hall/CRC
% %
% %
% %
% %
% %
% %
Store solution at base point y4b=y4; y5b=y5; tb=t; RK constant k1 k14=lambda*exp(-alpha*t)*y4*h; k15=lambda*exp(-alpha*t)*y5*h; RK constant k2 y4=y4b+0.25*k14; y5=y5b+0.25*k15; t= tb+0.25*h; k24=lambda*exp(-alpha*t)*y4*h; k25=lambda*exp(-alpha*t)*y5*h; RK constant k3 y4=y4b+(3.0/32.0)*k14... +(9.0/32.0)*k24; y5=y5b+(3.0/32.0)*k15... +(9.0/32.0)*k25; t= tb+(3.0/8.0)*h; k34=lambda*exp(-alpha*t)*y4*h; k35=lambda*exp(-alpha*t)*y5*h; RK constant k4 y4=y4b+(1932.0/2197.0)*k14... -(7200.0/2197.0)*k24... +(7296.0/2197.0)*k34; y5=y5b+(1932.0/2197.0)*k15... -(7200.0/2197.0)*k25... +(7296.0/2197.0)*k35; t= tb+(12.0/13.0)*h; k44=lambda*exp(-alpha*t)*y4*h; k45=lambda*exp(-alpha*t)*y5*h; RK constant k5 y4=y4b+( 439.0/ 216.0)*k14... -( 8.0 )*k24... +(3680.0/ 513.0)*k34... -( 845.0/4104.0)*k44; y5=y5b+( 439.0/ 216.0)*k15... -( 8.0 )*k25... +(3680.0/ 513.0)*k35...
Copyright 2004 by Chapman & Hall/CRC
-( 845.0/4104.0)*k45; t= tb+h; k54=lambda*exp(-alpha*t)*y4*h; k55=lambda*exp(-alpha*t)*y5*h; % %
% %
RK constant k6 y4=y4b-( 8.0/ 27.0)*k14... +( 2.0 )*k24... -(3544.0/2565.0)*k34... +(1859.0/4104.0)*k44... -( 11.0/ 40.0)*k54; y5=y5b-( 8.0/ 27.0)*k15... +( 2.0 )*k25... -(3544.0/2565.0)*k35... +(1859.0/4104.0)*k45... -( 11.0/ 40.0)*k55; t =tb+0.5*h; k65=lambda*exp(-alpha*t)*y5*h; RK step y4=y4b+( 25.0/ 216.0)*k14... +( 1408.0/2565.0)*k34... +( 2197.0/4104.0)*k44... -( 1.0/ 5.0)*k54; y5=y5b+( 16.0/ 135.0)*k15... +( 6656.0/12825.0)*k35... +(28561.0/56430.0)*k45... -( 9.0/ 50.0)*k55... +( 2.0/ 55.0)*k65; t =tb+h; end
Not much explanation is required for this code since it follows directly from Equations 1.49a to 1.49i. 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 k1 to k6 ). • The O(h 4 ) and O(h 5 ) solutions are computed independently, and we will next observe that they can be combined. • The code is a mixture of problem-specific coding, i.e., using Equations 1.3 and 1.4, and general coding, i.e., Equations 1.49a to 1.49i. 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; i.e., we are headed toward the development of general library routines.
Copyright 2004 by Chapman & Hall/CRC
The output from Program 1.3 is listed below (again, reformatted slightly to fit on a printed page):
h =
1.000 Fourth order method
t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y4 1.8814 2.3740 2.5860 2.6687 2.6997 2.7113 2.7155 2.7171 2.7177 2.7179
erry4 -0.0001703991 -0.0002465843 -0.0002711657 -0.0002799191 -0.0002831506 -0.0002843446 -0.0002847848 -0.0002849469 -0.0002850065 -0.0002850285
esty4 0.0000660329 0.0001027923 0.0001138050 0.0001183957 0.0001202510 0.0001209619 0.0001212276 0.0001213259 0.0001213621 0.0001213755
Fifth order method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
h =
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y5 1.8815 2.3741 2.5861 2.6688 2.6999 2.7114 2.7156 2.7172 2.7178 2.7180
erry5 -0.0001043662 -0.0001437920 -0.0001573607 -0.0001615234 -0.0001628996 -0.0001633827 -0.0001635572 -0.0001636210 -0.0001636444 -0.0001636530
0.100 Fourth order method
t 1.0 2.0 3.0 4.0
ye 1.8816 2.3742 2.5863 2.6689
Copyright 2004 by Chapman & Hall/CRC
y4 1.8816 2.3742 2.5863 2.6689
erry4 -0.0000000138 -0.0000000198 -0.0000000218 -0.0000000226
esty4 0.0000000135 0.0000000192 0.0000000212 0.0000000220
5.0 6.0 7.0 8.0 9.0 10.0
2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
-0.0000000229 -0.0000000230 -0.0000000231 -0.0000000231 -0.0000000231 -0.0000000231
0.0000000223 0.0000000224 0.0000000224 0.0000000225 0.0000000225 0.0000000225
Fifth order method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
h =
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y5 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry5 -0.0000000003 -0.0000000005 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006
0.010 Fourth order method
t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y4 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry4 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
Fifth order method t 1.0 2.0
ye 1.8816 2.3742
Copyright 2004 by Chapman & Hall/CRC
y5 1.8816 2.3742
erry5 0.0000000000 0.0000000000
esty4 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
h =
2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
0.001
Fourth order method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y4 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry4 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
esty4 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
Fifth order method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y5 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry5 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
This output is relatively easy to discuss since there are a lot of zeros! Specifically,
Copyright 2004 by Chapman & Hall/CRC
• At t = 1, with h = 1, which corresponds to a total of 10/1 = 10 steps, the O(h 5 ) method computed a solution accurate to at least four figures! 1.0
1.8816
1.8815
−0.0001043662
With 100 steps (h = 0.1) the error is only −0.0000000003 at t = 1 1.0
1.8816
1.8816
−0.0000000003
• The O(h 4 ) behavior of the fourth order method is evident (at least to a degree). At t = 1, h
ye
y4
erry4
1 0.1 0.01 0.001
1.8816 1.8816 1.8816 1.8816
1.8814 1.8816 1.8816 1.8816
−0.0001703991 −0.0000000138 0.0000000000 0.0000000000
esty4 0.0000660329 0.0000000135 0.0000000000 0.0000000000
Approximately four zeros are added to the exact and estimated errors when h is reduced from 1 to 0.1. When four more zeros (between h = 0.1 and h = 0.01) are added, the error drops below 0.0000000000 corresponding to the %15.10 f format of the fprintf statement in Program 1.3. Clearly, we can conclude that h = 0.01, 0.001 are excessively small for most practical applications in science and engineering. • The O(h 5 ) behavior of the fifth-order method is evident (also, to a degree). At t = 1, h
ye
y5
1 0.1 0.01 0.001
1.8816 1.8816 1.8816 1.8816
1.8815 1.8816 1.8816 1.8816
erry5 −0.0001043662 −0.0000000003 0.0000000000 0.0000000000
At least five zeros are added to the exact error when h is reduced from 1 to 0.1. When five more zeros (between h = 0.1 and h = 0.01) are added, the error drops far below 0.0000000000 (presumably) corresponding to the %15.10 f format of the fprintf statement in Program 1.3. Again, we can conclude that h = 0.01, 0.001 are excessively small for most practical applications, and a library routine would be far more efficient if it limited the reduction in h to somewhere in the range 0.1 ≤ h ≤ 1 rather than allow h to drop much below 0.1. • We can conclude the additional effort to compute the RK constants k1 to k6 is probably worthwhile since far larger steps (h) can be used to achieve
Copyright 2004 by Chapman & Hall/CRC
a solution of a 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 (beyond 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) O(h)? The first-order RK method includes the (dy/dt)(h/1!) term in the Taylor series, but excludes through truncation of the Taylor series the term (d 2 y/dt 2 ) × (h 2 /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 h for which the higher-order terms are negligible, then it would seem that the Euler method is second order (because of the h 2 in (d 2 y/dt 2 )(h 2 /2!)). However, this second derivative term is the local or one step error, that is, the error incurred by taking just one step along the solution of length h. But in computing a numerical solution using, for example, Programs 1.1 to 1.3, many steps are taken, and we are primarily interested in the total or global error after many steps (this is the error that we actually observe in the numerical solution to an ODE system, and which we want to control at some acceptable level). We can 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 is due to just the second derivative term: i =
d 2 yi h 2 dt 2 2!
then the local or one step error is O(h 2 ). If we integrate over a series of steps of length h from t = a to t = b using n steps, that is n=
b−a h
we can then estimate the total or global error as global error = (one step error)(number of steps) or global error =
d 2 yi h 2 dt 2 2!
b−a h
=
d 2 yi dt 2
b−a 2!
h
so that the global error is O(h) as we observed. Note that this is an approximate analysis based on two assumptions: 1. All of the local error is contained in just the one term (d 2 yi /dt 2 )(h 2 /2!). 2. The derivative d 2 yi /dt 2 is essentially constant over the interval a ≤ t ≤ b (or we can use some appropriate average value of this second derivative).
Copyright 2004 by Chapman & Hall/CRC
A more rigorous analysis to show that the Euler method is O(h) 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 O(h 5 )− O(h 6 ) locally, but O(h 4 )− O(h 5 ) globally (again, this can be established in a nonrigorous way for a general interval t = a to t = b as we did for the Euler method). We now consider the streamlining of the programming as mentioned previously for the (4, 5) pair.
1.5
Embedded RK Algorithms
We first note the interesting property of the RKF (4, 5) pair that the RK constants k1 to k5 given by Equations 1.49a to 1.49e are the same for both the O(h 4 ) and O(h 5 ) stepping formulas of Equations 1.49g and 1.49h (k6 is required for only the O(h 5 ) method of Equation 1.49h). Thus, we can consider the O(h 4 ) method of Equation 1.49g to be embedded in the O(h 5 ) method of Equation 1.49h. This has an important implication: k1 to k5 need be calculated only once for both methods (rather than for each method as in Program 1.3). With this idea in mind, the only difference between the two methods is the calculation of k6 for the O(h 5 ) method of Equation 1.49g, and the selection of a base point for the next step (either the O(h 4 ) or the O(h 5 ) 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 k1 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 1.44a, with the common k1 of Equation 1.42b (or Equation 1.44b) and the common k2 of Equation 1.42c (or Equation 1.44c). The embedding of the (1, 2) pair is illustrated by the following Program 1.4, which is a small revision of Program 1.1:
% % % % %
Program 1.4 Tumor model of eqs. (1.47), (1.48) (or eqs. (1.3), (1.4), (1.5)) Model parameters y0=1.0; lambda=1.0; alpha=1.0;
% % Step through cases for ncase=1:4 %
Copyright 2004 by Chapman & Hall/CRC
% Integration step if(ncase==1)h=1.0 ;nsteps=1 ;end if(ncase==2)h=0.1 ;nsteps=10 ;end if(ncase==3)h=0.01 ;nsteps=100 ;end if(ncase==4)h=0.001;nsteps=1000;end % % Variables for ODE integration tf=10.0; t=0.0; % % Initial condition y2=y0; % % Print heading fprintf('\n\nh = %6.3f\n',h); fprintf(... ' t ye y1 erry1 y2 erry2\n') % % Continue integration while t<0.999*tf % % Take nsteps integration steps for i=1:nsteps % % Store solution at base point yb=y2; tb=t; % % RK constant k1 k1=lambda*exp(-alpha*tb)*y2*h; % % RK constant k2 y2=yb+k1; t=tb+h; k2=lambda*exp(-alpha*t)*y2*h; % % RK step y1=yb+k1; y2=yb+(k1+k2)/2.0; esty1=y2-y1; end % % Print solutions and errors ye=y0*exp((lambda/alpha)*(1.0-exp(-alpha*t)));
Copyright 2004 by Chapman & Hall/CRC
esty1
erry1=y1-ye; erry2=y2-ye; fprintf('%5.1f%9.4f%9.4f%15.10f%15.10f%9.4f%15.10f\n',... t,ye,y1,erry1,esty1,y2,erry2); % % Continue integration end % % Next case end
Program 1.4 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 points about Program 1.4: • 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 keeping in mind that y1 is the O(h) (Euler method) and y2 is the O(h 2 ) (modified Euler method), the base point is selected as the running value of y2: % %
Store solution at base point yb=y2; tb=t;
where the initial value of y2 was set previously as an initial condition. • k1 and k2 are then calculated (according to Equations 1.27a and 1.27b): % % % %
RK constant k1 k1=lambda*exp(-alpha*tb)*y2*h; RK constant k2 y2=yb+k1; t=tb+h; k2=lambda*exp(-alpha*t)*y2*h;
• The first- and second-order stepping formulas are then used (according to Equations 1.28 and 1.29): % %
RK step y1=yb+k1;
Copyright 2004 by Chapman & Hall/CRC
y2=yb+(k1+k2)/2.0; esty1=y2-y1; end
Note in this code that: — The estimated error in y1, esty1, is computed by p refinement (subtraction of the O(h) solution from the O(h 2 ) solution). — The same value of k1 is used for both the first- and second-order stepping formulas (making use of the embedding of the (2, 3) pair, i.e., the first-order method is embedded in the second-order method) — The end statement terminates the for loop of nsteps of length h. 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): h =
1.000 First order method
t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y1 2.0000 2.5550 2.7070 2.7432 2.7528 2.7557 2.7567 2.7571 2.7572 2.7573
erry1 0.1184036125 0.1808239664 0.1207761366 0.0742305143 0.0527351769 0.0441724614 0.0409303986 0.0397250855 0.0392799583 0.0391159724
Second order method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
Copyright 2004 by Chapman & Hall/CRC
y2 1.8679 2.3843 2.6131 2.7033 2.7373 2.7499 2.7546 2.7563 2.7569 2.7572
erry2 -0.0137169464 0.0101398613 0.0268205142 0.0343032307 0.0372532762 0.0383660134 0.0387791239 0.0389316092 0.0389877746 0.0390084461
esty1 -0.1321205588 -0.1706841052 -0.0939556225 -0.0399272836 -0.0154819007 -0.0058064480 -0.0021512746 -0.0007934762 -0.0002921837 -0.0001075263
h =
0.100 First order method
t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y1 1.8837 2.3760 2.5874 2.6698 2.7008 2.7123 2.7165 2.7181 2.7186 2.7188
erry1 0.0021070298 0.0017594212 0.0011768161 0.0008767242 0.0007532605 0.0007059944 0.0006883524 0.0006818277 0.0006794227 0.0006785373
esty1 -0.0021479991 -0.0014290535 -0.0006384570 -0.0002516819 -0.0000949453 -0.0000352515 -0.0000130123 -0.0000047929 -0.0000017640 -0.0000006491
Second order method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
h =
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y2 1.8816 2.3745 2.5868 2.6696 2.7007 2.7122 2.7165 2.7180 2.7186 2.7188
erry2 -0.0000409693 0.0003303677 0.0005383591 0.0006250422 0.0006583152 0.0006707429 0.0006753402 0.0006770348 0.0006776587 0.0006778883
0.010 First order method
t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174
Copyright 2004 by Chapman & Hall/CRC
y1 1.8816 2.3742 2.5863 2.6690 2.7000 2.7116 2.7158 2.7174
erry1 0.0000217778 0.0000178106 0.0000121768 0.0000093347 0.0000081729 0.0000077291 0.0000075636 0.0000075024
esty1 -0.0000218425 -0.0000139313 -0.0000061436 -0.0000024108 -0.0000009079 -0.0000003369 -0.0000001243 -0.0000000458
9.0 10.0
2.7179 2.7182
2.7180 2.7182
0.0000074798 0.0000074715
-0.0000000169 -0.0000000062
Second order method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
h =
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y2 1.8816 2.3742 2.5863 2.6690 2.7000 2.7116 2.7158 2.7174 2.7180 2.7182
erry2 -0.0000000647 0.0000038793 0.0000060332 0.0000069239 0.0000072649 0.0000073922 0.0000074392 0.0000074566 0.0000074629 0.0000074653
0.001 First order method
t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y1 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry1 0.0000002185 0.0000001784 0.0000001222 0.0000000940 0.0000000824 0.0000000780 0.0000000764 0.0000000758 0.0000000756 0.0000000755
Second order method t 1.0 2.0 3.0 4.0 5.0 6.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116
Copyright 2004 by Chapman & Hall/CRC
y2 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116
erry2 -0.0000000003 0.0000000394 0.0000000610 0.0000000700 0.0000000734 0.0000000747
esty1 -0.0000002187 -0.0000001390 -0.0000000612 -0.0000000240 -0.0000000090 -0.0000000034 -0.0000000012 -0.0000000005 -0.0000000002 -0.0000000001
7.0 8.0 9.0 10.0
2.7158 2.7174 2.7179 2.7182
2.7158 2.7174 2.7179 2.7182
0.0000000751 0.0000000753 0.0000000754 0.0000000754
We can note the following points about this output: • The first-order method appears to be higher than O(h). For example, at t = 1, the output is
h
ye
y1
erry1
esty1
1 0.1 0.01 0.001
1.8816 1.8816 1.8816 1.8816
2.0000 1.8837 1.8816 1.8816
0.1184036125 0.0021070298 0.0000217778 0.0000002185
−0.1321205588 −0.0021479991 −0.0000218425 −0.0000002187
In fact, the first-order method appears to be second order correct! For example, reducing h from 0.1 to 0.01 reduces the exact error from 0.0021070298 to 0.0000217778 (two zeros are added after the decimal point). The reason for this is that the second-order solution, y2, is used as the base point for the next step along the solution, i.e., % %
Store solution at base point yb=y2; tb=t;
To state this in other words, y1 is corrected by esty1 before going on to the next point. For example, at t = 1 for h = 0.1, y1 + y1est = 1.8837 − 0.0021479991 = 1.8816 = y2 This is an important point discussed previously as Step 4 in the algorithm after Equation 1.26c. In other words, in a library ODE integrator, which automatically adjusts the step h, the estimated error esty1 will generally be computed to determine if the step h is small enough to satisfy a specified error tolerance. When h becomes small enough to meet the error criterion, the estimated error can be added as a correction before taking the next step along the solution. In this case (the (1, 2) pair), this in effect increases the accuracy of the solution from O(h) to O(h 2 ) as we observed in the preceding output from Program 1.4. • This error correction could be programmed in a slightly dfifferent, but equivalent, way (see Equations 1.30):
Copyright 2004 by Chapman & Hall/CRC
% %
RK step y1=yb+k1; esty1=(k2-k1)/2.0; y2=y1+esty1; end
Clearly, adding the estimated error as a correction (y2 = y1 + esty1) before taking the next step along the solution (as explained in the algorithm after Equation 1.26c) was worth doing (the first-order method becomes effectively second order). • The exact error in y2 from Program 1.4, err y2, at t = 1 appears to be greater than O(h 2 ): h
ye
y2
erry2
1 0.1 0.01 0.001
1.8816 1.8816 1.8816 1.8816
1.8679 1.8816 1.8816 1.8816
−0.0137169464 −0.0000409693 −0.0000000647 −0.0000000003
However, generally, this error is O(h 2 ). For example, at t = 2, the exact error in y2 is h
erry2
1 0.1 0.01 0.001
0.0101398613 0.0003303677 0.0000038793 0.0000000394
(so that two zeros are added after the decimal point for each 1/10 reduction in h, as expected). • The estimated error, esty1 appears to converge to the exact error, err y1, for small h: h
erry1
esty1
1 0.1 0.01 0.001
0.1184036125 0.0021070298 0.0000217778 0.0000002185
−0.1321205588 −0.0021479991 −0.0000218425 −0.0000002187
This convergence again illustrates the important point that the estimated error accurately estimates the exact error for small h (and thus adding it as a correction to the O(h) solution gives a substantially better solution).
Copyright 2004 by Chapman & Hall/CRC
We next investigate the embedding of the (2, 3) pair of Equations 1.42 and 1.44. The following Program 1.5, which is analogous to Program 1.4, illustrates how this embedding can be used.
% % % % %
Program 1.5 Tumor model of eqs. (1.47), (1.48) (or eqs. (1.3), (1.4), (1.5)) Model parameters y0=1.0; lambda=1.0; alpha=1.0;
% % Step through cases for ncase=1:4 % % Integration step if(ncase==1)h=1.0 ;nsteps=1 ;end if(ncase==2)h=0.1 ;nsteps=10 ;end if(ncase==3)h=0.01 ;nsteps=100 ;end if(ncase==4)h=0.001;nsteps=1000;end % % Variables for ODE integration tf=10.0; t=0.0; % % Initial condition y3=y0; % % Print heading fprintf('\n\nh = %6.3f\n',h); fprintf(... ' t ye y2 erry2 y3 erry3\n') % % Continue integration while t<0.999*tf % % Take nsteps integration steps for i=1:nsteps % % Store solution at base point yb=y3; tb=t;
Copyright 2004 by Chapman & Hall/CRC
esty2
% % % %
% %
% %
RK constant k1 k1=lambda*exp(-alpha*t)*y3*h; RK constant k2 y3=yb+(2.0/3.0)*k1; t=tb+(2.0/3.0)*h; k2=lambda*exp(-alpha*t)*y3*h; RK integration K3 y3=yb+(2.0/3.0)*k2; t=tb+(2.0/3.0)*h; k3=lambda*exp(-alpha*t)*y3*h; RK step y2=yb+(1.0/4.0)*k1+(3.0/4.0)*k2; y3=yb+(1.0/4.0)*k1+(3.0/8.0)*k2+(3.0/8.0)*k3; esty2=y3-y2; t=tb+h; end
% % Print solutions and errors ye=y0*exp((lambda/alpha)*(1.0-exp(-alpha*t))); erry2=y2-ye; erry3=y3-ye; fprintf('%5.1f%9.4f%9.4f%15.10f%15.10f%9.4f%15.10f\n',... t,ye,y2,erry2,esty2,y3,erry3); % % Continue integration end % % Next case end
Program 1.5 Program for the integration of Equation 1.48 by the (2, 3) pair of Equations 1.42 and 1.44 Program 1.5 closely parallels Program 1.4. As expected, k1 and k2 are used for both the second- and third-order stepping formulas (k3 is required for only the third-order stepping formula): % %
RK step y2=yb+(1.0/4.0)*k1+(3.0/4.0)*k2; y3=yb+(1.0/4.0)*k1+(3.0/8.0)*k2+(3.0/8.0)*k3;
Copyright 2004 by Chapman & Hall/CRC
esty2=y3-y2; t=tb+h; end
The output from Program 1.5 is listed below: h =
1.000 Second order method
t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y2 1.8918 2.3760 2.5785 2.6592 2.6900 2.7014 2.7057 2.7073 2.7078 2.7081
erry2 0.0101750113 0.0017600371 -0.0077128645 -0.0097573439 -0.0100669280 -0.0101048752 -0.0101076784 -0.0101071543 -0.0101067488 -0.0101065707
esty2 -0.0185221389 -0.0117360496 -0.0024469927 -0.0003848403 -0.0000549878 -0.0000075909 -0.0000010348 -0.0000001404 -0.0000000190 -0.0000000026
Third order method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
h =
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y3 1.8732 2.3642 2.5761 2.6588 2.6899 2.7014 2.7057 2.7073 2.7078 2.7081
erry3 -0.0083471276 -0.0099760125 -0.0101598572 -0.0101421842 -0.0101219158 -0.0101124662 -0.0101087132 -0.0101072948 -0.0101067678 -0.0101065733
0.100 Second order method
t 1.0 2.0
ye 1.8816 2.3742
Copyright 2004 by Chapman & Hall/CRC
y2 1.8816 2.3742
erry2 0.0000183521 -0.0000035143
esty2 -0.0000273880 -0.0000067556
3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
2.5863 2.6689 2.7000 2.7115 2.7158 2.7174 2.7179 2.7181
-0.0000092951 -0.0000102271 -0.0000103474 -0.0000103581 -0.0000103573 -0.0000103564 -0.0000103560 -0.0000103558
-0.0000011124 -0.0000001614 -0.0000000224 -0.0000000031 -0.0000000004 -0.0000000001 0.0000000000 0.0000000000
Third order method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
h =
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y3 1.8816 2.3742 2.5862 2.6689 2.7000 2.7115 2.7158 2.7174 2.7179 2.7181
erry3 -0.0000090358 -0.0000102699 -0.0000104074 -0.0000103885 -0.0000103698 -0.0000103611 -0.0000103577 -0.0000103564 -0.0000103560 -0.0000103558
0.010 Second order method
t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
Copyright 2004 by Chapman & Hall/CRC
y2 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry2 0.0000000183 -0.0000000033 -0.0000000086 -0.0000000094 -0.0000000095 -0.0000000095 -0.0000000095 -0.0000000095 -0.0000000095 -0.0000000095
esty2 -0.0000000269 -0.0000000063 -0.0000000010 -0.0000000001 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
Third order method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
h =
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y3 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry3 -0.0000000085 -0.0000000096 -0.0000000096 -0.0000000096 -0.0000000096 -0.0000000096 -0.0000000095 -0.0000000095 -0.0000000095 -0.0000000095
0.001
Second order method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y2 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry2 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
Third order method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
Copyright 2004 by Chapman & Hall/CRC
y3 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry3 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
esty2 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
We can note the following points about this output: • The corrected y2 appears to be O(h 3 ), e.g., for t = 1, h
ye
y2
erry2
esty2
1 0.1 0.01 0.001
1.8816 1.8816 1.8816 1.8816
1.8918 1.8816 1.8816 1.8816
0.0101750113 0.0000183521 0.0000000183 0.0000000000
−0.0185221389 −0.0000273880 −0.0000000269 0.0000000000
Note that when h is reduced from 0.1 to 0.01, err y2 is reduced from 0.0000183521 to 0.0000000183 so that three zeros were added after the decimal point. This is expected since y2 is corrected by esty2 before taking the next step along the solution, thereby giving an O(h 3 ) result (y3 is used as the base point value for the next step). • esty2 is not as reliable an estimate of the true error, err y2 as we would like. For some points along the solution, it underestimates the exact error in y2, and at other points, it overestimates the exact error. For example, when h = 0.01, h =
0.010 Second order method
t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y2 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry2 0.0000000183 -0.0000000033 -0.0000000086 -0.0000000094 -0.0000000095 -0.0000000095 -0.0000000095 -0.0000000095 -0.0000000095 -0.0000000095
esty2 -0.0000000269 -0.0000000063 -0.0000000010 -0.0000000001 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
An overestimate of the exact error is conservative in adjusting h, but an underestimate will possibly produce an h that is too large to actually limit the exact error to a specified value or tolerance. Certainly we would like to have a reliable (quantitatively correct) estimate of the true error so that we can reliably adjust h. Another way to interpret this result (estimate of limited accuracy) is to observe that the higher-order solution y3 also has some error and therefore the estimated error is not a perfect correction (it does not give a higher-order solution without error).
Copyright 2004 by Chapman & Hall/CRC
Finally, we investigate the embedding of the (4, 5) pair of Equations 1.49. The following Program 1.6, which is analogous to Programs 1.4 and 1.5, illustrates how this embedding can be used. % % % % % %
Program 1.6 Tumor model of eqs. (1.47), (1.48) (or eqs. (1.3), (1.4), (1.5)) Model parameters y0=1.0; lambda=1.0; alpha=1.0;
% % Step through cases for ncase=1:4 % % Integration step if(ncase==1)h=1.0 ;nsteps=1 ;end if(ncase==2)h=0.1 ;nsteps=10 ;end if(ncase==3)h=0.01 ;nsteps=100 ;end if(ncase==4)h=0.001;nsteps=1000;end % % Variables for ODE integration tf=10.0; t=0.0; % % Initial condition y5=y0; % % Print heading fprintf('\n\nh = %6.3f\n',h); fprintf(... ' t ye y4 erry4 esty4 % % Continue integration while t<0.999*tf % % Take nsteps integration steps for i=1:nsteps % % Store solution at base point yb=y5; tb=t;
Copyright 2004 by Chapman & Hall/CRC
y5
erry5\n')
% % % %
% %
% %
% %
% %
% %
RK constant k1 k1=lambda*exp(-alpha*t)*y5*h; RK constant k2 y5=yb+0.25*k1; t=tb+0.25*h; k2=lambda*exp(-alpha*t)*y5*h; RK constant k3 y5=yb+(3.0/32.0)*k1... +(9.0/32.0)*k2; t=tb+(3.0/8.0)*h; k3=lambda*exp(-alpha*t)*y5*h; RK constant k4 y5=yb+(1932.0/2197.0)*k1... -(7200.0/2197.0)*k2... +(7296.0/2197.0)*k3; t=tb+(12.0/13.0)*h; k4=lambda*exp(-alpha*t)*y5*h; RK constant k5 y5=yb+( 439.0/ 216.0)*k1... -( 8.0 )*k2... +(3680.0/ 513.0)*k3... -( 845.0/4104.0)*k4; t=tb+h; k5=lambda*exp(-alpha*t)*y5*h; RK constant k6 y5=yb-( 8.0/ 27.0)*k1... +( 2.0 )*k2... -(3544.0/2565.0)*k3... +(1859.0/4104.0)*k4... -( 11.0/ 40.0)*k5; t=tb+0.5*h; k6=lambda*exp(-alpha*t)*y5*h; RK step y4=yb+( +( +( -(
25.0/ 216.0)*k1... 1408.0/2565.0)*k3... 2197.0/4104.0)*k4... 1.0/ 5.0)*k5;
Copyright 2004 by Chapman & Hall/CRC
y5=yb+( 16.0/ 135.0)*k1... +( 6656.0/12825.0)*k3... +(28561.0/56430.0)*k4... -( 9.0/ 50.0)*k5... +( 2.0/ 55.0)*k6; esty4=y5-y4; t=tb+h; end % % Print solutions and errors ye=y0*exp((lambda/alpha)*(1.0-exp(-alpha*t))); erry4=y4-ye; erry5=y5-ye; fprintf('%5.1f%9.4f%9.4f%15.10f%15.10f%9.4f%15.10f\n',... t,ye,y4,erry4,esty4,y5,erry5); % % Continue integration end % % Next case end
Program 1.6 Program for the integration of Equation 1.48 by the RKF45 pair of Equations 1.49 Program 1.6 closely parallels Programs 1.4 and 1.5. As expected, k1 to k5 are used for both the fourth- and fifth-order stepping formulas (k6 is required for only the fifth-order stepping formula) % %
RK step y4=yb+( 25.0/ 216.0)*k1... +( 1408.0/2565.0)*k3... +( 2197.0/4104.0)*k4... -( 1.0/ 5.0)*k5; y5=yb+( 16.0/ 135.0)*k1... +( 6656.0/12825.0)*k3... +(28561.0/56430.0)*k4... -( 9.0/ 50.0)*k5... +( 2.0/ 55.0)*k6; esty4=y5-y4; t=tb+h; end
Note also that k2 is not used in either stepping formula (but it is required to calculate k3 to k6 ).
Copyright 2004 by Chapman & Hall/CRC
The output from Program 1.6 is listed below: h =
1.000 Fourth order method
t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y4 1.8814 2.3740 2.5861 2.6688 2.6999 2.7114 2.7156 2.7172 2.7178 2.7180
erry4 -0.0001703991 -0.0001632647 -0.0001591927 -0.0001624755 -0.0001633761 -0.0001635804 -0.0001636333 -0.0001636494 -0.0001636549 -0.0001636569
esty4 0.0000660329 0.0000194727 0.0000018321 0.0000009521 0.0000004765 0.0000001976 0.0000000760 0.0000000284 0.0000000105 0.0000000039
Fifth order method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
h =
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y5 1.8815 2.3741 2.5861 2.6688 2.6999 2.7114 2.7156 2.7172 2.7178 2.7180
erry5 -0.0001043662 -0.0001437920 -0.0001573607 -0.0001615234 -0.0001628996 -0.0001633827 -0.0001635572 -0.0001636210 -0.0001636444 -0.0001636530
0.100 Fourth order method
t 1.0 2.0 3.0 4.0 5.0 6.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116
Copyright 2004 by Chapman & Hall/CRC
y4 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116
erry4 -0.0000000009 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006
esty4 0.0000000006 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
7.0 8.0 9.0 10.0
2.7158 2.7174 2.7179 2.7182
2.7158 2.7174 2.7179 2.7182
-0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006
0.0000000000 0.0000000000 0.0000000000 0.0000000000
Fifth order method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
h =
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y5 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry5 -0.0000000003 -0.0000000005 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006
0.010 Fourth order method
t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y4 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry4 -0.0000000009 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006
Fifth order method t 1.0 2.0 3.0 4.0
ye 1.8816 2.3742 2.5863 2.6689
Copyright 2004 by Chapman & Hall/CRC
y5 1.8816 2.3742 2.5863 2.6689
erry5 -0.0000000003 -0.0000000005 -0.0000000006 -0.0000000006
esty4 0.0000000006 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
5.0 6.0 7.0 8.0 9.0 10.0
h =
2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
-0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006 -0.0000000006
0.001
Fourth order method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y4 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry4 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
esty4 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
Fifth order method t 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
y5 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry5 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
We can note the following points about this output: • For h = 1, esty4 is not a reliable estimate of the exact error, err y4. However, the solution for h = 1 results from only a total of ten steps in the interval 0 ≤ t ≤ 10, so we might expect that the estimated error esty4 will not be very accurate. Also, even with just ten steps, the (4, 5) pair produced a solution that is accurate to about five figures.
Copyright 2004 by Chapman & Hall/CRC
• For h = 0.1, the solution is so accurate that the exact and estimated errors appear in only the tenth decimal place (the final decimal place provided by the %15.10f format of the fprintf statement). • h = 0.01 and 0.001 appear to be excessively small. We conclude this section with a (2, 4) embedded pair, i.e., an O(h 2 ) method embedded in an O(h 4 ) method. The fourth-order method (the original RK method reported by Runge and Kutta in the 1890s) is k1 = f (yi , ti )h
(1.50a)
k2 = f (yi + k1 /2, ti + h/2)h
(1.50b)
k3 = f (yi + k2 /2, ti + h/2)h
(1.50c)
k4 = f (yi + k3 , ti + h)h
(1.50d)
with the stepping formula y4,i+1 = yi + (1/6)(k1 + 2k2 + 2k3 + k4 )
(1.50e)
As we discussed previously, Equation 1.50e fits the Taylor series up to and including the fourth-order derivative term, (d 4 y/dt 4 )(h 4 /4!); i.e., the resulting numerical solution is O(h 4 ). The second-order midpoint RK method of Equations 1.40 has the same k1 and k2 and therefore is embedded in the fourth-order method. An error estimate for this second-order method can be obtained by subtracting the second-order stepping formula from the fourth-order stepping formula of Equation 1.50e: i = y4,i+1 − y2,i+1 = yi + (1/6)(k1 + 2k2 + 2k3 + k4 ) − (yi + k2 ) = (1/6)(k1 − 4k2 + 2k3 + k4 )
(1.51)
Note how the k1 and k2 terms combine in arriving at Equation 1.51 since they are the same for both algorithms, i.e., Equations 1.40 and 1.50. i of Equation 1.51 can now be used to automatically adjust the integration step, h, which is the basis of the programming in a set of routines to be discussed in subsequent chapters. Note also that since this error estimate was achieved by subtracting the stepping formula for a second-order method (Equation 1.40a), from the stepping formula for a fourth-order method (Equation 1.50e), the error estimate actually represents two terms in the Taylor series, i.e., (d 3 y/dt 3 )(h 3 /3!) and (d 4 y/dt 4 )(h 4 /4!); i.e., i from Equation 1.51 is a two term error estimate, and therefore we might expect that it will be more accurate than the one term error estimates of the preceding (1, 2), (2, 3), and (4, 5) pairs. Experience has indicated this is the case. In fact, some additional embedded RK pairs are listed in Appendix A, which have three term error estimates.
Copyright 2004 by Chapman & Hall/CRC
The principal conclusions from this discussion of embedded methods are as follows: • The RK constants generally can be computed once for both the lowerorder and the higher-order methods of an embedded pair. In other words, the common RK constants are the basis for embedded pairs. • Correction of the lower-order solution using the estimated error (the difference between the higher- and lower-order methods) gives a substantially improved lower-order solution. In other words, the higher-order solution is used as the base point for the next step along the solution.
1.6
Library ODE Integrators
We have discussed several RK pairs ((1, 2), (2, 3), (2, 4), (4, 5)) that can be used in library routines. Because each pair produces an estimate of the truncation error, these four methods can be used to automatically adjust h to achieve a specified error tolerance. Furthermore, although the preceding programming of the four pairs has been for a 1x1 problem (Equations 1.3 and 1.4 with the analytical solution (Equation 1.5)), they can be applied directly to the nxn problem by using vectors for the RK constants and the stepping formulas. Thus, we now have everything we need for general-purpose ODE integration routines, which are discussed in subsequent chapters. To illustrate what we might accomplish, we consider briefly the ODE library routines in MATLAB (of the programming languages considered in the subsequent discussion, only Maple and MATLAB have built-in ODE utilities). MATLAB includes utilities for stiff and nonstiff ODEs (stiffness and stability are discussed briefly in the next section). However, we consider here only the nonstiff MATLAB integrators, ode23 and ode45, which is consistent with the four RK pairs discussed previously since they are only for nonstiff problems (they are explicit integrators). The development of stiff (implicit) integrators is considerably more involved than in the preceding development, so we merely consider in the next section why they might be required for a particular problem, Equations 1.6 to 1.17. Program 1.7 calls the two MATLAB nonstiff solvers, ode23 and ode45, for solution of Equations 1.3 and 1.4, with the evaluation of the exact solution, Equation 1.5, to assess the accuracy of the numerical solution. % % % % % %
Program 1.7 Tumor model of eqs. (1.47), (1.48) (or eqs. (1.3), (1.4), (1.5)) Global variables global lambda alpha ncall;
Copyright 2004 by Chapman & Hall/CRC
% % Model parameters lambda=1.0; alpha=1.0; % % Select method for mf=1:2 % % Error tolerances reltol=1.0e-02; abstol=1.0e-02; for ncase=1:4 reltol=1.0e-02*reltol; abstol=1.0e-02*abstol; % % Initialize counter for derivative evaluations ncall=0; % % Variables for ODE integration t0=0.0; tf=10.0; tout=[t0:1.0:tf]'; nout=11; % % Initial condition y0=1.0; % % Call ODE integrator options=odeset('RelTol',reltol,'AbsTol',abstol); if(mf==1)[t,y]=ode23('ode1p7',tout,y0,options); end if(mf==2)[t,y]=ode45('ode1p7',tout,y0,options); end % % Display solution and error fprintf('\n\n mf = %1d\n case = %1d\n reltol = %6.2e \n abstol = %6.2e\n\n',... mf,ncase,reltol,abstol); fprintf(' t ye y erry\n'); for i=1:nout ye(i)=y0*exp((lambda/alpha)*(1.0-exp(-alpha*t(i)))); erry(i)=ye(i)-y(i); fprintf('%5.1f%9.4f%9.4f%15.10f\n',t(i),ye(i),y(i), erry(i)); end fprintf('\n ncall = %5d\n',ncall); %
Copyright 2004 by Chapman & Hall/CRC
% Next case end % % Next method end % % Plot last solution plot(t,y); xlabel('t') ylabel('y(t)') title(' Program 1.7, dy/dt = \lambda*exp(-\alpha*t)*y)') print pro1p7.ps
Program 1.7 Program for the integration of Equation 1.48 by the library integrators ode23 and ode45 We can note the following points about Program 1.7: • Three global variables are defined, which can then be shared between Program 1.7 and a function, ode1 p7.m, called by Program 1.7 to define ODE (Equation 1.3). In other words, alpha, lambda, and ncall are used in function ode1 p7.m, but their values are initialized in Program 1.7: % % Global variables global lambda alpha ncall;
• lambda and alpha are then set numerically (and, again, these values will be available in ode1 p7.m because they are global variables): % % Model parameters lambda=1.0; alpha=1.0;
ncall is initialized numerically later in the code. • A method flag, m f , is set to one of two values: m f = 1 calls ode23 and m f = 2 calls ode45: % % Select method for mf=1:2
• For each value of m f , four solutions are computed, with relative and absolute error tolerances of 10−4 , 10−6 , 10−8 , and 10−10 . ode23 and ode45 then attempt to adjust h to meet these tolerances:
Copyright 2004 by Chapman & Hall/CRC
% % Error tolerances reltol=1.0e-02; abstol=1.0e-02; for ncase=1:4 reltol=1.0e-02*reltol; abstol=1.0e-02*abstol;
• At the beginning of each case (ncase = 1 to 4), a counter, ncall, is initialized that is then incremented each time the function ode1 p7.m is called. This procedure gives the total number of calls to ode1 p7.m and thus the number of derivative evaluations for each solution. % %
Initialize counter for derivative evaluations ncall=0;
Again, note that ncall is a global variable so its value is returned to Program 1.7. • The variables that define the interval in the independent variable, t, and when the solution is displayed are then initialized. tout is a vector containing t = 0, 1, . . . , 10 (a total of 11 output values of t): % %
Variables for ODE integration t0=0.0; tf=10.0; tout=[t0:1.0:tf]'; nout=11;
• Initial condition (Equation 1.4) is set to start the solution: % %
Initial condition y0=1.0;
• The solution to Equation 1.3 is computed by ode12 or ode45, depending on the value of m f : % %
Call ODE integrator options=odeset('RelTol',reltol,'AbsTol',abstol); if(mf==1)[t,y]=ode23('ode1p7',tout,y0,options); end if(mf==2)[t,y]=ode45('ode1p7',tout,y0,options); end
Function option is first called to set the relative and absolute error tolerances. Function ode1 p7.m defines ODE (Equation 1.3) by receiving the global variables alpha and lambda, and the current values of t and y to evaluate the right-hand side (RHS) of Equation 1.3:
Copyright 2004 by Chapman & Hall/CRC
function yt=ode1p7(t,y) % % Set global variables global lambda alpha ncall; % % ODE yt(1)=lambda*exp(-alpha*t)*y(1); % % Increment counter for derivative evaluations ncall=ncall+1;
Note that y is an input column vector (with one element, y(1), for the 1x1 ODE problem of Equation 1.3), and yt is an output column vector (with one element, yt(1)). t is an input scaler. Also, ncall is incremented by 1 each time ode1 p7.m is called, which then provides the total number of calls (derivative evaluations) reported in the output. • The parameters and numerical solution are then displayed by a series of fprintf statements, including the exact solution and the error in the solution: % %
Display solution and error fprintf('\n\n mf = %1d\n case = %1d\n reltol = %6.2e\n abstol = %6.2e\n\n',... mf,ncase,reltol,abstol); fprintf(' t ye y erry\n'); for i=1:nout ye(i)=y0*exp((lambda/alpha) *(1.0-exp(-alpha*t(i)))); erry(i)=ye(i)-y(i); fprintf('%5.1f%9.4f%9.4f%15.10f\n',t(i),ye(i), y(i),erry(i)); end fprintf('\n ncall = %5d\n',ncall);
The first line of the first fprintf statement has been put on two lines to fit on the printed page, but would have to be returned to one line (since the line break . . . cannot be used in the character string delimited by ). • After the four cases are completed for each of the two methods (a total of (4)(2) = 8 solutions), the final (eighth) solution is plotted via the plot and related statements and then saved via print pro1p7.ps. The resulting Postscript file is Figure 1.4; this plot is rather bumpy because only 11 output values of t (in vector tout) are used. Of course, this number could easily be increased.
Copyright 2004 by Chapman & Hall/CRC
Program 1.7, dy/dt = λ*exp( –α*t)*y)
2.8 2.6 2.4 2.2
y(t)
2 1.8 1.6 1.4 1.2 1
0
1
2
3
4
5
6
7
8
9
10
t FIGURE 1.4
Solution of Equations 1.3, 1.4, 1.5, from Program 1.7, m f = 2, ncase = 4.
% % Next case end % % Next method end % % Plot last solution plot(t,y); xlabel('t') ylabel('y(t)') title('Program 1.7, dy/dt = \lambda*exp(-\alpha*t)*y)') print pro1p7.ps
Note in the title statement that Greek letters can be included in the label in Figure 1.4 by using the codes \lambda and \alpha. • Finally, we should note that routine ode23 is based on a RK (2, 3) pair,3 and ode45 is based on a RKF (4, 5) pair.3 The error estimates in these two RK methods are used to adjust h to achieve the error tolerances specified in the call to function options. Some detailed coding for this type of error monitoring and control is given in the routines discussed in subsequent chapters.
Copyright 2004 by Chapman & Hall/CRC
The output from Program 1.7 is listed below:
mf = 1 case = 1 reltol = 1.00e-004 abstol = 1.00e-004 t 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.0000 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
ncall =
y 1.0000 1.8816 2.3743 2.5864 2.6691 2.7002 2.7117 2.7160 2.7175 2.7181 2.7183
erry 0.0000000000 -0.0000020034 -0.0000638807 -0.0000933201 -0.0001103656 -0.0001284242 -0.0001434300 -0.0001552112 -0.0001581733 -0.0001592608 -0.0001592537
73
mf = 1 case = 2 reltol = 1.00e-006 abstol = 1.00e-006 t 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.0000 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
ncall =
268
Copyright 2004 by Chapman & Hall/CRC
y 1.0000 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry 0.0000000000 -0.0000003221 -0.0000012721 -0.0000016231 -0.0000018847 -0.0000021190 -0.0000023452 -0.0000025176 -0.0000026866 -0.0000028883 -0.0000029806
mf = 1 case = 3 reltol = 1.00e-008 abstol = 1.00e-008 t 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.0000 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
ncall =
y 1.0000 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry 0.0000000000 -0.0000000072 -0.0000000188 -0.0000000229 -0.0000000260 -0.0000000285 -0.0000000309 -0.0000000331 -0.0000000354 -0.0000000374 -0.0000000394
1180
mf = 1 case = 4 reltol = 1.00e-010 abstol = 1.00e-010 t 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.0000 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
ncall =
5392
Copyright 2004 by Chapman & Hall/CRC
y 1.0000 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry 0.0000000000 -0.0000000001 -0.0000000003 -0.0000000003 -0.0000000003 -0.0000000004 -0.0000000004 -0.0000000004 -0.0000000004 -0.0000000005 -0.0000000005
mf = 2 case = 1 reltol = 1.00e-004 abstol = 1.00e-004 t 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.0000 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
ncall =
y 1.0000 1.8815 2.3742 2.5862 2.6689 2.7000 2.7115 2.7158 2.7174 2.7179 2.7181
erry 0.0000000000 0.0000785756 0.0000230677 0.0000132883 0.0000149230 0.0000165095 0.0000172423 0.0000175329 0.0000176427 0.0000176835 0.0000177068
73
mf = 2 case = 2 reltol = 1.00e-006 abstol = 1.00e-006 t 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.0000 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
ncall =
85
Copyright 2004 by Chapman & Hall/CRC
y 1.0000 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry 0.0000000000 0.0000010053 0.0000010986 0.0000011560 0.0000010643 0.0000010083 0.0000009776 0.0000009652 0.0000009604 0.0000009586 0.0000009579
mf = 2 case = 3 reltol = 1.00e-008 abstol = 1.00e-008 t 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.0000 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
ncall =
y 1.0000 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry 0.0000000000 0.0000000237 0.0000000012 0.0000000128 0.0000000112 0.0000000428 0.0000000092 0.0000000014 -0.0000000231 -0.0000000081 0.0000000077
163
mf = 2 case = 4 reltol = 1.00e-010 abstol = 1.00e-010 t 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
ye 1.0000 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
ncall =
y 1.0000 1.8816 2.3742 2.5863 2.6689 2.7000 2.7116 2.7158 2.7174 2.7179 2.7182
erry 0.0000000000 0.0000000001 -0.0000000001 -0.0000000001 0.0000000003 0.0000000004 -0.0000000004 0.0000000003 0.0000000002 -0.0000000003 0.0000000001
385
We can note the following points about this output: • Generally the output indicates that the relative error tolerances specified in the call to function options have been met. For example, for the first solution:
Copyright 2004 by Chapman & Hall/CRC
mf = 1 case = 1 reltol = 1.00e-004 abstol = 1.00e-004 t ye y 0.0 1.0000 1.0000 1.0 1.8816 1.8816 2.0 2.3742 2.3743 3.0 2.5863 2.5864 4.0 2.6689 2.6691 5.0 2.7000 2.7002 6.0 2.7116 2.7117 7.0 2.7158 2.7160 8.0 2.7174 2.7175 9.0 2.7179 2.7181 10.0 2.7182 2.7183 ncall =
erry 0.0000000000 -0.0000020034 -0.0000638807 -0.0000933201 -0.0001103656 -0.0001284242 -0.0001434300 -0.0001552112 -0.0001581733 -0.0001592608 -0.0001592537
73
• An error tolerance reltol = 1.00e–004 means that four figures of accuracy should be achieved in the numerical solution. In all cases, the numerical solution met this tolerance, e.g., 2.0
2.3742
2.3743
− 0.0000638807
indicates an error of −0.000064 or at least four figures in 2.3742. Similarly, absrel = 1.00e–004 indicates an absolute accuracy of 0.0001, and this was nearly achieved, e.g., 10.0
2.7182
2.7183
− 0.0001592537
or an absolute error of −0.00016. The same general conclusions apply to the other seven solutions. For example, with reltol = 1.00e–010, absrel = 1.00e–010, the solution was accurate to nearly ten figures for both m f = 1 and 2. • The apparent computational effort, as measured by the number of derivative evaluations (calls to function ode1 p7.m) differed substantially between ode23 and ode45. mf = 1 case = 4 reltol = 1.00e-010 abstol = 1.00e-010 ncall =
5392
Copyright 2004 by Chapman & Hall/CRC
mf = 2 case = 4 reltol = 1.00e-010 abstol = 1.00e-010 ncall =
385
Thus, the calls to ode45 were less than 1/10 those to ode23. This result illustrates the relative efficiency of the higher-order method ((4, 5) is more efficient than (2, 3)). Note that this is true even though the (2, 3) pair requires the evaluation of k1 , k2 , and k3 while the (4, 5) pair requires the evaluation ot k1 , k2 , k3 , k4 , k5 , and k6 , i.e., each ki evaluation adds one derivative evaluation. • For a general-purpose (library) ODE integrator, the coding for the problem and for the numerical integration algorithm should be separated. In this way, the coding for a new problem can be written, then combined with the coding for the algorithm (so that the algorithm coding remains unchanged). This is illustrated in Program 1.7 in which the problem ODE is defined in a function, in this case named ode1 p7.m, and the algorithms are contained in ode23 and ode45, which remain unchanged from one problem to the next. We shall use this division between problemspecific and general coding in the library routines to be considered subsequently. Note that this division was not used in Programs 1.1 to 1.6; in this sense, Programs 1.3 and 1.6 are the worst examples in that the ODE RHS (of Equation 1.3) was coded repeatedly (each time a ki was computed) rather than coding it just once as in function ode1 p7.m called by Program 1.7. While this repetitive coding of the ODE in Programs 1.1 to 1.6 was not too cumbersome, we can imagine what it would be like, for example, for a 1000x1000 ODE system, with all 1000 ODEs programmed for each ki ! We should also consider briefly the choice of error tolerances (the indiscriminate choice of error tolerances is probably the single most common reason for the failure of numerical library routines such as ode23 and ode45). In this case, there was only one dependent variable, y(t) defined by Equations 1.3 and 1.4. Further, since the range of values of y was approximately 1 ≤ y ≤ 3, the choice of the same value for the relative and absolute tolerances was reasonable (as suggested by the preceding discussion of the tolerances and the resulting accuracy of the solutions). However, the selection of a single tolerance for both the relative and absolute errors, or even the same absolute error tolerance for a problem with more than one dependent variable, is not always appropriate. For example, if we are interested in solving a 2x2 problem, with y1 having a typical value of 1000 (perhaps a temperature) and y2 having a typical value of 0.01 (perhaps a concentration), we might select an absolute error tolerance of 0.1 for y1 (1 part in 10, 000), but this would be entirely too large for y2 (10 parts in 1!), and
Copyright 2004 by Chapman & Hall/CRC
would result in a meaningless numerical solution for y2 , and most likely for y1 as well since the ODEs for y1 and y2 would most certainly be connected, i.e., dy1 = f 1 (y1 , y2 , t) dt dy2 = f 2 (y1 , y2 , t) dt On the other hand, if we select an error tolerance of 0.000001 for y2 (1 part in 10,000), this would be excessively small for y1 (1 part in 1,000,000,000), and would probably result in an excessively long computer run as the method tried to adjust h to meet this overly stringent error tolerance. The solution to this situation might appear to be to select a relative error tolerance such as 0.0001 (0.01% accuracy). However, a relative error is meaningful only if the corresponding dependent variables are not zero anywhere along the solution (but the absolute error criterion would not fail at such points). Thus, some care might also have to be given to the selection of a relative error. In general, the specification of both a relative tolerance and an absolute tolerance might avoid problems with error monitoring and control (automatic selection of h), but, again, different absolute tolerances might have to be selected for different dependent variables, and even different relative tolerances might also have to be selected for different dependent variables (depending on the sensitivity of the solution accuracy to the choice of the relative tolerance). In general, the library ODE integration routines to be considered subsequently will permit the selection of different relative and absolute tolerances for each dependent variable (but, again, appropriate values have to be selected for each dependent variable, and indiscriminate choices without much thought can lead to integrator failures, i.e., the failure to compute a solution with acceptable accuracy, or to even compute any solution). Parenthetically, function options will accept a vector for abstol and thus define an absolute error tolerance for each dependent variable (for the reason explained with the preceding illustration of y1 and y2 having typical values of 1000 and 0.01). However, reltol defined by a call to options will accept only a scalar, so the same relative error tolerance is applied to all of the dependent variables. To conclude this section, we include Program 1.8 for the 2x2 problem of Equations 1.6, 1.16, and 1.17, primarily to illustrate how ode23 and ode45 are used for a problem with more than one ODE. Program 1.8 is listed below: % % Program 1.8 % 2 x 2 system of eqs. (1.6), (1.16), (1.17) % % Global variables global a b; %
Copyright 2004 by Chapman & Hall/CRC
% Model parameters a=5.5; b=4.5; % % Select method for mf=1:2 % % Error tolerances reltol=1.0e-02; abstol=1.0e-02; for ncase=1:4 reltol=1.0e-02*reltol; abstol=1.0e-02*abstol; % % Variables for ODE integration t0=0.0; tf=10.0; tout=[t0:1.0:tf]'; nout=11; % % Initial condition y10=0.0; y20=2.0; y0=[y10 y20]'; % % Call ODE integrator options=odeset('RelTol',reltol,'AbsTol',abstol); if(mf==1)[t,y]=ode23('ode1p8',tout,y0,options); end if(mf==2)[t,y]=ode45('ode1p8',tout,y0,options); end % % Display solution and error fprintf('\n\n mf = %1d\n case = %1d\n reltol = %6.2e\n abstol = %6.2e\n\n',... mf,ncase,reltol,abstol); fprintf(' t y1e y1 erry1\n y2e y2 erry2\n'); for i=1:nout lambda1=-(a-b); lambda2=-(a+b); exp1=exp(lambda1*t(i)); exp2=exp(lambda2*t(i)); y1e=(y10+y20)/2.0*exp1-(y20-y10)/2.0*exp2; y2e=(y10+y20)/2.0*exp1+(y20-y10)/2.0*exp2; erry1=y1e-y(i,1); erry2=y2e-y(i,2);
Copyright 2004 by Chapman & Hall/CRC
fprintf('%5.1f%9.4f%9.4f%15.10f\n %9.4f%9.4f%15.10f\n\n',... t(i),y1e,y(i,1),erry1,y2e,y(i,2),erry2); end % % Next case end % % Next method end % % Plot last solution plot(t,y); xlabel('t') ylabel('y1(t),y2(t)') title(' Program 1.8, 2 x 2 Linear System') gtext('y1(t)'); gtext('y2(t)'); print pro1p8.ps
Program 1.8 Program for the integration of Equations 1.6, 1.16, and 1.17 by the library integrators ode23 and ode45 We can note the following points about Program 1.8: • The constants a and b in Equations 1.6, 1.16, and 1.17 are declared as global, then assigned numerical values: % % Global variables global a b; % % Model parameters a=5.5; b=4.5;
• As in Program 1.7, two methods are used (m f = 1 for ode23 and m f = 2 for ode45). For each of these methods, a set of four error tolerances is used (again, these tolerances are appropriate for y1 and y2 since these variables range over (approximately) 0 ≤ y1 , y2 ≤ 2). % % Select method for mf=1:2 % % Error tolerances reltol=1.0e-02;
Copyright 2004 by Chapman & Hall/CRC
abstol=1.0e-02; for ncase=1:4 reltol=1.0e-02*reltol; abstol=1.0e-02*abstol;
• The variables controlling the integration are the same as in Program 1.7: % %
Variables for ODE integration t0=0.0; tf=10.0; tout=[t0:1.0:tf]'; nout=11;
Thus, the t scales for Programs 1.7 and 1.8 are the same, 0 ≤ t ≤ 10, but clearly the t scale is problem dependent, and thus a final value of t = t f must generally be selected for each new initial value problem. In other words, we must select t f to be large enough to encompass the entire solution, but not too large so that the essential details of the solution are confined to a small interval in t (generally at the beginning of the solution). The selection of an appropriate t scale is particularly important for stiff ODEs, as we shall observe in the next section on stability. • The initial condition is now set as a vector (with two components): % %
Initial condition y10=0.0; y20=2.0; y0=[y10 y20]';
Note that the last statement converts a row vector to a column vector (through the transpose operator, ’) since an initial condition column vector is required by ode23 and ode45. • ode23 and ode45 are called in the same way as in Program 1.7: % %
Call ODE integrator options=odeset('RelTol',reltol,'AbsTol',abstol); if(mf==1)[t,y]=ode23('ode1p8',tout,y0,options); end if(mf==2)[t,y]=ode45('ode1p8',tout,y0,options); end
The only difference is that function ode1 p8 is used to define the ODEs, Equations 1.6 and 1.16. function yt=ode1p8(t,y) % % Set global variables global a b;
Copyright 2004 by Chapman & Hall/CRC
% % ODEs yt(1)=-a*y(1)+b*y(2); yt(2)= b*y(1)-a*y(2); yt=yt';
We can note the following points about ode1 p8.m: — A vector of derivatives, yt, with two elements is computed according to the ODEs, Equations 1.6 and 1.16. In other words, the dependent variable vector, y, is an input to ode1 p8.m (generated by the integrator, ode23 or ode45), and the derivative vector, yt, is the output from ode1 p8.m. — Note also that this output derivative vector must be a column vector (required by ode23 and ode45), so a transpose is taken at the end of ode1 p8.m. • The numerical and exact solutions for y1 and y2 are then displayed (again, the character strings are put on two lines so that they fit on a printed page). % %
Display solution and error fprintf('\n\n mf = %1d\n case = %1d\n reltol = %6.2e\n abstol = %6.2e\n\n',... mf,ncase,reltol,abstol); fprintf(' t y1e y1 erry1\n y2e y2 erry2\n'); for i=1:nout lambda1=-(a-b); lambda2=-(a+b); exp1=exp(lambda1*t(i)); exp2=exp(lambda2*t(i)); y1e=(y10+y20)/2.0*exp1-(y20-y10)/2.0*exp2; y2e=(y10+y20)/2.0*exp1+(y20-y10)/2.0*exp2; erry1=y1e-y(i,1); erry2=y2e-y(i,2); fprintf('%5.1f%9.4f%9.4f%15.10f\n %9.4f%9.4f%15.10f\n\n',... t(i),y1e,y(i,1),erry1,y2e,y(i,2),erry2); end
In computing the exact solutions, the two eigenvalues λ1 and λ2 of Equations 1.16 are first computed (lambda1 and lambda2). The exponentials in Equations 1.17 corresponding to these eigenvalues are then computed (exp1 and exp2). Finally, the exact analytical solutions,
Copyright 2004 by Chapman & Hall/CRC
Equations 1.17, are programmed (y1e and y2e), and then the corresponding truncation errors, erry1 and erry2, are computed. • Note also that ode23 and ode45 actually return a matrix as the solution, y, consisting of nout rows (for the nout values of t), and two columns (for the two dependent variables, y1 and y2 ). This matrix is then used in the output lines: erry1=y1e-y(i,1); erry2=y2e-y(i,2); fprintf('%5.1f%9.4f%9.4f%15.10f\n %9.4f%9.4f%15.10f\n\n',... t(i),y1e,y(i,1),erry1,y2e,y(i,2),erry2);
where i is set by the for statement for i=1:nout
• After the two for loops (which set m f and ncase) are finished, the solution is plotted (corresponding to m f = 2 and ncase = 4): % % Next case end % % Next method end % % Plot last solution plot(t,y); xlabel('t') ylabel('y1(t),y2(t)') title(' Program 1.8, 2 x 2 Linear System') gtext('y1(t)'); gtext('y2(t)'); print pro1p8.ps
Note that function plot is able to accept the matrix y directly since it checks for the correct number of rows in t (a column vector with nout rows). The resulting plot is then written to the Postscript file pro1 p8. ps for storage. The output from Program 1.8 is abbreviated below (to avoid excessive printed output) for m f = 1 and 2, nca se = 1 and 4:
Copyright 2004 by Chapman & Hall/CRC
mf = 1 case = 1 reltol = 1.00e-004 abstol = 1.00e-004 t
y1e y2e 0.0000 2.0000
y1 y2 0.0000 2.0000
erry1 erry2 0.0000000000 0.0000000000
1.0
0.3678 0.3679
0.3679 0.3679
-0.0000179424 0.0000643053
2.0
0.1353 0.1353
0.1353 0.1353
0.0000767180 0.0000769485
3.0
0.0498 0.0498
0.0497 0.0497
0.0001011036 0.0001052134
4.0
0.0183 0.0183
0.0182 0.0182
0.0000911283 0.0000835132
5.0
0.0067 0.0067
0.0068 0.0066
-0.0000336414 0.0001147145
6.0
0.0025 0.0025
0.0026 0.0023
-0.0001634482 0.0001981412
7.0
0.0009 0.0009
0.0010 0.0008
-0.0001346866 0.0001489110
8.0
0.0003 0.0003
0.0004 0.0002
-0.0001107129 0.0001164729
9.0
0.0001 0.0001
0.0002 0.0000
-0.0001063790 0.0001086973
10.0
0.0000 0.0000
0.0001 0.0000
-0.0000217537 0.0000226717
0.0
Copyright 2004 by Chapman & Hall/CRC
mf = 1 case = 4 reltol = 1.00e-010 abstol = 1.00e-010 t
y1e y2e 0.0000 2.0000 0.3678 0.3679
y1 y2 0.0000 2.0000 0.3678 0.3679
erry1 erry2 0.0000000000 0.0000000000 0.0000000000 0.0000000000
2.0
0.1353 0.1353
0.1353 0.1353
0.0000000001 0.0000000001
3.0
0.0498 0.0498
0.0498 0.0498
0.0000000001 0.0000000001
4.0
0.0183 0.0183
0.0183 0.0183
0.0000000001 0.0000000001
5.0
0.0067 0.0067
0.0067 0.0067
0.0000000001 0.0000000001
6.0
0.0025 0.0025
0.0025 0.0025
0.0000000001 0.0000000001
7.0
0.0009 0.0009
0.0009 0.0009
0.0000000001 0.0000000001
8.0
0.0003 0.0003
0.0003 0.0003
0.0000000001 0.0000000001
9.0
0.0001 0.0001
0.0001 0.0001
0.0000000001 0.0000000001
10.0
0.0000 0.0000
0.0000 0.0000
0.0000000001 0.0000000001
0.0 1.0
Copyright 2004 by Chapman & Hall/CRC
mf = 2 case = 1 reltol = 1.00e-004 abstol = 1.00e-004 t
y1e y2e 0.0000 2.0000
y1 y2 0.0000 2.0000
erry1 erry2 0.0000000000 0.0000000000
1.0
0.3678 0.3679
0.3678 0.3679
-0.0000030880 0.0000032018
2.0
0.1353 0.1353
0.1353 0.1353
0.0000102110 -0.0000087609
3.0
0.0498 0.0498
0.0498 0.0498
0.0000169871 -0.0000174932
4.0
0.0183 0.0183
0.0183 0.0183
0.0000136219 -0.0000139397
5.0
0.0067 0.0067
0.0068 0.0067
-0.0000481707 0.0000480671
6.0
0.0025 0.0025
0.0025 0.0025
0.0000142956 -0.0000143637
7.0
0.0009 0.0009
0.0009 0.0009
-0.0000255897 0.0000255619
8.0
0.0003 0.0003
0.0003 0.0004
0.0000202537 -0.0000202664
9.0
0.0001 0.0001
0.0001 0.0001
0.0000139694 -0.0000139749
10.0
0.0000 0.0000
0.0000 0.0001
0.0000145650 -0.0000145673
0.0
Copyright 2004 by Chapman & Hall/CRC
mf = 2 case = 4 reltol = 1.00e-010 abstol = 1.00e-010 t
y1e y2e 0.0000 2.0000
y1 y2 0.0000 2.0000
erry1 erry2 0.0000000000 0.0000000000
1.0
0.3678 0.3679
0.3678 0.3679
0.0000000000 0.0000000000
2.0
0.1353 0.1353
0.1353 0.1353
0.0000000000 0.0000000000
3.0
0.0498 0.0498
0.0498 0.0498
0.0000000000 0.0000000000
4.0
0.0183 0.0183
0.0183 0.0183
0.0000000000 0.0000000000
5.0
0.0067 0.0067
0.0067 0.0067
0.0000000000 0.0000000000
6.0
0.0025 0.0025
0.0025 0.0025
0.0000000000 0.0000000000
7.0
0.0009 0.0009
0.0009 0.0009
0.0000000000 0.0000000000
8.0
0.0003 0.0003
0.0003 0.0003
0.0000000000 0.0000000000
9.0
0.0001 0.0001
0.0001 0.0001
0.0000000000 0.0000000000
10.0
0.0000 0.0000
0.0000 0.0000
0.0000000000 0.0000000000
0.0
Generally we can conclude that the error monitoring and control (automatic h adjustment) worked as expected for both ode23 and ode45. The plot for m f = 2, ncase = 4 appears in Figure 1.5. We can note in Figure 1.5 the initial conditions y1 (0) = 0, y2 (0) = 2 of Equations 1.6; as the solution evolves, the
Copyright 2004 by Chapman & Hall/CRC
Program 1.8, 2 x 2 Linear System
2 1.8 1.6
y1(t),y2(t)
1.4 1.2 y2(t) 1 0.8 0.6 0.4 0.2 0
y1(t) 0
1
2
3
4
5
6
7
8
9
10
t FIGURE 1.5
Solution of Equations 1.6, 1.16, from Program 1.8, m f = 2, ncase = 4.
two components, y1 (t), y2 (t) come together at approximately t = 1, then follow a common path. This is consistent with the analytical solution, Equation 1.17. At t = 1 the exponential e λ2 t = e −(a +b)t = e −(5.5+4.5)t = e −10t decays to insignificance in both y1 (t) and y2 (t); the two solutions then decay according to the exponential e λ1 t = e −(a −b)t = e −(5.5−4.5)t = e −t , and we need to compute to t = 10 for this exponential to fully decay. This is a common feature of the solution to simultaneous linear ODEs. In particular: • There is an initial interval, e.g., 0 ≤ t ≤ 1, in which all of the exponentials (for all of the ODE eigenvalues) are significant. However, the exponentials with the largest eigenvalues, in this case λ2 = −10 decay rapidly. • If the eigenvalues are complex, then the magnitudes of their real parts determine how long this initial transient or boundary layer persists (and, of course, the real parts should all be negative for the ODE system to be stable; if any of the eigenvalues have positive real parts, the corresponding exponentials will grow with increasing t). • Once this initial transient is past, the solutions decay according to the smallest eigenvalues, in this case λ1 = −1, which defines the total t scale of the solution to be approximately 0 ≤ t ≤ 10 (as reflected in Figure 1.5).
Copyright 2004 by Chapman & Hall/CRC
• If the eigenvalues are widely separated (a stiff system), the initial transient determined by the largest eigenvalues will be very short (and thus will require many small integration steps for an accurate solution in this interval). However, the total length or scale of the solution in t will be determined by the smallest eigenvalues, but small steps will still have to be taken throughout the entire solution to maintain stability with a nonstiff (explicit) integrator such as the ones we have considered so far. It is this combination of small h to cover a large interval in t that makes stiff ODE systems relatively difficult to solve numerically with a nonstiff integrator. We now consider some of the stability properties of ODE integrators for stiff and nonstiff ODEs. In other words, in addition to the previous consideration of accuracy, typically in the form of the order conditions, e.g., O(h p ), p = 1, 2, 3, 4, 5, we must also consider a second important limitation of numerical integration algorithms, their stability, which is discussed in the next, and final, section of this chapter.
1.7
Stability of RK Methods
So far, we have assumed that h will somehow be selected (either manually as in Programs 1.1 to 1.6, or automatically as in Programs 1.7 and 1.8) so as to achieve a numerical ODE solution of acceptable accuracy. Thus accuracy, at least so far in this discussion, has determined h. However, there are situations for which h must be reduced to a level that ensures a stable numerical solution, and this restriction on h will occur at a smaller value of h than that determined by accuracy. The class of problems for which stability limits h is termed stiff. We start the discussion of stability by considering the model ODE (Equation 1.22) dy/dt = λy, y(0) = y0 , where we have chosen real(λ), < 0 so that the solution y(t) = y0 e λt
(1.52)
is stable (decays exponentially with t). If we apply the Euler method, Equation 1.19 or 1.28, to this system, starting at the initial condition y(0) = y0 , y1 = y0 +
dy0 h = y0 + (λy0 )h = y0 (1 + λh) dt
For the next step from y1 to y2 y2 = y1 +
dy1 h = y1 + (λy1 )h = y1 (1 + λh) = y0 (1 + λh)(1 + λh) = y0 (1 + λh)2 dt
Copyright 2004 by Chapman & Hall/CRC
In general, after n steps to go from y0 to yn yn = y0 (1 + λh)n
(1.53)
We can consider how this Euler solution (Equation 1.53) compares with the exact solution, Equation 1.52: λh = −2
n 1 2 3 λh = −3 n 1 2 3 λh = −0.5 n 1 2 3
yn y1 = y2 = y3 = yn y1 = y2 = y3 = yn y1 = y2 = y3 =
y0 (1 − 2) = −y0 y1 (1 − 2) = −y0 (−1) = y0 y2 (1 − 2) = y0 (−1) = −y0 , etc. y0 (1 − 3) = −2y0 y1 (1 − 3) = −2y0 (−2) = 4y0 y2 (1 − 3) = 4y0 (−2) = −8y0 , etc. y0 (1 − 0.5) = 0.5y0 y1 (1 − 0.5) = 0.5y0 (0.5) = 0.25y0 y2 (1 − 0.5) = 0.25y0 (0.5) = 0.125y0 , etc.
When |λh| = 2 (i.e., λh = −2), the solution oscillates between y0 and −y0 (when it should decay according to Equation 1.52). When |λh| > 2 (i.e., λh = −3), the solution grows in amplitude from y0 to −2y0 to 4y0 , etc. (the solution is unstable). However, when |λh| < 2 ( i.e., λh = −0.5), the solution decays (and is therefore stable). Thus, |λh| = 2 is the stability limit of the Euler method when applied to this model problem (Equation 1.22). Also, since the eigenvalues of ODEs can, in general, be complex, the stability criterion |λh| = 2 defines a circle in the complex plane with center at (−1, i0) and unit radius as illustrated in Figure 1.6. Note, in particular, the stability interval −2 ≤ λh ≤ 0 along the negative real axis when λ is real and negative (corresponding to a stable solution from Equation 1.52), and h is positive (the case we have considered, although the previous integration methods are valid for negative h corresponding to integration in the direction of decreasing t). We can establish the stability region of Figure 1.6 for the Euler method (the interior of the circle is the stable region) by plotting a series of points in the complex plane corresponding to a series of values of λh. Consider the stepping formula for the Euler method from point yi to point yi+1 , Equation 1.19. If the numerical solution from this stepping formulas is to be stable, we require yi+1 y ≤1 i In other words, the absolute value of the solution at i + 1 should be less than or equal to the absolute value at i as, for example, in the exponential decay of Equation 1.52 (the ratio |yi+1 /yi | is generally called the amplification factor or stability function, and it should be less than one for a stable solution).
Copyright 2004 by Chapman & Hall/CRC
Im(λh)
Exterior of circle is unstable region 1
(–
1
–1) + i
2
2
, θ = 3π/4
–1 + i1,θ = π/2 (
Interior of circle is stable region
1 2
–1) – i
1 2
2
–1) + i
1 2
,θ = π/4
Re(λh)
–2 + i0,θ = π
(–
1
0 + i0,θ = 0
–1 + i0
,θ = 5π/4
(
Circle of unit radius centered at –1 + i0
1 2
–1) –i
1 2
,θ = 7π/4
–1 –i1,θ = 3π/2
FIGURE 1.6
The stability region of the Euler method.
For the Euler method applied to the model ODE dy/dt = λy, y(0) = y0 (Equation 1.22) yi+1 = yi +
dyi h = yi + (λyi )h = yi (1 + λh) dt
or yi+1 = 1 + (λh) yi For this ratio to have an absolute value of 1, even for complex λ, we require 1 + (λh) = e iθ e iθ is a complex variable with unit magnitude, i.e., e iθ = 1. Since e iθ = cos θ + i sin θ (the Euler identity), 1 + Re(λh) + i Im(λh) = cos θ + i sin θ so that Re(λh) = cos θ − 1 Im(λh) = sin θ
Copyright 2004 by Chapman & Hall/CRC
We can determine the values of Re(λh) and Im(λh) for a selected sets of values of θ θ
eiθ
0 π/4 π/2 3π/4 π 5π/4 3π/2 7π/4 2π
√ 1 + i0 √ 1/ 2 + i(1/ 2) √0 + i1 √ −1/ 2 + i(1/ 2) √−1 + 0i √ −1/ 2 − i(1/ 2) √ 0 − i1 √ 1/ 2 − i(1/ 2) 1 + i0
λh √ 0 + i0 √ (1/ 2 − 1) + i(1/ 2) √ −1 + i1 √ (−1/ 2 − 1) + i(1/ 2) √ −2 + i0 √ (−1/ 2 − 1) − i(1/ 2) √ −1 − i1 √ (1/ 2 − 1) − i(1/ 2) 0 + i0
If Re(λh) is plotted vs. Im(λh), the resulting figure is a circle, centered at (−1, i0) with unit radius (see Figure 1.6). If h is chosen so that the complex point λh falls outside the circle, the numerical solution will be unstable (since |yi+1 /yi | > 1). Thus, a stability limit is placed on h for the explicit Euler method. Usually, the accuracy requirement will set the step h to a value smaller than for |λh| = 2, as discussed previously. However, there is an exception to this conclusion. Consider the 2x2 system of Equations 1.6 and the analytical solution, Equation 1.17, for the special case y1 (0) = 0, y2 (0) = 2 y1 (t) = e λ1 t − e λ2 t
(1.54a)
y2 (t) = e λ1 t + e λ2 t
(1.54b)
for which the eigenvalues are λ1 = −(a −b), λ2 = −(a +b) as noted previously (recall again how Equations 1.54 appear numerically in Figure 1.5 for a = 5.5, b = 4.5 and λ1 = −1, λ2 = −10). We now consider some additional particular values for a and b Values of a, b
Values of λ1 , λ2
Case 1 a = 50.5
λ2 = −100
b = 49.5
λ1 = −1
Case 2 a = 500.5
λ2 = −1000
b = 499.5
λ1 = −1
Case 3 a = 500, 000.5
λ2 = −1, 000, 000
b = 499, 999.5
λ1 = −1
Copyright 2004 by Chapman & Hall/CRC
|λ2 | and Description |λ1 | |λ2 | = 100 |λ1 | nonstiff |λ2 | = 1000 |λ1 | moderately stiff |λ2 | = 1, 000, 000 |λ1 | stiff
Consider the maximum Euler step for the stiff case. If λ2 = −1, 000, 000, the maximum stable step is given by |λh| = 2 or h = 2/1, 000, 000 = 0.000002. However, to compute a complete solution, we require a final t given approximately by λ1 t ≈ −10 (or t = 10 so that exp(λ1 t) = exp(−10) has decayed to insignificance compared to the initial condition y2 (0) = 2). Thus, we must take 10/0.000002 = 5 × 106 steps! If this does not seem like a large number of steps, consider a = 500, 000, 000.5, b = 499, 999, 999.5 for which the ratio |λmax | |λ2 | |largest eigenvalue| = = = 109 |λmin | |λ1 | |smallest eigenvalue| and 5 × 109 steps would be required to compute a complete solution (physical problems in which this stiffness ratio = |λmax |/|λmin | = 1012 to 1015 are not unusual). As an incidental point, note that the calculation of λ1 requires a subtraction, λ1 = −(a − b). If a and b are nearly equal, e.g., a = 500, 000.5, b = 499, 999.5, then this subtraction might be done with substantial error. For example, if the machine precision (often termed the machine epsilon or unit roundoff) is 10−7 (one part in 107 ) corresponding to 32-bit arithmetic, this stiff ODE system could not be integrated numerically since the calculation of (a − b) requires a precision better than more than one part in 107 . Although this is a heuristic argument, generally the conclusion is correct, i.e., stiff systems require a precision that is substantially better than set by |λmax |/|λmin | (1, 000, 000 in the preceding example). As an example of available precision, the machine epsilon for MATLAB and Java is approximately 10−15 so that an ODE system with a stiffness ratio approaching 1015 can be accommodated with these systems. Thus, if we require the solution to a system of stiff ODEs, we should not use the Euler method (because of the stability limit |λh| = 2). We might consider a higher-order method, e.g., the modified Euler method of Equation 1.29, the classical fourth-order RK method of Equations 1.50, but if we do a similar stability analysis, we arrive at a stability limit that is not much greater than for the Euler method. For example, consider application of the fourth-order RK of Equations 1.50 to the model ODE (Equation 1.22) dy/dt = λy, y(0) = y0 , where we have chosen real(λ) < 0 so that the solution, Equation 1.52 (or yi e λh for one step h as noted after Equation 1.22), is stable. The RK constants for the model problem are k1 = f (yi , ti )h = λyi h k2 = f (yi + k1 /2, ti + h/2)h = λ(yi + λhyi /2)h k3 = f (yi + k2 /2, ti + h/2)h = λ[yi + λ(yi + λhyi /2)h/2]h k4 = f (yi + k3 , ti + h)h = λ{yi + λ[yi + λ(yi + λhyi /2)h/2]h}h
Copyright 2004 by Chapman & Hall/CRC
and the stepping formula is yi+1 = yi + (1/6)(k1 + 2k2 + 2k3 + k4 ) = yi + (1/6)[λhyi + 2λ(yi + λhyi /2)h + 2λ[yi + λ(yi + λhyi /2)h/2]h +λ{yi + λ[yi + λ(yi + λhyi /2)h/2]h}h] = yi {1 + (1/6)[(1 + 2 + 2 + 1)(λh) + (1/6)(1 + 1 + 1)(λh)2 +(1/6)(1/2 + 1/2)(λh)3 + (1/6)(1/4)(λh)4 ]} = yi (1 + (λh)/1! + (λh)2 /2! + (λh)3 /3! + (λh)4 /4!)
(1.55a)
Since the exact solution to the model problem is y = y0 e λt for the distance t = h, the solution changes from yi to yi+1 according to the exact solution yi+1 = yi e λh = yi (1 + (λh)/1! + (λh)2 /2! + (λh)3 /3! + (λh)4 /4! + · · ·)
(1.55b)
Thus, the RK stepping formula fits the Taylor series solution of the exact solution up to and including the (λh)4 term (up to and including the (d 4 yi /dt 4 )(h 4 /4!) term), as expected (compare Equations 1.55). Also, although the exact solution, Equation 1.55b, is stable for λ < 0, the approximate solution, Equation 1.55a, is not necessarily stable (because of the truncation). In fact, we can chose h large enough to make the solution of Equation 1.55a unstable. Again, if the numerical solution is to remain stable, we require |yi+1 /yi | ≤ 1. Thus, for the limiting value |yi+1 /yi | = 1 1 + (λh)/1! + (λh)2 /2! + (λh)3 /3! + (λh)4 /4! = 1 (1.55c) and we can consider what values of λh will satisfy Equation 1.55c. Clearly real(λh) < 0. If λh = −2.785, 1 + (−2.785)/1! + (−2.785)2 /2! + (−2.785)3 /3! + (−2.785)4 /4! = 1 1 − 2.785 + 3.878 − 3.600 + 2.507 = 0.9996 ≈ 1 Thus, in place of the stability criterion for the Euler method, |λh| = 2, we have for the fourth-order RK method along the negative real axis |λh| = 2.785. In other words, the stability is not improved very much by going to a higherorder RK (or, in general, an explicit) method. For example, for the stiff case a = 500, 000.5, b = 499, 999.5 considered previously, for which the maximum stable step for the Euler method was h = 0.000002, the maximum stable step is now h = 0.000002785, and the required number of steps for a complete solution is reduced from 10/0.000002 = 5.0 × 106 to 10/0.000002785 = 3.59 × 106 (not a very significant reduction, particularly when we recall that the classical fourth-order RK of Equations 1.50 requires four derivative evaluations for step h while the Euler method requires only one derivative evaluation).
Copyright 2004 by Chapman & Hall/CRC
We could also repeat the previous calculations for the Euler method to determine the stability region for the fourth-order RK method that is analogous to the circle in Figure 1.6, that is, by finding the values of λh that satisfy 1 + (λh)/1! + (λh)2 /2! + (λh)3 /3! + (λh)4 /4! = e iθ
(1.55d)
This calculation is a little more involved than for the Euler method, so we merely state that the resulting stability diagram is in Schiesser,4 p. 157; Fortran and MATLAB program to calculate the boundary of the stability region is available from the authors (W.E.S.). In summary, any explicit method will have a stability limit similar to the Euler method. So we might logically pose the question, “How do we efficiently compute the solution to a stiff ODE system?” The answer is that we must use an implicit algorithm (which generally will have a much larger stability region than an explicit method). Another way we can understand the stability limit of the explicit Euler method, Equation 1.19 (in addition to the preceding analysis which led to |λh| = 2), is to recall the projection along a straight line in Figure 1.1. Specifically, if any eigenvalue of an ODE system is large, the solution changes rapidly (with respect to t), and the projection will therefore be highly inaccurate in going from yi to yi+1 unless h is very small. One way to avoid this required use of a small h is to evaluate the derivative dy/dt at i + 1 rather than at i, i.e., to use the implicit Euler method yi+1 = yi +
dyi+1 h dt
(1.56)
To show that using Equation 1.56 will be effective for stiff ODEs, we can repeat the preceding stability analysis for the ODE dy/dt = λy, y(0) = y0 , where we have again chosen real(λ) < 0 so that the exact solution y(t) = y0 e λt is stable (decays exponentially with t). If we apply the implicit Euler method, Equation 1.56, to this system, y1 = y0 +
dy1 1 h = y0 + (λy1 )h or y1 /y0 = dt 1 − λh
For the next step y2 = y1 +
dy2 y2 1 = h = y1 + (λy2 )h or dt y1 1 − λh
Then for two steps y2 y2 y1 = = y0 y1 y0
1 1 − λh
1 1 − λh
=
1 1 − λh
2
In general, after n steps yn = y0
Copyright 2004 by Chapman & Hall/CRC
1 1 − λh
n (1.57)
We can again consider how this Euler solution (Equation 1.57) compares with the exact solution, y(t) = y0 e λt . A few specific values of the numerical solution are computed as before λh = −2
n 1
yn y1 = y0 /(1 + 2) = y0 /3
2
y2 = y1 /(1 + 2) = y0 (1/3)2
3
y3 = y2 /(1 + 2) = y0 (1/3)3 etc.
Note that the solution decays in contrast to the explicit Euler method. This decay will occur no matter how large h is (in fact, the decay is faster with increasing h), so the implicit Euler method has no limit on λh with respect to stability, i.e., the implicit Euler method is unconditionally stable. The step h is therefore only limited by accuracy. However, there is generally a price to be paid for the enhanced stability of implicit methods. If the model equation is dy = f (y, t) dt and f (y, t) is nonlinear, then application of the implicit Euler method, Equation 1.56, gives yi+1 = yi +
dyi+1 h = yi + f (yi+1 , ti+1 )h dt
Note that the solution at the advanced point, yi+1 , now appears on both sides of the stepping formula, and we therefore must solve a nonlinear equation to compute yi+1 . Thus, the price we pay in general when using implicit methods is the solution of systems of nonlinear (algebraic) equations. For stiff ODEs, the additional effort of solving systems of nonlinear equations is usually well worthwhile since much larger integration steps are possible because of the improved stability characteristics of implicit methods (for example, for the previous stiff problem with λ1 = −1, λ2 = −1, 000, 000, rather than 5 × 106 required steps for the explicit Euler method, probably a few hundred steps would be sufficient with the implicit Euler method to achieve reasonable accuracy since stability is not an issue; thus orders-of-magnitude reductions in the number of integration steps can be achieved by using an implicit integrator). Conversely, using a stiff (implicit) integrator on a nonstiff problem will waste computer time since the solution of nonlinear equations is unnecessary (as we observed in Programs 1.1 to 1.6). We concluded the implicit Euler method is unconditionally stable, but it also has low accuracy (it is first order). Thus, we seek integration algorithms that have good stability and good accuracy. A widely used implicit method for stiff ODEs that has good combined stability and accuracy is based on the backward differentiation formulas (BDFs), which have the following general
Copyright 2004 by Chapman & Hall/CRC
form (References 5 and 6, pp. 183–184). The BDF stepping formula is α0 yi+1 + α1 yi + · · · + αν yi−ν+1 = h f (yi+1 ti+1 )
(1.58)
for dy/dt = f (y, t), where ν is the order of the method (that defines the coefficients in a particular row of the following table of coefficients) ν
α0
α1
α2
α3
α4
α5
1 2 3 4 5 6
1 3/2 11/6 25/12 137/60 147/60
−1 −2 −3 −4 −5 −6
1/2 3/2 3 −10/3 15/2
−1/3 −4/3 5/4 −20/3
1/4 −1/5 15/4
−6/5
α6
1/6
Note that the solution at the advanced point, yi+1 , appears on both sides of the stepping formula, Equation 1.58, so that in general the calculation of yi+1 requires the solution of a nonlinear equation (if f (y, t) from the ODE is nonlinear), or systems of nonlinear algebraic equations for the nxn ODE problem. The first-order BDF method (ν = 1) is just the implicit Euler method, Equation 1.56; note the weighting coefficients α0 = 1, α1 = −1) from the preceding table for which Equation 1.58 can be written as α0 yi+1 + α1 yi = h f (yi+1 ti+1 ) or (1)yi+1 + (−1)yi = h f (yi+1 ti+1 ) which is the implicit Euler method, Equation 1.56. As we noted previously, the implicit Euler method is stable over the entire left half of the complex plane. However, it has limited accuracy; recall from Figure 1.1 that the Euler method is based on a first-order polynomial (linear approximation to the ODE solution), which is the case for both the explicit and implicit Euler methods (both are O(h)). For ν = 2, . . . , 6, the BDF methods are based on second- to sixth-order polynomial approximations of the ODE solution, which accounts for their good accuracy. The BDF methods are implemented in the MATLAB routines ode23s and ode15s (presumably the “s” in these names denotes “stiff”). State-of-the-art implementations of the BDFs are available in the routines LSODE, LSODES, and DASSL (References 4, 7, pp. 55–64, and 8) that vary both ν and h automatically (termed variable order-variable step implementations). The stability properties of the BDFs are summarized by their stability diagrams, which are presented in Reference 4, p. 163 (Fortran and MATLAB programs for calculating the stability boundaries are available from the authors
Copyright 2004 by Chapman & Hall/CRC
(W.E.S.)). In particular, for ν = 1, the unstable region is within a circle centered at (1, i0) with unit radius. This follows from Equation 1.57 in the same way that we established the stability domain for the explicit Euler method from Equation 1.53. Specifically, • If λh = (1, i0) (real 1), there will be a division by zero in Equation 1.57. This demonstrates why the circle of instability for the implicit Euler method is centered at (1, 0i). • As we move away from (1, 0i), the instability remains until we reach the boundary of the circle where the implicit Euler method switches from unstable to stable (the amplification factor from Equation 1.57, yi+1 1 = y 1 − λh i switches from >1 to <1). • Since we are discussing the right half of the complex plane, for which λ has a positive real part, the exact solution to the ODE is unstable (grows exponentially with increasing t). This is an unusual situation in most applications of ODEs (e.g., unstable physical systems); in other words, the left half of the complex plane (where the real parts of all the ODE eigenvalues are negative) is usually of primary interest in applications. For the left half plane, the implicit Euler method is unconditionally stable. • For the BDF order ν > 2, the BDF methods have a region along the imaginary axis in the left half plane for which these methods are unstable. • This region of instability in the left half plane becomes larger with increasing ν. In summary, the entire left half of the complex plane is unconditionally stable for ν ≤ 2, but a portion of the left half plane along the imaginary axis is unstable for ν ≥ 3, and this unstable region increases in size with increasing ν. Thus, stiff ODEs with complex eigenvalues that fall close to the imaginary axis may cause long computer runs due to the limited stability along the imaginary axis (for this case, limiting ν to 2 can often increase the computational efficiency substantially, even though the order is relatively low). All of the BDFs for ν ≤ 6 are unconditionally stable along the negative real axis. These properties are readily observed in the BDF stability diagrams cited previously.4 We should also note that the BDF stepping formula, Equation 1.58, requires a series of values prior to yi+1 , i.e., yi , yi−1 , . . . , yi−ν+1 . At the beginning of the solution, when only one value is available, the initial condition y0 , we cannot use Equation 1.58 unless we start with the first-order method corresponding to ν = 1. This will permit the calculation of y1 and now we have two past values to calculate y2 , etc. Thus, we must build up the required past values starting with the ν = 1 method (the implicit Euler method). In other words, the BDFs for ν > 1 are not self-starting.
Copyright 2004 by Chapman & Hall/CRC
On the other hand, the RK methods are self-starting since their stepping formulas involve just yi to calculate yi+1 . Thus, we can use RK methods to calculate the first several values required by the BDF methods. However, for stiff problems, this requires the use of implicit RK methods (which we have not considered in the preceding discussion). Implicit RK methods are available, and have been implemented in a quality code, RADAU5.9 Finally, an extensive library of quality public domain scientific software is available from the Internet.10 This library includes the source code for an extensive set of ODE integrators, including LSODE, LSODES, and DASSL. Since stiff ODE systems are an important class of initial value ODE problems, additional discussion of stiff integrators is given in Appendix C. In particular, • The 2x2 system of Equations 1.6 for the stiff case a = 500, 000.5, b = 499, 999.5 is integrated by a fixed-step prototype BDF integrator for ν = 1 (implicit Euler method) using MATLAB. This prototype illustrates the solution of the nonlinear equations required by an implicit integrator using Newton’s method. MATLAB programs for ν = 2, 3 are available from the authors (W.E.S.). • The same 2x2 problem is integrated using ode23s and ode45s to demonstrate both the stability and accuracy of these integrators. These programs can easily be modified for application to other stiff ODE problems. This completes the introduction to ODE integrators, principally for the explicit RK integrator pairs (1, 2), (2, 3), (2, 4), and (4, 5) that are implemented in the routines to be discussed in the remainder of this book. We start with the solution of the 1x1 ODE system of Equations 1.3, 1.4, and 1.5 using integrators that vary h to achieve a prescribed accuracy, that is, variable step explicit RK integrators.
References 1. Braun, M., Differential Equations and Their Applications, 4th ed., Springer-Verlag, New York, 1993, 52–53. 2. Iserles, A., A First Course in the Numerical Analysis of Differential Equations, Cambridge University Press, Cambridge, U.K., 1996. 3. Shampine, L.F., I. Gladwell, and S. Thompson, Solving ODEs with MATLAB, Cambridge University Press, New York, 2003. 4. Schiesser, W.E., The Numerical Method of Lines: Integration of Partial Differential Equations, Academic Press, San Diego, CA, 1991. 5. Gear, C.W., Numerical Initial Value Problems in Ordinary Differential Equations, Prentice-Hall, Englewood Cliffs, NJ, 1971.
Copyright 2004 by Chapman & Hall/CRC
6. Shampine, L.F., Numerical Solution of Ordinary Differential Equations, Chapman & Hall, New York, 1994. 7. Hindmarsh, A.C., ODEPACK, A systematized collection of ODE solvers, in Scientific Computing, R.S. Stepleman et al., Eds., North-Holland, Amsterdam, 1983, 55–64. 8. Brenan, K.E., S.L. Campbell, and L.R. Petzold, Numerical Solution of Initial-Value Problems in Differential-Algebraic Equations, SIAM, Philadelphia, 1996. 9. Hairer, E., and G. Wanner, Solving Ordinary Differential Equations II: Stiff and Differential-Algebraic Problems, Springer-Verlag, Berlin, 1991. 10. The Netlib index is available from http://www.netlib.org/index.html.
Copyright 2004 by Chapman & Hall/CRC
2 Solution of a 1 x 1 ODE System
In this and subsequent chapters, we consider the programming of (a) the two ODE problems discussed in Chapter 1 and (b) two PDE problems. We start in this chapter with the 1x1 ODE problem of Equations 1.3 and 1.4, with the analytical solution of Equation 1.5. For each problem, we discuss the programming in C, C++, Fortran, Java, Maple, and MATLAB (these programming languages are listed here in alphabetical order, but they generally will be considered in a different order in the subsequent discussion). The intention in providing the solutions of the four test problems is to give the reader enough detail that the programming of other ODE/PDE problems should be facilitated, possibly by using the programs presented as starting points, i.e., templates that can be used for new problems. Also, the programming of a common problem in all six languages should facilitate the conversion from one language to another (through a comparison of the corresponding coding).
2.1
Programming in MATLAB
A main program for the numerical integration of Equation 1.3 subject to initial condition Equation 1.4 is listed below: % % % % % %
Main program ode1x1 computes the numerical solution to the 1 x 1 ODE system by one of six integrators Set integration parameters [neqn,nout,nsteps,int,t0,tf,abserr,relerr]=intpar;
%
Copyright 2004 by Chapman & Hall/CRC
% Initial condition vector [u0]=inital(neqn,t0); % % Output interval tp=tf-t0; % % Compute solution at nout output points for j=1:nout % % Print current solution [out]=fprint(int,neqn,t0,u0); % % Fixed step modified Euler integrator if int == 1 [u]=euler2a(neqn,t0,tf,u0,nsteps); end % % Variable step modified Euler integrator if int == 2 [u]=euler2b(neqn,t0,tf,u0,nsteps,abserr,relerr); end % % Fixed step classical fourth order RK integrator if int == 3 [u]=rkc4a(neqn,t0,tf,u0,nsteps); end % % Variable step classical fourth order RK integrator if int == 4 [u]=rkc4b(neqn,t0,tf,u0,nsteps,abserr,relerr); end % % Fixed step RK Fehlberg (RKF45) integrator if int == 5 [u]=rkf45a(neqn,t0,tf,u0,nsteps); end % % Variable step RK Fehlberg (RKF45) integrator if int == 6 [u]=rkf45b(neqn,t0,tf,u0,nsteps,abserr,relerr); end % % Advance solution t0=tf; tf=tf+tp;
Copyright 2004 by Chapman & Hall/CRC
u0=u; % % Next output end % % End of ode1x1
Program 2.1.1 MATLAB main program for the numerical integration of Equation 1.3 subject to initial condition Equation 1.4 We can note the following points about the preceding program: • Function intpar is called to set the parameters that control the numerical integration of Equation 1.3. % % Set integration parameters [neqn,nout,nsteps,int,t0,tf,abserr,relerr]=intpar;
intpar will be subsequently listed and discussed. • Function inital is then called to set initial condition Equation 1.4 % % Initial condition vector [u0]=inital(neqn,t0);
neqn, the number of first-order ODEs, in this case, 1 for Equation 1.3, was set by the call to intpar and is an input to inital; similarly, the initial value of the independent variable, t0 was set to 0 (zero) by the call to intpar. inital is listed below: function [u0]=inital(neqn,t) % % Function inital sets the initial condition vector % for the 1 x 1 ODE problem % u0(1)=1.0; % % End of inital
Program 2.1.2 Function inital for initial condition Equation 1.4 Note that the initial condition of Equation 1.4 is 1.0. Also, the initial value of the dependent variable is actually in a 1D array or a column vector u0, with a subscript 1. Note that the array is automatically defined by MATLAB, and does not have to be sized in a declarative statement.
Copyright 2004 by Chapman & Hall/CRC
• The interval in the independent variable, tp for the output of the numerical solution is the difference between the final value of the independent variable, t f , set to 1.0 in intpar and the initial value of the independent variable, t0 = 0.0. % % Output interval tp=tf-t0;
• The numerical solution is then computed and displayed at nout output points, with nout set in intpar to 6: % % Compute solution at nout output points for j=1:nout % % Print current solution [out]=fprint(int,neqn,t0,u0);
At the beginning of each pass through this for loop, function fprint is called to display (output) the solution. For the first pass through the loop ( j = 1), the initial conditions set in inital are displayed. Function fprint is subsequently listed and displayed. • Within the for loop, one of six integrators is called using int, which is set in intpar. The values of int and the corresponding integrators are listed below: int
1
2
3
4
5
6
Integration algorithm Discussion in Chapter 1 MATLAB function Fixed step modified Euler Equations 1.26, 1.27, 1.28, 1.29, 1.30 euler 2a Variable step modified Euler Discussion after Equations 1.26 euler 2b Fixed step classical fourth-order RK Equations 1.50, 1.51, Section 1.5 r kc4a Variable step classical fourth-order RK Equations 1.50, 1.51, discussion after Equations 1.26 r kc4b Fixed step Runge Kutta Fehlberg 45 Equations 1.49, Section 1.5 r k f 45a Variable step Runge Kutta Fehlberg 45 Equations 1.49, discussion after Equations 1.26 r k f 45b
Copyright 2004 by Chapman & Hall/CRC
The calls to these six integrators, determined by the value of int, are listed below: % %
% %
% %
% %
% %
% %
Fixed step modified Euler integrator if int == 1 [u]=euler2a(neqn,t0,tf,u0,nsteps); end Variable step modified Euler integrator if int == 2 [u]=euler2b(neqn,t0,tf,u0,nsteps,abserr,relerr); end Fixed step classical fourth order RK integrator if int == 3 [u]=rkc4a(neqn,t0,tf,u0,nsteps); end Variable step classical fourth order RK integrator if int == 4 [u]=rkc4b(neqn,t0,tf,u0,nsteps,abserr,relerr); end Fixed step RK Fehlberg (RKF45) integrator if int == 5 [u]=rkf45a(neqn,t0,tf,u0,nsteps); end Variable step RK Fehlberg (RKF45) integrator if int == 6 [u]=rkf45b(neqn,t0,tf,u0,nsteps,abserr,relerr); end
Each of these ODE integrators are listed and discussed subsequently. • After an interval tp is covered by the numerical integration from one of the six integrators, the solution is used as the starting point (in effect, a new initial condition), for the next interval, tp. Note that although u0 and u are 1D arrays (vectors), MATLAB automatically handles the individual elements of these arrays, i.e., indexing of the elements of these arrays is not required. % %
Advance solution t0=tf;
Copyright 2004 by Chapman & Hall/CRC
tf=tf+tp; u0=u; % % Next output end % % Complete solution computed
The end statement terminates the for loop (i.e., serves as the final statement in the loop to initiate the next pass through the loop). When all nout passes through the loop have been completed, the execution of ode1x1 is complete (in this case, a total of six passes since nout = 6). The setting of the integration control parameters in intpar has been covered to some extent in the preceding discussion. The remaining parameters that are set are indicated in the following listing of intpar: function [neqn,nout,nsteps,int,t0,tf,abserr,relerr]=intpar % % Function intpar sets the parameters to control the % integration of the 1 x 1 ODE system % % Number of first order ODEs neqn=1; % % Number of output points nout=6; % % Maximum number of steps in the interval t0 to tf nsteps=100; % % Integrator int=1; % % Initial, final values of independent variable t0=0.0; tf=1.0; % % Error tolerances abserr=1.0e-05; relerr=1.0e-05; % % End of intpar
Program 2.1.3 Function intpar called in Program 2.1.1 to set the integration parameters
Copyright 2004 by Chapman & Hall/CRC
The code in intpar that was not previously discussed is as follows: • The maximum number of steps that an integrator will take over the interval t0 ≤ t ≤ tf: % % Maximum number of steps in the interval t0 to tf nsteps=100;
• Selection of integrator 1 (euler2a ): % % Integrator int=1;
• Specification of the integration error tolerances: % % Error tolerances abserr=1.0e-05; relerr=1.0e-05;
All of these parameters are used to control the numerical integration of Equation 1.3 by serving as inputs to one of the six integrators (euler2a in this case since int = 1). Function fprint is listed below: function [out]=fprint(ncase,neqn,t,u) % % Function fprint displays the numerical and % exact solutions to the 1 x 1 ODE problem % % Return current value of independent variable % (MATLAB requires at least one return argument) out=t; % % Problem parameters u0=1.0; alpha=1.0; lambda=1.0; % % Print a heading for the solution at t = 0 if(t<=0.0) % % Label for ODE integrator % % Fixed step modified Euler if(ncase==1) fprintf('\n\n euler2a integrator\n\n');
Copyright 2004 by Chapman & Hall/CRC
% %
% %
% %
% %
% %
Variable step modified Euler elseif(ncase==2) fprintf('\n\n euler2b integrator\n\n'); Fixed step classical fourth order RK elseif(ncase==3) fprintf('\n\n rkc4a integrator\n\n'); Variable step classical fourth order RK elseif(ncase==4) fprintf('\n\n rkc4b integrator\n\n'); Fixed step RK Fehlberg 45 elseif(ncase==5) fprintf('\n\n rkf45a integrator\n\n'); Variable step RK Fehlberg 45 elseif(ncase==6) fprintf('\n\n rkf45b integrator\n\n'); end
% % Heading fprintf(' t u1 u1e u1-u1e\n'); % % End of t = 0 heading end % % Numerical and analytical solution output % % Analytical solution ue(1)=u0*exp(lambda/alpha*(1.0-exp(-alpha*t))); % % Difference between exact and numerical solutions diff=u-ue; % % Display the numerical and exact solutions, and their % difference fprintf('%10.2f %10.5f %10.5f %10.5f\n',t,u,ue(1), diff); % % End of fprint
Program 2.1.4 Function fprint called by Program 2.1.1 to display the numerical and analytical solutions to Equations 1.3 and 1.4
Copyright 2004 by Chapman & Hall/CRC
We can note the following points about fprint: • The parameters for the analytical solution, Equation 1.5, are first set: % % Problem parameters u0=1.0; alpha=1.0; lambda=1.0;
• A heading for the output is then displayed at t = 0: % % Print a heading for the solution at t = 0 if(t<=0.0) % % Label for ODE integrator % % Fixed step modified Euler if(ncase==1) fprintf('\n\n euler2a integrator\n\n'); % % Variable step modified Euler elseif(ncase==2) fprintf('\n\n euler2b integrator\n\n'); % % Fixed step classical fourth order RK elseif(ncase==3) fprintf('\n\n rkc4a integrator\n\n'); % % Variable step classical fourth order RK elseif(ncase==4) fprintf('\n\n rkc4b integrator\n\n'); % % Fixed step RK Fehlberg 45 elseif(ncase==5) fprintf('\n\n rkf45a integrator\n\n'); % % Variable step RK Fehlberg 45 elseif(ncase==6) fprintf('\n\n rkf45b integrator\n\n'); end % % Heading fprintf(' t u1 u1e u1-u1e\n');
Copyright 2004 by Chapman & Hall/CRC
% % End of t = 0 heading end
Basically, this heading displays the particular integrator used (through the value of int) and identifies the tabular numerical output. • The analytical solution, Equation 1.5, and the difference between the numerical and analytical solutions are then computed: % % Numerical and analytical solution output % % Analytical solution ue(1)=u0*exp(lambda/alpha*(1.0-exp(-alpha*t))); % % Difference between exact and numerical solutions diff=u-ue;
• Finally, these numerical results are displayed: % % %
Display the numerical and exact solutions, and their difference fprintf('%10.2f %10.5f %10.5f %10.5f \n', t,u,ue(1),diff);
Function derv for Equation 1.3 is listed below in Program 2.1.7. The output from this code is listed below: euler2a integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 1.00000 1.88160 2.37421 2.58627 2.66895 2.70004
u1e 1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
u1-u1e 0.00000 0.00000 0.00000 0.00001 0.00001 0.00001
We note that the integrator euler2a computed a solution accurate to about 0.00001 using nsteps = 100 steps or an integration step of h = 1/100 within each output interval (corresponding to nout = 6 or t = 0, 1, . . . , 5). Note that in this case the error tolerances set in intpar, i.e., abserr = 1.0e–05, relerr = 1.0e– 05 were not used since a fixed step integration is performed in euler2a . If the variable step modified Euler method of euler2b is used (int = 2 set in intpar), the following output is produced:
Copyright 2004 by Chapman & Hall/CRC
euler2b integrator t u1 0.00 1.00000 1.00 1.88159 2.00 2.37421 3.00 2.58626 4.00 2.66895 5.00 2.70003
u1e 1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
u1-u1e 0.00000 -0.00001 0.00000 0.00000 0.00000 0.00001
Note that the error criteria are satisfied by the variable step modified Euler integrator. Similar results can be produced by using int = 3, 4, 5, 6 in intpar. For example, with int = 6, the following output from rkf45b results: rkf45b integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 1.00000 1.88159 2.37421 2.58626 2.66894 2.70002
u1e 1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
u1-u1e 0.00000 0.00000 0.00000 -0.00001 -0.00001 -0.00001
Again, the error tolerances specified in intpar are satisfied. We now consider the coding in each of the six integrators. euler2a is listed below function [u]=euler2a(neqn,t0,tf,u0,nsteps) % % Function euler2a computes an ODE solution by a fixed step % modified Euler method for a series of points along the % solution by repeatedly calling function sseuler for a % single modified Euler step. % % Argument list % % neqn number of first order ODEs % % t0 initial value of independent variable % % tf final value of independent variable % % u0 initial condition vector of length neqn %
Copyright 2004 by Chapman & Hall/CRC
% nsteps number of modified Euler steps % % u ODE solution vector of length neqn after % nsteps steps % % Integration step h=(tf-t0)/nsteps; % % nsteps modified Euler steps for i=1:nsteps % % Modified Euler step [t,u,e]=sseuler(neqn,t0,u0,h); % % Reset base point values for next modified Euler step u0=u; t0=t; % % Next modified Euler step end % % End of euler2a
Program 2.1.5 Integrator euler2a We can note the following points about euler2a • The integration step, h, is set using parameters t0, tf, nsteps set in intpar: % % Integration step h=(tf-t0)/nsteps;
• nsteps modified Euler steps of length h are taken within the for loop by calling function sseuler (single step modified Euler): % % nsteps modified Euler steps for i=1:nsteps % % Modified Euler step [t,u,e]=sseuler(neqn,t0,u0,h);
• The solution from sseuler becomes the initial point for the next modified Euler step:
Copyright 2004 by Chapman & Hall/CRC
% %
Reset base point values for next modified Euler step u0=u; t0=t;
Again, note the use of the MATLAB feature for the arrays u, u0 (no subscripting is required). • The for loop is then terminated, and after nsteps = 100 passes, the modified Euler integration over the interval t0 ≤ t ≤ t f is complete. A return to the main program ode1x1 of Program 2.1.1 then initiates the next interval in the for loop for j = 1 : nout. % % Next modified Euler step end % % nsteps modified Euler steps completed
Function sseuler is an implementation of the modified Euler method discussed in Chapter 1. function [t,u,e] = sseuler(neqn,t0,u0,h) % % Function sseuler computes an ODE solution by the modified % Euler method for one step along the solution (by calls to % derv to define the ODE derivative vector). It also % estimates the truncation error of the solution, and % applies this estimate as a correction to the solution % vector. % % Argument list % % neqn number of first order ODEs % % t0 initial value of independent variable % % u0 initial condition vector of length neqn % % h integration step % % t independent variable % % u ODE solution vector of length neqn after % one modified Euler step % % e estimate of truncation error of the solu% tion vector
Copyright 2004 by Chapman & Hall/CRC
% % Derivative vector at initial (base) point [ut0]=derv(neqn,t0,u0); % % First order (Euler) step u=u0+ut0*h; t=t0+h; % % Derivative at advance point [ut]=derv(neqn,t,u); % % Truncation error estimate e=(ut-ut0)*h/2.0; % % Second order (modified Euler) solution vector u=u+e; % % End of sseuler
Program 2.1.6 Integrator sseuler for a single modified Euler step We can note the following points about sseuler: • The derivative vector at the base point is computed by a call to function derv: % % Derivative vector at initial (base) point [ut0]=derv(neqn,t0,u0);
derv, which sets the RHS of Equation 1.3, is listed below: function [ut]=derv(neqn,t,u) % % Function derv computes the derivative vector % of the 1 x 1 ODE problem % % Problem parameters alpha=1.0; lambda=1.0; % % Derivative vector ut(1)=lambda*exp(-alpha*t)*u(1); % % End of derv
Program 2.1.7 Function derv for the RHS of Equation 1.3
Copyright 2004 by Chapman & Hall/CRC
• Once the derivative at the base point is returned from derv, it is used in a Euler step: % % First order (Euler) step u=u0+ut0*h; t=t0+h;
This code is just an implementation of Equation 1.26a, where, again, we have used the array features of MATLAB in working with u, u0, ut0 (which are all one-element arrays or vectors since we are considering a 1x1 ODE system). • After the solution is computed at the advanced point, the derivative vector is computed at the advanced point by a second call to derv: % % Derivative at advance point [ut]=derv(neqn,t,u);
Note that the second-order modified Euler method requires two derivatives evaluations (calls to derv). In general, higher-order integration algorithms require additional derivative evaluations (this is the computational “price” that is paid for the improved accuracy of higher-order algorithms); also the order of the method, for example, second order for the modified Euler method, and the number of derivative evaluations, in this case two calls to derv do not necessarily have to be the same number; generally, the number of derivative evaluations will be equal to or greater than the order of the algorithm. • The derivative vectors at the base point and the advanced point are then used to estimate the integration (truncation) error according to Equation 1.26b: % % Truncation error estimate e=(ut-ut0)*h/2.0;
This error can be used in a variable step method to determine if the integration error is within the specified error tolerance; in the present case of euler2a , the estimated error is not used for this purpose since euler2a implements a fixed step modified Euler method with h set at the value discussed previously (and passed to sseuler through an argument). • Finally, the estimated error is used to correct the first-order Euler solution to produce the second-order modified Euler solution according to Equation 1.26c: % % Second order (modified Euler) solution vector u=u+e;
Copyright 2004 by Chapman & Hall/CRC
To complete the discussion of this 1x1 problem, fprint is called by euler2a as discussed previously to display the solution at the initial point t = t0 and at subsequent output points (at intervals of t f = 1.0 as set in ode1x1 or t = 0, 1, 2, . . . , 5). We have observed by taking nout = 100 fixed Euler steps, the numerical solution agreed with the analytical solution to an accuracy equal to the error tolerance. This agreement happened, however, by taking nout large enough to achieve the required accuracy. Generally, for ODE problems for which we do not have an analytical solution (usually the case—if we knew the analytical solution, we would not have to compute a numerical solution), we would like the ODE integration algorithm to set the step size h to produce a numerical solution that meets the specified error tolerance. While this may seem impossible (it suggests we know the error, and therefore must have an analytical solution), as we discussed in Chapter 1, we can in fact implement a variable step algorithm that automatically adjusts the integration step to (one hopes) meet the error tolerance. The details for the modified Euler method are discussed in the five-step algorithm discussed after Equation 1.26c. We now consider the code for implementing this algorithm in euler2b: function [u]=euler2b(neqn,t0,tf,u0,nsteps,abserr,relerr) % % Function euler2b computes an ODE solution by a variable % step modified Euler method for a series of points along % the solution by repeatedly calling function sseuler for % a single modified Euler step. The truncation error is % estimated along the solution to adjust the integration % step according to a specified error tolerance. % % Argument list % % neqn number of first order ODEs % % t0 initial value of independent variable % % tf final value of independent variable % % u0 initial condition vector of length neqn % % nsteps maximum number of modified Euler steps % % abserr absolute error tolerance % % relerr relative error tolerance %
Copyright 2004 by Chapman & Hall/CRC
% u ODE solution vector of length eqn after % nsteps steps % % Initial integration step h=(tf-t0)/8.0; % % Minimum allowable step hmin=(tf-t0)/nsteps; % % Start integration t=t0; % % While independent variable is less than the final % value, continue the integration while t <= tf*0.999 % % If the next step along the solution will go past % the final value of the independent variable, set % the step to the remaining distance to the final % value if t+h > tf h=tf-t; end % % Single modified Euler step [t,u,e]=sseuler(neqn,t0,u0,h); % % Flag for the end of the integration nfin1=1; % % Check if any of the ODEs have violated the error % criteria for i=1:neqn if abs(e(i)) > abs(u(i))*relerr+abserr % % Error violation, so integration is not complete. % Reduce integration step because of error violation % and repeat integration from the base point h=h/2.0; nfin1=0; break; end end % % If the current step is less than the minimum % allowable step, set the step to the minimum % allowable value and continue integration from % new base point if h < hmin h=hmin; nfin1=1; end
Copyright 2004 by Chapman & Hall/CRC
% % % % %
% %
% % % %
If there is no error violation, continue the integration from new base point if nfin1 == 1 u0=u; t0=t; Test if integration step can be increased for i=1:neqn if abs(e(i)) > (abs(u(i))*relerr+abserr)/4.0 Integration step cannot be increased nfin1=0; break; end end Increase integration step if nfin1 == 1 h=h*2.0; end Continue for no error violation (nfin1=1) end
% % Continue while end % % End of euler2b
Program 2.1.8 Integrator euler2b euler2b shares several features with euler2a . However, it also has additional coding to implement variable stepping in the modified Euler method. We consider now the details of this additional coding: • The initial integration step, h, is now set (arbitrarily) to 18 of the total output interval tf − t0. Note that this initial integration interval is larger than the constant value set in euler2a (h = (tf − t0)/nout, nout = 100) because we anticipate that euler 2b will generally reduce h only when the error tolerance has been exceeded; in other words, there will be a variable value of h, and we anticipate its value will not be reduced to h = (tf − t0)/nout, nout = 100 in order to meet the error tolerance. % % Initial integration step h=(tf-t0)/8.0; % % Minimum allowable step hmin=(tf-t0)/nsteps;
Copyright 2004 by Chapman & Hall/CRC
% % Start integration t=t0;
However, since euler2b is a library integrator that may be applied to a spectrum of problems with different characteristics, we should also provide a safeguard against having h reduced to excessively small values as it is adjusted in an attempt to satisfy the error tolerance. Therefore, we also define a minimum allowable integration step, hmin, which is given the same value as in euler2a ; this value is arbitrarily selected, and some other value could be used, especially if h is reduced to hmin frequently, in which case a smaller value of hmin would probably be used. Then the independent variable is set to the initial value, t = t0, to start the integration. • The numerical integration continues until the independent variable t reaches the final value, tf. This advancement of the solution is controlled by the while statement: % % While independent variable is less than the final % value, continue the integration while t <= tf*0.999 % % If the next step along the solution will go past % the final value of the independent variable, set % the step to the remaining distance to the final % value if t+h > tf h=tf-t; end
Also, if the remaining distance for t to reach tf is less than the current step h, the step is reduced to tf − t. This ensures that the final step taken by euler2b will be exactly to the next output point. • euler2b then takes a modified Euler step as discussed previously by a call to sseuler: % % % %
Single modified Euler step [t,u,e]=sseuler(neqn,t0,u0,h); Flag for the end of the integration nfin1=1;
Since this step might take the solution to the final value t = tf, a flag is set indicating that the solution may be complete, nfin1 = 1. • The estimated error e returned by sseuler is then used to test if any of the dependent variables violated the error criterion:
Copyright 2004 by Chapman & Hall/CRC
% % %
% % % % %
Check if any of the ODEs have violated the error criteria for i=1:neqn if abs(e(i)) > abs(u(i))*relerr+abserr Error violation, so integration is not complete. Reduce integration step because of error violation and repeat integration from the base point h=h/2.0; nfin1=0; break; end end
Note in the error test that both the absolute and relative error tolerances, abserr and relerr, are used. If an error violation is detected, the integration step is halved and the flag indicating the end of the integration nfin1 is set to zero (the integration is not complete since it will be repeated with one half the previous integration step to improve the accuracy of the solution). Note that if any of the dependent variables violate the error criterion, the integration step is halved, i.e., the for loop is exited through the break. Conversely, if all of the dependent variables meet the error criterion, nfin1 remains at 1 indicating that so far, so good. • If the preceding integration step reduction (halving) has reduced the integration step to or below the minimum allowable value, the integration step is set to the mimimum allowable value, and the integration then proceeds using this minimum value. % % % % %
If the current step allowable step, set allowable value and new base point if h < hmin h=hmin;
is less than the minimum the step to the minimum continue integration from nfin1=1; end
The idea here is that the integrator has done the best it can (reduced h to hmin), and it therefore might as well continue. Of course, at this point an error message could be printed signaling that another solution should probably be computed with a smaller value for hmin for comparison with the current solution. This point illustrates an important advantage of having access to the source code for the integrator; additional coding and features can be added such as an error message warning the user of a possible error violation. • If at this point nfin1 = 1, the integration can proceed by resetting the base solution values equal to the current solution values:
Copyright 2004 by Chapman & Hall/CRC
% % %
If there is no error violation, continue the integration from new base point if nfin1 == 1 u0=u; t0=t;
• Also, before the next step is taken along the solution, there is the possibility that the integration step could be increased. Thus, a test is made to determine if the estimated error for each dependent variable is less than 1 of the error tolerance. If so, the integration step is doubled before the 4 next step along the solution is taken: % %
% %
% % % %
Test if integration step can be increased for i=1:neqn if abs(e(i)) > (abs(u(i))*relerr+abserr)/4.0 Integration step cannot be increased nfin1=0; break; end end Increase integration step if nfin1 == 1 h=h*2.0; end Continue for no error violation (nfin1=1) end
% % Continue while end
Note that if all of the dependent variables have estimated errors less than 1 of the error tolerance, the integration step is doubled. In other words, 4 if any of the dependent variables have an estimated error exceeding 1 of the error tolerance, the integration step is not doubled. Also, the 4 factor of 14 comes from the second-order characteristic of the modified Euler method, i.e., if error = ch 2 , then doubling h will increase error by a factor of four (which would bring the error just up to the specified error tolerance if the estimated error is 14 of the error tolerance). If nfin1 = 1, the integration step is doubled, and the integration proceeds; if nfin1 = 0, the integration proceeds without doubling of the step. In either case, stepping continues until t reaches t f according to the original while. As noted previously, changes and new features can be added to this basic integrator. These could include:
Copyright 2004 by Chapman & Hall/CRC
• Selection of an initial integration step according to an established algorithm • Change in the minimum allowable step, hmin • Application of absolute and relative error tolerances to each dependent variable (so that these error tolerances can be specified for each dependent variable) • A more sophisticated algorithm for increasing the integration step (rather than just doubling) All of these features have been used in various library integrators. However, we have used only basic methods to facilitate understanding the variable stepping in euler2b. As we observed in the numerical output for the 1x1 ODE problem, these methods worked quite well (the error tolerance was maintained), and we have found that generally this is the case. Also, we see that it is possible to adjust the integration step automatically to achieve a prescribed accuracy in the numerical solution without knowing the analytical (exact) solution. This is possible because we are using an error estimate to adjust the integration step rather than the exact error (which generally is unknown). Later we study in a little more detail how h varies for this 1x1 ODE system. This procedure of using an error estimate can be applied to an nxn ODE system. Furthermore, it can be used with a higher-order ODE integrator. To illustrate how this can be done, we now consider the following: 1. A fixed step classical fourth-order Runge Kutta (RK) integrator, rkc4a , as defined by Equations 1.50 and 1.51. 2. A variable step classical fourth-order RK integrator, rkc4b, as defined by Equations 1.50 and 1.51. r kc4a and r kc4b closely parallel euler 2a and euler 2b, respectively. Therefore, in the following discussion, we emphasize only the small differences (generally additional code to increase the order from second to fourth). rkc4a (called by setting int = 3 in function intpar of Program 2.1.3) is listed first: function [u]=rkc4a(neqn,t0,tf,u0,nsteps) % % Function rkc4a computes an ODE solution by a fixed step % classical fourth order RK method for a series of points % along the solution by repeatedly calling function ssrkc4 % for a single classical fourth order RK step. % % Argument list %
Copyright 2004 by Chapman & Hall/CRC
% neqn number of first order ODEs % % t0 initial value of independent variable % % tf final value of independent variable % % u0 initial condition vector of length neqn % % nsteps number of rkc4 steps % % u ODE solution vector of length neqn after % nsteps steps % % Integration step h=(tf-t0)/nsteps; % % nsteps rkc4 steps for i=1:nsteps % % rkc4 step [t,u,e]=ssrkc4(neqn,t0,u0,h); % % Reset base point values for next rkc4 step u0=u; t0=t; % % Next rkc4 step end % % End of rkc4a
Program 2.1.9 Integrator rkc4a We see that rkc4a is essentially identical to euler2a ; the only difference is a call to ssrkc4 (for a single step classical fourth-order RK step) in place of sseuler. ssrkc4 is listed below: function [t,u,e] = ssrkc4(neqn,t0,u0,h) % % Function ssrkc4 computes an ODE solution by the classical % fourth order RK method for one step along the solution % (by calls to derv to define the ODE derivative vector). % It also estimates the truncation error of the solution,
Copyright 2004 by Chapman & Hall/CRC
% % % % % % % % % % % % % % % % % % % % % %
and applies this estimate as a correction to the solution vector. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
u0
initial condition vector of length neqn
h
integration step
t
independent variable
u
ODE solution vector of length neqn after one rkc4 step
e
estimate of truncation error of the solution vector
Derivative vector at initial (base) point [ut0]=derv(neqn,t0,u0);
% % k1, advance of dependent % independent variable for k1=h*ut0; u=u0+0.5*k1; t=t0+0.5*h; % % Derivative vector at new [ut]=derv(neqn,t,u); % % k2, advance of dependent % independent variable for k2=h*ut; u=u0+0.5*k2; t=t0+0.5*h; % % Derivative vector at new [ut]=derv(neqn,t,u); % % k3, advance of dependent % independent variable for
Copyright 2004 by Chapman & Hall/CRC
variable vector and calculation of k2
u, t
variable vector and calculation of k3
u, t
variable vector and calculation of k4
k3=h*ut; u=u0+k3; t=t0+h; % % Derivative vector at new u, t [ut]=derv(neqn,t,u); % % k4 k4=h*ut; % % Second order step sum2=u0+k2; % % Fourth order step sum4=u0+(1.0/6.0)*(k1+2.0*k2+2.0*k3+k4); t=t0+h; % % Truncation error estimate e=sum4-sum2; % % Fourth order solution vector (from (2,4) RK pair); % two ways to the same result are listed % u=sum2+e; u=sum4; % % End of ssrkc4
Program 2.1.10 Integrator ssrkc4 for a classical fourth-order RK step ssrkc4 is similar in structure to sseuler; the only essential difference is the coding of the classical fourth-order RK algorithm, defined by Equations 1.50 and 1.51, in place of the modified Euler method of Equations 1.26. To explain this difference a little further, • The integration starts with an evaluation of the derivative vector at the base point (as in euler2a ): % % Derivative vector at initial (base) point [ut0]=derv(neqn,t0,u0);
• From the derivative vector at the base point, the first RK “constant,” k1 , can be calculated according to Equation 1.50a: % % k1, advance of dependent variable vector and % independent variable for calculation of k2
Copyright 2004 by Chapman & Hall/CRC
k1=h*ut0; u=u0+0.5*k1; t=t0+0.5*h;
Note that at the same time, the dependent variable vector, u, is advanced in preparation for the calculation of k2 . Note also that the RK constants are vectors with a dimension equal to the number of first-order ODEs, one in this case (for the 1x1 ODE problem). • The derivative vector is then computed at the new value of u by a call to derv, and k2 is calculated according to Equation 1.50b; u is then advanced in preparation for the calculation of k3 : % % Derivative vector at new u, t [ut]=derv(neqn,t,u); % % k2, advance of dependent variable vector and % independent variable for calculation of k3 k2=h*ut; u=u0+0.5*k2; t=t0+0.5*h;
• The derivative vector is again computed at the new value of u by a call to derv, and k3 is calculated according to Equation 1.50c; u is then advanced in preparation for the calculation of k4 : % % Derivative vector at new u, t [ut]=derv(neqn,t,u); % % k3, advance of dependent variable vector and % independent variable for calculation of k4 k3=h*ut; u=u0+k3; t=t0+h;
• The derivative vector is again updated and k4 is calculated according to Equation 1.50d: % % Derivative vector at new u, t [ut]=derv(neqn,t,u); % % k4 k4=h*ut;
• The second- and fourth-order solutions are computed according to Equations 1.40 and 1.50e, respectively:
Copyright 2004 by Chapman & Hall/CRC
% % Second order step sum2=u0+k2; % % Fourth order step sum4=u0+(1.0/6.0)*(k1+2.0*k2+2.0*k3+k4); t=t0+h;
• The integration error is estimated as the difference between the secondand fourth-order numerical solution, according to Equation 1.51: % % Truncation error estimate e=sum4-sum2;
• The solution vector u is then returned as the fourth-order result: % % Fourth order solution vector (from (2,4) RK pair); % two ways to the same result are listed % u=sum2+e; u=sum4;
The output from rkc4a (which calls ssrkc4) is not listed here to conserve some space. However, it is similar to the output from euler2a , and can easily be produced by setting int = 3 in function (intpar). rkc4b, the variable step version of the classical fourth-order RK method of Equations 1.50 and 1.51, is listed below: function [u]=rkc4b(neqn,t0,tf,u0,nsteps,abserr,relerr) % % Function rkc4b computes an ODE solution by a variable % step classical fourth order RK method for a series of % points along the solution by repeatedly calling function % ssrkc4 for a single classical fourth order RK step. The % truncation error is estimated along the solution to % adjust the integration step according to a specified % error tolerance. % % Argument list % % neqn number of first order ODEs % % t0 initial value of independent variable % % tf final value of independent variable %
Copyright 2004 by Chapman & Hall/CRC
% u0 initial condition vector of length neqn % % nsteps maximum number of rkc4 steps % % abserr absolute error tolerance % % relerr relative error tolerance % % u ODE solution vector of length neqn after % nsteps steps % % Initial integration step h=(tf-t0)/2.0; % % Minimum allowable step hmin=(tf-t0)/nsteps; % % Start integration t=t0; % % While independent variable is less than the final % value, continue the integration while t <= tf*0.999 % % If the next step along the solution will go past % the final value of the independent variable, set the % step to the remaining distance to the final value if t+h > tf h=tf-t; end % % Single rkc4 step [t,u,e]=ssrkc4(neqn,t0,u0,h); % % Flag for the end of the integration nfin1=1; % % Check if any of the ODEs have violated the error % criteria for i=1:neqn if abs(e(i)) > abs(u(i))*relerr+abserr % % Error violation, so integration is not complete. % Reduce integration step because of error violation % and repeat integration from the base point h=h/2.0; nfin1=0; break; end
Copyright 2004 by Chapman & Hall/CRC
end % % % % % % % % % %
% %
% % % %
If the current step allowable step, set allowable value and new base point if h < hmin h=hmin;
is less than the minimum the step to the minimum continue integration from nfin1=1; end
If there is no error violation, continue the integration from new base point if nfin1 == 1 u0=u; t0=t; Test if integration step can be increased for i=1:neqn if abs(e(i)) > (abs(u(i))*relerr+abserr)/16.0 Integration step cannot be increased nfin1=0; break; end end Increase integration step if nfin1 == 1 h=h*2.0; end Continue for no error violation (nfin1=1) end
% % Continue while end % % End of rkc4b
Program 2.1.11 Integrator rkc4b rkc4b is basically identical to euler2b. The only essential difference is the call to ssrkc4 in place of sseuler. Also, the test for integration step doubling is if abs(e(i)) > (abs(u(i))*relerr+abserr)/16.0
i.e., there is a division by 16 rather than by 4 as in sseuler since the classical RK method is fourth order (rather than second order). To complete the picture, we include listings of the following: • rkf45a (analogous to euler2a and rkc4a ) as Program 2.1.12 • rkf45b (analogous to euler2b and rkc4b) as Program 2.1.13 • ssrkf45 (analogous to sseuler and ssrkc4) as Program 2.1.14
Copyright 2004 by Chapman & Hall/CRC
function [u]=rkf45a(neqn,t0,tf,u0,nsteps) % % Function rkf45a computes an ODE solution by the fixed % step RK Fehlberg 45 method for a series of points along % the solution by repeatedly calling function ssrkf45 for % a single RK Fehlberg 45 step. % % Argument list % % neqn number of first order ODEs % % t0 initial value of independent variable % % tf final value of independent variable % % u0 initial condition vector of length neqn % % nsteps number of rkf45 steps % % u ODE solution vector of length neqn after % nsteps steps % % Integration step h=(tf-t0)/nsteps; % % nsteps rkf45 steps for i=1:nsteps % % rkf45 step [t,u,e]=ssrkf45(neqn,t0,u0,h); % % Reset base point values for next rkc4 step u0=u; t0=t; % % Next rkf45 step end % % End of rkf45a
Program 2.1.12 Integrator rkf45a rkf45a is called by setting int = 5 in function intpar. The output is essentially the same as from euler2a and rkc4a , and therefore is not listed here.
Copyright 2004 by Chapman & Hall/CRC
function [u]=rkf45b(neqn,t0,tf,u0,nsteps,abserr,relerr) % % Function rkf45b computes an ODE solution by the variable % step RK Fehlberg 45 method for a series of points along % the solution by repeatedly calling function ssrkf45 for % a single RK Fehlberg 45 step. The truncation error is % estimated along the solution to adjust the integration % step according to a specified error tolerance. % % Argument list % % neqn number of first order ODEs % % t0 initial value of independent variable % % tf final value of independent variable % % u0 initial condition vector of length neqn % % nsteps maximum number of rkf45 steps % % abserr absolute error tolerance % % relerr relative error tolerance % % u ODE solution vector of length neqn after % nsteps steps % % Initial integration step h=(tf-t0)/2.0; % % Minimum allowable step hmin=(tf-t0)/nsteps; % % Start integration t=t0; % % While independent variable is less than the final % value, continue the integration while t <= tf*0.999 % % If the next step along the solution will go past % the final value of the independent variable, set the % step to the remaining distance to the final value if t+h > tf h=tf-t; end
Copyright 2004 by Chapman & Hall/CRC
% % % % % % %
% % % %
% % % % % % % % % %
% %
% % % %
Single rkf45 step [t,u,e]=ssrkf45(neqn,t0,u0,h); Flag for the end of the integration nfin1=1; Check if any of the ODEs have violated the error criteria for i=1:neqn if abs(e(i)) > abs(u(i))*relerr+abserr Error violation, so integration is not complete. Reduce integration step because of error violation and repeat integration from the base point h=h/2.0; nfin1=0; break; end end If the current step allowable step, set allowable value and new base point if h < hmin h=hmin;
is less than the minimum the step to the minimum continue integration from nfin1=1; end
If there is no error violation, continue the integration from new base point if nfin1 == 1 u0=u; t0=t; Test if integration step can be increased for i=1:neqn if abs(e(i)) > (abs(u(i))*relerr+abserr)/32.0 Integration step cannot be increased nfin1=0; break; end end Increase integration step if nfin1 == 1 h=h*2.0; end Continue for no error violation (nfin1=1) end
%
Copyright 2004 by Chapman & Hall/CRC
% Continue while end % % End of rkf45b
Program 2.1.13 Integrator rkf45b rkf45b is basically identical to rkc4b. The only essential difference is the call to ssrkf45 in place of ssrkc4. Also, the test for integration step doubling is if abs(e(i)) > (abs(u(i))*relerr+abserr)/32.0
i.e., there is a division by 32 rather than by 16 as in ssrkc4 since the RKF45 method is fifth order (rather than fourth order). rkf45b is called by setting int = 6 in function intpar. The output (listed previously) is essentially the same as from euler2b and rkc4b. function [t,u,e] = ssrkf45(neqn,t0,u0,h) % % Function ssrkf45 computes an ODE solution by the RK % Fehlberg 45 method for one step along the solution (by % calls to derv to define the ODE derivative vector). It % also estimates the truncation error of the solution, % and applies this estimate as a correction to the solution % vector. % % Argument list % % neqn number of first order ODEs % % t0 initial value of independent variable % % u0 initial condition vector of length neqn % % h integration step % % t independent variable % % u ODE solution vector of length neqn after % one rkf45 step % % e estimate of truncation error of the solu% tion vector %
Copyright 2004 by Chapman & Hall/CRC
% Derivative vector at initial (base) point [ut0]=derv(neqn,t0,u0); % % k1, advance of dependent variable vector and % independent variable for calculation of k2 k1=h*ut0; u=u0+0.25*k1; t=t0+0.25*h; % % Derivative vector at new u, t [ut]=derv(neqn,t,u); % % k2, advance of dependent variable vector and % independent variable for calculation of k3 k2=h*ut; u=u0+(3.0/32.0)*k1... +(9.0/32.0)*k2; t=t0+(3.0/8.0)*h; % % Derivative vector at new u, t [ut]=derv(neqn,t,u); % % k3, advance of dependent variable vector and % independent variable for calculation of k4 k3=h*ut; u=u0+(1932.0/2197.0)*k1... -(7200.0/2197.0)*k2... +(7296.0/2197.0)*k3; t=t0+(12.00/13.0)*h; % % Derivative vector at new u, t [ut]=derv(neqn,t,u); % % k4, advance of dependent variable vector and % independent variable for calculation of k5 k4=h*ut; u=u0+( 439.0/ 216.0)*k1... -( 8.0 )*k2... +(3680.0/ 513.0)*k3... -( 845.0/4104.0)*k4; t=t0+h; % % Derivative vector at new u, t [ut]=derv(neqn,t,u); %
Copyright 2004 by Chapman & Hall/CRC
% k5, advance of dependent variable vector and % independent variable for calculation of k6 k5=h*ut; u=u0-( 8.0/ 27.0)*k1... +( 2.0 )*k2... -(3544.0/2565.0)*k3... +(1859.0/4104.0)*k4... -( 11.0/ 40.0)*k5; t=t0+0.5*h; % % Derivative vector at new u, t [ut]=derv(neqn,t,u); % % k6 k6=h*ut; % % Fourth order step sum4=u0+( 25.0/ 216.0)*k1... +( 1408.0/2565.0)*k3... +( 2197.0/4104.0)*k4... -( 1.0/ 5.0)*k5; % % Fifth order step sum5=u0+( 16.0/ 135.0)*k1... +( 6656.0/12825.0)*k3... +(28561.0/56430.0)*k4... -( 9.0/ 50.0)*k5... +( 2.0/ 55.0)*k6; t=t0+h; % % Truncation error estimate e=sum5-sum4; % % Fifth order solution vector (from (4,5) RK pair); % two ways to the same result are listed % u=sum4+e; u=sum5; % % End of ssrkf45
Program 2.1.14 Integrator ssrkf45 for a RKF45 step The only essential difference for ssrkf45 is the coding of the ODE integration method, i.e.,
Copyright 2004 by Chapman & Hall/CRC
• sseuler2—single step modified Euler method ((1, 2) pair) • ssrk4c—single step classical fourth-order RK method ((2, 4) pair) • ssrkf45—single step RKF 45 method ((4, 5) pair) Another possibility would be to write a function analogous to ssrkf45 using the (2, 3) pair discussed previously. The only requirement would be to use the (2, 3) formulas in place of those for the (4, 5) pair; all the other coding in the preceding functions would remain essentially the same. The writing of a (2, 3) integrator is therefore left as an excerise for the reader. We also have observed in the previous routines an organizing principle that we will use consistently throughout the remainder of this book: • A set of library routines, which are general purpose and should therefore not be changed from one ODE application to the next. Specifically, the following routines can be applied generally to an ODE problem and should therefore not be modified: Routine Names ode1x1 euler2a , euler2b, sseuler rkc4a , rkc4b, ssrkc4 rkf45a , rkf45b, ssrkf45
Purpose Main program (1,2) RK pair (2,4) RK pair (4,5) RK pair
• A set of routines that apply specifically to the ODE problem at hand: Routine Names inital derv fprint
Purpose
Set initial conditions Compute ODE RHS vector Display ODE solution
While these routines pertain to a specific ODE application, in this case the 1x1 ODE problem, they can serve as templates for other ODE applications. In other words, they can be easily modified for the solution of other ODEs. For example, they could be modified from the 1x1 problem of Equations 1.3 and 1.4 to the 2x2 problem of Equations 1.6, 1.16, and 1.17. • One routine, intpar falls somewhere between the two preceding sets. It is general purpose in the sense that it sets the parameters that control the operation of the general-purpose ODE integration routines; e.g., it sets the error tolerances. On the other hand, the specific values of the parameters depend on the properties of the particular ODE problem; e.g., the number of integration steps, nsteps, may have to be adjusted. Thus, some trial and error generally will be required when coding intpar for a new ODE application. This is particularly the case with the error tolerances as
Copyright 2004 by Chapman & Hall/CRC
discussed in Chapter 1; in fact, probably the single most common cause of library integrator failures is the specification of inappropriate error tolerances. To summarize this organizing principle, the general-purpose (library) routines and the problem-specific routines should be separated as much as possible in the development of new ODE applications. This separation cannot be achieved completely because of the need to tune the integrator parameters, i.e., in intpar. This basically completes the discussion of the ODE integrators in MATLAB. In a subsequent section, we revisit some of the preceding routines to which some output statements are added to indicate what is occurring within them, particularly with regard to error monitoring and step size adjustment (in euler2b, rkc4b, and rkf45b). The purpose of this additional output is to (a) observe if the integrators operate internally as we would expect, and (b) present a method for debugging when approaching a new ODE problem. We now proceed to the programming of the preceding integrators in other languages.
2.2
Programming in C
Because the operation of the (1, 2), (2, 4), and (4, 5) integrators has been discussed in detail in Section 2.1, we now proceed with the analogous programming in C, but without the same detailed explanation. In other words, we consider the listing of the routines to be essentially self-explanatory (also, we will not consider the details of the differences in syntax between MATLAB and C). Our expectation is that the C code will be largely self-explanatory when compared with the preceding MATLAB code. In fact, we have tried to make the two sets of routines closely resemble each other to facilitate the understanding of one set in terms of the other. Also, we have used alphabetical ordering of the languages in the following discussion to demonstrate that we do not endorse one language over another. The one exception is the preceding use of MATLAB, which we selected first because of its simplicity in handling vectors (without subscripting). However, this does not mean we favor MATLAB, and in fact, we also have some reservations about this and other “automatic features” possibly leading to unintended errors and consequences. In other words, subscripting within do and for loops provides an unambiguous indication of how vectors (and matrices) are coded and manipulated with very little additional coding (efficiency may also be a consideration, especially with MATLAB, which tends to be more efficient without subscripting). Thus, we feel that all of the languages are about comparable in utility for the numerical integration of ODE/PDE systems. A main concern is therefore
Copyright 2004 by Chapman & Hall/CRC
to provide code that follows a common theme and format to demonstrate that all of the languages are about equal in terms of their utility in studying differential systems. To start, we list a main program analogous to Program 2.1.1: /* Numerical solution to the 1 x 1 ODE system by six integrators */ /* Include headers */ #include <stdio.h> #include <math.h> #include "ode1x1.h" #include "euler.h" #include "rk.h" /* Main program */ void main() { /* Type variables */ double u0[SIZE],u[SIZE]; double tp; int i, j, ncase; /* Open a file for output */ if((fid=fopen("ode1x1c.out","w+"))==NULL) { printf("\nError opening output file\n"); } /* Step through six integrators */ for(ncase=1;ncase<=6;ncase++) { /* ODE integration parameters */ intpar(); /* Initial condition */ inital(u0,t0); /* Output interval */ tp=tf-t0; /* Step through nout grid points */ for(j=1;j<=nout;j++)
Copyright 2004 by Chapman & Hall/CRC
{ /* Print current solution */ fprint(ncase,t0,u0); /* Select ODE integrator */ switch(ncase) { /* Fixed step modified Euler integrator */ case 1: euler2a(n,t0,tf,u,u0,nsteps); break;
/* Variable step modified Euler integrator */ case 2: euler2b(n,t0,tf,u,u0,nsteps,abserr,relerr); break; /* Fixed step classical fourth order RK integrator */ case 3: rkc4a(n,t0,tf,u,u0,nsteps); break; /* Variable step classical fourth order RK integrator */ case 4: rkc4b(n,t0,tf,u,u0,nsteps,abserr,relerr); break; /* Fixed step RK Fehlberg (RKF45) integrator */ case 5: rkf45a(n,t0,tf,u,u0,nsteps); break; /* Variable step RK Fehlberg (RKF45) integrator */ case 6: rkf45b(n,t0,tf,u,u0,nsteps,abserr,relerr); break; } /* Advance solution */ t0=tf; tf=tf+tp; for(i=1;i<=n;i++)
Copyright 2004 by Chapman & Hall/CRC
{ u0[i]=u[i]; } /* Next output */ } /* Next integrator */ } /* Complete solution computed. Close the output file */ fclose(fid); /* End of main */ }
Program 2.2.1 C main program for the numerical integration of Equation 1.3 subject to initial condition Equation 1.4 We can note the following points about this program: • Comments in C are generally enclosed within the delimiters /* */ (which seems safe since the arithmetic operators * and / generally will not be adjacent when writing C code). • The main program starts with the declaration of some header files: /* Include headers */ #include <stdio.h> #include <math.h> #include "ode1x1.h" #include "euler.h" #include "rk.h"
In addition to the standard header files for C (stdio.h and math.h), we have included three header files, ode1x1.h, euler.h and rk.h for the numerical integration of the 1x1 ODE (Equation 1.3) using the (1, 2), (2, 4), and (4, 5) pairs. • Instead of executing each of the six ODE integrators individually by changing int through the values int = 1, 2, . . . , 6, we call all six integrators in a loop: /* Step through six integrators */ for(ncase=1;ncase<=6;ncase++)
Otherwise, the coding in Program 2.2.1 closely parallels that in Program 2.1.1.
Copyright 2004 by Chapman & Hall/CRC
Header file ode1x1.h is listed below: /* Definition of functions intpar, inital, derv, fprint for the 1 x 1 ODE system */ /* Include headers */ #include <stdio.h> #include <math.h> /* Maximum number of ODEs */ #define SIZE 500 /* Type variables as extern (global) */ extern int n, nout, nsteps; extern double t0, tf, abserr, relerr; extern FILE *fid; /* Integration parameters */ void intpar(); /* Initial condition */ void inital(double u0[], double t); /* Derivative vector */ void derv(double ut[], double t, double u[]); /* Output */ void fprint(int ncase, double t, double u[]);
Program 2.2.2 Header file ode1x1.h included in Program 2.2.1 All of the routines defined in ode1x1.h have already been discussed in detail in Section 2.1. We can note the following details: • The maximum number of ODES has been set (arbitrarily) at 500: /* Maximum number of ODEs */ #define SIZE 500
This absolute sizing can be increased to any desired value (although it is more than adequate since we are integrating one ODE).
Copyright 2004 by Chapman & Hall/CRC
• Several global variables are defined that can be shared between routines: /* Type variables as extern (global) */ extern int neqn, nout, nsteps; extern double t0, tf, abserr, relerr; extern FILE *fid;
A file ID (pointer) is also declared as a global variable for writing an external output file. • Routines intpar, inital, derv, and fprint are declared. They have the same function as those of the same name in Section 2.1. All these routines are listed subsequently. Otherwise, the coding in Programs 2.2.1 and 2.2.2 follows directly from their counterparts in Section 2.1. intpar is listed below: #include "ode1x1.h" /* Type global variables */ int neqn, nout, nsteps; double t0, tf, abserr, relerr; /* Define file ID */ FILE *fid;
void intpar() /* Function intpar sets the parameters to control the integration of the 1 x 1 ODE system */ { /* Number of ODEs */ neqn=1; /* Number of output points */ nout=6; /* Maximum number of steps in the interval t0 to tf */ nsteps=100; /* Initial, final values of independent variable */ t0=0.0; tf=1.0;
Copyright 2004 by Chapman & Hall/CRC
/* Error tolerances */ abserr=pow(10,-5); relerr=pow(10,-5); /* End of intpar */ }
Program 2.2.3 Routine intpar called in Program 2.2.1 to set the integration parameters intpar is essentially identical to its counterpart in Program 2.1.3. inital, derv and fprint are listed below: void inital(double u0[],double t0) /* Function inital sets the initial condition vector for the 1 x 1 ODE problem */ { /* Initial condition */ u0[1]=1.0; /* End of inital */ }
void derv(double ut[], double t, double u[]) /* Function derv computes the derivative vector of the 1 x 1 ODE problem */ { /* Type variables */ double alpha, lambda; /* Problem parameters */ alpha=1.0; lambda=1.0; /* Derivative vector */ ut[1]=lambda*exp(-alpha*t)*u[1]; /* End of derv */ }
void fprint(int ncase, double t, double u[])
Copyright 2004 by Chapman & Hall/CRC
/* Function fprint displays the numerical and exact solutions to the 1 x 1 ODE problem */ { /* Type variables */ double ue[2], diff[2]; double u0, alpha, lambda; /* Problem parameters */ u0=1.0; alpha=1.0; lambda=1.0; /* Print a heading for the solution at t = 0 */ if(t<=0.0) { fprintf(fid,"\n\n int = %3d\n",ncase); /* Label for ODE integrator */ switch(ncase) { case 1: /* Fixed step modified Euler */ fprintf(fid,"\n euler2a integrator\n"); break; case 2: /* Variable step modified Euler */ fprintf(fid,"\n euler2b integrator\n"); break;
case 3: /* Fixed step classical fourth order RK */ fprintf(fid,"\n rkc4a integrator\n"); break; case 4: /* Variable step classical fourth order RK */ fprintf(fid,"\n rkc4b integrator\n"); break; case 5: /* Fixed step RK Fehlberg 45 */ fprintf(fid,"\n rkf45a integrator\n"); break; case 6: /* Variable step RK Fehlberg 45 */ fprintf(fid,"\n rkf45b integrator\n"); break; }
Copyright 2004 by Chapman & Hall/CRC
/* Heading */ fprintf(fid,"\n
t
u1(num)
u1(ex)
diff1\n\n");
/* End of t = 0 heading */ } /* Analytical solution vector */ ue[1]=u0*exp(lambda/alpha*(1.0-exp(-alpha*t))); /* Difference between exact and numerical solution vectors */ diff[1]=u[1]-ue[1]; /* Display the numerical and exact solutions, and their difference */ fprintf(fid,"%10.2f %10.5f %10.5f %13.4e\n", t,u[1],ue[1],diff[1]); /* End of fprint */ }
Program 2.2.4 inital, derv and fprint called in the solution of Equations 1.3 and 1.4
These routines should be self-explanatory, particularly when compared with their MATLAB counterparts in Section 2.1. The output from the six integrators called in Program 2.2.1 is listed below (written to file ode1x1c.out in fprint): int =
1
euler2a integrator t
u1(num)
u1(ex)
diff1
0.00 1.00 2.00 3.00 4.00 5.00
1.00000 1.88160 2.37421 2.58627 2.66895 2.70004
1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
0.0000e+00 -6.4665e-08 3.8793e-06 6.0332e-06 6.9239e-06 7.2649e-06
Copyright 2004 by Chapman & Hall/CRC
int =
2
euler2b integrator t
u1(num)
u1(ex)
diff1
0.00 1.00 2.00 3.00 4.00 5.00
1.00000 1.88159 2.37421 2.58626 2.66895 2.70003
1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
0.0000e+00 -6.2196e-06 -3.7783e-06 2.9325e-07 3.6651e-06 6.2494e-06
int =
3
rkc4a integrator t
u1(num)
u1(ex)
diff1
0.00 1.00 2.00 3.00 4.00 5.00
1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
0.0000e+00 -2.7188e-11 -3.2671e-11 -3.4825e-11 -3.5646e-11 -3.5952e-11
int =
4
rkc4b integrator t
u1(num)
u1(ex)
diff1
0.00 1.00 2.00 3.00 4.00 5.00
1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
0.0000e+00 -4.2632e-08 6.8773e-08 9.3031e-08 1.0305e-07 1.4624e-07
Copyright 2004 by Chapman & Hall/CRC
int =
5
rkf45a integrator t
u1(num)
u1(ex)
diff1
0.00 1.00 2.00 3.00 4.00 5.00
1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
0.0000e+00 6.6613e-16 2.6645e-15 1.3323e-15 1.3323e-15 -1.3323e-15
int =
6
rkf45b integrator t
u1(num)
u1(ex)
diff1
0.00 1.00 2.00 3.00 4.00 5.00
1.00000 1.88159 2.37421 2.58626 2.66894 2.70002
1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
0.0000e+00 -3.2870e-06 -4.6026e-06 -5.0371e-06 -5.1682e-06 -5.2117e-06
We can note the following details about this output: • The error tolerances set in intpar /* Error tolerances */ abserr=pow(10,-5); relerr=pow(10,-5);
are satisfied by all of the integrators. • The fixed step integrators, euler2a , rkc4a , and rkf 45a exceed the error tolerances by orders of magnitude when using 100 integration steps in each output interval as set in intpar: /* Maximum number of steps in the interval t0 to tf */ nsteps=100;
In fact, rkc4a and rkf45a provide accuracies that might be considered excessive. In the case of rkf45a , the errors are of the order of the machine epsilon (unit roundoff, machine precision) for C.
Copyright 2004 by Chapman & Hall/CRC
We now consider the six integrators by listing and briefly discussing the source code (again, we are relying on the preceding discussion of the six integrators in MATLAB to explain most of the details). euler2a is listed first: /* Include headers */ #include "euler.h" #include <stdio.h> double euler2a(int neqn, double t0, double tf, double u[], double u0[], int nsteps) /* Function euler2a computes an ODE solution by the fixed step modified Euler method for a series of points along the solution by repeatedly calling function sseuler for a single modified Euler step. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
number of modified Euler steps
u
ODE solution vector of length neqn after nsteps steps
*/ { /* Type variables */ double e[SIZE]; double h,t; int i,j; /* Integration step */ h=(tf-t0)/nsteps; /* nsteps modified Euler steps */ for(i=1;i<=nsteps;i++) { /* Single modified Euler step */ t=sseuler(neqn,t0,u0,h,u,e);
Copyright 2004 by Chapman & Hall/CRC
/* Reset base point values for next modified Euler step */ for(j=1;j<=neqn;j++) { u0[j]=u[j]; } t0=t; /* End for */ } return 0; /* End of euler2a */ }
Program 2.2.5 Integrator euler2a euler2a closely parallels the MATLAB routine of Program 2.1.5 and therefore will not be discussed further. euler2b is listed below: double euler2b(int neqn, double t0, double tf, double u[], double u0[], int nsteps, double abserr, double relerr) /* Function euler2b computes an ODE solution by the variable step modified Euler method for a series of points along the solution by repeatedly calling function sseuler for a single modified Euler step. The truncation error is estimated along the solution to adjust the integration step according to a specified error tolerance. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
number of modified Euler steps
Copyright 2004 by Chapman & Hall/CRC
u
ODE solution vector of length neqn after nsteps steps
*/ { /* Type variables */ double h, hmin, t; int i, nfin1; double e[SIZE]; /* Integration step */ h=(tf-t0)/8.0; /* Minimum allowable step */ hmin=(tf-t0)/(float)(nsteps); /* Start integration */ t=t0; /* While independent variable is less than the final value, continue the integration */ while (t <= tf*0.999) { /* If the next step along the solution will go past the final value of the independent variable, set the step to the remaining distance to the final value */ if((t+h) > tf) { h=tf-t; } /* Single modified Euler step */ t=sseuler(neqn,t0,u0,h,u,e); /* Flag for the end of the integration */ nfin1=1; /* Check if any of the ODEs have violated the error */ for(i=1;i<=neqn;i++) { if(fabs(e[i]) > (fabs(u[i])*relerr+abserr)) { /* Error violation, so integration is incomplete. Reduce integration step because of error violation and repeat integration from base point */
Copyright 2004 by Chapman & Hall/CRC
nfin1=0; h=h/2.0; /* If the current step is less than the minimum allowable step, set the step to the minimum allowable value and continue integration from new base point */ if(h < hmin) { h=(float)hmin; nfin1=1; } break; } } /* If there is no error violation, continue the integration from the new base point */ if(nfin1 == 1 ) { for(i=1;i<=neqn;i++) { u0[i]=u[i]; } t0=t; /* Test if integration step can be increased */ for(i=1;i<=neqn;i++) { if(fabs(e[i]) > ((fabs(u[i])*relerr +abserr)/4.0)) { /* Integration step cannot be increased */ nfin1=0; break; } } /* Increase integration step */ if(nfin1 == 1) { h=h*2.0; } /* End if */ }
Copyright 2004 by Chapman & Hall/CRC
/* End while */ } return 0; /* End of euler2b */ }
Program 2.2.6 Integrator euler2b Again, euler2b closely parallels the MATLAB routine of Program 2.1.8 except for differences in syntax between MATLAB and C and, therefore, will not be discussed further. sseuler is listed below: double sseuler(int neqn, double t0, double u0[], double h, double u[], double e[]) /* Function sseuler computes an ODE solution by the modified Euler method for one step along the solution (by calls to derv to define the ODE derivative vector). It also estimates the truncation error of the solution, and applies this estimate as a correction to the solution vector. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
u0
initial condition vector of length neqn
h
integration step
t
independent variable
u
ODE solution vector of length neqn after one modified Euler step
e
estimate of truncation error of the solution vector
*/
Copyright 2004 by Chapman & Hall/CRC
{ /* Type variables */ double ut0[SIZE], ut[SIZE]; double t; int j; /* Derivative vector at initial (base) point */ derv(ut0,t0,u0); /* First order (Euler) step */ for(j=1;j<=neqn;j++) { u[j]=u0[j]+ut0[j]*h; } t=t0+h; /* Derivative at advance point */ derv(ut,t,u); /* Second order (modified Euler) step */ for(j=1;j<=neqn;j++) { /* Truncation error estimate */ e[j]=(ut[j]-ut0[j])*h/2.0; /* Second order (modified Euler) solution vector */ u[j]=u[j]+e[j]; } return t; /* End of sseuler */ }
Program 2.2.7 Integrator sseuler for a single modified Euler step Again, sseuler is a direct analog of the MATLAB routine in Program 2.1.6. The header file for euler2a , euler2b, and sseuler is as follows: /* Routines for the Euler ODE Integration */
Copyright 2004 by Chapman & Hall/CRC
#include <math.h> #define SIZE 500 double euler2a(int neqn, double t0, double tf, double u[], double u0[], int nsteps); double euler2b(int neqn, double t0, double tf, double u[], double u0[], int nsteps, double abserr, double relerr); double sseuler(int neqn, double t0, double u0[], double h, double u[], double e[]);
Program 2.2.8 Header file euler.h for euler2a , euler2b, and sseuler rkc4a , rkc4b, and ssrkc4 are listed below without further comment (their MATLAB counterparts are Programs 2.1.9, 2.1.10, and 2.1.11): #include "rk.h" double rkc4a(int neqn, double t0, double tf, double u[], double u0[], int nsteps) /* Function rkc4a computes an ODE solution by the fixed step classical fourth order RK method for a series of points along the solution by repeatedly calling function ssrkc4 for a single classical fourth order RK step. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
number of rkc4 steps
u
ODE solution vector of length neqn after nsteps steps
Copyright 2004 by Chapman & Hall/CRC
*/ { /* Type variables */ int i, j; double t, h, e[SIZE]; /* Integration step */ h=(tf-t0)/nsteps; /* nsteps rkc4 steps */ for(i=1;i<=nsteps;i++) { /* Single rkc4 step */ t=ssrkc4(neqn,t0,u0,h,u,e); /* Reset base point values for next rk4c step */ for(j=1;j<=neqn;j++) { u0[j]=u[j]; } t0=t; } return 0; /* End of rkc4a */ }
Program 2.2.9 Integrator rkc4a
double rkc4b(int neqn, double t0, double tf, double u[], double u0[], int nsteps,double abserr, double relerr) /* Function rkc4b computes an ODE solution by a variable step classical fourth order RK method for a series of points along the solution by repeatedly calling function ssrkc4 for a single classical fourth order RK step. The truncation error is estimated along the solution to adjust the integration step according to a specified error tolerance.
Copyright 2004 by Chapman & Hall/CRC
Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
maximum number of rkc4 steps
abserr
absolute error tolerance
relerr
relative error tolerance
u
ODE solution vector of length neqn after nsteps steps
*/ { /* Type variables */ double h, hmin, t, e[SIZE]; int nfin1, i; /* Integration step */ h=(tf-t0)/2.0; /* Minimum allowable step */ hmin=(tf-t0)/nsteps; /* Start integration */ t=t0; /* While independent variable is less than the final value, continue the integration */ while(t <= (tf*0.999)) { /* If the next step along the solution will go past the final value of the independent variable, set the step to the remaining distance to the final value */ if((t+h) > tf) { h=tf-t; }
Copyright 2004 by Chapman & Hall/CRC
/* Single rkc4 step */ t=ssrkc4(neqn,t0,u0,h,u,e); /* Flag for the end of the integration */ nfin1=1; /* Check if any of the ODEs have violated the error criterion */ for(i=1;i<=neqn;i++) { if( fabs(e[i]) > (fabs(u[i])*relerr+abserr)) { /* Error violation, so integration is incomplete. Reduce integration step because of error violation and repeat integration from base point */ nfin1=0; h=h/2.0; /* If the current step is less than the minimum allowable step, set the step to the minimum allowable value and continue integration from new base point */ if(h < hmin) { h = hmin; nfin1=1; } break; } } /* If there is no error violation, continue the integration from the new base point */ if(nfin1 == 1) { for(i=1;i<=neqn;i++) { u0[i]=u[i]; } t0=t; /* Test if integration step can be increased */ for(i=1;i<=neqn;i++)
Copyright 2004 by Chapman & Hall/CRC
{ if(fabs(e[i]) > ((fabs(u[i])*relerr +abserr)/16.0)) { /* Integration step cannot be increased */ nfin1=0; break; } } /* Increase integration step */ if(nfin1 == 1) { h=h*2.0; } /* End if */ } /* End while */ } return 0; /* End of rkc4b */ }
Program 2.2.10 Integrator rkc4b
double ssrkc4(int neqn, double t0, double u0[], double h, double u[], double e[]) /* Function ssrkc4 computes an ODE solution by the classical fourth order RK method for one step along the solution (by calls to derv to define the ODE derivative vector). It also estimates the truncation error of the solution, and applies this estimate as a correction to the solution vector. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
Copyright 2004 by Chapman & Hall/CRC
u0
initial condition vector of length neqn
h
integration step
t
independent variable
u
ODE solution vector of length neqn after one rkc4 step
e
estimate of truncation error of the solution vector
*/ { /* Type variables */ double ut0[SIZE], ut[SIZE], u4[SIZE]; double t, k1[SIZE], k2[SIZE], k3[SIZE], k4[SIZE]; int j; /* Derivative vector at initial (base) point */ derv(ut0,t0,u0); /* k1; stepping for k2 */ for(j=1;j<=neqn;j++) { k1[j]=h*ut0[j]; u[j]=u0[j]+0.5*k1[j]; } t=t0+0.5*h; /* Derivative vector at next RK point */ derv(ut,t,u); /* k2; stepping for k3 */ for(j=1;j<=neqn;j++) { k2[j]=h*ut[j]; u[j]=u0[j]+0.5*k2[j]; } t=t0+0.5*h; /* Derivative vector at next RK point */ derv(ut,t,u); /* k3; stepping for k4 */ for(j=1;j<=neqn;j++)
Copyright 2004 by Chapman & Hall/CRC
{ k3[j]=h*ut[j]; u[j]=u0[j]+k3[j]; } t=t0+h; /* Derivative vector at next RK point */ derv(ut,t,u); /* k4; second and fourth order step; error estimate; error correction */ for(j=1;j<=neqn;j++) { k4[j]=h*ut[j]; u[j]=u0[j]+k2[j]; u4[j]=u0[j]+(1.0/6.0)*(k1[j]+2.0*k2[j]+2.0*k3[j] +k4[j]); e[j]=u4[j]-u[j]; u[j]=u[j]+e[j]; } t=t0+h; return t; /* End of ssrkc4 */ }
Program 2.2.11 Integrator ssrkc4 for a classical fourth-order RK step rkf45a , rkf45b, and ssrkf45 are listed below without further comment (their MATLAB counterparts are Programs 2.1.9, 2.1.10, and 2.1.11): double rkf45a(int neqn, double t0, double tf, double u[], double u0[], int nsteps) /* Function rkf45a computes an ODE solution by the fixed step RK Fehlberg 45 RK method for a series of points along the solution by repeatedly calling function ssrkf45 for a single RK Fehlberg 45 step. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
Copyright 2004 by Chapman & Hall/CRC
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
number of rkf45 steps
u
ODE solution vector of length neqn after nsteps steps
*/ { /* Type variables */ double t, h, e[SIZE]; int i,j; /* Integration step */ h=(tf-t0)/nsteps; /* nsteps rkf45 steps */ for(i=1;i<=nsteps;i++) { /* Single rkf45 step */ t=ssrkf45(neqn,t0,u0,h,u,e); /* Reset base point values for next rkf45 step */ for(j=1;j<=neqn;j++) { u0[j]=u[j]; } t0=t; } return 0; /* End rkf45a */ }
Program 2.2.12 Integrator rkf45a double rkf45b(int neqn, double t0, double tf, double u[], double u0[], int nsteps,double abserr, double relerr) /* Function rkf45b computes an ODE solution by a variable step classical RK Fehlberg 45 method for a series of
Copyright 2004 by Chapman & Hall/CRC
points along the solution by repeatedly calling function ssrkf45 for a single RK Fehlberg 45 step. The truncation error is estimated along the solution to adjust the integration step according to a specified error tolerance.
Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
maximum number of rkf45 steps
abserr
absolute error tolerance
relerr
relative error tolerance
u
ODE solution vector of length neqn after nsteps steps
*/ { /* Type variables */ double h, hmin, t, e[SIZE]; int i, nfin1; /* Integration step */ h=(tf-t0)/2.0; /* Minimum allowable step */ hmin=(tf-t0)/nsteps; /* Start integration */ t=t0; /* While independent variable is less than the final value, continue the integration */ while(t <= (tf*0.999)) {
Copyright 2004 by Chapman & Hall/CRC
/* If the next step along the solution will go past the final value of the independent variable, set the step to the remaining distance to the final value */ if((t+h) > tf) { h=tf-t; } /* Single rkf45 step */ t=ssrkf45(neqn,t0,u0,h,u,e); /* Flag for the end of the integration */ nfin1=1; /* Check if any of the ODEs have violated the error criterion */ for (i=1;i<=neqn;i++) { if(fabs(e[i]) > (fabs(u[i])*relerr+abserr)) { /* Error violation, so integration is incomplete. Reduce integration step because of error violation and repeat integration from base point */ nfin1=0; h=h/2.0; /* If the current step is less than the minimum allowable step, set the step to the minimum allowable value and continue integration from new base point */ if(h < hmin) { h = hmin; nfin1=1; } break; } } /* If there is no error violation, continue the integration from the new base point */ if(nfin1 == 1) {
Copyright 2004 by Chapman & Hall/CRC
for(i=1;i<=neqn;i++) { u0[i]=u[i]; } t0=t; /* Test if integration step can be increased */ for(i=1;i<=neqn;i++) { if(fabs(e[i]) > ((fabs(u[i])*relerr+abserr) /32.0) ) { /* Integration step cannot be increased */ nfin1=0; break; } } /* Increase integration step */ if(nfin1 == 1) { h=h*2.0; } /* End if */ } /* End while */ } return 0; /* End of rkf45b */ }
Program 2.2.13 Integrator rkf45b double ssrkf45(int neqn, double t0, double u0[], double h, double u[], double e[]) /* Function ssrkf45 computes an ODE solution by the RK Fehlberg 45 method for one step along the solution (by
Copyright 2004 by Chapman & Hall/CRC
calls to derv to define the ODE derivative vector). It also estimates the truncation error of the solution, and applies this estimate as a correction to the solution vector. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
u0
initial condition vector of length neqn
h
integration step
t
independent variable
u
ODE solution vector of length neqn after one rkf45 step
e
estimate of truncation error of the solution vector
*/ { /* Type variables */ double t, ut0[SIZE], ut[SIZE], u5[SIZE]; double k1[SIZE], k2[SIZE], k3[SIZE], k4[SIZE], k5[SIZE], k6[SIZE]; int j; /* Derivative vector at initial (base) point */ derv(ut0,t0,u0); /* k1; stepping for k2 */ for(j=1;j<=neqn;j++) { k1[j]=h*ut0[j]; u[j]=u0[j]+0.25*k1[j]; } t=t0+0.25*h; /* Derivative vector at next RK point */ derv(ut,t,u);
Copyright 2004 by Chapman & Hall/CRC
/* k2; stepping for k3 */ for(j=1;j<=neqn;j++) { k2[j]=h*ut[j]; u[j]=u0[j]+(3.0/32.0)*k1[j] +(9.0/32.0)*k2[j]; } t=t0+(3.0/8.0)*h; /* Derivative vector at next RK point */ derv(ut,t,u); /* k3; stepping for k4 */ for(j=1;j<=neqn;j++) { k3[j]=h*ut[j]; u[j]=u0[j]+(1932.0/2197.0)*k1[j] -(7200.0/2197.0)*k2[j] +(7296.0/2197.0)*k3[j]; } t=t0+(12.00/13.0)*h; /* Derivative vector at next RK point */ derv(ut,t,u); /* k4; stepping for k5 */ for(j=1;j<=neqn;j++) { k4[j]=h*ut[j]; u[j]=u0[j]+( 439.0/ 216.0)*k1[j] -( 8.0 )*k2[j] +(3680.0/ 513.0)*k3[j] -( 845.0/4104.0)*k4[j]; } t=t0+h; /* Derivative vector at next RK point */ derv(ut,t,u); /* k5; stepping for k6 */ for(j=1;j<=neqn;j++) { k5[j]=h*ut[j];
Copyright 2004 by Chapman & Hall/CRC
u[j]=u0[j]-( 8.0/ 27.0)*k1[j] +( 2.0 )*k2[j] -(3544.0/2565.0)*k3[j] +(1859.0/4104.0)*k4[j] -( 11.0/ 40.0)*k5[j]; } t=t0+0.5*h; /* Derivative vector at next RK point */ derv(ut,t,u); /* k6; fourth and fifth order step; error estimate; error correction */ for(j=1;j<=neqn;j++) { k6[j]=h*ut[j]; u[j]=u0[j]+( 25.0/ 216.0)*k1[j] +( 1408.0/2565.0)*k3[j] +( 2197.0/4104.0)*k4[j] -( 1.0/ 5.0)*k5[j]; u5[j]=u0[j]+( 16.0/ 135.0)*k1[j] +( 6656.0/12825.0)*k3[j] +(28561.0/56430.0)*k4[j] -( 9.0/ 50.0)*k5[j] +( 2.0/ 55.0)*k6[j]; e[j]=u5[j]-u[j]; u[j]= u[j]+e[j]; } t=t0+h; return t; /* End of ssrkf45 */ }
Program 2.2.14 Integrator ssrkf45 for a RKF45 step The header file for rkc4a , rkc4b, ssrkc4, rkf45a , rkf45b, and ssrkf45 is as follows: /* Routines for the RK Integration */
Copyright 2004 by Chapman & Hall/CRC
#include <math.h> #define SIZE 500 double rkc4a(int neqn, double t0, double tf, double u[], double u0[], int nsteps); double rkc4b(int neqn, double t0, double tf, double u[], double u0[], int nsteps,double abserr, double relerr); double ssrkc4(int neqn, double t0, double u0[], double h, double u[], double e[]); double rkf45a(int neqn, double t0, double tf, double u[], double u0[], int nsteps); double rkf45b(int neqn, double t0, double tf, double u[], double u0[], int nsteps, double abserr, double relerr); double ssrkf45(int neqn, double t0, double u0[], double h, double u[], double e[]);
Program 2.2.15 Header file for rkc4a , rkc4b, ssrkc4, rkf45a , rkf45b, ssrkf45 This completes the discussion of the C solution of the 1x1 ODE problem of Equations 1.3 to 1.5. We now proceed to the programming of this problem in C++.
2.3
Programming in C++
We present the programming for the remaining languages according to the organizational principle discussed previously; i.e., the general-purpose routines are listed first, followed by the application-specific routines, and then possibly any routines that are somewhere in between. To conclude the discussion of a specific language, we then list the output from the the entire group of routines. Thus, we start with the listing of the main program and ODE integrator routines:
Copyright 2004 by Chapman & Hall/CRC
/* Numerical solution to the 1 x 1 ODE system by six integrators */ /* Include headers */ #include <math.h> #include <stdlib.h> #include
#include #include "ode1x1.h" #include "Euler.h" #include "RK.h"
/* Main program */ void main() { /* Type variables */ double tp; int i,j,ncase; /* Derive objects */ DEF o1; Euler e1; RK rk; /* Open a file for output */ ofstream fout("ode1x1cpp.out",ios::out); /* Step through six integrators */ for(ncase=1;ncase<=6;ncase++) { /* ODE integration parameters */ o1.intpar(); /* Initial condition */ o1.inital(); /* Output interval */ tp=o1.tf-o1.t0;
Copyright 2004 by Chapman & Hall/CRC
/* Step through nout grid points */ for(j=1;j<=o1.nout;j++) { /* Print solution */ o1.fprint(fout,ncase,o1.neqn,o1.t0,o1.u0); /* Select ODE integrator */ switch(ncase) { /* Fixed step modified Euler integrator */ case 1: e1.euler2a(o1.neqn,o1.t0,o1.tf,o1.u,o1.u0, o1.nsteps); break; /* Variable step modified Euler integrator */ case 2: e1.euler2b(o1.neqn,o1.t0,o1.tf,o1.u,o1.u0, o1.nsteps,o1.abserr,o1.relerr); break; /* Fixed step classical fourth order RK integrator */ case 3: rk.rkc4a(o1.neqn,o1.t0,o1.tf,o1.u,o1.u0, o1.nsteps); break; /* Variable step classical fourth order RK integrator */ case 4: rk.rkc4b(o1.neqn,o1.t0,o1.tf,o1.u,o1.u0, o1.nsteps,o1.abserr,o1.relerr); break; /* Fixed step RK Fehlberg (RKF45) integrator */ case 5: rk.rkf45a(o1.neqn,o1.t0,o1.tf,o1.u,o1.u0, o1.nsteps); break; /* Variable step RK Fehlberg (RKF45) integrator */
Copyright 2004 by Chapman & Hall/CRC
case 6: rk.rkf45b(o1.neqn,o1.t0,o1.tf,o1.u,o1.u0, o1.nsteps,o1.abserr,o1.relerr); break; } /* Advance solution */ o1.t0=o1.tf; o1.tf+=tp; for(i=1;i<=o1.neqn;i++) { o1.u0[i]=o1.u[i]; } /* Next output */ } /* Next integrator */ } /* Complete solution computed. Close output file */ fout<<endl; fout.close(); /* End of main */ }
Program 2.3.1 C++ main program for the numerical integration of Equation 1.3 subject to initial condition Equation 1.4 Note, again, as in Program 2.2.1, that all six integrators are called within a single loop. /* Step through six integrators */ for(ncase=1;ncase<=6;ncase++)
Also, an output file, ode1x1cpp.out is written by this main program. The associated header files are as follows: /* Definition of functions intpar, inital, derv, fprint for the 1 x 1 ODE system
Copyright 2004 by Chapman & Hall/CRC
*/ /* Include headers */ #include <math.h> #include <stdio.h> #include <stdlib.h> #include #include #include "MOL.h" /* Type variables as extern (global) */ extern FILE *fid; class DEF:public MOL { public: /* Integration parameters */ void intpar(); /* Initial condition */ void inital(); /* Derivative vector */ void derv(double ut[],double t,double u[]); /* Output */ void fprint(int ncase, int neqn, double t, double u[]); void fprint(ofstream &fout, int ncase, int neqn, double t, double u[]); };
/* Define the common (global) variables for the 1 x 1 ODE problem */ /* Maximum (default) number of ODEs */ #define SIZE 500
Copyright 2004 by Chapman & Hall/CRC
class MOL { public: /* Variables for ODE integration */ int neqn, nout, nsteps; double t0, tf, abserr, relerr; double u[SIZE], u0[SIZE], e[SIZE]; };
Program 2.3.2 Header files ode1x1.h and MOL .h used by Program 2.3.1 The ODE integration routines are listed below (ode1x1.h is copied to DE F.h before execution): #include "DEF.h" #include "Euler.h" double Euler::euler2a(int neqn, double t0, double tf, double u[], double u0[], int nsteps) /* Function euler2a computes an ODE solution by the fixed step modified Euler method for a series of points along the solution by repeatedly calling function sseuler for a single modified Euler step. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
number of modified Euler steps
u
ODE solution vector of length neqn after nsteps steps
Copyright 2004 by Chapman & Hall/CRC
*/ { /* Type variables */ double e[SIZE]; double h,t; int i,j; /* Integration step */ h=(tf-t0)/nsteps; /* nsteps modified Euler steps */ for(i=1;i<=nsteps;i++) { /* Single modified Euler step */ t=sseuler(neqn,t0,u0,h,u,e); /* Reset base point values for next modified Euler step */ for(j=1;j<=neqn;j++) { u0[j]=u[j]; } t0=t; /* End for */ } return 0; /* End of euler2a */ }
Program 2.3.3 Integrator euler2a
double Euler::euler2b(int neqn, double t0, double tf, double u[], double u0[], int nsteps, double abserr, double relerr) /* Function euler2b computes an ODE solution by the variable step modified Euler method for a series of points along the solution by repeatedly calling function sseuler for a single modified Euler step. The truncation error is
Copyright 2004 by Chapman & Hall/CRC
estimated along the solution to adjust the integration step according to a specified error tolerance. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
number of modified Euler steps
u
ODE solution vector of length neqn after nsteps steps
*/ { /* Type variables */ double h, hmin,t; int i, nfin1; double e[SIZE]; /* Integration step */ h=(tf-t0)/8.0; /* Minimum allowable step */ hmin=(tf-t0)/(float)(nsteps); /* Start integration */ t=t0; /* While independent variable is less than the final */ while(t <= tf*0.999) { /* If the next step along the solution will go past the final value of the independent variable, set the step to the remaining distance to the final value */ if((t+h) > tf) { h=tf-t; } /* Single modified Euler step */ t=sseuler(neqn,t0,u0,h,u,e);
Copyright 2004 by Chapman & Hall/CRC
/* Flag for the end of the integration */ nfin1=1; /* Check if any of the ODEs have violated the error criterion */ for(i=1;i<=neqn;i++) { if(fabs(e[i]) > (fabs(u[i])*relerr+abserr)) { /* Error violation, so integration is incomplete. Reduce integration step because of error violation and repeat integration from base point */ nfin1=0; h=h/2.0; /* If the current step is less than the minimum allowable step, set the step to the minimum allowable value and continue integration from new base point */ if(h< hmin) { h = (float)hmin; nfin1=1; } break; } } /* If there is no error violation, continue the integration from the new base point */ if(nfin1 == 1) { for(i=1;i<=neqn;i++) { u0[i]=u[i]; } t0=t; /* Test if integration step can be increased */ for(i=1;i<=neqn;i++) {
if(fabs(e[i]) > ((fabs(u[i])*relerr+abserr)/4.0)) { /* Integration step cannot be increased */
Copyright 2004 by Chapman & Hall/CRC
nfin1=0; break; } } /* Increase integration step */ if(nfin1 == 1) { h=h*2.0; } /* End if */ } /* End while */ } return 0; /* End of euler2b */ }
Program 2.3.4 Integrator euler2b
double Euler::sseuler(int neqn, double t0, double u0[], double h, double u[], double e[]) /* Function sseuler computes an ODE solution by the modified Euler method for one step along the solution (by calls to derv to define the ODE derivative vector). It also estimates the truncation error of the solution, and applies this estimate as a correction to the solution vector. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
u0
initial condition vector of length neqn
h
integration step
Copyright 2004 by Chapman & Hall/CRC
t
independent variable
u
ODE solution vector of length neqn after one modified Euler step
e
estimate of truncation error of the solution vector
*/ { /* Type variables */ double ut0[SIZE], ut[SIZE]; double t; int j; /* Derivative vector at initial (base) point */ derv(ut0,t0,u0); /* First order (Euler) step */ for(j=1;j<=neqn;j++) { u[j]=u0[j]+ut0[j]*h; } t=t0+h; /* Derivative at advance point */ derv(ut,t,u); /* Second order (modified Euler) step */ for(j=1;j<=neqn;j++) { /* Truncation error estimate */ e[j]=(ut[j]-ut0[j])*h/2.0; /* Second order (modified Euler) solution vector */ u[j]=u[j]+e[j]; } return t; /* End of sseuler */ }
Program 2.3.5 Integrator sseuler for a single modified Euler step
Copyright 2004 by Chapman & Hall/CRC
/* Routines for the Euler ODE Integration */ class Euler:public DEF { public: double euler2a(int neqn, double t0, double tf, double u[], double u0[], int nsteps); double euler2b(int neqn, double t0, double tf, double u[], double u0[], int nsteps, double abserr, double relerr); double sseuler(int neqn, double t0, double u0[], double h, double u[], double e[]); };
Program 2.3.6 Header file euler.h for euler2a , euler2b, and sseuler
#include "DEF.h" #include "RK.h" double RK::rkc4a(int neqn, double t0, double tf, double u[], double u0[], int nsteps) /* Function rkc4a computes an ODE solution by the fixed step classical fourth order RK method for a series of points along the solution by repeatedly calling function ssrkc4 for a single classical fourth order RK step. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
Copyright 2004 by Chapman & Hall/CRC
u0
initial condition vector of length neqn
nsteps
number of rkc4 steps
u
ODE solution vector of length neqn after nsteps steps
*/ { /* Type variables */ int i, j; double t, h, e[SIZE]; /* Integration step */ h=(tf-t0)/nsteps; /* nsteps rkc4 steps */ for(i=1;i<=nsteps;i++) { /* Single rkc4 step */ t=ssrkc4(neqn,t0,u0,h,u,e); /* Reset base point values for next rk4c step */ for(j=1;j<=neqn;j++) { u0[j]=u[j]; } t0=t; } return 0; /* End of rkc4a */ }
Program 2.3.7 Integrator rkc4a double RK::rkc4b(int neqn, double t0, double tf, double u[], double u0[], int nsteps, double abserr, double relerr) /* Function rkc4b computes an ODE solution by a variable step classical fourth order RK method for a series of points along the solution by repeatedly calling function
Copyright 2004 by Chapman & Hall/CRC
ssrkc4 for a single classical fourth order RK step. The truncation error is estimated along the solution to adjust the integration step according to a specified error tolerance. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
maximum number of rkc4 steps
abserr
absolute error tolerance
relerr
relative error tolerance
u
ODE solution vector of length neqn after nsteps steps
*/ { /* Type variables */ double h, hmin, t, e[SIZE]; int nfin1, i; /* Integration step */ h=(tf-t0)/2.0; /* Minimum allowable step */ hmin=(tf-t0)/nsteps; /* Start integration */ t=t0; /* While independent variable is less than the final value, continue the integration */ while(t <= (tf*0.999)) { /* If the next step along the solution will go past the final value of the independent variable, set the step to the remaining distance to the final value */
Copyright 2004 by Chapman & Hall/CRC
if((t+h) > tf) { h=tf-t; } /* Single rkc4 step */ t=ssrkc4(neqn,t0,u0,h,u,e); /* Flag for the end of the integration */ nfin1=1; /* Check if any of the ODEs have violated the error criterion */ for(i=1;i<=neqn;i++) { if(fabs(e[i]) > (fabs(u[i])*relerr+abserr)) { /* Error violation, so integration is incomplete. Reduce integration step because of error violation and repeat integration from base point */ nfin1=0; h=h/2.0; /* If the current step is less than the minimum allowable step, set the step to the minimum allowable value and continue integration from new base point */ if(h < hmin) { h=hmin; nfin1=1; } break; } } /* If there is no error violation, continue the integration from the new base point */ if(nfin1 == 1) { for(i=1;i<=neqn;i++) { u0[i]=u[i]; } t0=t;
Copyright 2004 by Chapman & Hall/CRC
/* Test if integration step can be increased */ for(i=1;i<=neqn;i++) { if(fabs(e[i]) > ((fabs(u[i])*relerr +abserr)/16.0)) {
/* Integration step cannot be increased */ nfin1=0; break; } } /* Increase integration step */ if(nfin1 == 1) { h=h*2.0; } /* End if */ } /* End while */ } return 0; /* End of rkc4b */ }
Program 2.3.8 Integrator rkc4b double RK::ssrkc4(int neqn, double t0, double u0[], double h, double u[], double e[]) /* Function ssrkc4 computes an ODE solution by the classical fourth order RK method for one step along the solution (by calls to derv to define the ODE derivative vector). It also estimates the truncation error of the solution, and applies this estimate as a correction to the solution vector.
Copyright 2004 by Chapman & Hall/CRC
Argument list neqn
number of first order ODEs
t0
initial value of independent variable
u0
initial condition vector of length neqn
h
integration step
t
independent variable
u
ODE solution vector of length neqn after one rkc4 step
e
estimate of truncation error of the solution vector
*/ { /* Type variables */ double ut0[SIZE], ut[SIZE], u4[SIZE]; double t, k1[SIZE], k2[SIZE], k3[SIZE], k4[SIZE]; int j; /* Derivative vector at initial (base) point */ derv(ut0,t0,u0); /* k1; stepping for k2 */ for(j=1;j<=neqn;j++) { k1[j]=h*ut0[j]; u[j]=u0[j]+0.5*k1[j]; } t=t0+0.5*h; /* Derivative vector at next RK point */ derv(ut,t,u); /* k2; stepping for k3 */ for(j=1;j<=neqn;j++) { k2[j]=h*ut[j]; u[j]=u0[j]+0.5*k2[j]; } t=t0+0.5*h;
Copyright 2004 by Chapman & Hall/CRC
/* Derivative vector at next RK point */ derv(ut,t,u); /* k3; stepping for k4 */ for(j=1;j<=neqn;j++) { k3[j]=h*ut[j]; u[j]=u0[j]+k3[j]; } t=t0+h; /* Derivative vector at next RK point */ derv(ut,t,u); /*k4; second and fourth order step; error estimate; error correction */ for(j=1;j<=neqn;j++) { k4[j]=h*ut[j]; u[j]=u0[j]+k2[j]; u4[j]=u0[j]+(1.0/6.0)*(k1[j]+2.0*k2[j]+2.0*k3[j] +k4[j]); e[j]=u4[j]-u[j]; u[j]=u[j]+e[j]; } t=t0+h; return t; /* End of ssrkc4 */ }
Program 2.3.9 Integrator ssrkc4 for a classical fourth-order RK step double RK::rkf45a(int neqn, double t0, double tf, double u[], double u0[], int nsteps) /* Function rkf45a computes an ODE solution by the fixed step RK Fehlberg 45 RK method for a series of points along the solution by repeatedly calling function ssrkf45 for a single RK Fehlberg 45 step.
Copyright 2004 by Chapman & Hall/CRC
Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
number of rkf45 steps
u
ODE solution vector of length neqn after nsteps steps
*/ { /* Type variables */ double t, h, e[SIZE]; int i, j; /* Integration step */ h=(tf-t0)/nsteps; /* nsteps rkf45 steps */ for(i=1;i<=nsteps;i++) { /* Single rkf45 step */ t=ssrkf45(neqn,t0,u0,h,u,e); /* Reset base point values for next rkf45 step */ for(j=1;j<=neqn;j++) { u0[j]=u[j]; } t0=t; /* End for */ }
return 0; /* End of rkf45a */ }
Program 2.3.10 Integrator rkf45a
Copyright 2004 by Chapman & Hall/CRC
double RK::rkf45b(int neqn, double t0, double tf, double u[],double u0[],int nsteps, double abserr,double relerr) /* Function rkf45b computes an ODE solution by a variable step classical RK Fehlberg 45 method for a series of points along the solution by repeatedly calling function ssrkf45 for a single RK Fehlberg 45 step. The truncation error is estimated along the solution to adjust the integration step according to a specified error tolerance. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
maximum number of rkf45 steps
abserr
absolute error tolerance
relerr
relative error tolerance
u
ODE solution vector of length neqn after nsteps steps
*/ { /* Type variables */ double h, hmin, t, e[SIZE]; int i, nfin1; /* Integration step */ h=(tf-t0)/2.0; /* Minimum allowable step */ hmin=(tf-t0)/nsteps; /* Start integration */ t=t0;
Copyright 2004 by Chapman & Hall/CRC
/* While independent variable is less than the final value, continue the integration */ while(t <= (tf*0.999)) { /* If the next step along the solution will go past the final value of the independent variable, set the step to the remaining distance to the final value */ if((t+h) > tf) { h=tf-t; } /* Single rkf45 step */ t=ssrkc4(neqn,t0,u0,h,u,e); /* Flag for the end of the integration */ nfin1=1; /* Check if any of the ODEs have violated the error criterion */ for (i=1;i<=neqn;i++) { if(fabs(e[i]) > (fabs(u[i])*relerr+abserr)) { /* Error violation, so integration is incomplete. Reduce integration step because of error violation and repeat integration from base point */ nfin1=0; h=h/2.0; /* If the current step is less than the minimum allowable step, set the step to the minimum allowable value and continue integration from new base point */ if(h < hmin) { h = hmin; nfin1=1; } break; } }
Copyright 2004 by Chapman & Hall/CRC
/* If there is no error violation, continue the integration from the new base point */ if(nfin1 == 1) { for(i=1;i<=neqn;i++) { u0[i]=u[i]; } t0=t; /* Test if integration step can be increased */ for(i=1;i<=neqn;i++) { if(fabs(e[i]) > ((fabs(u[i])*relerr +abserr)/32.0) ) { /* Integration step cannot be increased */ nfin1=0; break; } } /* Increase integration step */ if(nfin1 == 1) { h=h*2.0; } /* End if */ } /* End while */ } return 0; /* End of rkf45b */ }
Program 2.3.11 Integrator rkf45b double RK::ssrkf45(int neqn, double t0, double u0[], double h, double u[], double e[])
Copyright 2004 by Chapman & Hall/CRC
/* Function ssrkf45 computes an ODE solution by the RK Fehlberg 45 method for one step along the solution (by calls to derv to define the ODE derivative vector). It also estimates the truncation error of the solution, and applies this estimate as a correction to the solution vector. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
u0
initial condition vector of length neqn
h
integration step
t
independent variable
u
ODE solution vector of length neqn after one rkf45 step
e
estimate of truncation error of the solution vector
*/ { /* Type variables */ double t, ut0[SIZE], ut[SIZE], u5[SIZE]; double k1[SIZE], k2[SIZE], k3[SIZE], k4[SIZE], k5[SIZE], k6[SIZE]; int j; /* Derivative vector at initial (base) point */ derv(ut0,t0,u0); /* k1; stepping for k2 */ for(j=1;j<=neqn;j++) { k1[j]=h*ut0[j]; u[j]=u0[j]+0.25*k1[j]; } t=t0+0.25*h;
Copyright 2004 by Chapman & Hall/CRC
/* Derivative vector at next RK point */ derv(ut,t,u); /* k2; stepping for k3 */ for(j=1;j<=neqn;j++) { k2[j]=h*ut[j]; u[j]=u0[j]+(3.0/32.0)*k1[j] +(9.0/32.0)*k2[j]; } t=t0+(3.0/8.0)*h; /* Derivative vector at next RK point */ derv(ut,t,u); /* k3; stepping for k4 */ for(j=1;j<=neqn;j++) { k3[j]=h*ut[j]; u[j]=u0[j]+(1932.0/2197.0)*k1[j] -(7200.0/2197.0)*k2[j] +(7296.0/2197.0)*k3[j]; } t=t0+(12.00/13.0)*h; /* Derivative vector at next RK point */ derv(ut,t,u); /* k4; stepping for k5 */ for(j=1;j<=neqn;j++) { k4[j]=h*ut[j]; u[j]=u0[j]+( 439.0/ 216.0)*k1[j] -( 8.0 )*k2[j] +(3680.0/ 513.0)*k3[j] -( 845.0/4104.0)*k4[j]; } t=t0+h; /* Derivative vector at next RK point */ derv(ut,t,u); /* k5; stepping for k6 */ for(j=1;j<=neqn;j++) {
Copyright 2004 by Chapman & Hall/CRC
k5[j]=h*ut[j]; u[j]=u0[j]-( 8.0/ 27.0)*k1[j] +( 2.0 )*k2[j] -(3544.0/2565.0)*k3[j] +(1859.0/4104.0)*k4[j] -( 11.0/ 40.0)*k5[j]; } t=t0+0.5*h; /* Derivative vector at next RK point */ derv(ut,t,u); /* k6; fourth and fifth order step; error estimate; error correction */ for(j=1;j<=neqn;j++) { k6[j]=h*ut[j]; u[j]=u0[j]+( 25.0/ 216.0)*k1[j] +( 1408.0/2565.0)*k3[j] +( 2197.0/4104.0)*k4[j] -( 1.0/ 5.0)*k5[j]; u5[j]=u0[j]+( 16.0/ 135.0)*k1[j] +( 6656.0/12825.0)*k3[j] +(28561.0/56430.0)*k4[j] -( 9.0/ 50.0)*k5[j] +( 2.0/ 55.0)*k6[j]; e[j]=u5[j]-u[j]; u[j]= u[j]+e[j]; } t=t0+h; return t; /* End of ssrkf45 */ }
Program 2.3.12 Integrator ssrkf45 for an RKF45 step
/* Routines for the RK Integration */
Copyright 2004 by Chapman & Hall/CRC
class RK:public DEF { public: double rkc4a(int neqn, double t0, double tf, double u[], double u0[], int nsteps); double rkc4b(int neqn, double t0, double tf, double u[], double u0[], int nsteps, double abserr, double relerr); double ssrkc4(int neqn, double t0, double u0[], double h, double u[], double e[]); double rkf45a(int neqn, double t0, double tf, double u[], double u0[], int nsteps); double rkf45b(int neqn, double t0, double tf, double u[], double u0[], int nsteps, double abserr, double relerr); double ssrkf45(int neqn, double t0, double u0[], double h, double u[], double e[]); };
Program 2.3.13 Header file for rkc4a , rkc4b, ssrkc4, rkf45a , rkf45b, ssrk f 45 #include "DEF.h" #include /* Define file ID */ FILE *fid; void DEF::intpar() /* Function intpar sets the parameters to control the integration of the 1 x 1 ODE system */ { /* Number of ODEs */ neqn=1; /* Number of output points */ nout=6;
Copyright 2004 by Chapman & Hall/CRC
/* Maximum number of steps in the interval t0 to tf */ nsteps=100; /* Initial, final values of independent variable */ t0=0.0; tf=1.0; /* Error tolerances */ abserr=pow(10.0,-5.0); relerr=pow(10.0,-5.0); /* End of intpar */ }
void DEF::inital() /* Function inital sets the initial condition vector for the 1 x 1 ODE problem */ { /* Initial condition */ u0[1]=1.0; /* End of inital */ }
void DEF::derv(double ut[], double t, double u[]) /* Function derv computes the derivatives vector of the 1 x 1 ODE problem */ { /* Type variables */ double alpha, lambda; /* Problem parameters */ alpha=1.0; lambda=1.0; /* Derivative vector */ ut[1]=lambda*exp(-alpha*t)*u[1]; /* End of derv */ }
Copyright 2004 by Chapman & Hall/CRC
void DEF::fprint(int ncase, int neqn, double t, double u[]) /* Function fprint displays the numerical and exact solutions to the 1 x 1 ODE problem; this routine is implemented in the traditional C style*/ { /* Type variables */ double ue[2], diff[2]; double u0, alpha, lambda; /* Problem parameters */ u0=1.0; alpha=1.0; lambda=1.0; /* Print a heading for the solution at t = 0 */ if(t<=0.0) { /* Label for ODE integrator */ switch(ncase) { /*Fixed step modified Euler */ case 1: fprintf(fid,"\n\n euler2a integrator\n\n"); break; /* Variable step modified Euler */ case 2: fprintf(fid,"\n\n euler2b integrator\n\n"); break; /* Fixed step classical fourth order RK */ case 3: fprintf(fid,"\n\n rkc4a integrator\n\n"); break; /* Variable step classical fourth order RK */ case 4: fprintf(fid,"\n\n rkc4b integrator\n\n"); break; /* Fixed step RK Fehlberg 45 */ case 5: fprintf(fid,"\n\n rkf45a integrator\n\n"); break;
Copyright 2004 by Chapman & Hall/CRC
/* Variable step RK Fehlberg 45 */ case 6: fprintf(fid,"\n\n rkf45b integrator\n\n"); break; } /* Heading */ fprintf(fid,"\n
t
u1(num)
u1(ex)
diff1\n\n");
} /* Analytical solution */ ue[1]=u0*exp(lambda/alpha*(1.0-exp(-alpha*t))); /* Difference between exact and numerical solutions */ diff[1]=u[1]-ue[1]; /* Display the numerical and exact solutions, and their difference */ fprintf(fid,"%10.2f %10.5f %10.5f %13.4e\n\n",t,u[1], ue[1],diff[1]); /* End of fprint */ }
void DEF::fprint(ofstream &fout, int ncase, int neqn, double t, double u[]) /* Function fprint displays the numerical and exact solutions to the 1 x 1 ODE problem; this function is implemented in the C++ style */ { /* Type variables */ double ue[2], diff[2]; double u0, alpha, lambda; /* Problem parameters */ u0=1.0; alpha=1.0; lambda=1.0; /* Set printing format */ fout<<setiosflags(ios::showpoint|ios::fixed) <<setprecision(7);
Copyright 2004 by Chapman & Hall/CRC
/* Print a heading for the solution at t = 0 */ if(t<=0.0) { /* Label for ODE integrator */ switch(ncase) { /* Fixed step modified Euler */ case 1: fout<<"\n\n euler2a integrator\n"; break; /* Variable step modified Euler */ case 2: fout<<"\n\n euler2b integrator\n"; break; /* Fixed step classical fourth order RK */ case 3: fout<<"\n\n rkc4a integrator\n"; break; /* Variable step classical fourth order RK */ case 4: fout<<"\n\n rkc4b integrator\n"; break; /* Fixed step RK Fehlberg 45 */ case 5: fout<<"\n\n rkf45a integrator\n"; break; /* Variable step RK Fehlberg 45 */ case 6: fout<<"\n\n rkf45b integrator\n"; break; } /* Heading */ fout<<endl; fout<<" t"<<setw(18)<<"u1(num)"<<setw(11) <<"u1(ex)"<<setw(10)<<"diff"<<"\n"; /* End of t = 0 heading */ }
Copyright 2004 by Chapman & Hall/CRC
/* Analytical solution */ ue[1]=u0*exp(lambda/alpha*(1.0-exp(-alpha*t))); /* Difference between exact and numerical solutions */ diff[1]=u[1]-ue[1]; fout<<endl; /* Display the numerical and exact solutions, and their difference */ fout<<setw(10)<
Program 2.3.14 intpar, inital, derv, and fprint called in the solution of Equations 1.3 and 1.4
The output from the preceding routines (written to file ode1x1cpp.out in Program 2.3.1) is as follows: euler2a integrator t
u1(num)
u1(ex)
0.0000000 1.0000000 2.0000000 3.0000000 4.0000000 5.0000000
1.0000000 1.8815963 2.3742138 2.5862663 2.6689549 2.7000350
1.0000000 1.8815964 2.3742099 2.5862603 2.6689479 2.7000278
diff 0.0000000 -0.0000001 0.0000039 0.0000060 0.0000069 0.0000073
euler2b integrator t
u1(num)
u1(ex)
0.0000000 1.0000000 2.0000000 3.0000000 4.0000000 5.0000000
1.0000000 1.8815902 2.3742061 2.5862606 2.6689516 2.7000340
1.0000000 1.8815964 2.3742099 2.5862603 2.6689479 2.7000278
Copyright 2004 by Chapman & Hall/CRC
diff 0.0000000 -0.0000062 -0.0000038 0.0000003 0.0000037 0.0000062
rkc4a integrator t
u1(num)
u1(ex)
diff
0.0000000 1.0000000 2.0000000 3.0000000 4.0000000 5.0000000
1.0000000 1.8815964 2.3742099 2.5862603 2.6689479 2.7000278
1.0000000 1.8815964 2.3742099 2.5862603 2.6689479 2.7000278
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
rkc4b integrator t
u1(num)
u1(ex)
diff
0.0000000 1.0000000 2.0000000 3.0000000 4.0000000 5.0000000
1.0000000 1.8815963 2.3742100 2.5862604 2.6689480 2.7000279
1.0000000 1.8815964 2.3742099 2.5862603 2.6689479 2.7000278
0.0000000 0.0000000 0.0000001 0.0000001 0.0000001 0.0000001
rkf45a integrator t
u1(num)
u1(ex)
diff
0.0000000 1.0000000 2.0000000 3.0000000 4.0000000 5.0000000
1.0000000 1.8815964 2.3742099 2.5862603 2.6689479 2.7000278
1.0000000 1.8815964 2.3742099 2.5862603 2.6689479 2.7000278
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
rkf45b integrator t
u1(num)
u1(ex)
diff
0.0000000 1.0000000 2.0000000 3.0000000 4.0000000 5.0000000
1.0000000 1.8815963 2.3742100 2.5862604 2.6689480 2.7000279
1.0000000 1.8815964 2.3742099 2.5862603 2.6689479 2.7000278
0.0000000 0.0000000 0.0000001 0.0000001 0.0000001 0.0000001
We note again that the output verifies the operation of all six integrators. The fixed step integrators (rkc4a , rkf45a , and ssrkc4) generally surpass the error
Copyright 2004 by Chapman & Hall/CRC
tolerances set in intpar, with rkf45a and ssrkc4 producing excessive accuracy; the variable step integrators (rkc4b, rkf45b, and ssrkc4) produce numerical solutions consistent with the error tolerances. This completes the programming of the 1x1 ODE problem in C++. We now move on to Fortran.
2.4
Programming in Fortran
Again, we start with the listing of the main program and ODE integrator routines: program ode1x1 C C Numerical solution to the 1 x 1 ODE system by six C integrators C C Double precision coding is used implicit double precision(a-h,o-z) C C Size arrays parameter(neq=500) dimension u0(neq),u(neq) C C Open a file for output no=2 open(no,file='ode1x1for.out') C C Step through six integrators do ncase=1,6 C C Integration parameters call intpar(neqn,nout,nsteps,t0,tf,abserr,relerr) C C Initial condition call inital(neqn,t0,u0) C C Output time tp=tf-t0 C C Step through nout grid points do j=1,nout C C Print solution call fprint(no,ncase,neqn,t0,u0)
Copyright 2004 by Chapman & Hall/CRC
C C C C
C C
C C
C C
C C
C C
Select ODE integrator Fixed step modified Euler integrator if(ncase.eq.1)then call euler2a(neqn,t0,tf,u0,nsteps,u) end if Variable step modified Euler integrator if(ncase.eq.2)then call euler2b(neqn,t0,tf,u0,nsteps,abserr, relerr,u) end if Fixed step classical fourth order RK integrator if(ncase.eq.3)then call rkc4a(neqn,t0,tf,u0,nsteps,u) end if Variable step classical fourth order RK integrator if(ncase.eq.4)then call rkc4b(neqn,t0,tf,u0,nsteps,abserr,relerr,u) end if Fixed step RK Fehlberg (RKF45) integrator if(ncase.eq.5)then call rkf45a(neqn,t0,tf,u0,nsteps,u) end if Variable step Fehlberg (RKF45) integrator if(ncase.eq.6)then call rkf45b(neqn,t0,tf,u0,nsteps,abserr, relerr,u) end if
C C Advance solution t0=tf tf=tf+tp do i=1,neqn u0(i)=u(i) end do C C Next output end do C
Copyright 2004 by Chapman & Hall/CRC
C Next integrator end do C C End of ode1x1 end
Program 2.4.1 Fortran main program ode1x1 for the numerical integration of Equation 1.3 subject to initial condition Equation 1.4 Note again, as in Programs 2.2.1 and 2.3.1, that all six integrators are called within a single loop. C C Step through six integrators do ncase=1,6
Also, an output file, ode1x1f.out is written by this main program. The ODE integration routines are listed below: subroutine euler2a(neqn,t0,tf,u0,nsteps,u) C C C C C C C C C C C C C C C C C C C C C C
Subroutine euler2a computes an ODE solution by a fixed step modified Euler method for a series of points along the solution by repeatedly calling subroutine sseuler for a single modified Euler step. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
number of modified Euler steps
u
ODE solution vector of length neqn after nsteps steps
Double precision coding is used implicit double precision(a-h,o-z)
C C Size arrays dimension u0(neqn), u(neqn), e(neqn)
Copyright 2004 by Chapman & Hall/CRC
C C Integration step h=(tf-t0)/dfloat(nsteps) C C nsteps modified Euler steps do j=1,nsteps C C Modified Euler step call sseuler(neqn,t0,u0,h,t,u,e) C C Reset base point values for the next modified C Euler step do i=1,neqn u0(i)=u(i) end do t0=t C C Next modified Euler step end do C C nsteps modified Euler steps completed return C C End of euler2a end
Program 2.4.2 Integrator euler2a subroutine euler2b(neqn,t0,tf,u0,nsteps,abserr,relerr,u) C C C C C C C C C C C C C C
Subroutine euler2b computes an ODE solution by a variable step modified Euler method for a series of points along the solution by repeatedly calling subroutine sseuler for a single modified Euler step. The truncation error is estimated along the solution to adjust the integration step according to a specified error tolerance. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
Copyright 2004 by Chapman & Hall/CRC
C tf final value of independent variable C C u0 initial condition vector of length neqn C C nsteps maximum number of modified Euler steps C C abserr absolute error tolerance C C relerr relative error tolerance C C u ODE solution vector of length neqn after C nsteps steps C C Double precision coding is used implicit double precision(a-h,o-z) C C Size the arrays parameter(neq=500) dimension e(neq) dimension u0(neqn), u(neqn) C C Initial integration step h=(tf-t0)/8.0d0 C C Minimum allowable step hmin=(tf-t0)/dfloat(nsteps) C C Start integration t=t0 C C While independent variable is less than the final C value, continue the integration do while(t.le.tf*0.999d0) C C If the next step along the solution will go past C the final value of the independent variable, set C the step to the remaining distance to the final C value if((t+h).gt.tf)then h=tf-t end if C C Single modified Euler step call sseuler(neqn,t0,u0,h,t,u,e) C
Copyright 2004 by Chapman & Hall/CRC
C
Flag for the end of the integration nfin1=1
C C C
Check if any of the ODEs have violated the error criteria do i=1,neqn if(dabs(e(i)).gt.dabs(u(i))*relerr+abserr)then
C C C C C
Error violation, so integration is not complete. Reduce integration step because of error violation and repeat integration from the base point nfin1=0 h=h/2.0d0
C C C C C
If the current step is less than the minimum allowable step, set the step to the minimum allowable value and continue integration from new base point if(h.lt.hmin)then h=hmin nfin1=1 end if go to 1 end if end do
C C C 1
C C
C C
If there is no error violation, continue the integration from new base point if(nfin1.eq.1)then do i=1,neqn u0(i)=u(i) end do t0=t Test if integration step can be increased do i=1,neqn if(dabs(e(i)).gt.(dabs(u(i))*relerr+abserr) /4.0d0)then Integration step cannot be increased go to 2 end if end do
C
Copyright 2004 by Chapman & Hall/CRC
C
Increase integration step h=h*2.0d0
C C
Continue for no error violation (nfin1=1) end if
2
C C Continue do while end do return C C End of euler2b end
Program 2.4.3 Integrator euler2b subroutine sseuler(neqn,t0,u0,h,t,u,e) C C C C C C C C C C C C C C C C C C C C C C C C C C C
Subroutine sseuler computes an ODE solution by the modified Euler method for one step along the solution (by calls to derv to define the ODE derivative vector). It also estimates the truncation error of the solution, and applies this estimate as a correction to the solution vector. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
u0
initial condition vector of length neqn
h
integration step
t
independent variable
u
ODE solution vector of length neqn after one modified Euler step
e
estimate of truncation error of the solution vector Double precision coding is used implicit double precision(a-h,o-z)
Copyright 2004 by Chapman & Hall/CRC
C C
Size the arrays parameter(neq=500) dimension ut(neq),ut0(neq) dimension u0(neqn), u(neqn),
e(neqn) C C Derivative vector at initial (base) point call derv(neqn,t0,u0,ut0) C C First order (Euler) step do i=1,neqn u(i)=u0(i)+ut0(i)*h end do t=t0+h C C Derivative vector at advance point call derv(neqn,t,u,ut) C C Second order step do i=1,neqn C C Truncation error estimate e(i)=(ut(i)-ut0(i))*h/2.0d0 C C Second order (modified Euler) solution vector u(i)=u(i)+e(i) end do return C C End of sseuler end
Program 2.4.4 Integrator sseuler for a single modified Euler step subroutine rkc4a(neqn,t0,tf,u0,nsteps,u) C C C C C C C C C
Subroutine rkc4a computes an ODE solution by a fixed step rkc4 method for a series of points along the solution by repeatedly calling subroutine ssrkc4 for a single rkc4 step. Argument list neqn
number of first order ODEs
Copyright 2004 by Chapman & Hall/CRC
C C t0 initial value of independent variable C C tf final value of independent variable C C u0 initial condition vector of length neqn C C nsteps number of rkc4 steps C C u ODE solution vector of length neqn after C nsteps steps C C Double precision coding is used implicit double precision(a-h,o-z) C C Size arrays parameter(neq=500) dimension e(neq) dimension u0(neqn), u(neqn) C C Integration step h=(tf-t0)/dfloat(nsteps) C C nsteps rkc4 steps do j=1,nsteps C C Single rkc4 step call ssrkc4(neqn,t0,u0,h,t,u,e) C C Reset base point values for the next rkc4 step do i=1,neqn u0(i)=u(i) end do t0=t C C Next rkc4 step end do C C nsteps rkc4 steps completed return C C End of rkc4a end
Program 2.4.5 Integrator rkc4a
Copyright 2004 by Chapman & Hall/CRC
subroutine rkc4b(neqn,t0,tf,u0,nsteps,abserr,relerr,u) C C C C C C C C C C C C C C C C C C C C C C C C C C C C C
Subroutine rkc4b computes an ODE solution by a variable step classical fourth order RK method for a series of points along the solution by repeatedly calling subroutine ssrkc4 for a single classical fourth order RK step. The truncation error is estimated along the solution to adjust the integration step according to a specified error tolerance. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
maximum number of rkc4 steps
abserr
absolute error tolerance
relerr
relative error tolerance
u
ODE solution vector of length neqn after nsteps steps
Double precision coding is used implicit double precision(a-h,o-z)
C C Size the arrays parameter(neq=500) dimension e(neq) dimension u0(neqn), u(neqn) C C Integration step h=(tf-t0)/2.0d0 C C Minimum allowable step hmin=(tf-t0)/dfloat(nsteps) C C Start integration t=t0
Copyright 2004 by Chapman & Hall/CRC
C C While independent variable is less than the final C value, continue the integration do while(t.le.tf*0.999d0) C C If the next step along the solution will go past C the final value of the independent variable, set C the step to the remaining distance to the final C value if((t+h).gt.tf)then h=tf-t end if C C Single rkc4 step call ssrkc4(neqn,t0,u0,h,t,u,e) C C Flag for the end of the integration nfin1=1 C C Check if any of the ODEs have violated the error C critreria do i=1,neqn if(dabs(e(i)).gt.dabs(u(i))*relerr+abserr)then C C Error violation, so integration is not C complete. Reduce integration step because C of error violation and repeat integration C from the base point nfin1=0 h=h/2.0d0 C C If the current step is less than the minimum C allowable step, set the step to the minimum C allowable value and continue integration from C new base point if(h.lt.hmin)then h=hmin nfin1=1 end if go to 1 end if end do C C If there is no error violation, continue the C integration from new base point
Copyright 2004 by Chapman & Hall/CRC
1
C C
if(nfin1.eq.1)then do i=1,neqn u0(i)=u(i) end do t0=t Test if integration step can be increased do i=1,neqn if(dabs(e(i)).gt.(dabs(u(i))*relerr+abserr) /16.0d0)then
C C
Integration step cannot be increased go to 2 end if end do
C C
Increase integration step h=h*2.0d0
C C 2
Continue for no error violation (nfin1=1) end if
C C Continue do while end do return C C End of rkc4b end
Program 2.4.6 Integrator rkc4b subroutine ssrkc4(neqn,t0,u0,h,t,u,e) C C C C C C C C C C C C
Subroutine ssrkc4 computes an ODE solution by the classical fourth order RK method for one step along the solution (by calls to derv to define the ODE derivative vector). It also estimates the truncation error of the solution, and applies this estimate as a correction to the solution vector. Argument list neqn
number of first order ODEs
Copyright 2004 by Chapman & Hall/CRC
C t0 initial value of independent variable C C u0 initial condition vector of length neqn C C h integration step C C t independent variable C C u ODE solution vector of length neqn after C one rkc4 step C C e estimate of truncation error of the soluC tion vector C C Double precision coding is used implicit double precision(a-h,k,o-z) C C Size the arrays parameter(neq=500) dimension ut0(neq), ut(neq), u4(neq), + k1(neq), k2(neq), k3(neq), k4(neq) dimension u0(neqn), u(neqn), e(neqn) C C Derivative vector at initial (base) point call derv(neqn,t0,u0,ut0) C C k1, advance of dependent variable vector and C independent variable for calculation of k2 do i=1,neqn k1(i)=h*ut0(i) u(i)=u0(i)+0.5d0*k1(i) end do t=t0+0.5d0*h C C Derivative vector at new u, t call derv(neqn,t,u,ut) C C k2, advance of dependent variable vector and C independent variable for calculation of k3 do i=1,neqn k2(i)=h*ut(i) u(i)=u0(i)+0.5d0*k2(i) end do t=t0+0.5d0*h C
Copyright 2004 by Chapman & Hall/CRC
C Derivative vector at new u, t call derv(neqn,t,u,ut) C C k3, advance of dependent variable vector and C independent variable for calculation of k4 do i=1,neqn k3(i)=h*ut(i) u(i)=u0(i)+k3(i) end do t=t0+h C C Derivative vector at new u, t call derv(neqn,t,u,ut) C C k4, stepping do i=1,neqn k4(i)=h*ut(i) C C Second order step u(i)=u0(i)+k2(i) C C Fourth order step u4(i)=u0(i)+(1.0d0/6.0d0)*(k1(i)+2.0d0*k2(i) +2.0d0*k3(i)+k4(i)) end do do i=1,neqn C C Truncation error estimate e(i)=u4(i)-u(i) C C Fourth order solution vector (from (2,4) RK pair) u(i)=u(i)+e(i) end do return C C End of ssrkc4 end
Program 2.4.7 Integrator ssrkc4 for a classical fourth-order RK step subroutine rkf45a(neqn,t0,tf,u0,nsteps,u) C C Subroutine rkf45a computes an ODE solution by a fixed C step rkf45 method for a series of points along the
Copyright 2004 by Chapman & Hall/CRC
C C C C C C C C C C C C C C C C C C C
solution by repeatedly calling subroutine ssrkf45 for a single rkf45 step. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
number of rkf45 steps
u
ODE solution vector of length neqn after nsteps steps
Double precision coding is used implicit double precision(a-h,o-z)
C C C Size arrays parameter(neq=500) dimension e(neq) dimension u0(neqn), u(neqn) C C Integration step h=(tf-t0)/dfloat(nsteps) C C nsteps rkf45 steps do j=1,nsteps C C Single rkf45 step call ssrkf45(neqn,t0,u0,h,t,u,e) C C Reset base point values for the next rkf45 step do i=1,neqn u0(i)=u(i) end do t0=t C C Next rkf45 step end do C
Copyright 2004 by Chapman & Hall/CRC
C nsteps rkf45 steps completed return C C End of rkf45a end
Program 2.4.8 Integrator rkf45a
subroutine rkf45b(neqn,t0,tf,u0,nsteps,abserr,relerr,u) C C C C C C C C C C C C C C C C C C C C C C C C C C C C
Subroutine rkf45b computes an ODE solution by a variable step RK Fehlberg method for a series of points along the solution by repeatedly calling subroutine ssrkf45 for a single RK Fehlberg step. The truncation error is estimated along the solution to adjust the integration step according to a specified error tolerance. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
maximum number of rkf45 steps
abserr
absolute error tolerance
relerr
relative error tolerance
u
ODE solution vector of length neqn after nsteps steps
Double precision coding is used implicit double precision(a-h,o-z)
C C Size the arrays parameter(neq=500) dimension e(neq) dimension u0(neqn), u(neqn) C
Copyright 2004 by Chapman & Hall/CRC
C Initial integration step h=(tf-t0)/2.0d0 C C Minimum allowable step hmin=(tf-t0)/dfloat(nsteps) C C Start integration t=t0 C C While independent variable is less than the final C value, continue the integration do while(t.le.tf*0.999d0) C C If the next step along the solution will go past C the final value of the independent variable, set C the step to the remaining distance to the final C value if((t+h).gt.tf)then h=tf-t end if C C Single rkf45 step call ssrkf45(neqn,t0,u0,h,t,u,e) C C Flag for the end of the integration nfin1=1 C C Check if any of the ODEs have violated the error C criteria do i=1,neqn if(dabs(e(i)).gt.dabs(u(i))*relerr+abserr)then C C Error violation, so integration is not C complete. Reduce integration step because C of error violation and repeat integration C from the base point nfin1=0 h=h/2.0d0 C C If the current step is less than the minimum C allowable step, set the step to the minimum C allowable value and continue integration from C new base point if(h.lt.hmin)then h=hmin
Copyright 2004 by Chapman & Hall/CRC
nfin1=1 end if go to 1 end if end do C C C 1
C C
If there is no error violation, continue the integration from new base point if(nfin1.eq.1)then do i=1,neqn u0(i)=u(i) end do t0=t Test if integration step can be increased do i=1,neqn if(dabs(e(i)).gt.(dabs(u(i))*relerr+abserr) /32.0d0)then
C C
Integration step cannot be increased go to 2 end if end do
C C
Increase integration step h=h*2.0d0
C C 2
Continue for no error violation (nfin1=1) end if
C C Continue do while end do return C C End of rkf45b end
Program 2.4.9 Integrator rkf45b subroutine ssrkf45(neqn,t0,u0,h,t,u,e) C C Subroutine ssrkf45 computes an ODE solution by the RK C Fehlberg 45 method for one step along the solution (by C calls to derv to define the ODE derivative vector).
Copyright 2004 by Chapman & Hall/CRC
C C C C C C C C C C C C C C C C C C C C C C C
It also estimates the truncation error of the solution, and applies this estimate as a correction to the solution vector. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
u0
initial condition vector of length neqn
h
integration step
t
independent variable
u
ODE solution vector of length neqn after one rkf45 step
e
estimate of truncation error of the solution vector
Double precision coding is used implicit double precision(a-h,k,o-z)
C C Size the arrays parameter(neq=500) dimension ut0(neq), ut(neq), u5(neq), + k1(neq), k2(neq), k3(neq), + k4(neq), k5(neq), k6(neq) dimension u0(neqn), u(neqn), e(neqn) C C Derivative vector at initial (base) point call derv(neqn,t0,u0,ut0) C C k1, advance of dependent variable vector and C independent variable for calculation of k2 do i=1,neqn k1(i)=h*ut0(i) u(i)=u0(i)+0.25d0*k1(i) end do t=t0+0.25d0*h C C Derivative vector at new u, t call derv(neqn,t,u,ut)
Copyright 2004 by Chapman & Hall/CRC
C C k2, advance of dependent variable vector and C independent variable for calculation of k3 do i=1,neqn k2(i)=h*ut(i) u(i)=u0(i)+(3.0d0/32.0d0)*k1(i) + +(9.0d0/32.0d0)*k2(i) end do t=t0+(3.0d0/8.0d0)*h C C Derivative vector at new u, t call derv(neqn,t,u,ut) C C k3, advance of dependent variable vector and C independent variable for calculation of k4 do i=1,neqn k3(i)=h*ut(i) u(i)=u0(i)+(1932.0d0/2197.0d0)*k1(i) + -(7200.0d0/2197.0d0)*k2(i) + +(7296.0d0/2197.0d0)*k3(i) end do t=t0+(12.0d0/13.0d0)*h C C Derivative vector at new u, t call derv(neqn,t,u,ut) C C k4, advance of dependent variable vector and C independent variable for calculation of k5 do i=1,neqn k4(i)=h*ut(i) u(i)=u0(i)+( 439.0d0/ 216.0d0)*k1(i) + -( 8.0d0 )*k2(i) + +(3680.0d0/ 513.0d0)*k3(i) + -( 845.0d0/4104.0d0)*k4(i) end do t=t0+h C C Derivative vector at new u, t call derv(neqn,t,u,ut) C C k5, advance of dependent variable vector and C independent variable for calculation of k6 do i=1,neqn k5(i)=h*ut(i)
Copyright 2004 by Chapman & Hall/CRC
u(i)=u0(i)-( 8.0d0/ 27.0d0)*k1(i) +( 2.0d0 )*k2(i) -(3544.0d0/2565.0d0)*k3(i) +(1859.0d0/4104.0d0)*k4(i) -( 11.0d0/ 40.0d0)*k5(i) end do t=t0+0.5d0*h
+ + + +
C C Derivative vector at new u, t call derv(neqn,t,u,ut) C C k6, stepping do i=1,neqn k6(i)=h*ut(i) C C Fourth order step u(i)=u0(i)+( 25.0d0 / 216.0d0)*k1(i) + +(1408.0d0 / 2565.0d0)*k3(i) + +(2197.0d0 / 4104.0d0)*k4(i) + -( 1.0d0 / 5.0d0)*k5(i) C C Fifth order step u5(i)=u0(i)+( 16.0d0/ 135.0d0)*k1(i) + +( 6656.0d0/12825.0d0)*k3(i) + +(28561.0d0/56430.0d0)*k4(i) + -( 9.0d0/ 50.0d0)*k5(i) + +( 2.0d0/ 55.0d0)*k6(i) end do do i=1,neqn C C Truncation error estimate e(i)=u5(i)-u(i) C C Fifth order solution vector (from (4,5) RK pair) u(i)=u(i)+e(i) end do t=t0+h return C C End of ssrkf45 end
Program 2.4.10 Integrator ssrkf45 for an RKF45 step
Copyright 2004 by Chapman & Hall/CRC
subroutine intpar(neqn,nout,nsteps,t0,tf,abserr,relerr) C C Subroutine intpar sets the parameters to control the C integration of the 1 x 1 ODE system C C Double precision coding is used implicit double precision(a-h,o-z) C C Number of ODEs neqn=1 C C Number of output points nout=6 C C Maximum number of steps in the interval t0 to tf nsteps=100 C C Initial, final values of the independent variable t0=0.0d0 tf=1.0d0 C C Error tolerances abserr=1.0d-05 relerr=1.0d-05 return C C End of intpar end
subroutine inital(neqn,t,u0) C C Subroutine inital sets the initial condition vector C for the 1 x 1 ODE problem C C Double precision coding is used implicit double precision(a-h,o-z) C C Size the arrays dimension u0(neqn) C C Initial condition u0(1)=1.0d0 return C
Copyright 2004 by Chapman & Hall/CRC
C End of inital end
subroutine derv(neqn,t,u,ut) C C Subroutine derv computes the derivative vector C of the 1 x 1 ODE problem C C Double precision coding is used implicit double precision(a-h,l,o-z) C C Size the arrays dimension u(neqn), ut(neqn) C C Problem parameters alpha=1.0d0 lambda=1.0d0 C C Derivative vector ut(1)=lambda*dexp(-alpha*t)*u(1) return C C End of derv end
subroutine fprint(no,ncase,neqn,t,u) C C Subroutine fprint displays the numerical and C analytical solutions to the 1 x 1 ODE problem C C Double precision coding is used implicit double precision(a-h,l,o-z) C C Size the arrays dimension u(neqn) C C Problem parameters u0=1.0d0 alpha=1.0d0 lambda=1.0d0 C C Print a heading for the solution at t = 0 if(t.le.0.0d0)then C
Copyright 2004 by Chapman & Hall/CRC
C C C
Label for ODE integrator
11 C C
12 C C
13 C C
14 C C
15 C C
16
Fixed step modfied Euler if(ncase.eq.1)then write(no,11) format(//,6x,'euler2a integrator') Variable step modified Euler else if(ncase.eq.2)then write(no,12) format(//,6x,'euler2b integrator') Fixed step classical fourth order RK else if(ncase.eq.3)then write(no,13) format(//,6x,'rkc4a integrator') Variable step classical fourth order RK else if(ncase.eq.4)then write(no,14) format(//,6x,'rkc4b integrator') Fixed step RK Fehlberg 45 else if(ncase.eq.5)then write(no,15) format(//,6x,'rkf45a integrator') Variable step RK Fehlberg 45 else if(ncase.eq.6)then write(no,16) format(//,6x,'rkf45b integrator') end if
C C Heading write(no,2) 2 format(/,9x,'t',3x,'u1(num)',4x,'u1(ex)',8x,'diff1',/) C C End of t = 0 heading end if C C Analytical solution u1exact=u0*dexp(lambda/alpha*(1.0d0-dexp(-alpha*t))) C C Difference between exact and numerical solution vectors diff1=u(1)-u1exact
Copyright 2004 by Chapman & Hall/CRC
C C Display the numerical and exact solutions, and their C difference write(no,3)t,u(1),u1exact,diff1 3 format(f10.2,2f10.5,e13.4) return C C End of fprint end
Program 2.4.11 intpar, inital, derv, and fprint called in the solution of Equations 1.3 and 1.4 The output from the preceding routines (written to file ode1x1for.out in Program 2.4.1) is as follows: euler2a integrator t
u1(num)
u1(ex)
diff1
0.00 1.00 2.00 3.00 4.00 5.00
1.00000 1.88160 2.37421 2.58627 2.66895 2.70004
1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
0.0000E+00 -0.6467E-07 0.3879E-05 0.6033E-05 0.6924E-05 0.7265E-05
euler2b integrator t
u1(num)
u1(ex)
diff1
0.00 1.00 2.00 3.00 4.00 5.00
1.00000 1.88159 2.37421 2.58626 2.66895 2.70003
1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
0.0000E+00 -0.6220E-05 -0.3778E-05 0.2933E-06 0.3665E-05 0.6249E-05
rkc4a integrator t
u1(num)
u1(ex)
diff1
0.00 1.00
1.00000 1.88160
1.00000 1.88160
0.0000E+00 -0.2719E-10
Copyright 2004 by Chapman & Hall/CRC
2.00 3.00 4.00 5.00
2.37421 2.58626 2.66895 2.70003
2.37421 2.58626 2.66895 2.70003
-0.3267E-10 -0.3483E-10 -0.3565E-10 -0.3595E-10
rkc4b integrator t
u1(num)
u1(ex)
diff1
0.00 1.00 2.00 3.00 4.00 5.00
1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
0.0000E+00 -0.4263E-07 0.6877E-07 0.9303E-07 0.1030E-06 0.1462E-06
rkf45a integrator t
u1(num)
u1(ex)
diff1
0.00 1.00 2.00 3.00 4.00 5.00
1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
0.0000E+00 0.6661E-15 0.2665E-14 0.1332E-14 0.1332E-14 -0.1332E-14
rkf45b integrator t
u1(num)
u1(ex)
diff1
0.00 1.00 2.00 3.00 4.00 5.00
1.00000 1.88159 2.37421 2.58626 2.66894 2.70002
1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
0.0000E+00 -0.3287E-05 -0.4603E-05 -0.5037E-05 -0.5168E-05 -0.5212E-05
We note again that the output verifies the operation of all six integrators. The fixed step integrators (rkc4a , rkf45a , and ssrkc4) generally surpass the error tolerances set in intpar, with rkf45a and ssrkc4 producing excessive accuracy;
Copyright 2004 by Chapman & Hall/CRC
the variable step integrators (rkc4b, rkf45b, and ssrkc4) produce numerical solutions consistent with the error tolerances. This completes the programming of the 1x1 ODE problem in Fortran. We now move on to Java.
2.5
Programming in Java
As before, we start with the listing of the main program and ODE integrator routines (intpar, inital, derv, and fprint are copied to DEF.java before execution):
/* Numerical solution to the 1 x 1 ODE system by six integrators */ /* Include libraries */ import java.io.*; import java.text.*; import java.math.*; import mol.*; /* Main program */ public class ode1x1m { public static void main(String[] args) { /* Type variables */ double tp; int i, j, ncase; /* Declare an object from DEF */ DEF o1; /* Instantiate a class from Euler */ Euler e1=new Euler(); /* Instantiate a class from RK */ RK r1=new RK();
Copyright 2004 by Chapman & Hall/CRC
/* Use a buffer stream for the solution */ PrintWriter f=null; /* Open a file for output */ try { f = new PrintWriter(new FileOutputStream ("ode1x1jv.out"), true); } catch(IOException ioe) { System.out.println("Attempt to open file for writing failed"); System.exit(-1); } /* Step through six integrators */ for(ncase=1;ncase<=6;ncase++) { /* Integration parameters, initial condition */ o1=new DEF(); /* Output interval */ tp=o1.tf-o1.t0; /* Complete solution at nout output points */ for(j=1;j<=o1.nout;j++) { /* Print current solution */ o1.fprint(f,ncase,o1.neqn,o1.t0,o1.u0); /* Select ODE integrator */ switch(ncase) { /* Fixed step modified Euler integrator */ case 1: e1.euler2a(o1.neqn,o1.t0,o1.tf,o1.u,o1.u0, o1.nsteps); break;
/* Variable step modified Euler integrator */ case 2: e1.euler2b(o1.neqn,o1.t0,o1.tf,o1.u,o1.u0, o1.nsteps, o1.abserr, o1.relerr); break;
Copyright 2004 by Chapman & Hall/CRC
/* Fixed step classical fourth order RK integrator */ case 3: r1.rkc4a(o1.neqn,o1.t0,o1.tf,o1.u,o1.u0, o1.nsteps); break; /* Variable step classical fourth order RK integrator */ case 4: r1.rkc4b(o1.neqn,o1.t0,o1.tf,o1.u,o1.u0, o1.nsteps, o1.abserr, o1.relerr); break; /* Fixed step RK Fehlberg (RKF45) integrator */ case 5: r1.rkf45a(o1.neqn,o1.t0,o1.tf,o1.u,o1.u0, o1.nsteps); break; /* Variable step RK Fehlberg (RKF45) integrator */ case 6: r1.rkf45b(o1.neqn,o1.t0,o1.tf,o1.u,o1.u0, o1.nsteps, o1.abserr, o1.relerr); break; } /* Advance solution */ o1.t0=o1.tf; o1.tf+=tp; for(i=1;i<=o1.neqn;i++) { o1.u0[i]=o1.u[i]; } /* Next output */ } /* Next integrator */ }
Copyright 2004 by Chapman & Hall/CRC
/* Complete solution computed. f.close();
Close the file stream */
} /* End of ode1x1m */ }
Program 2.5.1 Java main program ode1x1m for the numerical integration of Equation 1.3 subject to initial condition Equation 1.4 Note again, as in Program 2.4.1, that all six integrators are called within a single loop. /* Step through six integrators */ for(ncase=1;ncase<=6;ncase++)
Also, an output file, ode1x1jv.out is written by this main program. The associated interface routines are as follows: /* Definition of functions intpar, inital, derv, fprint for the 1 x 1 ODE system */ package mol; import java.io.*; public interface ode1x1interface { /* Integration parameters */ public void intpar(); /* Initial condition */ public void inital(); /* Derivative vector */ public void derv(double ut[], double t, double u[]);
Copyright 2004 by Chapman & Hall/CRC
/* Output */ public void fprint(PrintWriter f, int ncase, int neqn, double t, double u[]); } /* Define the common (global) variables for the 1 x 1 ODE problem */ package mol; public class MOL { /* Maximum (default) number of ODEs */ public int SIZE=500; /* Variables for ODE integration */ public int neqn, nout, nsteps; public double t0, tf, abserr, relerr; public double u[], u0[], e[]; public MOL() { } }
Program 2.5.2 Interface routines used by Program 2.5.1 The ODE integration routines are listed below: package mol; import java.math.*; import java.io.*;
Copyright 2004 by Chapman & Hall/CRC
public class Euler extends DEF implements EulerInterface { public Euler() { } public void euler2a(int neqn, double t0, double tf, double u[],double u0[],int nsteps) /* Function euler2a computes an ODE solution by the fixed step modified Euler method for a series of points along the solution by repeatedly calling function sseuler for a single modified Euler step. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
number of modified Euler steps
u
ODE solution vector of length neqn after nsteps steps
*/ { /* Type variables */ double ut0[],ut[]; double h,t; int i,j; /* Integration step */ h=(tf-t0)/nsteps; /* nsteps modified Euler steps */ for(i=1;i<=nsteps;i++) { /* Single modified Euler step */ t=sseuler(neqn,t0,u0,h,u,e);
Copyright 2004 by Chapman & Hall/CRC
/* Reset base point values for next modified Euler step */ for(j=1;j<=neqn;j++) { u0[j]=u[j]; } t0=t; /* End for */ } /* End of euler2a */ }
Program 2.5.3 Integrator euler2a
public void euler2b(int neqn, double t0, double tf, double u[], double u0[], int nsteps, double abserr, double relerr) /* Function euler2b computes an ODE solution by the variable step modified Euler method for a series of points along the solution by repeatedly calling function sseuler for a single modified Euler step. The truncation error is estimated along the solution to adjust the integration step according to a specified error tolerance. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
number of modified Euler steps
u
ODE solution vector of length neqn after nsteps steps
*/ { /* Type variables */
Copyright 2004 by Chapman & Hall/CRC
double e[]=new double[SIZE]; double hmin, h, t; int i, nfin1; /* Integration step */ h=(tf-t0)/8.0; /* Minimum allowable step */ hmin=(tf-t0)/(float)(nsteps); /* Start integration */ t=t0; /* While independent variable is less than the final value, continue the integration */ while(t <= tf*0.999) { /* If the next step along the solution will go past the final value of the independent variable, set the step to the remaining distance to the final value */ if((t+h) > tf) { h=tf-t; } /* Single modified Euler step */ t=sseuler(neqn,t0,u0,h,u,e); /* Flag for the end of the integration */ nfin1=1; /* Check if any of the ODEs have violated the error criterion */ for(i=1;i<=neqn;i++) { if( Math.abs(e[i]) > (Math.abs(u[i])*relerr +abserr)) /* Error violation, so integration is incomplete. Reduce integration step because of error violation and repeat integration from base point */ { nfin1=0; h=h/2.0;
Copyright 2004 by Chapman & Hall/CRC
/* If the current step is less than the minimum allowable step, set the step to the minimum allowable value and continue integration from new base point */ if( h < hmin ) { h=hmin; nfin1=1; } break; } } /* If there is no error violation, continue the integration from the new base point */ if(nfin1 == 1 ) { for(i=1;i<=neqn;i++) { u0[i]=u[i]; } t0=t; /* Test if integration step can be increased */ for(i=1;i<=neqn;i++) { if( Math.abs(e[i]) > ((Math.abs(u[i]) *relerr+abserr)/4.0) ) { /* Integration step cannot be increased */ nfin1=0; break; } } /* Increase integration step */ if(nfin1 == 1 ) { h=h*2.0; } /* End if */ }
Copyright 2004 by Chapman & Hall/CRC
/* End while */ } /* End of euler2b */ }
Program 2.5.4 Integrator euler2b
public double sseuler(int neqn, double t0, double u0[], double h,double u[],double e[]) /* Function sseuler computes an ODE solution by the modified Euler method for one step along the solution (by calls to derv to define the ODE derivative vector). It also estimates the truncation error of the solution, and applies this estimate as a correction to the solution vector. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
u0
initial condition vector of length neqn
h
integration step
t
independent variable
u
ODE solution vector of length neqn after one modified Euler step
e
estimate of truncation error of the solution vector
*/ { /* Type variables */ double ut0[], ut[]; double t; int j;
Copyright 2004 by Chapman & Hall/CRC
/* Declare arrays */ ut0=new double[SIZE]; ut=new double[SIZE]; /* Derivative vector at initial (base) point */ derv(ut0,t0,u0); /* First order (Euler) step */ for(j=1;j<=neqn;j++) { u[j]=u0[j]+ut0[j]*h; } t=t0+h; /* Derivative at advance point */ derv(ut,t,u); /* Second order (modified Euler) step */ for(j=1;j<=neqn;j++) { /* Truncation error estimate */ e[j]=(ut[j]-ut0[j])*h/2.0; /* Second order (modified Euler) solution vector */ u[j]=u[j]+e[j]; } return t; /* End of sseuler */ } /* End of public class Euler */ }
Program 2.5.5 Integrator sseuler for a single modified Euler step
/* Routines for the Euler ODE Integration */
Copyright 2004 by Chapman & Hall/CRC
package mol; import java.math.*; import java.io.*; public interface EulerInterface { /* Fixed step modified Euler method */ public void euler2a(int neqn, double t0, double tf, double u[],double u0[],int nsteps); /* Variable step modified Euler method */ public void euler2b(int neqn, double t0, double tf, double u[], double u0[], int nsteps, double abserr, double relerr); /* Single step modified Euler method */ public double sseuler(int neqn, double t0, double u0[], double h, double u[],double e[]); }
Program 2.5.6 Interface routine for euler2a , euler2b, and sseuler
package mol; import java.io.*; import java.math.*; public class RK extends DEF implements RKInterface { public RK() { } public void rkc4a(int neqn, double t0, double tf, double u[], double u0[], int nsteps) /* Function rkc4a computes an ODE solution by the fixed step classical fourth order RK method for a series of points along the solution by repeatedly calling function ssrkc4 for a single classical fourth order RK step.
Copyright 2004 by Chapman & Hall/CRC
Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
number of RKC4 steps
u
ODE solution vector of length neqn after nsteps steps
*/ { /* Type variables */ double h, t; double e[]=new double[SIZE]; int i, j; /* Integration step */ h=(tf-t0)/nsteps; /* nsteps rkc4 steps */ for(i=1;i<=nsteps;i++) { /* Single rkc4 step */ t=ssrkc4(neqn,t0,u0,h,u,e); /* Reset base point values for next rk4c step */ for(j=1;j<=neqn;j++) { u0[j]=u[j]; } t0=t; } /* End of rkc4a */ }
Program 2.5.7 Integrator rkc4a
Copyright 2004 by Chapman & Hall/CRC
public void rkc4b(int neqn, double t0, double tf, double u[],double u0[],int nsteps, double abserr,double relerr) /* Function rkc4b computes an ODE solution by a variable step classical fourth order RK method for a series of points along the solution by repeatedly calling function ssrkc4 for a single classical fourth order RK step. The truncation error is estimated along the solution to adjust the integration step according to a specified error tolerance. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
maximum number of rkc4 steps
abserr
absolute error tolerance
relerr
relative error tolerance
u
ODE solution vector of length neqn after nsteps steps
*/ { /* Type variables */ double hmin, h, t; double e[]=new double[SIZE]; int nfin1, i; /* Integration step */ h=(tf-t0)/2.0; /* Minimum allowable step */ hmin=(tf-t0)/nsteps; /* Start integration */ t=t0;
Copyright 2004 by Chapman & Hall/CRC
/* While independent variable is less than the final value, continue the integration */ while(t <= (tf*0.999)) { /* If the next step along the solution will go past the final value of the independent variable, set the step to the remaining distance to the final value */ if((t+h) > tf ) { h=tf-t; } /* Single rkc4 step */ t=ssrkc4(neqn,t0,u0,h,u,e); /* Flag for the end of the integration */ nfin1=1; /* Check if any of the ODEs have violated the error criterion */ for(i=1;i<=neqn;i++) { if(Math.abs(e[i]) > (Math.abs(u[i])*relerr+abserr)) { /* Error violation, so integration is incomplete. Reduce integration step because of error violation and repeat integration from base point */ nfin1=0; h=h/2.0; /* If the current step is less than the minimum allowable step, set the step to the minimum allowable value and continue integration from new base point */ if(h < hmin) { h=hmin; nfin1=1; } break; } } /* If there is no error violation, continue the integration from the new base point */
Copyright 2004 by Chapman & Hall/CRC
if(nfin1 == 1) { for(i=1;i<=neqn;i++) { u0[i]=u[i]; } t0=t; /* Test if integration step can be increased */ for(i=1;i<=neqn;i++) { if(Math.abs(e[i]) > ((Math.abs(u[i]) *relerr+abserr)/16.0)) { /* Integration step cannot be increased */ nfin1=0; break; } } /* Increase integration step */ if(nfin1 == 1) { h=h*2.0; } /* End if */ } /* End while */ } /* End of rkc4b */ }
Program 2.5.8 Integrator rkc4b
public double ssrkc4(int neqn, double t0, double u0[], double h, double u[], double e[]) /* Function ssrkc4 computes an ODE solution by the classical fourth order RK method for one step along the solution (by calls to derv to define the ODE derivative vector). It also estimates the truncation error of the solution,
Copyright 2004 by Chapman & Hall/CRC
and applies this estimate as a correction to the solution vector. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
u0
initial condition vector of length neqn
h
integration step
t
independent variable
u
ODE solution vector of length neqn after one rkc4 step
e
estimate of truncation error of the solution vector
*/ { /* Type variables */ double ut0[], ut[], u4[]; double t, k1[], k2[], k3[], k4[]; int j; /* Declare arrays */ ut0=new double[SIZE]; ut=new double[SIZE]; u4=new double[SIZE]; k1=new double[SIZE]; k2=new double[SIZE]; k3=new double[SIZE]; k4=new double[SIZE]; /* Derivative vector at initial (base) point */ derv(ut0,t0,u0); /* k1; stepping for k2 */ for(j=1;j<=neqn;j++) { k1[j]=h*ut0[j]; u[j]=u0[j]+0.5*k1[j]; } t=t0+0.5*h;
Copyright 2004 by Chapman & Hall/CRC
/* Derivative vector at next RK point */ derv(ut,t,u); /* k2; stepping for k3 */ for(j=1;j<=neqn;j++) { k2[j]=h*ut[j]; u[j]=u0[j]+0.5*k2[j]; } t=t0+0.5*h; /* Derivative vector at next RK point */ derv(ut,t,u); /* k3; stepping for k4 */ for(j=1;j<=neqn;j++) { k3[j]=h*ut[j]; u[j]=u0[j]+k3[j]; } t=t0+h; /* Derivative vector at next RK point */ derv(ut,t,u); /* k4; second and fourth order step; error estimate; error correction */ for(j=1;j<=neqn;j++) { k4[j]=h*ut[j]; u[j]=u0[j]+k2[j]; u4[j]=u0[j]+(1.0/6.0)*(k1[j]+2.0*k2[j]+2.0*k3[j] +k4[j]); e[j]=u4[j]-u[j]; u[j]=u[j]+e[j]; } t=t0+h; return t; /* End of ssrkc4 */ }
Program 2.5.9 Integrator ssrkc4 for a classical fourth-order RK step
Copyright 2004 by Chapman & Hall/CRC
public void rkf45a(int neqn, double t0, double tf, double u[], double u0[], int nsteps) /* Function rkf45a computes an ODE solution by the fixed step RK Fehlberg 45 RK method for a series of points along the solution by repeatedly calling function ssrkf45 for a single RK Fehlberg 45 step.
Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
number of rkf45 steps
u
ODE solution vector of length neqn after nsteps steps
*/ { /* Type variables */ double h, t; double e[]=new double[SIZE]; int i, j; /* Integration step */ h=(tf-t0)/nsteps; /* nsteps rkf45 steps */ for(i=1;i<=nsteps;i++) { /* Single rkf45 step */ t=ssrkf45(neqn,t0,u0,h,u,e); /* Reset base point values for next rkf45 step */ for(j=1;j<=neqn;j++) { u0[j]=u[j]; } t0=t;
Copyright 2004 by Chapman & Hall/CRC
} /* End of rkf45a */ }
Program 2.5.10 Integrator rkf45a public void rkf45b(int neqn, double t0, double tf, double u[], double u0[], int nsteps, double abserr, double relerr) /* Function rkf45b computes an ODE solution by a variable step classical RK Fehlberg 45 method for a series of points along the solution by repeatedly calling function ssrkf45 for a single RK Fehlberg 45 step. The truncation error is estimated along the solution to adjust the integration step according to a specified error tolerance. Argument list neqn
number of first order ODEs
t0
initial value of independent variable
tf
final value of independent variable
u0
initial condition vector of length neqn
nsteps
maximum number of rkf45 steps
abserr
absolute error tolerance
relerr
relative error tolerance
u
ODE solution vector of length neqn after nsteps steps
*/ { /* Type variables */ double hmin, h, t; double e[]=new double[SIZE]; int i, nfin1;
Copyright 2004 by Chapman & Hall/CRC
/* Integration step */ h=(tf-t0)/2.0; /* Minimum allowable step */ hmin=(tf-t0)/nsteps; /* Start integration */ t=t0; /* While independent variable is less than the final value, continue the integration */ while(t <= (tf*0.999)) { /* If the next step along the solution will go past the final value of the independent variable, set the step to the remaining distance to the final value */ if((t+h) > tf) { h=tf-t; } /* Single rkf45 step */ t=ssrkf45(neqn,t0,u0,h,u,e); /* Flag for the end of the integration */ nfin1=1; /* Check if any of the ODEs have violated the error criterion */ for(i=1;i<=neqn;i++) { if(Math.abs(e[i]) > (Math.abs(u[i]) *relerr+abserr)) { /* Error violation, so integration is incomplete. Reduce integration step because of error violation and repeat integration from base point */ nfin1=0; h=h/2.0; /* If the current step is less than the minimum allowable step, set the step to the minimum allowable value and continue integration from new base point */
Copyright 2004 by Chapman & Hall/CRC
if(h < hmin) { h = hmin; nfin1=1; } break; } } /* If there is no error violation, continue the integration from the new base point */ if(nfin1 == 1) { for(i=1;i<=neqn;i++) { u0[i]=u[i]; } t0=t; /* Test if integration step can be increased */ for(i=1;i<=neqn;i++) { if(Math.abs(e[i]) > ((Math.abs(u[i]) *relerr+abserr)/32.0) ) { /* Integration step cannot be increased */ nfin1=0; break; } } /* Increase integration step */ if(nfin1 == 1) { h=h*2.0; } /* End if */ } /* End while */ }
Copyright 2004 by Chapman & Hall/CRC
/* End of rkf45b */ }
Program 2.5.11 Integrator rkf45b public double ssrkf45(int neqn, double t0, double u0[], double h, double u[], double e[]) /* Function ssrkf45 computes an ODE solution by the RK Fehlberg 45 method for one step along the solution (by calls to derv to define the ODE derivative vector). It also estimates the truncation error of the solution, and applies this estimate as a correction to the solution vector.
Argument list neqn
number of first order ODEs
t0
initial value of independent variable
u0
initial condition vector of length neqn
h
integration step
t
independent variable
u
ODE solution vector of length neqn after one rkf45 step
e
estimate of truncation error of the solution vector
*/ { /* Type variables */ double t, ut0[], ut[], u5[]; double k1[], k2[], k3[], k4[], k5[], k6[]; int j; /* Declare arrays */ ut0=new double[SIZE]; ut=new double[SIZE]; u5=new double[SIZE];
Copyright 2004 by Chapman & Hall/CRC
k1=new k2=new k3=new k4=new k5=new k6=new
double[SIZE]; double[SIZE]; double[SIZE]; double[SIZE]; double[SIZE]; double[SIZE];
/* Derivative vector at initial (base) point */ derv(ut0,t0,u0); /* k1; stepping for k2 */ for(j=1;j<=neqn;j++) { k1[j]=h*ut0[j]; u[j]=u0[j]+0.25*k1[j]; } t=t0+0.25*h; /* Derivative vector at next RK point */ derv(ut,t,u); /* k2; stepping for k3 */ for(j=1;j<=neqn;j++) { k2[j]=h*ut[j]; u[j]=u0[j]+(3.0/32.0)*k1[j] +(9.0/32.0)*k2[j]; } t=t0+(3.0/8.0)*h; /* Derivative vector at next RK point */ derv(ut,t,u); /* k3; stepping for k4 */ for(j=1;j<=neqn;j++) { k3[j]=h*ut[j]; u[j]=u0[j]+(1932.0/2197.0)*k1[j] -(7200.0/2197.0)*k2[j] +(7296.0/2197.0)*k3[j]; } t=t0+(12.00/13.0)*h; /* Derivative vector at next RK point */ derv(ut,t,u);
Copyright 2004 by Chapman & Hall/CRC
/* k4; stepping for k5 */ for(j=1;j<=neqn;j++) { k4[j]=h*ut[j]; u[j]=u0[j]+( 439.0/ 216.0)*k1[j] -( 8.0 )*k2[j] +(3680.0/ 513.0)*k3[j] -( 845.0/4104.0)*k4[j]; } t=t0+h; /* Derivative vector at next RK point */ derv(ut,t,u); /* k5; stepping for k6 */ for(j=1;j<=neqn;j++) { k5[j]=h*ut[j]; u[j]=u0[j]-( 8.0/ 27.0)*k1[j] +( 2.0 )*k2[j] -(3544.0/2565.0)*k3[j] +(1859.0/4104.0)*k4[j] -( 11.0/ 40.0)*k5[j]; } t=t0+0.5*h; /* Derivative vector at next RK point */ derv(ut,t,u); /* k6; fourth and fifth order step; error estimate; error correction */ for(j=1;j<=neqn;j++) { k6[j]=h*ut[j]; u[j] =u0[j]+( 25.0/ 216.0)*k1[j] +( 1408.0/2565.0)*k3[j] +( 2197.0/4104.0)*k4[j] -( 1.0/ 5.0)*k5[j]; u5[j]=u0[j]+( 16.0/ 135.0)*k1[j] +( 6656.0/12825.0)*k3[j] +(28561.0/56430.0)*k4[j] -( 9.0/ 50.0)*k5[j] +( 2.0/ 55.0)*k6[j]; e[j]=u5[j]-u[j]; u[j]=u[j]+e[j];
Copyright 2004 by Chapman & Hall/CRC
} t=t0+h; return t; /* End of ssrkf45 */ } /* End public class RK */ }
Program 2.5.12 Integrator ssrkf45 for a RKF45 step /* Routines for the Runge Kutta ODE Integration */ package mol; import java.math.*; import java.io.*; public interface RKInterface { /* Fixed step classical fourth order RK method */ public void rkc4a(int neqn, double t0, double tf, double u[], double u0[], int nsteps); /* Variable step classical fourth order RK method */ public void rkc4b(int neqn, double t0, double tf, double u[], double u0[], int nsteps, double abserr, double relerr); /* Single step classical fourth order RK method */ public double ssrkc4(int neqn, double t0, double u0[], double h, double u[], double e[]); /* Fixed step RK Fehlberg 45 method */ public void rkf45a(int neqn, double t0, double tf,
double u[], double u0[], int nsteps) ;
Copyright 2004 by Chapman & Hall/CRC
/* Variable step RK Fehlberg 45 method */ public void rkf45b(int neqn, double t0, double tf, double u[], double u0[], int nsteps, double abserr, double relerr); /* Single step RK Fehlberg 45 method */ public double ssrkf45(int neqn, double t0, double u0[],
double h, double u[], double e[]); }
Program 2.5.13 Interface routine for rkc4a , rkc4b, ssrkc4,rkf45a , rkf45b, ssrk f 45 intpar, inital, derv, and fprint follow: /* This file is a member of the package mol */ package mol; import import import import
mol.MOL; java.math.*; java.io.*; java.text.*;
public class DEF extends MOL implements ode1x1interface { public DEF() { /* Integration parameters */ this.intpar(); /* Declare arrays */ u0=new double[SIZE]; u=new double[SIZE]; e=new double[SIZE]; /* Initial condition vector */ this.inital(); } public void intpar() /* Function intpar sets the parameters to control the integration of the 1 x 1 ODE system */ {
Copyright 2004 by Chapman & Hall/CRC
/* Number of ODEs */ neqn=1; /* Size of arrays in MOL library */ SIZE=neqn+1; /* Number of output points */ nout=6; /* Maximum number of steps in the interval t0 to tf */ nsteps=100; /* Initial, final values of the independent variable */ t0=0.0; tf=1.0; /* Error tolerances */ abserr=Math.pow(10.0,-5.0); relerr=Math.pow(10.0,-5.0); /* End of intpar */ } public void inital() /* Function inital sets the initial condition vector for the 1 x 1 ODE problem */ { u0[1]=1.0E0; /* End of inital */ } public void derv(double ut[], double t, double u[]) /* Function derv computes the derivative vector of the 1 x 1 ODE problem */ { /* Type variables */ double alpha, lambda;
/* Problem parameters */ alpha=1.0E0; lambda=1.0E0;
Copyright 2004 by Chapman & Hall/CRC
/* Derivative vector */ ut[1]=lambda*Math.exp(-alpha*t)*u[1]; /* End of derv */ }
public void fprint(PrintWriter f, int ncase, int neqn, double t, double u[]) /* Function fprint displays the numerical and exact solutions to the 1 x 1 ODE problem */ { /* Type variables */ double ue1; double diff1; double e1; double V0, alpha, lambda;
/* Problem parameters */ V0=1.0E0; alpha=1.0E0; lambda=1.0E0; /* Print a heading for the solution at t = 0 */ if(t<=0.0) { /* Label for ODE integrator */ switch(ncase) { /*Fixed step modified Euler */ case 1: f.println("\n\n euler2a integrator\n"); break; /* Variable step modified Euler */ case 2: f.println("\n\n euler2b integrator\n"); break; /* Fixed step classical fourth order RK */ case 3: f.println("\n\n rkc4a integrator\n"); break;
Copyright 2004 by Chapman & Hall/CRC
/* Variable step classical fourth order RK */ case 4: f.println("\n\n rkc4b integrator\n"); break; /* Fixed step RK Fehlberg 45 */ case 5: f.println("\n\n rkf45a integrator\n"); break; /* Variable step RK Fehlberg 45 */ case 6: f.println("\n\n rkf45b integrator\n"); break; } /* Heading */ f.println(" t
u1(num)
ue1
diff1");
/* End of t = 0 heading */ } /* Analytical solution */ ue1=V0*Math.exp(lambda/alpha*(1.0E0-Math .exp(-alpha*t))); /* Difference between exact and numerical solution vectors */ diff1=u[1]-ue1; /* Display format for floating numbers */ DecimalFormat df1 = new DecimalFormat(" 0.00"); DecimalFormat df2 = new DecimalFormat("0.0000000"); /* Display the numerical and exact solutions, and their difference */ f.println(df1.format(t)+"\t"+df2.format(u[1]) +"\t"+df2.format(ue1)+"\t"+df2.format(diff1)); /* End of fprint */ } /* End of DEF */ }
Copyright 2004 by Chapman & Hall/CRC
Program 2.5.14 intpar, inital, derv, and fprint called in the solution of Equations 1.3 and 1.4 The output from the preceding routines (written to file ode1x1jv.out in Program 2.5.1) is as follows: euler2a integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1(num) 1.0000000 1.8815963 2.3742138 2.5862663 2.6689549 2.7000350
ue1 1.0000000 1.8815964 2.3742099 2.5862603 2.6689479 2.7000278
diff1 0.0000000 -0.0000001 0.0000039 0.0000060 0.0000069 0.0000073
ue1 1.0000000 1.8815964 2.3742099 2.5862603 2.6689479 2.7000278
diff1 0.0000000 -0.0000062 -0.0000038 0.0000003 0.0000037 0.0000062
ue1 1.0000000 1.8815964 2.3742099 2.5862603 2.6689479 2.7000278
diff1 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
ue1 1.0000000 1.8815964 2.3742099
diff1 0.0000000 0.0000000 0.0000001
euler2b integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1(num) 1.0000000 1.8815902 2.3742061 2.5862606 2.6689516 2.7000340
rkc4a integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1(num) 1.0000000 1.8815964 2.3742099 2.5862603 2.6689479 2.7000278
rkc4b integrator t 0.00 1.00 2.00
u1(num) 1.0000000 1.8815963 2.3742100
Copyright 2004 by Chapman & Hall/CRC
3.00 4.00 5.00
2.5862604 2.6689480 2.7000279
2.5862603 2.6689479 2.7000278
0.0000001 0.0000001 0.0000001
ue1 1.0000000 1.8815964 2.3742099 2.5862603 2.6689479 2.7000278
diff1 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
ue1 1.0000000 1.8815964 2.3742099 2.5862603 2.6689479 2.7000278
diff1 0.0000000 -0.0000033 -0.0000046 -0.0000050 -0.0000052 -0.0000052
rkf45a integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1(num) 1.0000000 1.8815964 2.3742099 2.5862603 2.6689479 2.7000278
rkf45b integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1(num) 1.0000000 1.8815931 2.3742053 2.5862553 2.6689428 2.7000225
We note again that the output verifies the operation of all six integrators. The fixed step integrators (rkc4a , rkf45a , and ssrkc4) generally surpass the error tolerances set in intpar, with rkf45a and ssrkc4 producing excessive accuracy; the variable step integrators (rkc4b, rkf45b, and ssr kc4) produce numerical solutions consistent with the error tolerances. This completes the programming of the 1x1 ODE problem in Java. We now move on to Maple.
2.6
Programming in Maple
We conclude the programming of the 1x1 ODE problem using Maple. Also, we have used the following approach, which is a small departure from the preceding programming and the more typical programming in Maple: • Maple files, with the extension .mws (for Maple Worksheet), are ASCII files, but they cannot be read using, for example, an ASCII text editor; rather they must be viewed under Maple. We, therefore, have used Maple coded
Copyright 2004 by Chapman & Hall/CRC
as readable ASCII files, and have given the resulting files the extension .txt. Then these .txt files are opened within the Maple code (via read statements). This may seem complicated, but the following coding will demonstrate that this procedure is straightforward. This approach has the major advantage that the Maple code can be written outside of Maple (in fact, we used the MATLAB routines in Section 2.1, which are readable ASCII, as templates for writing the corresponding Maple). • Since the .txt files are opened using the Maple read statement, the separation between general-purpose (library) routines and the applicationspecific routines cannot be maintained as well as before. In other words, the read statements in the general-purpose routines have to be modified to open specific applications files. This is a disadvantage, but we view it as minor compared with the advantage of using readable ASCII (.txt) files. The details of this approach will become clear through the following coding for the 1x1 ODE problem. We start with a small (three line) .mws main program that in turn executes a series of read statements for the .txt files: > restart: > read "c:\\odelib\\maple\\ode1x1\\ode1x1.txt"; > ode1x1();
Program 2.6.1 Maple main program ode1x1.mws for the numerical integration of Equation 1.3 subject to initial condition Equation 1.4 We can note the following points about this main program: • The main program ode1x1.txt is read (which means that it is now available for execution): > read "c:\\odelib\\maple\\ode1x1\\ode1x1.txt";
• ode1x1, which is a Maple procedure, is then executed: > ode1x1();
Procedure ode1x1 is listed below: ode1x1:=proc() # # Main program ode1x1 computes the numerical # solution to the 1 x 1 ODE system by one of # six integrators #
Copyright 2004 by Chapman & Hall/CRC
# Type variables global neqn, nout, nsteps, t0, tf, abserr, relerr: local u0, u, tp, ncase, i, j: # # Step through six integrators for ncase from 1 to 6 do # # Integration parameters read "c:\\odelib\\maple\\ode1x1\\intpar.txt": intpar(): # # Size arrays u0:=array(1..neqn): u:=array(1..neqn): # # Initial condition vector read "c:\\odelib\\maple\\ode1x1\\inital.txt": inital(n,t0,u0): # # Output interval tp:=tf-t0: # # Compute solution at nout output points for j from 1 to nout do # # Print current solution read "c:\\odelib\\maple\\ode1x1\\fprint.txt": fprint(ncase,neqn,t0,u0): # # Fixed step modified Euler integrator if (ncase = 1) then read "c:\\odelib\\maple\\ode2x2\\euler2a.txt": euler2a(neqn,t0,tf,u0,nsteps,u): end if: # # Variable step modified Euler integrator if (ncase = 2) then read "c:\\odelib\\maple\\ode2x2\\euler2b.txt": euler2b(neqn,t0,tf,u0,nsteps,abserr,relerr,u): end if: # # Fixed step classical fourth order RK integrator if (ncase = 3) then read "c:\\odelib\\maple\\ode2x2\\rkc4a.txt": rkc4a(neqn,t0,tf,u0,nsteps,u): end if:
Copyright 2004 by Chapman & Hall/CRC
# #
# #
# #
# #
Variable step classical fourth order RK integrator if (ncase = 4) then read "c:\\odelib\\maple\\ode2x2\\rkc4b.txt": rkc4b(neqn,t0,tf,u0,nsteps,abserr,relerr,u): end if: Fixed step RK Fehlberg (RKF45) integrator if (ncase = 5) then read "c:\\odelib\\maple\\ode2x2\\rkf45a.txt": rkf45a(neqn,t0,tf,u0,nsteps,u): end if: Variable step RK Fehlberg (RKF45) integrator if (ncase = 6) then read "c:\\odelib\\maple\\ode2x2\\rkf45b.txt": rkf45b(neqn,t0,tf,u0,nsteps,abserr,relerr,u): end if: Advance solution t0:=tf: tf:=tf+tp: for i from 1 to neqn do u0[i]:=u[i]: end do:
# # Next output end do: # # Next integrator end do: # # End of ode1x1.txt end:
Program 2.6.2 Maple main program ode1x1.txt for the numerical integration of Equation 1.3 subject to initial condition Equation 1.4
ode1x1.txt has a familiar structure. We can note the following points: • # denotes a comment in Maple. • Again, all six integrators are called within a single loop:
Copyright 2004 by Chapman & Hall/CRC
# # Step through six integrators for ncase from 1 to 6 do
• Within this loop, each of the ODE integrators, euler2a , euler2b, sseuler, rkc4a , rkc4b, ssrkc4, rkf45a , rkf45b, and ssrkf45, is opened via a read statement. • Additionally, routines intpar, inital, and fprint, which are specific to the 1x1 ODE application, are opened via read statements; thus, the separation of general-purpose and applications-specific routines is not as clear-cut as in the discussion of the preceding five languages. The ODE integration routines are listed below. Generally they have the same form as before; the file read statements are the only main difference. euler2a:=proc(neqn,t0,tf,u0,nsteps,u) # # Procedure euler2a computes an ODE solution by a fixed # step modified Euler method for a series of points along # the solution by repeatedly calling procedure sseuler # for a single modified Euler step. # # Argument list # # neqn number of first order ODEs # # t0 initial value of independent variable # # tf final value of independent variable # # u0 initial condition vector of length neqn # # nsteps number of modified Euler steps # # u ODE solution vector of length neqn after # nsteps steps # # Type variables local e, h, i, j, tm, t: # # Size arrays e:=array(1..neqn): # # Integration step h:=(tf-t0)/nsteps:
Copyright 2004 by Chapman & Hall/CRC
# # nsteps modified Euler steps tm:=t0: for j from 1 to nsteps do # # Modified Euler step read "c:\\odelib\\maple\\ode2x2\\sseuler.txt": sseuler(neqn,tm,u0,h,u,e): t:=tm+h: # # Reset base point values for next modified Euler step for i from 1 to neqn do u0[i]:=u[i]: end do: tm:=t: # # Next modified Euler step end do: # # End of euler2a end:
Program 2.6.3 Integrator euler2a euler2b:=proc(neqn,t0,tf,u0,nsteps,abserr,relerr,u) # # Procedure euler2b computes an ODE solution by a variable # step modified Euler method for a series of points along # the solution by repeatedly calling procedure sseuler for # a single modified Euler step. The truncation error is # estimated along the solution to adjust the integration # step according to a specified error tolerance. # # Argument list # # neqn number of first order ODEs # # t0 initial value of independent variable # # tf final value of independent variable # # u0 initial condition vector of length neqn # # nsteps maximum number of modified Euler steps
Copyright 2004 by Chapman & Hall/CRC
# # abserr absolute error tolerance # # relerr relative error tolerance # # u ODE solution vector of length eqn after # nsteps steps # # Type variables local e, h, i, j, tm, t, hmin, nfin1: # # Size arrays e:=array(1..neqn): # # Initial integration step h:=(tf-t0)/8.0: # # Minimum allowable step hmin:=(tf-t0)/nsteps: # # Start integration tm:=t0: # # While independent variable is less than the final # value, continue the integration while(tm <= tf*0.999) do # # If the next step along the solution will go past # the final value of the independent variable, set # the step to the remaining distance to the final # value if(tm+h > tf) then h:=tf-tm: end if: # # Single modified Euler step read "c:\\odelib\\maple\\ode2x2\\sseuler.txt": sseuler(neqn,tm,u0,h,u,e): t:=tm+h: # # Flag for the end of the integration nfin1:=1: # # Check if any of the ODEs have violated the error # criteria for i from 1 to neqn do if(abs(e[i]) > abs(u[i])*relerr+abserr) then
Copyright 2004 by Chapman & Hall/CRC
# # # #
# # # # # # # #
# #
# #
# # # #
Error violation, so integration is not complete. Reduce integration step because of error violation and repeat integration from the base point h:=h/2.0: nfin1:=0: break: end if: end do: If the current step is less than the minimum allowable step, set the step to the minimum allowable value and continue integration from new base point if(h < hmin) then h:=hmin: nfin1:=1: end if: If there is no error violation, continue the integration from new base point if(nfin1 = 1) then for i from 1 to neqn do u0[i]:=u[i]: end do: tm:=t: Test if integration step can be increased for i from 1 to neqn do if(abs(e[i]) > (abs(u[i])*relerr+abserr)/4.0) then Integration step cannot be increased nfin1:=0: break: end if: end do: Increase integration step if(nfin1 = 1) then h:=h*2.0: end if: Continue for no error violation (nfin1=1) end if:
# # Continue while end do: # # End of euler2b end:
Program 2.6.4 Integrator euler2b
Copyright 2004 by Chapman & Hall/CRC
sseuler:=proc(neqn,t0,u0,h,u,e) # # Procedure sseuler computes an ODE solution by the # modified Euler method for one step along the solution # (by calls to derv to define the ODE derivative vector). # It also estimates the truncation error of the solution, # and applies this estimate as a correction to the # solution vector. # # Argument list # # neqn number of first order ODEs # # t0 initial value of independent variable # # u0 initial condition vector of length neqn # # h integration step # # u ODE solution vector of length neqn after # one modified Euler step # # e estimate of truncation error of the solu# tion vector # # Type variables local ut0, ut, t, i: # # Declare arrays ut0:=array(1..neqn): ut:=array(1..neqn): # # Derivative vector at initial (base) point read "c:\\odelib\\maple\\ode1x1\\derv.txt": derv(neqn,t0,u0,ut0): # # First order (Euler) step for i from 1 to neqn do u[i]:=u0[i]+ut0[i]*h: end do: t:=t0+h: # # Derivative vector at advance point derv(neqn,t,u,ut): # # Truncation error estimate
Copyright 2004 by Chapman & Hall/CRC
for i from 1 to neqn do e[i]:=(ut[i]-ut0[i])*h/2.0: end do: # # Second order (modified Euler) solution vector for i from 1 to neqn do u[i]:=u[i]+e[i]: end do: # # End of sseuler end:
Program 2.6.5 Integrator sseuler for a single modified Euler step Note in sseuler the read statement to the application specific derv: # # Derivative vector at initial (base) point read "c:\\odelib\\maple\\ode1x1\\derv.txt": derv(neqn,t0,u0,ut0): rkc4a:=proc(neqn,t0,tf,u0,nsteps,u) # # Procedure rkc4a computes an ODE solution by a fixed step # classical fourth order RK method for a series of points # along the solution by repeatedly calling procedure ssrkc4 # for a single classical fourth order RK step. # # Argument list # # neqn number of first order ODEs # # t0 initial value of independent variable # # tf final value of independent variable # # u0 initial condition vector of length neqn # # nsteps number of rkc4 steps # # u ODE solution vector of length neqn after # nsteps steps # # Type variables local e, h, i, j, tm, t:
Copyright 2004 by Chapman & Hall/CRC
# # Size arrays e:=array(1..neqn): # # Integration step h:=(tf-t0)/nsteps: # # nsteps rkc4 steps tm:=t0: for j from 1 to nsteps do # # rkc4 step read "c:\\odelib\\maple\\ode2x2\\ssrkc4.txt": ssrkc4(neqn,tm,u0,h,u,e): t:=tm+h: # # Reset base point values for next rkc4 step for i from 1 to neqn do u0[i]:=u[i]: end do: tm:=t: # # Next rkc4 step end do: # # End of rkc4a end:
Program 2.6.6 Integrator rkc4a rkc4b:=proc(neqn,t0,tf,u0,nsteps,abserr,relerr,u) # # Procedure rkc4b computes an ODE solution by a variable # step classical fourth order RK method for a series of # points along the solution by repeatedly calling procedure # ssrkc4 for a single classical fourth order RK step. The # truncation error is estimated along the solution to # adjust the integration step according to a specified # error tolerance. # # Argument list # # neqn number of first order ODEs #
Copyright 2004 by Chapman & Hall/CRC
# t0 initial value of independent variable # # tf final value of independent variable # # u0 initial condition vector of length neqn # # nsteps maximum number of rkc4 steps # # abserr absolute error tolerance # # relerr relative error tolerance # # u ODE solution vector of length neqn after # nsteps steps # # Type variables local e, h, i, j, tm, t, hmin, nfin1: # # Size arrays e:=array(1..neqn): # # Initial integration step h:=(tf-t0)/2.0: # # Minimum allowable step hmin:=(tf-t0)/nsteps: # # Start integration tm:=t0: # # While independent variable is less than the final # value, continue the integration while(tm <= tf*0.999) do # # If the next step along the solution will go past # the final value of the independent variable, set # the step to the remaining distance to the final # value if(tm+h > tf) then h:=tf-tm: end if: # # Single rkc4 step read "c:\\odelib\\maple\\ode2x2\\ssrkc4.txt": ssrkc4(neqn,tm,u0,h,u,e): t:=tm+h: #
Copyright 2004 by Chapman & Hall/CRC
# # # #
# # # #
# # # # # # # #
# # #
# #
# # # #
Flag for the end of the integration nfin1:=1: Check if any of the ODEs have violated the error criteria for i from 1 to neqn do if(abs(e[i]) > abs(u[i])*relerr+abserr) then Error violation, so integration is not complete. Reduce integration step because of error violation and repeat integration from the base point h:=h/2.0: nfin1:=0: break: end if: end do: If the current step is less than the minimum allowable step, set the step to the minimum allowable value and continue integration from new base point if(h < hmin) then h:=hmin: nfin1:=1: end if: If there is no error violation, continue the integration from new base point if(nfin1 = 1) then for i from 1 to neqn do u0[i]:=u[i]: end do: tm:=t:
Test if integration step can be increased for i from 1 to neqn do if(abs(e[i]) > (abs(u[i])*relerr+abserr)/16.0) then Integration step cannot be increased nfin1:=0: break: end if: end do: Increase integration step if(nfin1 = 1) then h:=h*2.0: end if: Continue for no error violation (nfin1=1) end if:
#
Copyright 2004 by Chapman & Hall/CRC
# Continue while end do: # # End of rkc4b end:
Program 2.6.7 Integrator rkc4b
ssrkc4:=proc(neqn,t0,u0,h,u,e) # # Procedure ssrkc4 computes an ODE solution by the # classical fourth order RK method for one step along # the solution (by calls to derv to define the ODE # derivative vector). It also estimates the truncation # error of the solution, and applies this estimate as a # correction to the solution vector. # # Argument list # # neqn number of first order ODEs # # t0 initial value of independent variable # # u0 initial condition vector of length neqn # # h integration step # # u ODE solution vector of length neqn after # one modified Euler step # # e estimate of truncation error of the solu# tion vector # # Type variables local ut0, ut, t, i, k1, k2, k3, k4, sum2, sum4: # # Declare arrays ut0:=array(1..neqn): ut:=array(1..neqn): k1:=array(1..neqn): k2:=array(1..neqn): k3:=array(1..neqn): k4:=array(1..neqn): sum2:=array(1..neqn): sum4:=array(1..neqn): # # Derivative vector at initial (base) point read "c:\\odelib\\maple\\ode1x1\\derv.txt":
Copyright 2004 by Chapman & Hall/CRC
derv(neqn,t0,u0,ut0): # # k1, advance of dependent variable vector and # independent variable for calculation of k2 for i from 1 to neqn do k1[i]:=h*ut0[i]: u[i]:=u0[i]+0.5*k1[i]: end do: t:=t0+0.5*h: # # Derivative vector at new u, t derv(neqn,t,u,ut): # # k2, advance of dependent variable vector and # independent variable for calculation of k3 for i from 1 to neqn do k2[i]:=h*ut[i]: u[i]:=u0[i]+0.5*k2[i]: end do: t:=t0+0.5*h: # # Derivative vector at new u, t derv(neqn,t,u,ut): # # k3, advance of dependent variable vector and # independent variable for calculation of k4 for i from 1 to neqn do k3[i]:=h*ut[i]: u[i]:=u0[i]+k3[i]: end do: t:=t0+h: # # Derivative vector at new u, t derv(neqn,t,u,ut): # # k4 for i from 1 to neqn do k4[i]:=h*ut[i]: end do: # # Second order step for i from 1 to neqn do sum2[i]:=u0[i]+k2[i]: end do: #
Copyright 2004 by Chapman & Hall/CRC
# Fourth order step for i from 1 to neqn do sum4[i]:=u0[i]+(1.0/6.0)*(k1[i]+2.0*k2[i] +2.0*k3[i]+k4[i]): end do: t:=t0+h: # # Truncation error estimate for i from 1 to neqn do e[i]:=sum4[i]-sum2[i]: end do: # # Fourth order solution vector (from 2,4 RK pair); # two ways to the same result are listed for i from 1 to neqn do # u[i]:=sum2[i]+e[i]: u[i]:=sum4[i]: end do: # # End of ssrkc4 end:
Program 2.6.8 Integrator ssrkc4 for a classical fourth-order RK step
Again, in ssrkc4 the read statement is for the application specific derv: # # Derivative vector at initial (base) point read "c:\\odelib\\maple\\ode1x1\\derv.txt": derv(neqn,t0,u0,ut0): rkf45a:=proc(neqn,t0,tf,u0,nsteps,u) # # Procedure rkf45a computes an ODE solution by the fixed # step RK Fehlberg 45 method for a series of points along # the solution by repeatedly calling procedure ssrkf45 for # a single RK Fehlberg 45 step. # # Argument list # # neqn number of first order ODEs #
Copyright 2004 by Chapman & Hall/CRC
# t0 initial value of independent variable # # tf final value of independent variable # # u0 initial condition vector of length neqn # # nsteps number of rkf45 steps # # u ODE solution vector of length neqn after # nsteps steps # # Type variables local e, h, i, j, tm, t: # # Size arrays e:=array(1..neqn): # # Integration step h:=(tf-t0)/nsteps: # # nsteps rkf45 steps tm:=t0: for j from 1 to nsteps do # # rkf45 step read "c:\\odelib\\maple\\ode2x2\\ssrkf45.txt": ssrkf45(neqn,tm,u0,h,u,e): t:=tm+h: # # Reset base point values for next rkf45 step for i from 1 to neqn do u0[i]:=u[i]: end do: tm:=t: # # Next rkf45 step end do: # # End of rkf45a end:
Program 2.6.9 Integrator rkf45a
Copyright 2004 by Chapman & Hall/CRC
rkf45b:=proc(neqn,t0,tf,u0,nsteps,abserr,relerr,u) # # Procedure rkf45b computes an ODE solution by the variable # step RK Fehlberg 45 method for a series of points along # the solution by repeatedly calling procedure ssrkf45 for # a single RK Fehlberg 45 step. The truncation error is # estimated along the solution to adjust the integration # step according to a specified error tolerance. # # Argument list # # neqn number of first order ODEs # # t0 initial value of independent variable # # tf final value of independent variable # # u0 initial condition vector of length neqn # # nsteps maximum number number of rkf45 steps # # abserr absolute error tolerance # # relerr relative error tolerance # # u ODE solution vector of length neqn after # nsteps steps # # Type variables local e, h, i, j, tm, t, hmin, nfin1: # # Size arrays e:=array(1..neqn): # # Initial integration step h:=(tf-t0)/2.0: # # Minimum allowable step hmin:=(tf-t0)/nsteps: # # Start integration tm:=t0: # # While independent variable is less than the final # value, continue the integration
Copyright 2004 by Chapman & Hall/CRC
while(tm <= tf*0.999) do # # # # # # #
# # # # #
# # # #
# # # # # # # #
# #
If the next step along the solution will go past the final value of the independent variable, set the step to the remaining distance to the final value if(tm+h > tf) then h:=tf-tm: end if: Single rkf45 step read "c:\\odelib\\maple\\ode2x2\\ssrkf45.txt": ssrkf45(neqn,tm,u0,h,u,e): t:=tm+h: Flag for the end of the integration nfin1:=1: Check if any of the ODEs have violated the error criteria for i from 1 to neqn do if(abs(e[i]) > abs(u[i])*relerr+abserr) then Error violation, so integration is not complete. Reduce integration step because of error violation and repeat integration from the base point h:=h/2.0: nfin1:=0: break: end if: end do: If the current step is less than the minimum allowable step, set the step to the minimum allowable value and continue integration from new base point if(h < hmin) then h:=hmin: nfin1:=1: end if: If there is no error violation, continue the integration from new base point if(nfin1 = 1) then for i from 1 to neqn do u0[i]:=u[i]: end do: tm:=t: Test if integration step can be increased for i from 1 to neqn do if(abs(e[i]) > (abs(u[i])*relerr+abserr)/32.0) then
Copyright 2004 by Chapman & Hall/CRC
# #
# # # #
Integration step cannot be increased nfin1:=0: break: end if: end do: Increase integration step if(nfin1 = 1) then h:=h*2.0: end if: Continue for no error violation (nfin1=1) end if:
# # Continue while end do: # # End of rkf45b end:
Program 2.6.10 Integrator rkf45b
ssrkf45:=proc(neqn,t0,u0,h,u,e) # # Procedure ssrkf45 computes an ODE solution by the RK # Fehlberg 45 method for one step along the solution (by # calls to derv to define the ODE derivative vector). It # also estimates the truncation error of the solution, and # applies this estimate as a correction to the solution # vector. # # Argument list # # neqn number of first order ODEs # # t0 initial value of independent variable # # u0 initial condition vector of length neqn # # h integration step # # t independent variable # # u ODE solution vector of length neqn after # one rkf45 step
Copyright 2004 by Chapman & Hall/CRC
# # e estimate of truncation error of the solu# tion vector # # Type variables local ut0, ut, t, i, k1, k2, k3, k4, k5, k6, sum4, sum5: # # Declare arrays ut0:=array(1..neqn): ut:=array(1..neqn): k1:=array(1..neqn): k2:=array(1..neqn): k3:=array(1..neqn): k4:=array(1..neqn): k5:=array(1..neqn): k6:=array(1..neqn): sum4:=array(1..neqn): sum5:=array(1..neqn): # # Derivative vector at initial (base) point read "c:\\odelib\\maple\\ode1x1\\derv.txt": derv(neqn,t0,u0,ut0): # # k1, advance of dependent variable vector and # independent variable for calculation of k2 for i from 1 to neqn do k1[i]:=h*ut0[i]: u[i]:=u0[i]+0.25*k1[i]: end do: t:=t0+0.25*h: # # Derivative vector at new u, t derv(neqn,t,u,ut): # # k2, advance of dependent variable vector and # independent variable for calculation of k3 for i from 1 to neqn do k2[i]:=h*ut[i]: u[i]:=u0[i]+(3.0/32.0)*k1[i]\ +(9.0/32.0)*k2[i]: end do: t:=t0+(3.0/8.0)*h: # # Derivative vector at new u, t derv(neqn,t,u,ut): # # k3, advance of dependent variable vector and # independent variable for calculation of k4 for i from 1 to neqn do k3[i]:=h*ut[i]:
Copyright 2004 by Chapman & Hall/CRC
u[i]:=u0[i]+(1932.0/2197.0)*k1[i]\ -(7200.0/2197.0)*k2[i]\ +(7296.0/2197.0)*k3[i]: end do: t:=t0+(12.00/13.0)*h: # # Derivative vector at new u, t derv(neqn,t,u,ut): # # k4, advance of dependent variable vector and # independent variable for calculation of k5 for i from 1 to neqn do k4[i]:=h*ut[i]: u[i]:=u0[i]+( 439.0/ 216.0)*k1[i]\ -( 8.0 )*k2[i]\ +(3680.0/ 513.0)*k3[i]\ -( 845.0/4104.0)*k4[i]: end do: t:=t0+h: # # Derivative vector at new u, t derv(neqn,t,u,ut): # # k5, advance of dependent variable vector and # independent variable for calculation of k6 for i from 1 to neqn do k5[i]:=h*ut[i]: u[i]:=u0[i]-( 8.0/ 27.0)*k1[i]\ +( 2.0 )*k2[i]\ -(3544.0/2565.0)*k3[i]\ +(1859.0/4104.0)*k4[i]\ -( 11.0/ 40.0)*k5[i]: end do: t:=t0+0.5*h: # # Derivative vector at new u, t derv(neqn,t,u,ut): # # k6 for i from 1 to neqn do k6[i]:=h*ut[i]: end do: # # Fourth order step for i from 1 to neqn do
Copyright 2004 by Chapman & Hall/CRC
sum4[i]:=u0[i]+( +( +( -( end do:
25.0/ 216.0)*k1[i]\ 1408.0/2565.0)*k3[i]\ 2197.0/4104.0)*k4[i]\ 1.0/ 5.0)*k5[i]:
# # Fifth order step for i from 1 to neqn do sum5[i]:=u0[i]+( 16.0/ 135.0)*k1[i]\ +( 6656.0/12825.0)*k3[i]\ +(28561.0/56430.0)*k4[i]\ -( 9.0/ 50.0)*k5[i]\ +( 2.0/ 55.0)*k6[i]: end do: t:=t0+h; # # Truncation error estimate for i from 1 to neqn do e[i]:=sum5[i]-sum4[i]: end do: # # Fifth order solution vector (from 4,5 RK pair); # two ways to the same result are listed for i from 1 to neqn do # u[i]:=sum4[i]+e[i]: u[i]:=sum5[i]: end do: # # End of ssrkf45 end:
Program 2.6.11 Integrator ssrkf45 for an RKF45 step Finally, intpar, inital, derv, and fprint are listed next: intpar:=proc() # # Function intpar sets the parameters to control the # integration of the 1 x 1 ODE system # # Type variables global neqn, nout, nsteps, t0, tf, abserr, relerr: # # Number of first order ODEs neqn:=1:
Copyright 2004 by Chapman & Hall/CRC
# # Number of output points nout:=6: # # Maximum number of steps in the interval t0 to tf nsteps:=100: # # Initial, final values of independent variable t0:=0.0: tf:=1.0: # # Error tolerances abserr:=1.0e-05: relerr:=1.0e-05: # # End of intpar end:
inital:=proc(neqn,t,u0) # # Procedure inital sets the initial condition vector # for the 1 x 1 ODE problem # u0[1]:=1.0: # # End of inital end:
derv:=proc(neqn,t,u,ut) # # Procedure derv computes the derivative vector # of the 1 x 1 ODE problem # # Type variables local alpha, lambda: # # Problem parameters alpha:=1.0: lambda:=1.0: # # Derivative vector ut[1]:=lambda*exp(-alpha*t)*u[1]: #
Copyright 2004 by Chapman & Hall/CRC
# End of derv end:
fprint:=proc(ncase,neqn,t,u) # # Procedure fprint displays the numerical and # exact solutions to the 1 x 1 ODE problem # # Type variables local u0, alpha, lambda, ue, diff, i: # # Problem parameters u0:=1.0: alpha:=1.0: lambda:=1.0: # # Define arrays ue:=array(1..neqn): diff:=array(1..neqn): # # Print a heading for the solution at t = 0 if (t <= 0.0) then # # Label for ODE integrator # # Fixed step modified Euler if (ncase = 1) then printf(`\n\n euler2a integrator\n\n`); # # Variable step modified Euler elif (ncase = 2) then printf(`\n\n euler2b integrator\n\n`); # # Fixed step classical fourth order RK elif (ncase = 3) then printf(`\n\n rkc4a integrator\n\n`); # # Variable step classical fourth order RK elif (ncase = 4) then printf(`\n\n rkc4b integrator\n\n`); # # Fixed step RK Fehlberg 45 elif (ncase = 5) then printf(`\n\n rkf45a integrator\n\n`); #
Copyright 2004 by Chapman & Hall/CRC
#
Variable step RK Fehlberg 45 elif (ncase = 6) then printf(`\n\n rkf45b integrator\n\n`); end if:
# # Heading printf(` t u1 u1e u1-u1e\n`): # # End of t = 0 heading end if: # # Numerical and analytical solution output # # Analytical solution ue[1]:=u0*exp(lambda/alpha*(1.0-exp(-alpha*t))): # # Difference between exact and numerical solutions for i from 1 to neqn do diff[i]:=u[i]-ue[i]: end do: # # Display the numerical and exact solutions, and their # difference printf(`%10.2f %10.5f %10.5f %10.5f \n`,t,u[1],ue[1], diff[1]); # # End of fprint end:
Program 2.6.12 intpar, inital, derv, and fprint called in the solution of Equations 1.3 and 1.4 The output from the preceding routines is as follows: euler2a integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 1.00000 1.88160 2.37421 2.58627 2.66895 2.70004
Copyright 2004 by Chapman & Hall/CRC
u1e 1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
u1-u1e 0.00000 -.00000 .00000 .00001 .00001 .00001
euler2b integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 1.00000 1.88159 2.37421 2.58626 2.66895 2.70003
u1e 1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
u1-u1e 0.00000 -.00001 -.00000 .00000 .00000 .00001
u1e 1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
u1-u1e 0.00000 .00000 .00000 .00000 .00000 .00000
u1e 1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
u1-u1e 0.00000 -.00000 -.00000 -.00000 -.00000 .00000
u1e 1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
u1-u1e 0.00000 -.00000 -.00000 -.00000 -.00000 -.00000
rkc4a integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
rkc4b integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
rkf45a integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
Copyright 2004 by Chapman & Hall/CRC
rkf45b integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 1.00000 1.88159 2.37421 2.58626 2.66894 2.70002
u1e 1.00000 1.88160 2.37421 2.58626 2.66895 2.70003
u1-u1e 0.00000 -.00000 -.00000 -.00001 -.00001 -.00001
This output is similar to that considered previously, e.g., the error tolerances set in intpar have been observed. This concludes the programming of the 1x1 ODE problem in the six languages. We have discussed general-purpose routines for the solution of systems of ODEs in the following sections of Chapter 2:
Section
Language
2.1 2.2 2.3 2.4 2.5 2.6
MATLAB C C++ Fortran Java Maple
These routines Pair
Routines
(1, 2) (2, 4) (4, 5)
euler2a , euler2b, sseuler rkc4a , rkc4b, ssrkc4 rkf45a , rkf45b, ssrkf45
can now be applied to systems of ODEs (the nxn problem with n > 1). By a straightforward extension, they can also be applied to PDE problems. These topics are discussed in the remaining chapters of this book.
Copyright 2004 by Chapman & Hall/CRC
3 Solution of a 2 x 2 ODE System
We now consider the programming of the 2x2 ODE problem of Equations 1.6, 1.16, and 1.17 using the general-purpose ODE library integrators from Chapter 2. Since these integrators were listed and discussed in some detail in Chapter 2, they are not discussed here, except as they are called in a main program for the solution of the 2x2 ODE problem. The order in which the six languages are considered is the same as in Chapter 2.
3.1
Programming in MATLAB
A main program for the solution of the 2x2 ODE problem is listed below: % % % % % %
Main program ode2x2 computes the numerical solution to the 2 x 2 ODE system by six integrators Step through six integrators for int=1:6
% % Integration parameters [neqn,nout,nsteps,t0,tf,abserr,relerr]=intpar; % % Initial condition vector [u0]=inital(neqn,t0); % % Output interval tp=tf-t0;
Copyright 2004 by Chapman & Hall/CRC
% % Compute solution at nout output points for j=1:nout % % Print current solution [out]=fprint(int,neqn,t0,u0); % % Fixed step modified Euler integrator if int == 1 [u]=euler2a(neqn,t0,tf,u0,nsteps); end % % Variable step modified Euler integrator if int == 2 [u]=euler2b(neqn,t0,tf,u0,nsteps,abserr,relerr); end % % Fixed step classical fourth order RK integrator if int == 3 [u]=rkc4a(neqn,t0,tf,u0,nsteps); end % % Variable step classical fourth order RK integrator if int == 4 [u]=rkc4b(neqn,t0,tf,u0,nsteps,abserr,relerr); end % %
% %
% %
Fixed step RK Fehlberg (RKF45) integrator if int == 5 [u]=rkf45a(neqn,t0,tf,u0,nsteps); end Variable step RK Fehlberg (RKF45) integrator if int == 6 [u]=rkf45b(neqn,t0,tf,u0,nsteps,abserr,relerr); end Advance solution t0=tf; tf=tf+tp; u0=u;
% % Next output end
Copyright 2004 by Chapman & Hall/CRC
% % Next integrator end % % End of ode2x2
Program 3.1.1 MATLAB main program for the numerical integration of Equations 1.6, 1.16, with analytical solution Equation 1.17 The only essential difference between Program 3.1.1 and 2.1.1 is the use of a loop in the latter to cycle through all six integrators: % % Step through six integrators for int=1:6
Thus, int, which was set for a particular integrator by a call to intpar in Program 2.1.1, is now set by this for loop. Routines intpar, inital, derv, and fprint are listed below: function [neqn,nout,nsteps,t0,tf,abserr,relerr]=intpar % % Function intpar sets the parameters to control the % integration of the 2 x 2 ODE system % % Number of first order ODEs neqn=2; % % Number of output points nout=6; % % Maximum number of steps in the interval t0 to tf nsteps=100; % % Initial, final values of independent variable t0=0.0; tf=1.0; % % Error tolerances abserr=1.0e-05; relerr=1.0e-05; % % End of intpar
Copyright 2004 by Chapman & Hall/CRC
function [u0]=inital(neqn,t) % % Function inital sets the initial condition vector % for the 2 x 2 ODE problem % u0(1)=0; u0(2)=2; % % End of inital
function [ut]=derv(neqn,t,u) % % Function derv computes the derivative vector % of the 2 x 2 ODE problem % % Problem parameters a=5.5; b=4.5; % % Derivative vector ut(1)=-a*u(1)+b*u(2); ut(2)= b*u(1)-a*u(2); % % End of derv
function [out]=fprint(ncase,neqn,t,u) % % % % % %
Function fprint displays the numerical and exact solutions to the 2 x 2 ODE problem Return current value of independent variable (MATLAB requires at least one return argument) out=t;
% % Problem parameters a=5.5; b=4.5; % % Print a heading for the solution at t = 0 if(t<=0.0) %
Copyright 2004 by Chapman & Hall/CRC
% % %
% %
% %
% %
% %
% %
Label for ODE integrator Fixed step modified Euler if(ncase==1) fprintf('\n\n euler2a integrator\n\n'); Variable step modified Euler elseif(ncase==2) fprintf('\n\n euler2b integrator\n\n'); Fixed step classical fourth order RK elseif(ncase==3) fprintf('\n\n rkc4a integrator\n\n'); Variable step classical fourth order RK elseif(ncase==4) fprintf('\n\n rkc4b integrator\n\n'); Fixed step RK Fehlberg 45 elseif(ncase==5) fprintf('\n\n rkf45a integrator\n\n'); Variable step RK Fehlberg 45 elseif(ncase==6) fprintf('\n\n rkf45b integrator\n\n'); end
% % Heading fprintf(' t u1 u2 u1-ue1 % % End of t = 0 heading end % % Numerical and analytical solution output %
% %
% %
u2-ue2\n');
Exact solution eigenvalues e1=-(a-b); e2=-(a+b); Analytical solution ue(1)=exp(e1*t)-exp(e2*t); ue(2)=exp(e1*t)+exp(e2*t); Difference between exact and numerical solutions diff=u-ue;
Copyright 2004 by Chapman & Hall/CRC
% % %
Display the numerical and exact solutions, and their difference fprintf('%10.2f %10.5f %10.5f %10.5f %10.5f \n',t,u, diff);
% % End of fprint
Program 3.1.2 intpar, inital, derv, and fprint for the solution of Equations 1.6 and 1.16 We can note the following points about these routines: • The initial conditions of Equations 1.6 are set in inital as y1 (0) = y10 = 0, y2 (0) = y20 = 2: % u0(1)=0; u0(2)=2;
• The RHS of Equations 1.6 are programmed in derv as % % Problem parameters a=5.5; b=4.5; % % Derivative vector ut(1)=-a*u(1)+b*u(2); ut(2)= b*u(1)-a*u(2);
• The analytical solution, Equation 1.17, is programmed in fprint as % %
% %
Exact solution eigenvalues e1=-(a-b); e2=-(a+b); Analytical solution ue(1)=exp(e1*t)-exp(e2*t); ue(2)=exp(e1*t)+exp(e2*t);
• The numerical and analytical solutions, and their difference, are then displayed in fprint: % %
Difference between exact and numerical solutions diff=u-ue;
%
Copyright 2004 by Chapman & Hall/CRC
% %
Display the numerical and exact solutions, and their difference fprintf('%10.2f %10.5f %10.5f %10.5f %10.5f \n',t, u,diff);
Since the library integration routines called by Program 3.1.1, euler2a , euler2b, sseuler, rkc4a , rkc4b, ssrkc4, rkf45a , rkf45ba , and ssrkf45, are considered in detail in Chapter 2, they will not be discussed here. The output from Programs 3.1.1 and 3.1.2 is listed below:
euler2a integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 0.00000 0.36784 0.13534 0.04979 0.01832 0.00674
u2 2.00000 0.36793 0.13534 0.04979 0.01832 0.00674
u1-ue1 0.00000 0.00001 0.00000 0.00000 0.00000 0.00000
u2-ue2 0.00000 0.00001 0.00000 0.00000 0.00000 0.00000
u2 2.00000 0.36793 0.13534 0.04979 0.01832 0.00674
u1-ue1 0.00000 0.00001 0.00000 0.00000 0.00000 0.00000
u2-ue2 0.00000 0.00001 0.00000 0.00000 0.00000 0.00000
u2 2.00000 0.36792 0.13534 0.04979 0.01832 0.00674
u1-ue1 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
u2-ue2 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
euler2b integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 0.00000 0.36784 0.13534 0.04979 0.01832 0.00674
rkc4a integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 0.00000 0.36783 0.13534 0.04979 0.01832 0.00674
Copyright 2004 by Chapman & Hall/CRC
rkc4b integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 0.00000 0.36783 0.13534 0.04979 0.01832 0.00674
u2 2.00000 0.36792 0.13534 0.04979 0.01832 0.00674
u1-ue1 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
u2-ue2 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
u2 2.00000 0.36792 0.13534 0.04979 0.01832 0.00674
u1-ue1 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
u2-ue2 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
u2 2.00000 0.36792 0.13534 0.04978 0.01831 0.00674
u1-ue1 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
u2-ue2 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
rkf45a integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 0.00000 0.36783 0.13534 0.04979 0.01832 0.00674
rkf45b integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 0.00000 0.36783 0.13534 0.04978 0.01831 0.00673
We conclude from this output that the error tolerances set in intpar (1.0×10−5 ) are observed by all six integrators.
3.2
Programming in C
Since main Program 2.2.1 and the associated header file Program 2.2.2 are unchanged in the 2x2 ODE problem, they are not listed here. intpar, par, inital, derv, and fprint are listed below:
Copyright 2004 by Chapman & Hall/CRC
#include "ode2x2.h" /* Type global variables */ int neqn, nout, nsteps; double t0, tf, abserr, relerr; /* Define file ID */ FILE *fid;
void intpar() /* Function intpar sets the parameters to control the integration of the 2 x 2 ODE system */ { /* Number of ODEs */ neqn=2; /* Number of output points */ nout=6; /* Maximum number of steps in the interval t0 to tf */ nsteps=100; /* Initial, final values of independent variable */ t0=0.0; tf=1.0; /* Error tolerances */ abserr=pow(10,-5); relerr=pow(10,-5); /* End of intpar */ }
void par(double a[]) /* Function par sets the parameters for the 2 x 2 ODE problem */ { a[1]=5.5; a[2]=4.5;
Copyright 2004 by Chapman & Hall/CRC
/* End of par */ }
void inital(double u0[],double t0) /* Function inital sets the initial condition vector for the 2 x 2 ODE problem */ { /* Initial condition */ u0[1]=0.0; u0[2]=2.0; /* End of inital */ }
void derv(double ut[], double t, double u[]) /* Function derv computes the derivative vector of the 2 x 2 ODE problem */ { /* Type variables */ double a[3]; /* Problem parameters */ par(a); /* Derivative vector */ ut[1]=-a[1]*u[1]+a[2]*u[2]; ut[2]= a[2]*u[1]-a[1]*u[2]; /* End of derv */ }
void fprint(int ncase, double t, double u[]) /* Function fprint displays the numerical and exact solutions to the 2 x 2 ODE problem */ { /* Type variables */ double a[3], ue[3], diff[3], e1, e2;
Copyright 2004 by Chapman & Hall/CRC
/* Problem parameters */ par(a); /* Print a heading for the solution at t = 0 */ if(t<=0.0) { /* Label for ODE integrator */ switch(ncase) { case 1: /* Fixed step modified Euler */ fprintf(fid,"\n euler2a integrator\n"); break; case 2: /* Variable step modified Euler */ fprintf(fid,"\n euler2b integrator\n"); break;
case 3: /* Fixed step classical fourth order RK */ fprintf(fid,"\n rkc4a integrator\n"); break; case 4: /* Variable step classical fourth order RK */ fprintf(fid,"\n rkc4b integrator\n"); break; case 5: /* Fixed step RK Fehlberg 45 */ fprintf(fid,"\n rkf45a integrator\n"); break; case 6: /* Variable step RK Fehlberg 45 */ fprintf(fid,"\n rkf45b integrator\n"); break; } /* Heading */ fprintf(fid,"\n fprintf(fid,"\n
t
u1(num) u2(num)
u1(ex) u2(ex)
/* End of t = 0 heading */ } /* Analytical solution eigenvalues */ e1=-(a[1]-a[2]); e2=-(a[1]+a[2]);
Copyright 2004 by Chapman & Hall/CRC
diff1"); diff2\n\n");
/* Analytical solution vector */ ue[1]=exp(e1*t)-exp(e2*t); ue[2]=exp(e1*t)+exp(e2*t); /* Difference between exact and numerical solution vectors */ diff[1]=u[1]-ue[1]; diff[2]=u[2]-ue[2]; /* Display the numerical and exact solutions, and their difference */ fprintf(fid,"%10.2f %10.5f %10.5f %13.4e \n", t,u[1],ue[1],diff[1]); fprintf(fid," %10.5f %10.5f %13.4e \n\n", u[2],ue[2],diff[2]); /* End of fprint */ }
Program 3.2.1 intpar, par, inital, derv, and fprint for the solution of Equations 1.6 and 1.16 The only new feature of these routines is the addition of par, which sets the problem parameters a = 5.5, b = 4.5. These parameters are then used in derv and fprint by a call to par: /* Type variables */ double a[3]; /* Problem parameters */ par(a);
Of course, these parameters could be set directly in derv and fprint as in Program 3.1.2. The use of par is just an alternative, which would be more attractive as the number of parameters becomes large (so that the code for the assignment statements is programmed once in par, then used in more than one place, such as in derv and fprint, by calls to par). The output from the preceding routines is as follows: euler2a integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.00
0.00000 2.00000
0.00000 2.00000
0.0000e+00 0.0000e+00
Copyright 2004 by Chapman & Hall/CRC
1.00
0.36784 0.36793
0.36783 0.36792
5.3545e-06 7.0006e-06
2.00
0.13534 0.13534
0.13534 0.13534
4.5451e-06 4.5453e-06
3.00
0.04979 0.04979
0.04979 0.04979
2.5082e-06 2.5082e-06
4.00
0.01832 0.01832
0.01832 0.01832
1.2303e-06 1.2303e-06
5.00
0.00674 0.00674
0.00674 0.00674
5.6575e-07 5.6575e-07
euler2b integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.00
0.00000 2.00000
0.00000 2.00000
0.0000e+00 0.0000e+00
1.00
0.36784 0.36793
0.36783 0.36792
5.4556e-06 7.1360e-06
2.00
0.13534 0.13534
0.13534 0.13534
4.6322e-06 4.6323e-06
3.00
0.04979 0.04979
0.04979 0.04979
2.6575e-06 2.6575e-06
4.00
0.01832 0.01832
0.01832 0.01832
1.9472e-06 1.9472e-06
5.00
0.00674 0.00674
0.00674 0.00674
1.8392e-06 1.8392e-06
rkc4a integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.00
0.00000 2.00000
0.00000 2.00000
0.0000e+00 0.0000e+00
Copyright 2004 by Chapman & Hall/CRC
1.00
0.36783 0.36792
0.36783 0.36792
-3.8034e-10 4.4217e-10
2.00
0.13534 0.13534
0.13534 0.13534
2.2707e-11 2.2782e-11
3.00
0.04979 0.04979
0.04979 0.04979
1.2551e-11 1.2551e-11
4.00
0.01832 0.01832
0.01832 0.01832
6.1563e-12 6.1563e-12
5.00
0.00674 0.00674
0.00674 0.00674
2.8310e-12 2.8310e-12
rkc4b integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.00
0.00000 2.00000
0.00000 2.00000
0.0000e+00 0.0000e+00
1.00
0.36783 0.36792
0.36783 0.36792
-1.8012e-08 2.0370e-08
2.00
0.13534 0.13534
0.13534 0.13534
1.5347e-09 1.5407e-09
3.00
0.04979 0.04979
0.04979 0.04979
7.2352e-09 7.2352e-09
4.00
0.01832 0.01832
0.01832 0.01832
5.1152e-09 5.1152e-09
5.00
0.00674 0.00674
0.00674 0.00674
1.7098e-08 1.7098e-08
rkf45a integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.00
0.00000 2.00000
0.00000 2.00000
0.0000e+00 0.0000e+00
Copyright 2004 by Chapman & Hall/CRC
1.00
0.36783 0.36792
0.36783 0.36792
4.4252e-12 -4.4910e-12
2.00
0.13534 0.13534
0.13534 0.13534
-2.4120e-14 -2.4869e-14
3.00
0.04979 0.04979
0.04979 0.04979
-1.3572e-14 -1.3593e-14
4.00
0.01832 0.01832
0.01832 0.01832
-6.6648e-15 -6.6648e-15
5.00
0.00674 0.00674
0.00674 0.00674
-3.0739e-15 -3.0739e-15
rkf45b integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.00
0.00000 2.00000
0.00000 2.00000
0.0000e+00 0.0000e+00
1.00
0.36783 0.36792
0.36783 0.36792
8.6412e-07 -8.7008e-07
2.00
0.13534 0.13534
0.13534 0.13534
-1.4522e-07 -1.4887e-07
3.00
0.04978 0.04978
0.04979 0.04979
-2.1496e-06 -2.1386e-06
4.00
0.01831 0.01831
0.01832 0.01832
-1.6852e-06 -1.4300e-06
5.00
0.00673 0.00674
0.00674 0.00674
-3.8224e-06 2.1107e-06
Generally, the accuracy of the numerical solution meets or exceeds the tolerances set in intpar.
Copyright 2004 by Chapman & Hall/CRC
3.3
Programming in C++
Again, since main Program 2.3.1 and the associated header file Program 2.3.2 are unchanged in the 2x2 ODE problem, they are not listed here. intpar, par, inital, derv, and fprint are listed below:
#include "DEF.h" #include /* Define file ID */ FILE *fid;
void DEF::intpar() /* Function intpar sets the parameters to control the integration of the 2 x 2 ODE system */ { /* Number of ODEs */ neqn=2; /* Number of output points */ nout=6; /* Maximum number of steps in the interval t0 to tf */ nsteps=100; /* Initial, final values of independent variable */ t0=0.0; tf=1.0; /* Error tolerances */ abserr=pow(10.0,-5.0); relerr=pow(10.0,-5.0); /* End of intpar */ }
void DEF::inital() /* Function inital sets the initial condition vector for the 2 x 2 ODE problem */
Copyright 2004 by Chapman & Hall/CRC
{ /* Initial condition */ u0[1]=0.0; u0[2]=2.0; /* End of inital */ }
void DEF::derv(double ut[], double t, double u[]) /* Function derv computes the derivative vector of the 2 x 2 ODE problem */ { /* Type variables */ double a, b; /* Problem parameters */ a=5.5; b=4.5;
/* Derivative vector */ ut[1]=-a*u[1]+b*u[2]; ut[2]= b*u[1]-a*u[2]; /* End of derv */ }
void DEF::fprint(int ncase, int neqn, double t, double u[]) /* Function fprint displays the numerical and exact solutions to the 2 x 2 ODE problem; this function has two override-defined functions */ { /* Type variables */ double ue[3], diff[3]; double a, b, e1, e2; /* Problem parameters */ a=5.5; b=4.5;
Copyright 2004 by Chapman & Hall/CRC
/* Print a heading for the solution at t = 0 */ if(t<=0.0) { /* Label for ODE integrator */ switch(ncase) { /*Fixed step modified Euler */ case 1: fprintf(fid,"\n\n euler2a integrator\n\n"); break; /* Variable step modified Euler */ case 2: fprintf(fid,"\n\n euler2b integrator\n\n"); break; /* Fixed step classical fourth order RK */ case 3: fprintf(fid,"\n\n rkc4a integrator\n\n"); break; /* Variable step classical fourth order RK */ case 4: fprintf(fid,"\n\n rkc4b integrator\n\n"); break; /* Fixed step RK Fehlberg 45 */ case 5: fprintf(fid,"\n\n rkf45a integrator\n\n"); break; /* Variable step RK Fehlberg 45 */ case 6: fprintf(fid,"\n\n rkf45b integrator\n\n"); break; } /* Heading */ fprintf(fid,"\n fprintf(fid,"\n
t
u1(num) u2(num)
/* End of t = 0 heading */ }
Copyright 2004 by Chapman & Hall/CRC
u1(ex) u2(ex)
diff1\n"); diff2\n\n");
/* Analytical solution eigenvalues*/ e1=-(a-b); e2=-(a+b); /* Analytical solution vector */ ue[1]=exp(e1*t)-exp(e2*t); ue[2]=exp(e1*t)+exp(e2*t); /* Difference between exact and numerical solutions */ diff[1]=u[1]-ue[1]; diff[2]=u[2]-ue[2]; /* Display the numerical and exact solutions, and their difference */ fprintf(fid,"%10.2f %10.5f %10.5f %13.4e\n",t,u[1],ue[1], diff[1]); fprintf(fid,"%10.2f %10.5f %10.5f %13.4e\n",t,u[2],ue[2], diff[2]); /* End of fprint */ }
void DEF::fprint(ofstream &fout, int ncase, int neqn, double t, double u[]) /* Function fprint displays the numerical and exact solutions to the 2 x 2 ODE problem; this function has two override-defined functions */ { /* Type variables */ double ue[3], diff[3]; double a, b, e1, e2; /* Problem parameters */ a=5.5; b=4.5; /* Set printing format */ fout<<setiosflags(ios::showpoint|ios::fixed) <<setprecision(7); /* Print a heading for the solution at t = 0 */ if(t<=0.0) {
Copyright 2004 by Chapman & Hall/CRC
/* Label for ODE integrator */ switch(ncase) { /* Fixed step modified Euler */ case 1: fout<<"\n\n euler2a integrator\n"; break; /* Variable step modified Euler */ case 2: fout<<"\n\n euler2b integrator\n"; break; /* Fixed step classical fourth order RK */ case 3: fout<<"\n\n rkc4a integrator\n"; break; /* Variable step classical fourth order RK */ case 4: fout<<"\n\n rkc4b integrator\n"; break; /* Fixed step RK Fehlberg 45 */ case 5: fout<<"\n\n rkf45a integrator\n"; break; /* Variable step RK Fehlberg 45 */ case 6: fout<<"\n\n rkf45b integrator\n"; break; }
/* Heading */ fout<<endl; fout<<" t"<<setw(18)<<"u1(num)"<<setw(11) <<"u1(ex)"<<setw(11)<<"diff1"<<"\n"; fout <<setw(20)<<"u2(num)"<<setw(11) <<"u2(ex)"<<setw(11)<<"diff2"<<"\n"; /* End of t = 0 heading */ }
Copyright 2004 by Chapman & Hall/CRC
/* Analytical solution eigenvalues*/ e1=-(a-b); e2=-(a+b); /* Analytical solution vector */ ue[1]=exp(e1*t)-exp(e2*t); ue[2]=exp(e1*t)+exp(e2*t); /* Difference between exact and numerical solutions */ diff[1]=u[1]-ue[1]; diff[2]=u[2]-ue[2]; fout<<endl; /* Display the numerical and exact solutions, and their difference */ fout<<setw(10)<
/* End of fprint */ }
Program 3.3.1 intpar, inital, derv, and fprint for the solution of Equations 1.6 and 1.16 The output from the preceding routines is as follows: euler2a integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.0000000
0.0000000 2.0000000
0.0000000 2.0000000
0.0000000 0.0000000
1.0000000
0.3678394 0.3679318
0.3678340 0.3679248
0.0000054 0.0000070
2.0000000
0.1353398 0.1353398
0.1353353 0.1353353
0.0000045 0.0000045
3.0000000
0.0497896 0.0497896
0.0497871 0.0497871
0.0000025 0.0000025
Copyright 2004 by Chapman & Hall/CRC
4.0000000
0.0183169 0.0183169
0.0183156 0.0183156
0.0000012 0.0000012
5.0000000
0.0067385 0.0067385
0.0067379 0.0067379
0.0000006 0.0000006
euler2b integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.0000000
0.0000000 2.0000000
0.0000000 2.0000000
0.0000000 0.0000000
1.0000000
0.3678395 0.3679320
0.3678340 0.3679248
0.0000055 0.0000071
2.0000000
0.1353399 0.1353399
0.1353353 0.1353353
0.0000046 0.0000046
3.0000000
0.0497897 0.0497897
0.0497871 0.0497871
0.0000027 0.0000027
4.0000000
0.0183176 0.0183176
0.0183156 0.0183156
0.0000019 0.0000019
5.0000000
0.0067398 0.0067398
0.0067379 0.0067379
0.0000018 0.0000018
rkc4a integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.0000000
0.0000000 2.0000000
0.0000000 2.0000000
0.0000000 0.0000000
1.0000000
0.3678340 0.3679248
0.3678340 0.3679248
0.0000000 0.0000000
2.0000000
0.1353353 0.1353353
0.1353353 0.1353353
0.0000000 0.0000000
Copyright 2004 by Chapman & Hall/CRC
3.0000000
0.0497871 0.0497871
0.0497871 0.0497871
0.0000000 0.0000000
4.0000000
0.0183156 0.0183156
0.0183156 0.0183156
0.0000000 0.0000000
5.0000000
0.0067379 0.0067379
0.0067379 0.0067379
0.0000000 0.0000000
rkc4b integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.0000000
0.0000000 2.0000000
0.0000000 2.0000000
0.0000000 0.0000000
1.0000000
0.3678340 0.3679249
0.3678340 0.3679248
0.0000000 0.0000000
2.0000000
0.1353353 0.1353353
0.1353353 0.1353353
0.0000000 0.0000000
3.0000000
0.0497871 0.0497871
0.0497871 0.0497871
0.0000000 0.0000000
4.0000000
0.0183156 0.0183156
0.0183156 0.0183156
0.0000000 0.0000000
5.0000000
0.0067380 0.0067380
0.0067379 0.0067379
0.0000000 0.0000000
rkf45a integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.0000000
0.0000000 2.0000000
0.0000000 2.0000000
0.0000000 0.0000000
1.0000000
0.3678340 0.3679248
0.3678340 0.3679248
0.0000000 0.0000000
Copyright 2004 by Chapman & Hall/CRC
2.0000000
0.1353353 0.1353353
0.1353353 0.1353353
0.0000000 0.0000000
3.0000000
0.0497871 0.0497871
0.0497871 0.0497871
0.0000000 0.0000000
4.0000000
0.0183156 0.0183156
0.0183156 0.0183156
0.0000000 0.0000000
5.0000000
0.0067379 0.0067379
0.0067379 0.0067379
0.0000000 0.0000000
rkf45b integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.0000000
0.0000000 2.0000000
0.0000000 2.0000000
0.0000000 0.0000000
1.0000000
0.3678340 0.3679248
0.3678340 0.3679248
0.0000000 0.0000000
2.0000000
0.1353353 0.1353353
0.1353353 0.1353353
0.0000000 0.0000000
3.0000000
0.0497871 0.0497871
0.0497871 0.0497871
0.0000000 0.0000000
4.0000000
0.0183156 0.0183156
0.0183156 0.0183156
0.0000000 0.0000000
5.0000000
0.0067380 0.0067380
0.0067379 0.0067379
0.0000000 0.0000000
Generally, the accuracy of the numerical solution meets or exceeds the tolerances set in intpar.
3.4
Programming in Fortran
Again, since main Program 2.4.1 is unchanged in the 2x2 ODE problem, it is not listed here. intpar, par, inital, derv, and fprint are listed below:
Copyright 2004 by Chapman & Hall/CRC
subroutine intpar(neqn,nout,nsteps,t0,tf,abserr,relerr) C C Subroutine intpar sets the parameters to control the C integration of the 2 x 2 ODE system C C Double precision coding is used implicit double precision(a-h,o-z) C C Number of ODEs neqn=2 C C Number of output points nout=6 C C Maximum number of steps in the interval t0 to tf nsteps=100 C C Initial, final values of the independent variable t0=0.0d0 tf=0.2d0 C C Error tolerances abserr=1.0d-05 relerr=1.0d-05 return C C End of intpar end
subroutine inital(neqn,t,u0) C C Subroutine inital sets the initial condition vector C for the 2 x 2 ODE problem C C Double precision coding is used implicit double precision(a-h,o-z) C C Size the arrays dimension u0(neqn) C C Initial condition u0(1)=0.0d0 u0(2)=2.0d0 return
Copyright 2004 by Chapman & Hall/CRC
C C End of inital end
subroutine par(a,b) C C Subroutine par sets the parameters for the 2 x 2 ODE C problem C C Double precision coding is used implicit double precision(a-h,o-z) C C Problem parameters a=5.5d0 b=4.5d0 return C C End of par end
subroutine derv(neqn,t,u,ut) C C Subroutine derv computes the derivative vector C of the 2 x 2 ODE problem C C Double precision coding is used implicit double precision(a-h,o-z) C C Size the arrays dimension u(neqn), ut(neqn) C C Problem parameters call par(a,b) C C Derivative vector ut(1)=-a*u(1)+b*u(2) ut(2)= b*u(1)-a*u(2) return C C End of derv end
Copyright 2004 by Chapman & Hall/CRC
subroutine fprint(no,ncase,neqn,t,u) C C C Subroutine fprint displays the numerical and C analytical solutions to the 2 x 2 ODE problem C C Double precision coding is used implicit double precision(a-h,o-z) C C Size the arrays dimension u(neqn) C C Problem parameters call par(a,b) C C Print a heading for the solution at t = 0 if(t.le.0.0d0)then C C Label for ODE integrator C C Fixed step modfied Euler if(ncase.eq.1)then write(no,11) 11 format(/,6x,'euler2a integrator') C C Variable step modified Euler else if(ncase.eq.2)then write(no,12) 12 format(/,6x,'euler2b integrator') C C Fixed step classical fourth order RK else if(ncase.eq.3)then write(no,13) 13 format(/,6x,'rkc4a integrator') C C Variable step classical fourth order RK else if(ncase.eq.4)then write(no,14) 14 format(/,6x,'rkc4b integrator') C C Fixed step RK Fehlberg 45 else if(ncase.eq.5)then write(no,15) 15 format(/,6x,'rkf45a integrator') C
Copyright 2004 by Chapman & Hall/CRC
C
16
Variable step RK Fehlberg 45 else if(ncase.eq.6)then write(no,16) format(/,6x,'rkf45b integrator') end if
C C Heading write(no,2) 2 format(/,9x,'t',3x,'u1(num)',4x,'u1(ex)',8x,'diff1',/, 1 10x, 3x,'u2(num)',4x,'u2(ex)',8x,'diff2',/) C C End of t = 0 heading end if C C Analytical solution u1exact=dexp(-(a-b)*t)-dexp(-(a+b)*t) u2exact=dexp(-(a-b)*t)+dexp(-(a+b)*t) C C Difference between exact and numerical solution vectors diff1=u(1)-u1exact diff2=u(2)-u2exact C C Display the numerical and exact solutions, C and their difference write(no,3)t,u(1),u1exact,diff1,u(2),u2exact,diff2 3 format(f10.2,2f10.5,e13.4,/,10x,2f10.5,e13.4,/) return C C End of fprint end
Program 3.4.1 intpar, inital, par, derv, and fprint for the solution of Equations 1.6 and 1.16 The output from the preceding routines is as follows: euler2a integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.00
0.00000 2.00000
0.00000 2.00000
0.0000E+00 0.0000E+00
1.00
0.36784 0.36793
0.36783 0.36792
0.5354E-05 0.7001E-05
Copyright 2004 by Chapman & Hall/CRC
2.00
0.13534 0.13534
0.13534 0.13534
0.4545E-05 0.4545E-05
3.00
0.04979 0.04979
0.04979 0.04979
0.2508E-05 0.2508E-05
4.00
0.01832 0.01832
0.01832 0.01832
0.1230E-05 0.1230E-05
5.00
0.00674 0.00674
0.00674 0.00674
0.5657E-06 0.5657E-06
euler2b integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.00
0.00000 2.00000
0.00000 2.00000
0.0000E+00 0.0000E+00
1.00
0.36784 0.36793 0.13534 0.13534
0.36783 0.36792 0.13534 0.13534
0.5456E-05 0.7136E-05 0.4632E-05 0.4632E-05
3.00
0.04979 0.04979
0.04979 0.04979
0.2658E-05 0.2658E-05
4.00
0.01832 0.01832
0.01832 0.01832
0.1947E-05 0.1947E-05
5.00
0.00674 0.00674
0.00674 0.00674
0.1839E-05 0.1839E-05
2.00
rkc4a integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.00
0.00000 2.00000
0.00000 2.00000
0.0000E+00 0.0000E+00
1.00
0.36783 0.36792
0.36783 0.36792
-0.3803E-09 0.4422E-09
Copyright 2004 by Chapman & Hall/CRC
2.00
0.13534 0.13534
0.13534 0.13534
0.2271E-10 0.2278E-10
3.00
0.04979 0.04979
0.04979 0.04979
0.1255E-10 0.1255E-10
4.00
0.01832 0.01832
0.01832 0.01832
0.6156E-11 0.6156E-11
5.00
0.00674 0.00674
0.00674 0.00674
0.2831E-11 0.2831E-11
rkc4b integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.00
0.00000 2.00000
0.00000 2.00000
0.0000E+00 0.0000E+00
1.00
0.36783 0.36792
0.36783 0.36792
-0.1801E-07 0.2037E-07
2.00
0.13534 0.13534
0.13534 0.13534
0.1535E-08 0.1541E-08
3.00
0.04979 0.04979
0.04979 0.04979
0.7235E-08 0.7235E-08
4.00
0.01832 0.01832
0.01832 0.01832
0.5115E-08 0.5115E-08
5.00
0.00674 0.00674
0.00674 0.00674
0.1710E-07 0.1710E-07
rkf45a integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.00
0.00000 2.00000
0.00000 2.00000
0.0000E+00 0.0000E+00
1.00
0.36783 0.36792
0.36783 0.36792
0.4425E-11 -0.4491E-11
Copyright 2004 by Chapman & Hall/CRC
2.00
0.13534 0.13534
0.13534 0.13534
-0.2412E-13 -0.2487E-13
3.00
0.04979 0.04979
0.04979 0.04979
-0.1357E-13 -0.1359E-13
4.00
0.01832 0.01832
0.01832 0.01832
-0.6665E-14 -0.6665E-14
5.00
0.00674 0.00674
0.00674 0.00674
-0.3074E-14 -0.3074E-14
rkf45b integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.00
0.00000 2.00000
0.00000 2.00000
0.0000E+00 0.0000E+00
1.00
0.36783 0.36792
0.36783 0.36792
0.8641E-06 -0.8701E-06
2.00
0.13534 0.13534
0.13534 0.13534
-0.1452E-06 -0.1489E-06
3.00
0.04978 0.04978
0.04979 0.04979
-0.2150E-05 -0.2139E-05
4.00
0.01831 0.01831
0.01832 0.01832
-0.1685E-05 -0.1430E-05
5.00
0.00673 0.00674
0.00674 0.00674
-0.3822E-05 0.2111E-05
Generally, the accuracy of the numerical solution meets or exceeds the tolerances set in intpar.
3.5
Programming in Java
Again, since main Program 2.5.1 and interface routines 2.5.2 are unchanged in the 2x2 ODE problem, they are not listed here. intpar, par, inital, derv, and fprint are listed below:
Copyright 2004 by Chapman & Hall/CRC
/* This file is a member of the package mol */ package mol; import import import import
mol.MOL; java.math.*; java.io.*; java.text.*;
public class DEF extends MOL implements ode2x2interface { public DEF() { /* Integration parameters */ this.intpar(); /* Declare arrays */ u0=new double[SIZE]; u=new double[SIZE]; e=new double[SIZE]; /* Problem parameters */ this.par(); /* Initial condition vector */ this.inital(); }
public void intpar() /* Function intpar sets the parameters to control the integration of the 2 x 2 ODE system */ { /* Number of ODEs */ neqn=2; /* Size of arrays in MOL library */ SIZE=neqn+1; /* Number of output points */ nout=6;
/* Maximum number of steps in the interval t0 to tf */
Copyright 2004 by Chapman & Hall/CRC
nsteps=100; /* Initial, final values of the independent variable */ t0=0.0; tf=1.0; /* Error tolerances */ abserr=Math.pow(10.0,-5.0); relerr=Math.pow(10.0,-5.0); /* End of inpar */ }
public void inital() /* Function inital sets the initial condition vector for the 2 x 2 ODE problem */ { u0[1]=0.0E0; u0[2]=2.0E0; /* End of inital */ }
public void par() /* Function par sets the parameters for the 2 x 2 ODE problem */ { a=5.5; b=4.5; /* End of par */ }
public void derv(double ut[], double t, double u[]) /* Function derv computes the derivative vector of the 2 x 2 ODE problem */ { /* Problem parameters */ par();
Copyright 2004 by Chapman & Hall/CRC
/* Derivative vector */ ut[1]=-a*u[1] + b*u[2]; ut[2]= b*u[1] - a*u[2]; /* End of derv */ }
public void fprint(PrintWriter f, int ncase, int neqn, double t, double u[]) /* Function fprint displays the numerical and exact solutions to the 2 x 2 ODE problem */ { /* Type variables */ double ue1, ue2; double diff1, diff2; double e1, e2; /* Print a heading for the solution at t = 0 */ if(t<=0.0) { /* Label for ODE integrator */ switch(ncase) { /*Fixed step modified Euler */ case 1: f.println("\n euler2a integrator\n"); break; /* Variable step modified Euler */ case 2: f.println("\n euler2b integrator\n"); break; /* Fixed step classical fourth order RK */ case 3: f.println("\n rkc4a integrator\n"); break; /* Variable step classical fourth order RK */ case 4: f.println("\n rkc4b integrator\n"); break;
Copyright 2004 by Chapman & Hall/CRC
/* Fixed step RK Fehlberg 45 */ case 5: f.println("\n rkf45a integrator\n"); break; /* Variable step RK Fehlberg 45 */ case 6: f.println("\n rkf45b integrator\n"); break; } /* Heading */ f.println(" f.println("
t
u1(num) u2(num)
u1(ex) u2(ex)
diff1"); diff2");
/* End of t = 0 heading */ } /* Analytical solution */ ue1=Math.exp(-(a-b)*t)-Math.exp(-(a+b)*t); ue2=Math.exp(-(a-b)*t)+Math.exp(-(a+b)*t); /* Difference between exact and numerical solution vectors */ diff1=u[1]-ue1; diff2=u[2]-ue2; /* Display format for floating numbers */ DecimalFormat df1 = new DecimalFormat(" 0.00"); DecimalFormat df2 = new DecimalFormat("0.0000000"); /* Display the numerical and exact solutions, and their difference */ f.println("\n"+df1.format(t)+"\t"+df2.format(u[1]) +"\t"+df2.format(ue1)+"\t"+df2.format(diff1)); f.println(" \t"+df2.format(u[2]) +"\t"+df2.format(ue2)+"\t"+df2.format(diff2)); /* End of fprint */ } /* End of DEF */ }
Program 3.5.1 intpar, inital, par, derv, and fprint for the solution of Equations 1.6 and 1.16
Copyright 2004 by Chapman & Hall/CRC
The output from the preceding routines is as follows:
euler2a integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.00
0.0000000 2.0000000
0.0000000 2.0000000
0.0000000 0.0000000
1.00
0.3678394 0.3679318
0.3678340 0.3679248
0.0000054 0.0000070
2.00
0.1353398 0.1353398
0.1353353 0.1353353
0.0000045 0.0000045
3.00
0.0497896 0.0497896
0.0497871 0.0497871
0.0000025 0.0000025
4.00
0.0183169 0.0183169
0.0183156 0.0183156
0.0000012 0.0000012
5.00
0.0067385 0.0067385
0.0067379 0.0067379
0.0000006 0.0000006
euler2b integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.00
0.0000000 2.0000000
0.0000000 2.0000000
0.0000000 0.0000000
1.00
0.3678395 0.3679320
0.3678340 0.3679248
0.0000055 0.0000071
2.00
0.1353399 0.1353399
0.1353353 0.1353353
0.0000046 0.0000046
3.00
0.0497897 0.0497897
0.0497871 0.0497871
0.0000027 0.0000027
Copyright 2004 by Chapman & Hall/CRC
4.00
0.0183176 0.0183176
0.0183156 0.0183156
0.0000019 0.0000019
5.00
0.0067398 0.0067398
0.0067379 0.0067379
0.0000018 0.0000018
rkc4a integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.00
0.0000000 2.0000000
0.0000000 2.0000000
0.0000000 0.0000000
1.00
0.3678340 0.3679248
0.3678340 0.3679248
-0.0000000 0.0000000
2.00
0.1353353 0.1353353
0.1353353 0.1353353
0.0000000 0.0000000
3.00
0.0497871 0.0497871
0.0497871 0.0497871
0.0000000 0.0000000
4.00
0.0183156 0.0183156
0.0183156 0.0183156
0.0000000 0.0000000
5.00
0.0067379 0.0067379
0.0067379 0.0067379
0.0000000 0.0000000
rkc4b integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.00
0.0000000 2.0000000
0.0000000 2.0000000
0.0000000 0.0000000
1.00
0.3678340 0.3679249
0.3678340 0.3679248
-0.0000000 0.0000000
2.00
0.1353353 0.1353353
0.1353353 0.1353353
0.0000000 0.0000000
Copyright 2004 by Chapman & Hall/CRC
3.00
0.0497871 0.0497871
0.0497871 0.0497871
0.0000000 0.0000000
4.00
0.0183156 0.0183156
0.0183156 0.0183156
0.0000000 0.0000000
5.00
0.0067380 0.0067380
0.0067379 0.0067379
0.0000000 0.0000000
rkf45a integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.00
0.0000000 2.0000000
0.0000000 2.0000000
0.0000000 0.0000000
1.00
0.3678340 0.3679248
0.3678340 0.3679248
0.0000000 -0.0000000
2.00
0.1353353 0.1353353
0.1353353 0.1353353
-0.0000000 -0.0000000
3.00
0.0497871 0.0497871
0.0497871 0.0497871
-0.0000000 -0.0000000
4.00
0.0183156 0.0183156
0.0183156 0.0183156
-0.0000000 -0.0000000
5.00
0.0067379 0.0067379
0.0067379 0.0067379
-0.0000000 -0.0000000
rkf45b integrator t
u1(num) u2(num)
u1(ex) u2(ex)
diff1 diff2
0.00
0.0000000 2.0000000
0.0000000 2.0000000
0.0000000 0.0000000
1.00
0.3678349 0.3679240
0.3678340 0.3679248
0.0000009 -0.0000009
Copyright 2004 by Chapman & Hall/CRC
2.00
0.1353351 0.1353351
0.1353353 0.1353353
-0.0000001 -0.0000001
3.00
0.0497849 0.0497849
0.0497871 0.0497871
-0.0000021 -0.0000021
4.00
0.0183140 0.0183142
0.0183156 0.0183156
-0.0000017 -0.0000014
5.00
0.0067341 0.0067401
0.0067379 0.0067379
-0.0000038 0.0000021
Generally, the accuracy of the numerical solution meets or exceeds the tolerances set in intpar.
3.6
Programming in Maple
Since main Program 3.6.1 (and subordinate routines) accesses specific files by read statements, it is listed first: > restart: > read "c:\\odelib\\maple\\ode2x2\\ode2x2.txt"; > ode2x2();
Program 3.6.1 Maple main program ode2x2.mws for the numerical integration of Equations 1.6 and 1.16
ode2x2:=proc() # # Main program ode2x2 computes the numerical # solution to the 2 x 2 ODE system by one of # six integrators # # Type variables global neqn, nout, nsteps, t0, tf, abserr, relerr: local u0, u, tp, ncase, i, j: # # Step through six integrators for ncase from 1 to 6 do
Copyright 2004 by Chapman & Hall/CRC
# # Integration parameters read "c:\\odelib\\maple\\ode2x2\\intpar.txt": intpar(): # # Size arrays u0:=array(1..neqn): u:=array(1..neqn): # # Initial condition vector read "c:\\odelib\\maple\\ode2x2\\inital.txt": inital(n,t0,u0): # # Output interval tp:=tf-t0: # # Compute solution at nout output points for j from 1 to nout do # # Print current solution read "c:\\odelib\\maple\\ode2x2\\fprint.txt": fprint(ncase,neqn,t0,u0): # # Fixed step modified Euler integrator if (ncase = 1) then read "c:\\odelib\\maple\\ode2x2\\euler2a.txt": euler2a(neqn,t0,tf,u0,nsteps,u): end if: # # Variable step modified Euler integrator if (ncase = 2) then read "c:\\odelib\\maple\\ode2x2\\euler2b.txt": euler2b(neqn,t0,tf,u0,nsteps,abserr,relerr,u): end if: # # Fixed step classical fourth order RK integrator if (ncase = 3) then read "c:\\odelib\\maple\\ode2x2\\rkc4a.txt": rkc4a(neqn,t0,tf,u0,nsteps,u): end if: # # Variable step classical fourth order RK integrator if (ncase = 4) then read "c:\\odelib\\maple\\ode2x2\\rkc4b.txt": rkc4b(neqn,t0,tf,u0,nsteps,abserr,relerr,u): end if:
Copyright 2004 by Chapman & Hall/CRC
# #
# #
# #
Fixed step RK Fehlberg (RKF45) integrator if (ncase = 5) then read "c:\\odelib\\maple\\ode2x2\\rkf45a.txt": rkf45a(neqn,t0,tf,u0,nsteps,u): end if: Variable step RK Fehlberg (RKF45) integrator if (ncase = 6) then read "c:\\odelib\\maple\\ode2x2\\rkf45b.txt": rkf45b(neqn,t0,tf,u0,nsteps,abserr,relerr,u): end if: Advance solution t0:=tf: tf:=tf+tp: for i from 1 to neqn do u0[i]:=u[i]: end do:
# # Next output end do: # # Next integrator end do: # # End of ode2x2.txt end:
Program 3.6.2 Maple main program ode2x2.txt for the numerical integration of Equations 1.6 and 1.16 Note the reference to specific files by read statements, e.g., # # Initial condition vector read "c:\\odelib\\maple\\ode2x2\\inital.txt": inital(neqn,t0,u0):
intpar, inital, derv, and fprint are listed below: intpar:=proc() # # Function intpar sets the parameters to control the # integration of the 2 x 2 ODE problem
Copyright 2004 by Chapman & Hall/CRC
# # Type variables global neqn, nout, nsteps, t0, tf, abserr, relerr: # # Number of first order ODEs neqn:=2: # # Number of output points nout:=6: # # Maximum number of steps in the interval t0 to tf nsteps:=100: # # Initial, final values of independent variable t0:=0.0: tf:=1.0: # # Error tolerances abserr:=1.0e-05: relerr:=1.0e-05: # # End of intpar end:
inital:=proc(neqn,t,u0) # # Procedure inital sets the initial condition vector # for the 2 x 2 ODE problem # u0[1]:=0: u0[2]:=2: # # End of inital end:
derv:=proc(neqn,t,u,ut) # # Procedure derv computes the derivative vector # of the 2 x 2 ODE problem # # Type variables global a, b: #
Copyright 2004 by Chapman & Hall/CRC
# Problem parameters a:=5.5: b:=4.5: # # Derivative vector ut[1]:=-a*u[1]+b*u[2]: ut[2]:= b*u[1]-a*u[2]: # # End of derv end:
fprint:=proc(ncase,neqn,t,u) # # Procedure fprint displays the numerical and # exact solutions to the 2 x 2 ODE problem # # Type variables global a, b: local e1, e2, ue, diff, i: # # Define arrays ue:=array(1..neqn): diff:=array(1..neqn): # # Print a heading for the solution at t = 0 if (t <= 0.0) then # # Label for ODE integrator # # Fixed step modified Euler if (ncase = 1) then printf(`\n\n euler2a integrator\n\n`); # # Variable step modified Euler elif (ncase = 2) then printf(`\n\n euler2b integrator\n\n`); # # Fixed step classical fourth order RK elif (ncase = 3) then printf(`\n\n rkc4a integrator\n\n`); # # Variable step classical fourth order RK elif (ncase = 4) then printf(`\n\n rkc4b integrator\n\n`);
Copyright 2004 by Chapman & Hall/CRC
# #
# #
Fixed step RK Fehlberg 45 elif (ncase = 5) then printf(`\n\n rkf45a integrator\n\n`); Variable step RK Fehlberg 45 elif (ncase = 6) then printf(`\n\n rkf45b integrator\n\n`); end if:
# # Heading printf(` t u1 u2 u1-ue1 u2-ue2\n`); # # End of t = 0 heading end if: # # Numerical and analytical solution output # # Exact solution eigenvalues e1:=-(a-b): e2:=-(a+b): # # Analytical solution ue[1]:=exp(e1*t)-exp(e2*t): ue[2]:=exp(e1*t)+exp(e2*t): # # Difference between exact and numerical solutions for i from 1 to neqn do diff[i]:=u[i]-ue[i]: end do: # # Display the numerical and exact solutions, # and their difference printf(`%10.2f %10.5f %10.5f %10.5f %10.5f \n`,t,u[1], u[2],diff[1],diff[2]); # # End of fprint end:
Program 3.6.3 intpar, inital, derv, and fprint for the solution of Equations 1.6 and 1.16
The output from the preceding routines is as follows:
Copyright 2004 by Chapman & Hall/CRC
euler2a integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 0.00000 .36784 .13534 .04979 .01832 .00674
u2 2.00000 .36793 .13534 .04979 .01832 .00674
u1-ue1 0.00000 .00001 .00000 .00000 .00000 .00000
u2-ue2 0.00000 .00001 .00000 .00000 .00000 .00000
u2 2.00000 .36793 .13534 .04979 .01832 .00674
u1-ue1 0.00000 .00001 .00000 .00000 .00000 .00000
u2-ue2 0.00000 .00001 .00000 .00000 .00000 .00000
u2 2.00000 .36792 .13534 .04979 .01832 .00674
u1-ue1 0.00000 .00000 .00000 .00000 .00000 .00000
u2-ue2 0.00000 .00000 .00000 .00000 .00000 .00000
u2 2.00000 .36792 .13534 .04979 .01832 .00674
u1-ue1 0.00000 -.00000 .00000 .00000 .00000 .00000
u2-ue2 0.00000 .00000 .00000 .00000 .00000 .00000
euler2b integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 0.00000 .36784 .13534 .04979 .01832 .00674
rkc4a integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 0.00000 .36783 .13534 .04979 .01832 .00674
rkc4b integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 0.00000 .36783 .13534 .04979 .01832 .00674
Copyright 2004 by Chapman & Hall/CRC
rkf45a integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 0.00000 .36783 .13534 .04979 .01832 .00674
u2 2.00000 .36792 .13534 .04979 .01832 .00674
u1-ue1 0.00000 -.00000 -.00000 0.00000 .00000 .00000
u2-ue2 0.00000 -.00000 -.00000 0.00000 .00000 .00000
u2 2.00000 .36792 .13534 .04978 .01831 .00674
u1-ue1 0.00000 .00000 -.00000 -.00000 -.00000 -.00000
u2-ue2 0.00000 -.00000 -.00000 -.00000 -.00000 -.00000
rkf45b integrator t 0.00 1.00 2.00 3.00 4.00 5.00
u1 0.00000 .36783 .13534 .04978 .01831 .00674
Generally, the accuracy of the numerical solution meets or exceeds the tolerances set in intpar. This completes the discussion of the 2x2 ODE problem programmed in the six languages. Basically, what we have considered is the use of the library integrations for the solution of nxn systems of ODEs (as illustrated by the solution of the 2x2 system). We again point out that the preceding numerical solutions are for a = 5.5, b = 4.5 corresponding to the nonstiff case λ1 = − 1, λ2 = −10. As expected, this problem can be handled efficiently and with good accuracy by the six nonstiff integrators (stability is not a problem). However, for the stiff case a = 500, 000.5, b = 499, 999.5 listed after Equations 1.54, a stiff integrator should be used to efficiently handle the problem of stability. This requirement (for a stiff or implicit integrator) is discussed in some detail in Appendix C. Thus, we emphasize that the library integrators discussed previously have limitations (as do all numerical algorithms). They are therefore intended to serve as a starting point, and to demonstrate some basic concepts and approaches. But success in solving any particular problem cannot be guaranteed in advance, and generally some experimentation with the choice of integrators and parameters (such as error tolerances) is required to arrive at a solution with acceptable accuracy and computational effort.
Copyright 2004 by Chapman & Hall/CRC
For example, we offer the suggestion that for a new ODE problem, a nonstiff (explicit) integrator should be tried first. Our experience has indicated that a broad spectrum of problems can be handled in this way. If the calculations appear to be excessive, possibly signaling stiffness, then a switch to a stiff integrator is a logical next step. We now consider two problems in PDEs in the next two chapters. We shall see that the preceding techniques for ODEs can also be applied to PDEs.
Copyright 2004 by Chapman & Hall/CRC
4 Solution of a Linear PDE
The preceding methods, e.g., the (1, 2), (2, 4) and (4, 5) RK pairs, can be applied to PDEs by using the numerical method of lines. Briefly, the spatial (boundary value) derivatives in the PDEs are replaced with algebraic approximations, typically finite differences. The resulting equations are ODEs in the initial value variable, previously designated t, so that we have an initial value problem in ODEs. This is exactly the problem we considered in Chapters 1 through 3. We now illustrate this approach to PDEs through two examples: (1) a linear PDE problem in this chapter and (2) a nonlinear PDE problem in the next chapter. The linear PDE problem is just Fourier’s second law, which is also called the heat conduction equation: ∂u ∂ 2u = 2 ∂t ∂x
(4.1)
Briefly, here is some terminology and associated mathematical conditions for Equation 4.1: • u is the dependent variable. Although Equation 4.1 typically describes the temperature distribution in a solid, and therefore T (for temperature) would seem a more logical name for the dependent variable, by convention, at least in much of the mathematical literature, u is used for a PDE dependent variable. • x and t are the independent variables. There are two types of such independent variables: — x is a spatial or boundary value (BV) variable. As these names imply, x defines a spatial scale, i.e., a distance. Also, our physical world is three dimensional, so Equation 4.1 is a one-dimensional (1D) approximation. Such an approximation might seem unrealistic (since the world is three dimensional), but there are often circumstances under which a 1D (or 2D) approximation is quite adequate; we do not go into
Copyright 2004 by Chapman & Hall/CRC
• • •
•
•
details here, but the use of a lower-dimensional approximation is quite common. — t is an initial value (IV) variable, generally time. The features that distinguish IV from BV variables are considered subsequently. Equation 4.1 is linear since the dependent variable u is to the first power. In other words, Equation 4.1 is first degree. Equation 4.1 might appear to not be linear or first degree because of the derivative ∂ 2 u/∂ x 2 . However, this is a second-order derivative. In other words, Equation 4.1 is first degree (linear) and second order (in x). It is easy to confuse order and degree! Equation 4.1 is a parabolic PDE. This geometric classification is one of three possibilities: elliptic, parabolic, or hyperbolic. We do not get into the details of this classification of PDEs, but it is quite important in understanding the properties of the PDE solution and in the selection of numerical methods for the calculation of numerical solutions. More details of the geometric classification of PDEs, and some of the other nomenclature we are considering here, can be found in many books (e.g., Wouver et al.,1 chap. 1). The solution to Equation 4.1 is the dependent variable, u, as a function of the independent variables, x and t, that is u(x, t). As in the case of the ODEs discussed in Chapters 1 through 3, the solution to a PDE might be (1) exact or analytical or (2) numerical.
• Since Equation 4.1 is first order in t, it requires one initial condition in t, which we take as (4.2) u(x, 0) = u0 (x) We can note the following points about this initial condition: — The initial value independent variable is specified at only one value, in this case t = 0. Even if we had a system of PDEs, each requiring, an initial condition, all of the initial conditions would be specified at the same value of the initial value independent variable. In other words, we can think of this single value as a starting point for the solution; hence the name “initial.” — The initial condition can be a function of the other PDE independent variables, in this case u0 (x). For Equation 4.1, we take u0 (x) = sin(π x). — For a PDE which is nth order in the initial value variable, n initial conditions are required. In other words, a differential equation requires a number of initial conditions equal to the order of the highestorder derivative in the initial value independent variable. In the case of Equation 4.1, n = 1. Also, all of the ODEs considered in Chapters 1 through 3 were first order in t, and therefore each ODE required only one initial condition.
Copyright 2004 by Chapman & Hall/CRC
• Since Equation 4.1 is second order in x, it requires two boundary conditions. We will take these to be u(0, t) = 0
(4.3)
u(1, t) = 0
(4.4)
We can note the following about Equations 4.3 and 4.4: — The boundary value independent variable, x, is specified at two different values, x = 0 and x = 1. In other words, boundary conditions are specified at two more values of the boundary value independent variable, in contrast to initial conditions, which are specified at the same value of the independent variable. — The boundary conditions could be functions of the other independent variables, e.g., u(0, t) = f 1 (t),
u(1, t) = f 2 (t)
(4.5)
For Equations 4.3 and 4.4, we have taken f 1 (t) = f 2 (t) = 0. — As the name “boundary condition” implies, the values of the boundary value independent variable at which the boundary conditions are specified are often boundaries of a physical system. — Whereas the initial value problem is open-ended in the sense that the range of the initial value variable (e.g., t) is unlimited and typically is the semi-infinite interval, 0 ≤ t ≤ ∞, the range of the boundary value variable is often finite; in the case of Equations 4.3 and 4.3, 0 ≤ x ≤ 1. However, the limits of the boundary value independent variable can be semi-infinite, e.g., 0 ≤ x ≤ ∞ or fully infinite −∞ ≤ x ≤ ∞. This implies a physical system of infinite dimensions, which may actually be a good approximation. • PDEs are often expressed in an alternative subscript notation. For example, we can express Equation 4.1 as ut = uxx
(4.6)
where — A subscript denotes a partial derviative with respect to an independent variable, e.g., ut = ∂u/∂t. — The subscript is repeated a number of times equal to the order of the derivative, e.g., uxx = ∂ 2 u/∂ x 2 . This subscript notation has the advantage of simplicity in expressing the PDE and in writing the associated computer code (the use of conventional partial derivatives is avoided). Equations 4.1 (or 4.6), 4.2, 4.3, and 4.4 constitute a complete, well-posed PDE problem and we can proceed to the solution, u(x, t). To reiterate, we seek
Copyright 2004 by Chapman & Hall/CRC
as the solution of a system of differential equations the dependent variables as functions of the independent variables. We have already observed this in the case of the two preceding ODE problems, i.e., (1) Equation 1.5 for y(t) is the analytical solution to Equation 1.3 and (2) Equations 1.17 for [y1 (t) y2 (t)]T are the analytical solution to Equations 1.6. We now consider the analytical and numerical solutions of Equations 4.1 to 4.4. The analytical solution is u(x, t) = sin(πx)e −π
2
t
(4.7)
Equation 4.7 can easily be verified as the solution to Equations 4.1 to 4.4:
Equation Term ∂u 4.1 ∂t ∂ 2u − 2 ∂x =0 4.2 u(x, 0) 4.3 u(0, t) 4.4 u(1, t)
Equation 4.7 −π 2 sin(π x)e −π π 2 sin(π x)e −π
2t
2t
=0 sin(π x) 0 0
Thus, we can use Equation 4.7 to evaluate a numerical solution to Equations 4.1 to 4.4; that is, we can compute the exact error in the numerical solution using Equation 4.7. Before we can proceed to the calculation of a numerical solution to Equations 4.1 to 4.4, we first have to develop a method to program PDEs, and in particular, Equation 4.1. We actually faced this problem with the ODEs of Chapter 1; i.e., there is no way that a computer can accept ODEs directly (since computers can basically only do high-speed arithmetic and store the results). In the case of ODEs, we adopted RK algorithms, which reduce the solution of ODEs to high-speed arithmetic. In other words, calculation of ODE numerical solutions required only the numerical evaluation of derivatives in the routine derv, and stepping along the solution using the RK stepping formulas such as Equation 1.19 for the Euler method—note that Equation 1.19 required only the numerical value of the derivative dyi /dt (from derv), followed by multiplication by h and addition to yi , that is, just high-speed arithmetic! In general, all of the RK methods, (1, 2), (2, 3), (2, 4), and (4, 5), ultimately require only the numerical calculation of derivatives and arithmetic. Thus, we first must develop a method for solving PDEs using only what the computer can do. Certainly a computer is not well suited for understanding partial derivatives and generating a solution like Equation 4.7 (admittedly, computer algebra systems such as Maple can produce analytical solutions
Copyright 2004 by Chapman & Hall/CRC
to a limited class of PDE problems, including Equation 4.7, but we want to develop a general numerical method for PDEs that can be applied to large, complex systems of PDEs for which computer algebra systems are unable to produce analytical solutions). To this end, we first replace the derivative ∂ 2 u/∂ x 2 in Equation 4.1 (uxx in Equation 4.6) with a finite difference (FD) approximation: ∂ 2u ∂ 2 ui ui+1 − 2ui + ui−1 = + O(x 2 ), 2 2 ∂ x x=i ∂x x 2
i = 1, 2, · · · , n
(4.8)
We can note the following points about Equation 4.8: • A spatial grid in x is defined over the interval 0 ≤ x ≤ 1. Along this grid, the value of a particular value of x is designated with the index i. Thus, i = 1 corresponds to x = 0 and i = n corresponds to x = 1. The spacing between adjacent grid points is x. In other words, x = xi − xi−1 = xi+1 − xi or x = (1 − 0)/(n − 1). • At a particular point along the grid, i, the second-order derivative ∂ 2 u/∂ x 2 x=i is approximated by the RHS of Equation 4.8, i.e., a three point FD approximation since it involves the value of u at three points, ui+1 , ui , and ui−1 . • Most importantly, note that the RHS of Equation 4.8 does not have a derivative; rather, it is entirely algerbraic, and can therefore be easily computed (since a computer does high-speed arithmetic). In other words, we have replaced the derivative ∂ 2 u/∂ x 2 , which a computer cannot handle, with an algebraic approximation, which a computer can easily evaluate. • O(x 2 ) indicates that the approximation of Equation 4.8 is second order in x, i.e., the truncation error of the approximation is proportional to x 2 . We can therefore expect to compute more accurate values of ∂ 2 u/∂ x 2 from Equation 4.8 as we use more grid points (larger n, and therefore smaller x). If the FD approximation of Equation 4.8 is substituted in Equation 4.1, we have dui ui+1 − 2ui + ui−1 , i = 1, 2, · · · , n (4.9) dt x 2 We can note the following points about Equation 4.9: • It has only one independent variable, t. Thus, it is an ODE! • Actually, Equation 4.9 represents a system of n ODEs for i = 1, 2, · · · , n. Furthermore, they are initial value ODEs since Equation 4.2 provides an initial condition, that is, u(xi , 0) = ui (t) = sin(πxi ),
i = 1, 2, · · · , n
(4.10)
Thus, we now have a system of initial value ODEs, Equation 4.9, in place of a PDE, Equation 4.1.
Copyright 2004 by Chapman & Hall/CRC
344
Ordinary and Partial Differential Equation Routines
• Since Chapters 1 to 3 are devoted to the solution of systems of initial value ODEs, we now have methods and routines for the solution of Equations 4.9, and therefore, we can compute a numerical to PDE Equation 4.1 for comparison with the analytical solution of Equation 4.8. • Basically all that is required is to step along the spatial grid and integrate the corresponding ODEs from Equations 4.9. For i = 1, we can apply BC (Equation 4.2) as u(x1 , t) = u1 (t) = 0,
du1 =0 dt
(4.11)
Similarly, for i = n, BC (Equation 4.3) becomes u(xn , t) = un (t) = 0,
dun =0 dt
(4.12)
Equations 4.11 and 4.12 merely express the fact that if the boundary values are constant (u1 (t) = 0, un (t) = 0), their derivatives are zero (du1 /dt = 0, dun /dt = 0). To conclude this brief discussion of the approximation of PDEs as systems of ODEs (termed the method of lines), we point out that FD approximations of derivatives, such as Equation 4.8, are well developed. FD approximations of any order (e.g., O(x 2 )) for derivatives of any order (e.g., ∂ 2 u/∂ x 2 ) are readily available.2–4 use an O(x 4 ) approximation of ∂ 2 u/∂ x 2 as discussed in Appendix D. We now proceed to the calculation of a numerical solution to Equations 4.1 to 4.4 using the six languages, starting with MATLAB. In each case, we use Equation 4.7 to evaluate the error in the numerical solution.
4.1
Programming in MATLAB
A main program for the solution of the linear PDE problems, Equations 4.1 to 4.4, is the same as Program 3.1.1 and therefore is not listed here. intpar, inital, derv, and fprint are listed below followed by some discussion for each. function [neqn,nout,nsteps,t0,tf,abserr,relerr]=intpar % % Function intpar sets the parameters to control the % integration of the linear PDE problem % % Number of first order ODEs neqn=21; %
Copyright 2004 by Chapman & Hall/CRC
% Number of output points nout=6; % % Maximum number of steps in the interval t0 to tf nsteps=100; % % Initial, final values of independent variable t0=0.0; tf=0.2; % % Error tolerances abserr=1.0e-05; relerr=1.0e-05;
Program 4.1.1 intpar for the solution of Equations 4.1 to 4.4 We can note the following points about intpar: • The number of ODEs is 21: % % Number of first order ODEs neqn=21;
In other words, we have defined a spatial grid with 21 points in using Equations 4.9. At this point, this selection of 21 is arbitrary and we will have to assess whether this is an adequate number of points, that is, whether it gives acceptable accuracy. Thus, we have an additional consideration that we did not have with ODEs, the spatial discretization error as set by the number of grid points. • The output interval is set to 0.2: % % Initial, final values of independent variable t0=0.0; tf=0.2;
This is different from the output interval used for the preceding ODE problems (t f = 1.0). The necessity to use a smaller output interval is 2 suggested by the analytical solution, Equation 4.7, u(x, t) = sin(π x)e −π t . The rate of change of the solution with t is determined by the exponential 2 e −π t . If the output interval is t f = 0.2, the exponential changes only 2 modestly (by e −π (0.2) at each output) and the change in the solution with t can therefore be observed. If t f = 1.0 is used, the solution would decay 2 to a small value at the end of one interval (it would change to e −π (1) of its initial value). This raises an important point; when selecting the output
Copyright 2004 by Chapman & Hall/CRC
interval t f in intpar, we must consider the timescale of the problem. If we underestimate the timescale (make the total time interval too small), we will miss a significant part of the solution since the solution will not be completely computed (t f = 0.01 would do this with nout = 6 for Equations 4.1 to 4.4); if we overestimate the timescale (make the total time interval too large), we will again miss a significant part of the solution since the solution will essentially be completely computed within the first output interval (t f = 1.0 would do this with nout = 6). Thus, we must be careful in specifying the output interval t f and the number of output points nout so as to achieve reasonable resolution of the solution in time. In other words, some tuning of the integration parameters is required; using values that are not well thought out will cause the integrator to fail (as we shall observe when we use the intpar in Program 4.1.1). In fact, a poor choice of the integration parameters is the single most common cause of integrator failures. The rest of intpar is essentially self-explanatory (it follows from the previous discussion of the two ODE problems). inital is listed below: function [u]=inital(neqn,t) % % Function inital sets the initial condition vector % of the linear PDE problem % % Problem parameters xl=0.0; xu=1.0; % % Initial condition for i=1:neqn x=xl+(i-1)/(neqn-1)*(xu-xl); u(i)=sin(pi*x); end
Program 4.1.2 inital for the solution of Equations 4.1 to 4.4 inital is an implementation of IC Equation 4.10. We can note the following points: • The initial condition function, u(xi , 0) = sin(π xi ), is computed over the grid in x using the for loop
Copyright 2004 by Chapman & Hall/CRC
% % Initial condition for i=1:neqn x=xl+(i-1)/(neqn-1)*(xu-xl); u(i)=sin(pi*x); end
Note that x covers the interval 0 ≤ x ≤ 1. • pi is a predefined constant in MATLAB so it does not have to be assigned a numerical value (this is not the case in most of the other programming languages). Thus, the 21 starting (initial) values of u(x, 0) have been assigned and we can now consider the 21 ODEs from Equations 4.9, which are programmed in derv: function [ut]=derv(neqn,t,u) % % Function derv computes the derivative vector % of the linear PDE problem % % Problem parameters xl=0.0; xu=1.0; % % BC at x = 0 ut(1)=0.0; % % BC at x = 1 ut(neqn)=0.0; % % Interior points dx=(xu-xl)/(neqn-1); dxs=dx*dx; for i=2:neqn-1 ut(i)=(u(i+1)-2.0*u(i)+u(i-1))/dxs; end
Program 4.1.3 derv for the solution of Equations 4.1 to 4.4 We can note the following points about derv: • The boundary values of x are first set: % % Problem parameters xl=0.0; xu=1.0;
Copyright 2004 by Chapman & Hall/CRC
• ODEs 4.11 and 4.12 are then programmed (here we make good use of the subscript notation of Equation 4.6 for partial derivatives, i.e., ∂u/∂t = ut ↔ ut(i)): % % BC at x = 0 ut(1)=0.0; % % BC at x = 1 ut(neqn)=0.0;
• The remaining ODEs for i = 2, 3, · · · , neqn − 1 are then programmed in a for loop according to Equation 4.9: % % Interior points dx=(xu-xl)/(neqn-1); dxs=dx*dx; for i=2:neqn-1 ut(i)=(u(i+1)-2.0*u(i)+u(i-1))/dxs; end
At the end of derv, all 21 ODEs of Equation 4.9 are programmed. These 21 derivatives (ut(i)) are then returned to one of the ODE integrators, euler2a to rkf45b, to take the next step along the solution. In this way, the complete solution to Equations 4.1 to 4.4 is computed through t and x, that is, we have u(x, t) in numerical form. This numerical solution is displayed by fprint: function [out]=fprint(ncase,neqn,t,u) % % Function fprint displays the numerical and exact % solutions to the linear PDE problem % % Declare global variables global nsteps; % % Return current value of independent variable % (MATLAB requires at least one return argument) out=t; % % Problem parameters xl=0.0; xu=1.0; %
Copyright 2004 by Chapman & Hall/CRC
% Print a heading for the solution at t = 0 if(t<=0.0) % % Label for ODE integrator % % Fixed step modified Euler if(ncase==1) fprintf('\n\n euler2a integrator\n\n'); % % Variable step modified Euler elseif(ncase==2) fprintf('\n\n euler2b integrator\n\n'); % % Fixed step classical fourth order RK elseif(ncase==3) fprintf('\n\n rkc4a integrator\n\n'); % % Variable step classical fourth order RK elseif(ncase==4) fprintf('\n\n rkc4b integrator\n\n'); % % Fixed step RK Fehlberg 45 elseif(ncase==5) fprintf('\n\n rkf45a integrator\n\n'); % % Variable step RK Fehlberg 45 elseif(ncase==6) fprintf('\n\n rkf45b integrator\n\n'); end % % Heading fprintf(' ncase = %2d neqn = %2d nsteps = %3d \n\n',ncase,neqn,nsteps); fprintf(' t u(num) u(exact) diff\n'); % % End of t = 0 heading end % % Numerical and analytical solution output % % Midpoint value of x x=(xu-xl)/2.0; % % Analytical solution at midpoint ue=exp(-pi*pi*t)*sin(pi*x);
Copyright 2004 by Chapman & Hall/CRC
% % % % %
Grid index of midpoint im=round((neqn+1)/2); Display the numerical and exact solutions, and their difference fprintf('%5.2f %11.6f %11.6f %13.4e\n',t,u(im),ue, u(im)-ue);
Program 4.1.4 fprint for the solution of Equations 4.1 to 4.4 fprint has the same function as the previous fprint routines for the two ODE problems, i.e., • Labeling of the ODE integrator • Calculation of the analytical or exact solution (ue from Equation 4.7) • Display of the numerical and analytical solutions, and their difference (u(im) − ue) (note that this comparison is made at the midpoint value x = 1/2) This completes the programming of Equations 4.1 to 4.4 (or Equations 4.9). Execution of this program produced some interesting and unexpected results. First, the solution was unstable. This instability was apparent since the displayed numbers either (1) overflowed the formats of the fprintf statement in fprint or (2) produced N AN (not a number). Thus, the stability analysis of Chapter 1 becomes quite relevant. Recall in Section 1.7 that the explicit RK methods discussed in Chapter 1 have a stability limit. For example, for the Euler method, |λh| < 2 set the maximum value of h for which an Euler integration remains stable. In the present case of the linear PDE approximated by Equation 4.9, this stability limit has apparently been reached (or exceeded). To test this idea, we executed the program again with nsteps in intpar increased from 100 to 250 (so that h could be reduced by a factor of 100/250). This reduction in nsteps was, in fact, enough to stabilize the solution, which is listed below: euler2a integrator ncase = t 0.00 0.20 0.40
1
neqn = 21
u(num) 1.000000 0.139478 0.019454
Copyright 2004 by Chapman & Hall/CRC
nsteps = 250
u(exact) 1.000000 0.138911 0.019296
diff 0.0000e+00 5.6734e-04 1.5794e-04
0.60 0.80 1.00
0.002713 0.000378 0.000053
0.002680 0.000372 0.000052
3.2977e-05 6.1203e-06 1.0649e-06
euler2b integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
2
neqn = 21
u(num) 1.000000 0.139530 0.019464 0.002719 0.000382 0.000059
nsteps = 250
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 6.1893e-04 1.6763e-04 3.8243e-05 9.2987e-06 7.0554e-06
rkc4a integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
3
neqn = 21
u(num) 1.000000 0.139476 0.019453 0.002713 0.000378 0.000053
nsteps = 250
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 5.6448e-04 1.5714e-04 3.2810e-05 6.0893e-06 1.0595e-06
rkc4b integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
4
neqn = 21
u(num) 1.000000 0.139479 0.019454 0.002714 0.000380 0.000054
Copyright 2004 by Chapman & Hall/CRC
nsteps = 250
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 5.6748e-04 1.5742e-04 3.3680e-05 7.3932e-06 2.0762e-06
rkf45a integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
5
neqn = 21
u(num) 1.000000 0.139476 0.019453 0.002713 0.000378 0.000053
nsteps = 250
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 5.6448e-04 1.5714e-04 3.2810e-05 6.0893e-06 1.0595e-06
rkf45b integrator ncase = 6 t 0.00 0.20 0.40 0.60 0.80 1.00
neqn = 21
u(num) 1.000000 0.139484 0.019453 0.002713 0.000378 0.000051
nsteps = 250
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 5.7324e-04 1.5714e-04 3.2809e-05 6.0901e-06 -5.0979e-07
However, we note that the numerical solution does not meet the error criteria set in intpar % % Error tolerances abserr=1.0e-05; relerr=1.0e-05;
For example, from euler2a, the following output has errors (diff ) that exceed the error tolerances: t 0.00 0.20 0.40 0.60
u(num) 1.000000 0.139478 0.019454 0.002713
u(exact) 1.000000 0.138911 0.019296 0.002680
diff 0.0000e+00 5.6734e-04 1.5794e-04 3.2977e-05
However, this does not mean that euler2a did not integrate correctly because now we have a second source of error in the numerical solution, the spatial discretization error from the FD approximation of Equation 4.8.
Copyright 2004 by Chapman & Hall/CRC
To investigate this possible explanation for the errors in the output, the preceding MATLAB program was executed with neqn = 31 in intpar (31 grid points). Again the solution became unstable, and it was necessary to increase nsteps to 500. When this was done, the output listed below resulted:
euler2a integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
1
neqn = 31
u(num) 1.000000 0.139163 0.019366 0.002695 0.000375 0.000052
nsteps = 500
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 2.5143e-04 6.9915e-05 1.4581e-05 2.7031e-06 4.6979e-07
euler2b integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
2
neqn = 31
u(num) 1.000000 0.139182 0.019369 0.002695 0.000365 0.000042
nsteps = 500
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 2.7122e-04 7.3151e-05 1.4604e-05 -6.9759e-06 -1.0031e-05
rkc4a integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
3
neqn = 31
u(num) 1.000000 0.139162 0.019366 0.002695 0.000375 0.000052
Copyright 2004 by Chapman & Hall/CRC
nsteps = 500
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 2.5071e-04 6.9716e-05 1.4540e-05 2.6954e-06 4.6845e-07
rkc4b integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
4
neqn = 31
u(num) 1.000000 0.139159 0.019221 0.002698 0.000327 0.000042
nsteps = 500
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 2.4782e-04 -7.5061e-05 1.7490e-05 -4.4904e-05 -9.3554e-06
rkf45a integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
5
neqn = 31
u(num) 1.000000 0.139162 0.019366 0.002695 0.000375 0.000052
nsteps = 500
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 2.5071e-04 6.9716e-05 1.4540e-05 2.6954e-06 4.6845e-07
rkf45b integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
6
neqn = 31
u(num) 1.000000 0.139161 0.019368 0.002690 0.000389 0.000052
nsteps = 500
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 2.5000e-04 7.1557e-05 9.5294e-06 1.6293e-05 6.4318e-08
Note now that the errors are smaller, e.g., for euler2a; neqn = 21 t 0.00 0.20
u(num) 1.000000 0.139478
Copyright 2004 by Chapman & Hall/CRC
u(exact) 1.000000 0.138911
diff 0.0000e+00 5.6734e-04
0.40 0.60
0.019454 0.002713
0.019296 0.002680
1.5794e-04 3.2977e-05
u(exact) 1.000000 0.138911 0.019296 0.002680
diff 0.0000e+00 2.5143e-04 6.9915e-05 1.4581e-05
neqn = 31 t 0.00 0.20 0.40 0.60
u(num) 1.000000 0.139163 0.019366 0.002695
Since the FD approximation of Equation 4.8 is O(x 2 ), we can estimate the error 31 for neqn = 31 from the error 21 for neqn = 21: 2 31 = c 31 x31 2 21 = c 21 x21
Division of one equation by the other, assuming c 21 = c 31 gives 2 x31 31 = 2 21 x21
and therefore
31 = 21
x31 x21
2
We can apply this result to estimate 31 . For neqn = 21, x21 = (1 − 0)/ (21 − 1) and for neqn = 31, x31 = (1 − 0)/(31 − 1). Therefore, x31 /x21 = (21 − 1)/(31 − 1). For t = 0.20 (using numbers from the preceding output) 5.6734e − 04 × [(21 − 1)/(31 − 1)]2 = 2.5219e − 04 2.5143e − 04 Thus, we can infer that the errors above the error tolerances set in intpar are due to the spatial gridding or discretization, i.e., the choice of a low value of neqn. In other words, if the integration t contributed a significant error to the preceding output, the O(x 2 ) analysis would not necessarily give these consistent numerical results. This conclusion was explored further by computing a solution at neqn = 41 (and nsteps = 500) in intpar. Again the solution became unstable, but by using nsteps = 1000, the solution stabilized to the values indicated below:
Copyright 2004 by Chapman & Hall/CRC
euler2a integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
1
neqn = 41
u(num) 1.000000 0.139052 0.019336 0.002689 0.000374 0.000052
nsteps = 1000
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 1.4117e-04 3.9240e-05 8.1805e-06 1.5159e-06 2.6336e-07
euler2b integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
2
neqn = 41
u(num) 1.000000 0.139235 0.019400 0.002701 0.000374 0.000046
nsteps = 1000
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 3.2417e-04 1.0374e-04 2.0959e-05 1.9047e-06 -5.8058e-06
rkc4a integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
3
neqn = 41
u(num) 1.000000 0.139052 0.019335 0.002689 0.000374 0.000052
nsteps = 1000
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 1.4099e-04 3.9191e-05 8.1702e-06 1.5140e-06 2.6302e-07
rkc4b integrator ncase = t 0.00 0.20
4
neqn = 41
u(num) 1.000000 0.139118
Copyright 2004 by Chapman & Hall/CRC
nsteps = 1000
u(exact) 1.000000 0.138911
diff 0.0000e+00 2.0675e-04
0.40 0.60 0.80 1.00
0.019292 0.002700 0.000376 0.000052
0.019296 0.002680 0.000372 0.000052
-4.3926e-06 1.9308e-05 3.3049e-06 -1.4026e-08
rkf45a integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
5
neqn = 41
u(num) 1.000000 0.139052 0.019335 0.002689 0.000374 0.000052
nsteps = 1000
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 1.4099e-04 3.9191e-05 8.1702e-06 1.5140e-06 2.6302e-07
rkf45b integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
6
neqn = 41
u(num) 1.000000 0.139052 0.019340 0.002694 0.000381 0.000048
nsteps = 1000
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 1.4091e-04 4.4135e-05 1.3738e-05 9.0848e-06 -3.6816e-06
Again, the errors are reduced below those for neqn = 21 and neqn = 31: neqn = 21 t 0.00 0.20 0.40 0.60
u(num) 1.000000 0.139478 0.019454 0.002713
u(exact) 1.000000 0.138911 0.019296 0.002680
diff 0.0000e+00 5.6734e-04 1.5794e-04 3.2977e-05
u(exact) 1.000000 0.138911
diff 0.0000e+00 2.5143e-04
neqn = 31 t 0.00 0.20
u(num) 1.000000 0.139163
Copyright 2004 by Chapman & Hall/CRC
0.40 0.60
0.019366 0.002695
0.019296 0.002680
6.9915e-05 1.4581e-05
u(num) 1.000000 0.139052 0.019336 0.002689
u(exact) 1.000000 0.138911 0.019296 0.002680
diff 0.0000e+00 1.4117e-04 3.9240e-05 8.1805e-06
neqn=41 t 0.00 0.20 0.40 0.60
The error analysis at t = 0.20 gives 5.6734e − 04 × [(21 − 1)/(41 − 1)]2 = 1.4184e − 04 1.4117e − 04 Again, this error analysis using O(x 2 ) is consistent with the dominant effect of the truncation errors in x (the error in t is relatively small). To reiterate the principal points of this analysis: • By increasing neqn (the number of grid points) and thereby decreasing the spatial discretization error in x, we would eventually reach a point where the truncation error in t might become comparable to the error in x (assuming we could keep the solution stable by increasing nsteps). In other words, the errors in the x and t integrations occur simultaneously. • To restate the preceding point, we were able to perform the error analysis in this case (using O(x 2 ) from Equation 4.9) only because the spatial error in x dominated the solution error. We in effect varied x in Equation 4.9 (by changing neqn) to estimate the spatial error while the error due to h in the t integration remained relatively small (or at least smaller than the x error in the x integration). • As the grid is refined in x (by increasing neqn), the resulting system of ODEs becomes larger and stiffer (the increased stiffness was apparent from the need to increase nsteps or reduce h). The separation in the ODE eigenvalues is proportional to 1/x 2 , and as x becomes smaller (with increasing neqn), the ODE eigenvalues become more widely separated (and so the ODE system becomes stiffer). • Thus, there is a twofold effect that increases the computational effort, increased number of ODEs and increase in stiffness of these ODEs. This increased computational effort to improve the accuracy in x is apparent in running the preceding program with increased neqn; i.e., it runs perceptibly slower. As a second interesting observation (in addition to instability), we observed that the lower-order algorithms (in euler2a and euler2b) became unstable while the higher-order algorithms (in rkc4a, rkc4b, rkf45a , and rkf45b) generally remained stable. This effect is due to the slightly larger stability domain for the
Copyright 2004 by Chapman & Hall/CRC
higher-order methods, e.g., |λh| < 2 for the Euler method and |λh| < 2.785 for the classical fourth-order RK method as determined from Equation 55c. As a third observation, the variable step integrators, euler2b, rkc4b, and rkf45b executed perceptibly faster than the fixed step integrators, euler2a , rkc4a , and rkf45a . This can be explained by the use of the smallest integration step, h = (t f − t0)/nsteps, throughout the solution by the fixed step integrators, while the variable step integrators could increase h above this value, and thus, by taking larger steps, the solutions could be computed faster. In other words, this observation clearly demonstrates the relative computational efficiency of the variable step integrators. To summarize the preceding observations, when numerically integrating PDEs, we must investigate and control the errors in time and space (e.g., t and x) to arrive at solutions with acceptable accuracy. This investigation usually takes the form of numerical experimentation, since in most cases, we will not have analytical solutions (like Equation 4.7) to give us the exact errors; i.e., we can only estimate the errors in time and space. More advanced integration algorithms in x and t could, at least in principle, adjust h and x automatically to achieve solutions of prescribed accuracy, but we should still keep in mind the requirement to control truncation errors in time and space, and to carefully examine and test the solutions. This completes the solution of Equations 4.1 to 4.4 with MATLAB. We now proceed to equivalent solutions in the five other languages.
4.2
Programming in C
Since main Program 2.2.1 is unchanged in the nonlinear PDE problem, it is not listed here. The header file, intpar, par, inital, derv, and fprint are listed below: /* Definition of functions intpar, par, inital, derv, fprint for the linear PDE */ /* Include headers */ #include <math.h> #include <stdio.h> /* Maximum number of ODES */ #define SIZE 500
Copyright 2004 by Chapman & Hall/CRC
/* Type variables as extern (global) */ extern int neqn, nout, nsteps; extern double t0, tf, abserr, relerr; extern double pi; extern double xl, xu, dx, dxs; extern FILE *fid; /* Integration parameters */ void intpar(); /* Problem parameters */ void par(); /* Initial condition */ void inital(double u0[]); /* Derivative vector */ void derv(double ut[], double t, double u[]); /* Output */ void fprint(int ncase, double t, double u[]);
#include "pdelin.h" #define SIZE 500 /* Type global variables */ int neqn, nout, nsteps; double t0, tf, abserr, relerr; double pi, xl, xu, dx, dxs; /* Define file ID */ FILE *fid;
void intpar() /* Function intpar sets the parameters to control the integration of the linear PDE system */ {
Copyright 2004 by Chapman & Hall/CRC
/* Number of ODEs */ neqn=21; /* Number of output points */ nout=6; /* Maximum number of steps in the interval t0 to tf */ nsteps=250; /* Initial, final values of the independent variable */ t0=0.0; tf=0.2; /* Error tolerances */ abserr=pow(10.0,-5); relerr=pow(10.0,-5); /* End of intpar */ }
void par() /* Function par sets the parameters for the linear PDE problem */ { pi=4.0*atan(1.0); xl=0.0; xu=1.0; dx=(xu-xl)/(neqn-1); dxs=dx*dx; }
void inital(double u0[]) /* Function inital sets the initial condition vector for the linear PDE problem */ { /* Type variables */ int i; double x; /* Problem parameters */ par();
Copyright 2004 by Chapman & Hall/CRC
/* Initial condition */ for(i=1;i<=neqn;i++) { x=xl+(float)(i-1)/(neqn-1)*(xu-xl); u0[i]=sin(pi*x); } /* End of inital */ }
void derv(double ut[], double t,
double u[])
/* Function derv computes the derivative vector of the linear PDE problem */ { /* Type variables */ int i; /* Problem parameters */ par(); /* BC at x = xl */ ut[1]=0.0; /* BC at x = xu */ ut[neqn]=0.0; /* Derivative vector */ for(i=2;i<=(neqn-1);i++) { /* Approximation to the PDE */ ut[i]=(u[i+1]-2.0*u[i]+u[i-1])/dxs; } /* End of derv */ }
void fprint(int ncase, double t, double u[]) /* Function fprint displays the numerical and exact solutions to the linear PDE problem */ { /* Type variables */ double x, ue; int im;
Copyright 2004 by Chapman & Hall/CRC
/* Print a heading for the solution at t = 0 */ if(t<=0.0) { /* Label for ODE integrator */ switch(ncase) { /*Fixed step modified Euler */ case 1: fprintf(fid,"\n\n euler2a integrator\n\n"); break; /* Variable step modified Euler */ case 2: fprintf(fid,"\n\n euler2b integrator\n\n"); break; /* Fixed step classical fourth order RK */ case 3: fprintf(fid,"\n\n rkc4a integrator\n\n"); break; /* Variable step classical fourth order RK */ case 4: fprintf(fid,"\n\n rkc4b integrator\n\n"); break; /* Fixed step RK Fehlberg 45 */ case 5: fprintf(fid,"\n\n rkf45a integrator\n\n"); break; /* Variable step RK Fehlberg 45 */ case 6: fprintf(fid,"\n\n rkf45b integrator\n\n"); break; }
/* Heading */ fprintf(fid," ncase = %1d neqn = %3d\n\n",ncase,neqn); fprintf(fid," t u(0.5) ue(0.5) diff\n");
/* End of t = 0 heading */ }
Copyright 2004 by Chapman & Hall/CRC
/* Numerical and analytical solution output */
/* Problem parameters */ par(); /* Midpoint value of x */ x=(xu-xl)/2.0; /* Analytical solution at midpoint */ ue=exp(-pi*pi*t)*sin(pi*x); /* Grid index of midpoint */ im=(neqn+1)/2; /* Display the numerical and exact solutions, and their difference */ fprintf(fid,"%5.2f %11.6f %11.6f %13.4e\n",t,u[im], ue,u[im]-ue); /* End of fprint */ }
Program 4.2.1 Header file, intpar, par, inital, derv, and fprint for the solution of Equations 4.1 to 4.4 The preceding intpar, par, inital, derv, and fprint closely parallel the analogous routines of Programs 4.1.1 to 4.1.4; therefore, no further discussion is given here, except to point out that a routine par was used to set the problem parameters in Program 4.2.1 while the parameters were set in each routine where they are used in Programs 4.1.2 to 4.1.4. The output from the routines of Program 4.2.1 is listed below: euler2a integrator ncase = 1 t 0.00 0.20 0.40 0.60 0.80 1.00
neqn =
u(0.5) 1.000000 0.139478 0.019454 0.002713 0.000378 0.000053
Copyright 2004 by Chapman & Hall/CRC
21 ue(0.5) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 5.6734e-04 1.5794e-04 3.2977e-05 6.1203e-06 1.0649e-06
euler2b integrator ncase = 2 t 0.00 0.20 0.40 0.60 0.80 1.00
neqn =
u(0.5) 1.000000 0.139530 0.019466 0.002713 0.000378 0.000047
21 ue(0.5) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 6.1894e-04 1.7004e-04 3.2297e-05 5.7139e-06 -4.3060e-06
rkc4a integrator ncase = 3 t 0.00 0.20 0.40 0.60 0.80 1.00
neqn =
u(0.5) 1.000000 0.139476 0.019453 0.002713 0.000378 0.000053
21 ue(0.5) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 5.6448e-04 1.5714e-04 3.2810e-05 6.0893e-06 1.0595e-06
rkc4b integrator ncase = 4 t 0.00 0.20 0.40 0.60 0.80 1.00
neqn =
u(0.5) 1.000000 0.139476 0.019454 0.002717 0.000380 0.000054
21 ue(0.5) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 5.6480e-04 1.5815e-04 3.6838e-05 7.9663e-06 2.2905e-06
rkf45a integrator ncase = 5 t 0.00 0.20
neqn =
u(0.5) 1.000000 0.139476
Copyright 2004 by Chapman & Hall/CRC
21 ue(0.5) 1.000000 0.138911
diff 0.0000e+00 5.6448e-04
0.40 0.60 0.80 1.00
0.019453 0.002713 0.000378 0.000053
0.019296 0.002680 0.000372 0.000052
1.5714e-04 3.2810e-05 6.0893e-06 1.0595e-06
rkf45b integrator ncase = 6 t 0.00 0.20 0.40 0.60 0.80 1.00
neqn =
u(0.5) 1.000000 0.139493 0.019449 0.002714 0.000379 0.000053
21 ue(0.5) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 5.8147e-04 1.5306e-04 3.3790e-05 6.1732e-06 1.0393e-06
This output is essentially the same as the MATLAB output in Section 4.1, i.e., the error tolerances in intpar are exceeded because of the spatial discretization errors. We now proceed to the C++ programming of the linear PDE.
4.3
Programming in C++
Again, since main Program 2.3.1 is unchanged, it is not listed here. The associated header files are listed below: /* Definition of functions intpar, inital, par, derv, fprint for the linear PDE system */
#include <stdio.h> #include <math.h> #include "MOL.h" /* Type variables as extern (global) */ extern FILE *fid;
Copyright 2004 by Chapman & Hall/CRC
class DEF:public MOL { public: /* Integration parameters */ void intpar(); /* Problem parameters */ void par(); /* Initial condition */ void inital(); /* Derivative vector */ void derv(double ut[], double t, double u[]); /* Output */ void fprint(int ncase, int neqn, double t, double u[]); };
/* Define the common (global) variables for the linear PDE integration */
/* Maximum (default) number of ODEs */ #define SIZE 500 class MOL { public: /* Variables for ODE integration */ int neqn, nout, nsteps; double t0, tf, abserr, relerr; double u[SIZE], u0[SIZE], e[SIZE];
Copyright 2004 by Chapman & Hall/CRC
/* Problem parameters */ double pi, xl, xu, dx, dxs; };
Program 4.3.1 Header files pdelin.h and MOL.h for the solution of Equations 4.1 to 4.4 intpar, par, inital, derv, and fprint are listed below: #include "DEF.h" void DEF::intpar() /* Function intpar sets the parameters to control the integration of the linear PDE system */ { /* Number of ODEs */ neqn=21; /* Number of output points */ nout=6; /* Maximum number of steps in the interval t0 to tf */ nsteps=250; /* Initial, final values of the independent variable */ t0=0.0; tf=0.2; /* Error tolerances */ abserr=pow(10.0,-5); relerr=pow(10.0,-5); /* End of intpar */ }
void DEF::par() /* Function par sets the parameters for the linear PDE problem */ { pi=4.0*atan(1.0); xl=0.0;
Copyright 2004 by Chapman & Hall/CRC
xu=1.0; dx=(xu-xl)/(neqn-1); dxs=dx*dx; }
void DEF::inital() /* Function inital sets the initial condition vector for the linear PDE problem */ { /* Type variables */ double x; int i; /* Initial condition */ for(i=1;i<=neqn;i++) { x=xl+(float)(i-1)/(neqn-1)*(xu-xl); u0[i]=sin(pi*x); } /* End of inital */ }
void DEF::derv(double ut[], double t, double u[]) /* Function derv computes the derivative vector of the linear PDE problem */ { /* Type variables */ int i; /* Integration parameters */ intpar(); /* Problem parameters */ par(); /* BC at x = xl */ ut[1]=0.0; /* BC at x = xu */ ut[neqn]=0.0;
Copyright 2004 by Chapman & Hall/CRC
/* Derivative vector */ for(i=2;i<=(neqn-1);i++) { /* Approximation to the PDE */ ut[i]=(u[i+1]-2.0*u[i]+u[i-1])/dxs; } /* End of derv */ }
void DEF::fprint(int ncase, int neqn, double t, double u[]) /* Function fprint displays the numerical and exact solutions to the linear PDE problem */ { /* Type variables */ double x, ue; int im; /* Problem parameters */ par(); /* Print a heading for the solution at t = 0 */ if(t<=0.0) { /* Label for ODE integrator */ switch(ncase) { /*Fixed step modified Euler */ case 1: fprintf(fid,"\n\n euler2a integrator\n\n"); break; /* Variable step modified Euler */ case 2: fprintf(fid,"\n\n euler2b integrator\n\n"); break; /* Fixed step classical fourth order RK */ case 3: fprintf(fid,"\n\n rkc4a integrator\n\n"); break;
Copyright 2004 by Chapman & Hall/CRC
/* Variable step classical fourth order RK */ case 4: fprintf(fid,"\n\n rkc4b integrator\n\n"); break; /* Fixed step RK Fehlberg 45 */ case 5: fprintf(fid,"\n\n rkf45a integrator\n\n"); break; /* Variable step RK Fehlberg 45 */ case 6: fprintf(fid,"\n\n rkf45b integrator\n\n"); break; } /* Heading */ fprintf(fid," ncase = %1d, neqn = %2d \n\n",ncase,n); fprintf(fid," t u(num) u(exact) diff\n"); /* End of t = 0 heading */ } /* Numerical and analytical solution output */ /* Midpoint value of x */ x=(xu-xl)/2.0; /* Analytical solution at midpoint */ ue=exp(-pi*pi*t)*sin(pi*x); /* Grid index of midpoint */ im=(neqn+1)/2; /* Display the numerical and exact solutions, and their difference */ fprintf(fid,"%5.2f %11.6f %11.6f %13.4e\n",t,u[im], ue,u[im]-ue); /* End of fprint */ }
Program 4.3.2 intpar, par, inital, derv, and fprint for the solution of Equations 4.1 to 4.4
Copyright 2004 by Chapman & Hall/CRC
The output from the routines of Programs 4.3.1 and 4.3.2 is listed below:
euler2a integrator ncase = 1, neqn = 21 t 0.00 0.20 0.40 0.60 0.80 1.00
u(num) 1.000000 0.139478 0.019454 0.002713 0.000378 0.000053
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 5.6734e-04 1.5794e-04 3.2977e-05 6.1203e-06 1.0649e-06
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 6.1894e-04 1.7004e-04 3.2297e-05 5.7139e-06 -4.3060e-06
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 5.6448e-04 1.5714e-04 3.2810e-05 6.0893e-06 1.0595e-06
euler2b integrator ncase = 2, neqn = 21 t 0.00 0.20 0.40 0.60 0.80 1.00
u(num) 1.000000 0.139530 0.019466 0.002713 0.000378 0.000047
rkc4a integrator ncase = 3, neqn = 21 t 0.00 0.20 0.40 0.60 0.80 1.00
u(num) 1.000000 0.139476 0.019453 0.002713 0.000378 0.000053
Copyright 2004 by Chapman & Hall/CRC
rkc4b integrator ncase = 4, neqn = 21 t 0.00 0.20 0.40 0.60 0.80 1.00
u(num) 1.000000 0.139476 0.019454 0.002717 0.000380 0.000054
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 5.6480e-04 1.5815e-04 3.6838e-05 7.9663e-06 2.2905e-06
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 5.6448e-04 1.5714e-04 3.2810e-05 6.0893e-06 1.0595e-06
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
diff 0.0000e+00 5.6472e-04 1.5721e-04 3.3024e-05 2.4645e-05 1.7941e-06
rkf45a integrator ncase = 5, neqn = 21 t 0.00 0.20 0.40 0.60 0.80 1.00
u(num) 1.000000 0.139476 0.019453 0.002713 0.000378 0.000053
rkf45b integrator ncase = 6, neqn = 21 t 0.00 0.20 0.40 0.60 0.80 1.00
u(num) 1.000000 0.139476 0.019454 0.002713 0.000397 0.000054
This output is equivalent to the output from the preceding MATLAB (Section 4.1) and C (Section 4.2); i.e., the error tolerances in intpar are exceeded because of the spatial discretization errors. We now proceed to the Fortran programming of the linear PDE.
Copyright 2004 by Chapman & Hall/CRC
4.4
Programming in Fortran
Again, since main Program 2.4.1 is unchanged, it is not listed here. intpar, par, inital, derv, and fprint are listed below: subroutine intpar(neqn,nout,nsteps,t0,tf,abserr,relerr) C C Subroutine intpar sets the parameters to control the C integration of the linear PDE problem C C Double precision coding is used implicit double precision(a-h,o-z) C C Number of ODEs neqn=21 C C Number of output points nout=6 C C Maximum number of steps in the interval t0 to tf nsteps=250 C C Initial, final values of the independent variable t0=0.0d0 tf=0.2d0 C C Error tolerances abserr=1.0d-05 relerr=1.0d-05 return C C End of intpar end
subroutine par(xl,xu,pi) C C Subroutine par sets the parameters for the linear C PDE problem C C Double precision coding is used implicit double precision(a-h,o-z) C C Problem parameters
Copyright 2004 by Chapman & Hall/CRC
xl=0.0d0 xu=1.0d0 pi=4.0d0*datan(1.0d0) return C C End of par end
subroutine inital(neqn,t,u0) C C Subroutine inital sets the initial condition vector C for the linear PDE problem C C Double precision coding is used implicit double precision(a-h,o-z) C C Size the arrays dimension u0(neqn) C C Problem parameters call par(xl,xu,pi) C C Initial condition do i=1,neqn x=xl+dfloat(i-1)/dfloat(neqn-1)*(xu-xl) u0(i)=dsin(pi*x) end do return C C End of inital end
subroutine derv(neqn,t,u,ut) C C Subroutine derv computes the derivative vector C of the linear PDE problem C C Double precision coding is used implicit double precision(a-h,l,o-z) C C Size the arrays dimension u(neqn), ut(neqn) C
Copyright 2004 by Chapman & Hall/CRC
C Problem parameters call par(xl,xu,pi) C C BC at x = 0 ut(1)=0.0d0 C C BC at x = 1 ut(neqn)=0.0d0 C C Interior points dx=(xu-xl)/dfloat(neqn-1) dxs=dx*dx do i=2,neqn-1 ut(i)=(u(i+1)-2.0d0*u(i)+u(i-1))/dxs end do return C C End of derv end
subroutine fprint(no,ncase,neqn,t,u) C C Subroutine fprint displays the numerical and C exact solutions to the linear PDE problem C C Double precision coding is used implicit double precision(a-h,l,o-z) C C Size the arrays dimension u(neqn) C C Problem parameters call par(xl,xu,pi) C C Print a heading for the solution at t = 0 if(t.le.0.0d0)then C C Label for ODE integrator C C Fixed step modfied Euler if(ncase.eq.1)then write(no,11) 11 format(//,' euler2a integrator') C
Copyright 2004 by Chapman & Hall/CRC
C
12 C C
13 C C
14 C C
15 C C
16
Variable step modified Euler else if(ncase.eq.2)then write(no,12) format(//,' euler2b integrator') Fixed step classical fourth order RK else if(ncase.eq.3)then write(no,13) format(//,' rkc4a integrator') Variable step classical fourth order RK else if(ncase.eq.4)then write(no,14) format(//,' rkc4b integrator') Fixed step RK Fehlberg 45 else if(ncase.eq.5)then write(no,15) format(//,' rkf45a integrator') Variable step RK Fehlberg 45 else if(ncase.eq.6)then write(no,16) format(//,' rkf45b integrator') end if
C C Heading write(no,4)ncase,neqn 4 format(/,' ncase = ',i1,' neqn = ',i2) write(no,2) 2 format(/,4x,'t',4x,'u1(num)',5x,'u1(ex)',8x,'diff1',/) C C End of t = 0 heading end if C C Numerical and analytical solution output C C Midpoint value of x x=(xu-xl)/2.0d0 C C Analytical solution at midpoint ue=dexp(-pi*pi*t)*dsin(pi*x) C C Grid index of midpoint im=dfloat((neqn+1)/2)
Copyright 2004 by Chapman & Hall/CRC
C C C
Display the numerical and exact solutions, and their difference write(no,3)t,u(im),ue,u(im)-ue format(f5.2,2f11.6,e13.4) return
3
C C End of fprint end
Program 4.4.1 intpar, par, inital, derv, and fprint for the solution of Equations 4.1 to 4.4 The output from the routines of Program 4.4.1 is listed below: euler2a integrator ncase = 1
neqn = 21
t
u1(num)
u1(ex)
diff1
0.00 0.20 0.40 0.60 0.80 1.00
1.000000 0.139478 0.019454 0.002713 0.000378 0.000053
1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
0.0000E+00 0.5673E-03 0.1579E-03 0.3298E-04 0.6120E-05 0.1065E-05
euler2b integrator ncase = 2
neqn = 21
t
u1(num)
u1(ex)
diff1
0.00 0.20 0.40 0.60 0.80 1.00
1.000000 0.139530 0.019464 0.002717 0.000379 0.000057
1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
0.0000E+00 0.6189E-03 0.1678E-03 0.3661E-04 0.6835E-05 0.5205E-05
Copyright 2004 by Chapman & Hall/CRC
rkc4a integrator ncase = 3
neqn = 21
t
u1(num)
u1(ex)
diff1
0.00 0.20 0.40 0.60 0.80 1.00
1.000000 0.139476 0.019453 0.002713 0.000378 0.000053
1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
0.0000E+00 0.5645E-03 0.1571E-03 0.3281E-04 0.6089E-05 0.1059E-05
rkc4b integrator ncase = 4
neqn = 21
t
u1(num)
u1(ex)
diff1
0.00 0.20 0.40 0.60 0.80 1.00
1.000000 0.139472 0.019453 0.002712 0.000378 0.000052
1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
0.0000E+00 0.5605E-03 0.1568E-03 0.3165E-04 0.5946E-05 -0.1941E-06
rkf45a integrator ncase = 5
neqn = 21
t
u1(num)
u1(ex)
diff1
0.00 0.20 0.40 0.60 0.80 1.00
1.000000 0.139476 0.019453 0.002713 0.000378 0.000053
1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
0.0000E+00 0.5645E-03 0.1571E-03 0.3281E-04 0.6089E-05 0.1059E-05
Copyright 2004 by Chapman & Hall/CRC
rkf45b integrator ncase = 6
neqn = 21
t
u1(num)
u1(ex)
diff1
0.00 0.20 0.40 0.60 0.80 1.00
1.000000 0.139476 0.019446 0.002715 0.000380 0.000053
1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
0.0000E+00 0.5644E-03 0.1498E-03 0.3457E-04 0.7272E-05 0.1161E-05
This output is equivalent to the output from the preceding MATLAB (Section 4.1), C (Section 4.2), and C++ (Section 4.3); i.e., the error tolerances in intpar are exceeded because of the spatial discretization errors. We now proceed to the Java programming of the linear PDE.
4.5
Programming in Java
Again, since main Program 2.5.1 is unchanged in the linear PDE problem, it is not listed here. An interface routine is listed below: /* Define the common (global) variables for the linear PDE integration */ package mol; public class MOL { /* Maximum (default) number of ODEs */ public int SIZE=500; /* Variables for ODE integration */ public int neqn, nout, nsteps; public double t0, tf, abserr, relerr; public double u[], u0[], e[];
Copyright 2004 by Chapman & Hall/CRC
/* Parameters for PDE */ public double pi, xl, xu, dx, dxs; public MOL() { } }
Program 4.5.1 Interface routine for the solution of Equations 4.1 to 4.4 intpar, par, inital, derv, and fprint are listed next: /* This file is a member of the package mol */ package mol; import import import import
mol.MOL; java.math.*; java.io.*; java.text.*;
public class DEF extends MOL implements pdelininterface { public DEF() { /* Integration parameters */ this.intpar(); /* Declare arrays */ u0=new double[SIZE]; u=new double[SIZE]; e=new double[SIZE]; /* Problem parameters */ this.par(); /* Initial condition vector */ this.inital(); } public void intpar() /* Function intpar sets the parameters to control the integration of the linear PDE system */
Copyright 2004 by Chapman & Hall/CRC
{ /* Number of ODEs */ neqn=21; /* Size of arrays in MOL library */ SIZE=neqn+1; /* Number of output points */ nout=6; /* Maximum number of steps in the interval t0 to tf */ nsteps=250; /* Initial, final values of the independent variable */ t0=0.0; tf=0.2; /* Error tolerances */ abserr=Math.pow(10.0,-5.0); relerr=Math.pow(10.0,-5.0); /* End of intpar */ }
public void par() /* Function par sets the parameters for the linear PDE problem */ { pi=4.0*Math.atan(1.0); xl=0.0; xu=1.0; dx=(xu-xl)/(neqn-1); dxs=dx*dx; /* End of par */ }
public void inital() /* Function inital sets the initial condition vector for the linear PDE problem */ {
Copyright 2004 by Chapman & Hall/CRC
/* Type variables */ double x; int i; /* Initial condition */ for(i=1;i<=neqn;i++) { x=xl+(double)(i-1)/(neqn-1)*(xu-xl); u0[i]=Math.sin(pi*x); } /* End of inital */ }
public void derv(double ut[], double t, double u[]) /* Function derv computes the derivative vector of the linear PDE problem */ { /* Type variables */ int i; /* Problem parameters */ par(); /* BC at x = xl */ ut[1]=0.0; /* BC at x = xu */ ut[neqn]=0.0; /* Derivative vector */ for(i=2;i<=(neqn-1);i++) { /* Approximation to the PDE */ ut[i]=(u[i+1]-2.0*u[i]+u[i-1])/dxs; } /* End of derv */ }
public void fprint(PrintWriter f, int ncase, int neqn, double t, double u[])
Copyright 2004 by Chapman & Hall/CRC
/* Function fprint displays the numerical and exact solutions to the linear PDE problem */ { /* Type variables */ double x, ue; int im; /* Print a heading for the solution at t = 0 */ if(t<=0.0) { /* Label for ODE integrator */ switch(ncase) { /*Fixed step modified Euler */ case 1: f.println("\n\n euler2a integrator\n"); break; /* Variable step modified Euler */ case 2: f.println("\n\n euler2b integrator\n"); break; /* Fixed step classical fourth order RK */ case 3: f.println("\n\n rkc4a integrator\n"); break; /* Variable step classical fourth order RK */ case 4: f.println("\n\n rkc4b integrator\n"); break; /* Fixed step RK Fehlberg 45 */ case 5: f.println("\n\n rkf45a integrator\n"); break; /* Variable step RK Fehlberg 45 */ case 6: f.println("\n\n rkf45b integrator\n"); break; }
Copyright 2004 by Chapman & Hall/CRC
/* Heading */ f.println(" ncase = "+ncase+" neqn = "+neqn+"\n"); f.println(" t u(num) u(exact) diff"); /* End of t = 0 heading */ } /* Numerical and analytical solution output */ /* Midpoint value of x */ x=(xu-xl)/2.0; /* Analytical solution at midpoint */ ue=Math.exp(-pi*pi*t)*Math.sin(pi*x); /* Grid index of midpoint */ im=(neqn+1)/2; /* Display format for floating numbers */ DecimalFormat df1=new DecimalFormat("0.00"); DecimalFormat df2=new DecimalFormat("0.0000000000"); /* Display the numerical and exact solutions, and their difference */ f.println(df1.format(t)+"\t"+df2.format(u[im]) +"\t"+df2.format(ue)+"\t"+df2.format(u[im]-ue)); /* End of fprint */ } /* End of DEF */ }
Program 4.5.2 intpar, par, inital, derv, and fprint for the solution of Equations 4.1 to 4.4 The output from the preceding routines is euler2a integrator ncase = 1 t 0.00 0.20
neqn = 21
u(num) 1.0000000000 0.1394784736
Copyright 2004 by Chapman & Hall/CRC
u(exact) 1.0000000000 0.1389111331
diff 0.0000000000 0.0005673405
0.40 0.60 0.80 1.00
0.0194542446 0.0027134483 0.0003784676 0.0000527881
0.0192963029 0.0026804713 0.0003723473 0.0000517232
0.0001579417 0.0000329770 0.0000061203 0.0000010649
u(exact) 1.0000000000 0.1389111331 0.0192963029 0.0026804713 0.0003723473 0.0000517232
diff 0.0000000000 0.0006189292 0.0001678024 0.0000366113 0.0000068354 0.0000052046
u(exact) 1.0000000000 0.1389111331 0.0192963029 0.0026804713 0.0003723473 0.0000517232
diff 0.0000000000 0.0005644806 0.0001571439 0.0000328101 0.0000060893 0.0000010595
u(exact) 1.0000000000 0.1389111331 0.0192963029 0.0026804713 0.0003723473 0.0000517232
diff 0.0000000000 0.0005604628 0.0001567688 0.0000316458 0.0000059458 -0.0000001941
euler2b integrator ncase = 2 t 0.00 0.20 0.40 0.60 0.80 1.00
neqn = 21
u(num) 1.0000000000 0.1395300624 0.0194641053 0.0027170826 0.0003791827 0.0000569278
rkc4a integrator ncase = 3 t 0.00 0.20 0.40 0.60 0.80 1.00
neqn = 21
u(num) 1.0000000000 0.1394756138 0.0194534468 0.0027132814 0.0003784366 0.0000527827
rkc4b integrator ncase = 4 t 0.00 0.20 0.40 0.60 0.80 1.00
neqn = 21
u(num) 1.0000000000 0.1394715959 0.0194530717 0.0027121171 0.0003782931 0.0000515291
Copyright 2004 by Chapman & Hall/CRC
rkf45a integrator ncase = 5 t 0.00 0.20 0.40 0.60 0.80 1.00
neqn = 21
u(num) 1.0000000000 0.1394756138 0.0194534468 0.0027132814 0.0003784366 0.0000527827
u(exact) 1.0000000000 0.1389111331 0.0192963029 0.0026804713 0.0003723473 0.0000517232
diff 0.0000000000 0.0005644806 0.0001571439 0.0000328101 0.0000060893 0.0000010595
u(exact) 1.0000000000 0.1389111331 0.0192963029 0.0026804713 0.0003723473 0.0000517232
diff 0.0000000000 0.0005644444 0.0001498268 0.0000345656 0.0000072719 0.0000011608
rkf45b integrator ncase = 6 t 0.00 0.20 0.40 0.60 0.80 1.00
neqn = 21
u(num) 1.0000000000 0.1394755775 0.0194461297 0.0027150369 0.0003796192 0.0000528840
This output is equivalent to the output from the preceding MATLAB (Section 4.1), C (Section 4.2), C++ (Section 4.3), and Fortran (Section 4.4); i.e., the error tolerances in intpar are exceeded because of the spatial discretization errors. We now proceed to the Maple programming of the linear PDE.
4.6
Programming in Maple
Since main Program 4.6.1 (and subordinate routines) accesses specific files by read statements, it is listed first: > restart: > read "c:\\odelib\\maple\\pdelin\\pdelin.txt"; > pdelin();
Program 4.6.1 Maple main program pdelin.mws for the numerical integration of Equations 4.1 to 4.4
Copyright 2004 by Chapman & Hall/CRC
pdelin:=proc() # # Main program pdelin computes the numerical # solution to the linear PDE system by one of # six integrators # # Type variables global neqn, nout, nsteps, t0, tf, abserr, relerr: local u0, u, tp, ncase, i, j: # # Step through six integrators for ncase from 1 to 6 do # # Integration parameters read "c:\\odelib\\maple\\pdelin\\intpar.txt": intpar(): # # Size arrays u0:=array(1..neqn): u:=array(1..neqn): # # Initial condition vector read "c:\\odelib\\maple\\pdelin\\inital.txt": inital(neqn,t0,u0): # # Output interval tp:=tf-t0: # # Compute solution at nout output points for j from 1 to nout do # # Print current solution read "c:\\odelib\\maple\\pdelin\\fprint.txt": fprint(ncase,neqn,t0,u0): # # Fixed step modified Euler integrator if (ncase = 1) then read "c:\\odelib\\maple\\pdelin\\euler2a.txt": euler2a(neqn,t0,tf,u0,nsteps,u): end if: # # Variable step modified Euler integrator if (ncase = 2) then read "c:\\odelib\\maple\\pdelin\\euler2b.txt": euler2b(neqn,t0,tf,u0,nsteps,abserr,relerr,u): end if:
Copyright 2004 by Chapman & Hall/CRC
# #
# #
# #
# #
# #
Fixed step classical fourth order RK integrator if (ncase = 3) then read "c:\\odelib\\maple\\pdelin\\rkc4a.txt": rkc4a(neqn,t0,tf,u0,nsteps,u): end if: Variable step classical fourth order RK integrator if (ncase = 4) then read "c:\\odelib\\maple\\pdelin\\rkc4b.txt": rkc4b(neqn,t0,tf,u0,nsteps,abserr,relerr,u): end if: Fixed step RK Fehlberg (RKF45) integrator if (ncase = 5) then read "c:\\odelib\\maple\\pdelin\\rkf45a.txt": rkf45a(neqn,t0,tf,u0,nsteps,u): end if: Variable step RK Fehlberg (RKF45) integrator if (ncase = 6) then read "c:\\odelib\\maple\\pdelin\\rkf45b.txt": rkf45b(neqn,t0,tf,u0,nsteps,abserr,relerr,u): end if: Advance solution t0:=tf: tf:=tf+tp: for i from 1 to neqn do u0[i]:=u[i]: end do:
# # Next output end do: # # Next integrator end do: # # End of pdelin end:
Program 4.6.2 Maple main program pdelin.txt for the numerical integration of Equations 4.1 to 4.4
Copyright 2004 by Chapman & Hall/CRC
Note the reference to specific files by read statements, e.g., # # Initial condition vector read "c:\\odelib\\maple\\pdelin\\inital.txt": inital(neqn,t0,u0):
intpar, inital, derv, and fprint are listed below: intpar:=proc() # # Function intpar sets the parameters to control the # integration of the linear PDE problem # # Type variables global neqn, nout, nsteps, t0, tf, abserr, relerr: # # Number of first order ODEs neqn:=21: # # Number of output points nout:=6: # # Maximum number of steps in the interval t0 to tf nsteps:=250: # # Initial, final values of independent variable t0:=0.0: tf:=0.2: # # Error tolerances abserr:=1.0e-05: relerr:=1.0e-05: # # End of intpar end:
inital:=proc(neqn,t,u0) # # Function inital sets the initial condition vector # of the linear PDE problem #
Copyright 2004 by Chapman & Hall/CRC
# Type variables local xl, xu, pi, x, i: # # Problem parameters xl:=0.0: xu:=1.0: # # Initial condition pi:=evalf(Pi): for i from 1 to neqn do x:=xl+(i-1)/(neqn-1)*(xu-xl): u0[i]:=sin(pi*x): end do: # # End of inital end:
derv:=proc(neqn,t,u,ut) # # Function derv computes the derivative vector # of the linear PDE problem # # Type variables local xl, xu, dx, dxs, i: # # Problem parameters xl:=0.0: xu:=1.0: # # BC at x = 0 ut[1]:=0.0: # # BC at x = 1 ut[neqn]:=0.0: # # Interior points dx:=(xu-xl)/(neqn-1): dxs:=dx*dx: for i from 2 to neqn-1 do ut[i]:=(u[i+1]-2.0*u[i]+u[i-1])/dxs: end do: # # End of derv end:
Copyright 2004 by Chapman & Hall/CRC
fprint:=proc(ncase,neqn,t,u) # # Function fprint displays the numerical and exact # solutions to the linear PDE problem # # Type variables local xl, xu, x, pi, ue, im: # # Declare global variables global nsteps: # # Problem parameters xl:=0.0: xu:=1.0: # # Print a heading for the solution at t = 0 if (t <= 0.0) then # # Label for ODE integrator # # Fixed step modified Euler if (ncase = 1) then printf(`\n\n euler2a integrator\n\n`); # # Variable step modified Euler elif (ncase = 2) then printf(`\n\n euler2b integrator\n\n`); # # Fixed step classical fourth order RK elif (ncase = 3) then printf(`\n\n rkc4a integrator\n\n`); # # Variable step classical fourth order RK elif (ncase = 4) then printf(`\n\n rkc4b integrator\n\n`); # # Fixed step RK Fehlberg 45 elif (ncase = 5) then printf(`\n\n rkf45a integrator\n\n`); # # Variable step RK Fehlberg 45 elif (ncase = 6) then printf(`\n\n rkf45b integrator\n\n`); end if: #
Copyright 2004 by Chapman & Hall/CRC
# Heading printf(` ncase = %2d neqn = %2d nsteps = %3d \n\n`, ncase,neqn,nsteps); printf(` t u(num) u(exact) diff\n`); # # End of t = 0 heading end if: # # Numerical and analytical solution output # # Midpoint value of x x:=(xu-xl)/2.0: # # Analytical solution at midpoint pi:=evalf(Pi): ue:=exp(-pi*pi*t)*sin(pi*x): # # Grid index of midpoint im:=round((neqn+1)/2): # # Display the numerical and exact solutions, # and their difference printf(`%5.2f %11.6f %11.6f %13.4e\n`,t,u[im], ue,u[im]-ue); # # End of fprint end:
Program 4.6.3 intpar, inital, derv, and fprint for the solution of Equations 4.1 to 4.4 Note the use of the Maple in-line value of π in inital, derv, and fprint, e.g., # # Initial condition pi:=evalf(Pi): for i from 1 to neqn do x:=xl+(i-1)/(neqn-1)*(xu-xl): u0[i]:=sin(pi*x): end do:
Pi is a symbolic value of π which is then converted to a numerical value, evalf(Pi), for use in subsequent calculations.
Copyright 2004 by Chapman & Hall/CRC
The output from the preceding routines is listed below:
euler2a integrator ncase = t 0.00 .20 .40 .60 .80 1.00
1
neqn = 21
u(num) 1.000000 .139478 .019454 .002713 .000378 .000053
nsteps = 250
u(exact) 1.000000 .138911 .019296 .002680 .000372 .000052
diff 0.0000e-01 5.6734e-04 1.5794e-04 3.2977e-05 6.1201e-06 1.0647e-06
euler2b integrator ncase = t 0.00 .20 .40 .60 .80 1.00
2
neqn = 21
u(num) 1.000000 .139530 .019462 .002712 .000378 .000049
nsteps = 250
u(exact) 1.000000 .138911 .019296 .002680 .000372 .000052
diff 0.0000e-01 6.1893e-04 1.6544e-04 3.1091e-05 5.6249e-06 -3.1368e-06
rkc4a integrator ncase = t 0.00 .20 .40 .60 .80 1.00
3
neqn = 21
u(num) 1.000000 .139476 .019453 .002713 .000378 .000053
Copyright 2004 by Chapman & Hall/CRC
nsteps = 250
u(exact) 1.000000 .138911 .019296 .002680 .000372 .000052
diff 0.0000e-01 5.6448e-04 1.5714e-04 3.2810e-05 6.0891e-06 1.0593e-06
rkc4b integrator ncase = t 0.00 .20 .40 .60 .80 1.00
4
neqn = 21
u(num) 1.000000 .139473 .019453 .002713 .000376 .000053
nsteps = 250
u(exact) 1.000000 .138911 .019296 .002680 .000372 .000052
diff 0.0000e-01 5.6189e-04 1.5690e-04 3.2058e-05 3.7570e-06 8.4148e-07
rkf45a integrator ncase = t 0.00 .20 .40 .60 .80 1.00
5
neqn = 21
u(num) 1.000000 .139476 .019453 .002713 .000378 .000053
nsteps = 250
u(exact) 1.000000 .138911 .019296 .002680 .000372 .000052
diff 0.0000e-01 5.6448e-04 1.5714e-04 3.2810e-05 6.0891e-06 1.0593e-06
rkf45b integrator ncase = t 0.00 .20 .40 .60 .80 1.00
6
neqn = 21
u(num) 1.000000 .139476 .019449 .002714 .000379 .000053
nsteps = 250
u(exact) 1.000000 .138911 .019296 .002680 .000372 .000052
diff 0.0000e-01 5.6448e-04 1.5311e-04 3.3779e-05 6.1720e-06 9.4329e-07
This output is equivalent to the output from the preceding MATLAB (Section 4.1), C (Section 4.2), C++ (Section 4.3), Fortran (Section 4.4), and Java
Copyright 2004 by Chapman & Hall/CRC
(Section 4.5), i.e., the error tolerances in intpar are exceeded because of the spatial discretization errors. This completes the discussion of the linear PDE problem programmed in the six languages. Basically, we have observed that PDEs can also be integrated using the methods for ODEs discussed in Chapters 2 and 3. The additional step is the algebraic approximation of the spatial (boundary value) derivatives in the PDEs, including the boundary conditions, so that the PDEs are replaced by an approximating system of initial values ODEs. This procedure is the essence of the method of lines. We now consider the final problem, a nonlinear PDE, in the remaining chapter. Again, as in the case of the preceding linear PDE, the techniques for ODEs in Chapters 2 and 3 can also be applied to the nonlinear PDE problem.
References 1. Vande Wouwer, A., Ph. Saucez, and W.E. Schiesser, Adaptive Method of Lines, CRC Press, Boca Raton, FL, 2001. 2. Schiesser, W.E., The Numerical Method of Lines Integration of Partial Differential Equations, Academic Press, San Diego, CA, 1991. 3. Schittkowski, K., Numerical Data Fitting in Dynamical Systems, Kluwer, Dordrecht, the Netherlands, 2002. 4. Hundsdorfer, W., and J.G. Verwer, Numerical Solution of Time-Dependent AdvectionDiffusion-Reaction Equations, Springer Series in Computational Mathematics, Vol. 33, Springer-Verlag, New York, 2003.
Copyright 2004 by Chapman & Hall/CRC
5 Solution of a Nonlinear PDE
The concluding PDE example is based on a physical system comprising a section of steel with a layer of external insulation, as depicted in Figure 5.1. The external surface of the insulation is heated by radiation from a flame, and the basic question to be answered then is how the temperature of the steel changes with time due to the radiative heating (the insulation is designed to prevent rapid heating of the steel). To analyze this system, we first realize that the temperature varies across the insulation as well as with time. Thus, we have a problem in time and space, as we did with the PDE of Chapter 4, Equation 4.1, and we therefore will again use a PDE to analyze the system of Figure 5.1. Since we can generally not model a physical system in complete detail, we will make some simplifying assumptions so that the analysis becomes tractable, yet we expect that the resulting mathematical model will still capture the essential features (the firstorder effects) of the problem. Here then is a list of assumptions: 1. The conduction of heat along the insulation is significant in only one direction, the smallest dimension (transverse to the cross section); thus the insulation “thickness” is assumed to be much smaller than its other two (longitudinal) dimensions. Again we use x and t to denote position (in the insulation) and time. 2. Consequently, the temperature variation is also only one dimensional (in the same direction as the heat conduction). 3. Because the steel has a much higher thermal conductivity than the insulation, the variation of temperature in the steel with position will be much smaller than the spatial temperature variation in the insulation. Thus, we will assume that the steel is at a single, uniform temperature (which can vary with time). 4. Since the steel temperature does not vary spatially, we have only one independent variable for the steel, time, and the steel is therefore modeled by an ODE in time.
Copyright 2004 by Chapman & Hall/CRC
Steel column Flame
(in cross section)
Insulation Flame
Flame
Flame FIGURE 5.1
Composite steel–insulation system.
Steel column Flame
(in cross section)
Insulation
Steel column
Insulation
T(x,∞) = Tf = 2000 K Insulation Flame
t
Flame Section for analysis T(x,0) = T0 = 25°C = 298 K x=0
t=0 x=L
Flame Line of symmetry FIGURE 5.2
Transient heating of composite steel–insulation system.
5. The physical properties of the insulation and steel are constant (do not vary with temperature). This may be a reasonable assumption, or may be sufficiently accurate if we use properties averaged over the temperatures that the insulation and steel experience. If this is not a reasonable assumption, the properties can be computed as a function of temperature and used for each x and t. The temperature profiles that we will be calculating are illustrated in Figure 5.2. Note that the system (insulation and steel) starts at a temperature of 25◦ C = 298 K. The left face of the insulation (z = 0) is then subjected to a flame at a temperature of T f = 2000 K. The temperatures in the insulation and the steel then begin to rise, and if the solution is computed to sufficiently large time t, the entire system would come to a uniform temperature of 2000 K. Some intermediate temperature profiles (for finite t) are illustrated
Copyright 2004 by Chapman & Hall/CRC
∆x Steel column Insulation T(x,∞) = Tf = 2000 K Heat fluxes t Isothermal lines T(x,0) = T0 = 25°C = 298 K x=0
t=0 x=L x
x−(1/2)∆x
x + (1/2)∆x
FIGURE 5.3
Transient heating of composite steel–insulation system. By energy conservation on a finite volume
Insulation
∆x
accumulation = input - output
dTi = A( qi −1 / 2 − qi +1 / 2 ) dt By a FD approximation to Fourier’s first law A ∆ xρ C p
Ti −1
Ti
Ti +1
qx −(1 / 2 ) ∆x
qx +(1 / 2 ) ∆x
= qi −1 / 2
= qi +1 / 2
qi +1 / 2 = − k
A ∆ x ρC p x=0
x=L
(i − 1 / 2) ∆x
i = 1,2,
( i + 1 / 2 ) ∆x
,N
(Ti +1 − Ti ) ∆x
qi −1 / 2 = − k
dTi T − Ti = A k i −1 dt ∆x
k ρC p dTi = dt
(Ti − Ti −1 ) ∆x
T − Ti +1 − k i ∆x
Ti −1 − Ti Ti − Ti +1 ∆x − ∆x ∆x
dTi T − 2Ti + Ti −1 = α i +1 ∆x 2 dt For ∆x → 0, Fourier’s second law: One IC, 2 BCs
∂T ∂ 2T =α 2 ∂t ∂x
FIGURE 5.4
Energy balance on the incremental volume of insulation.
in Figure 5.2, and in more detail in Figure 5.3. Note in Figure 5.3 the definition of a finite volume of the insulation of thickness z. The mathematical model for the insulation is based on an energy balance for this incremental volume. The derivation of the mathematical model is outlined in Figure 5.4.
Copyright 2004 by Chapman & Hall/CRC
The final result of the energy balance, in the limit z → 0, is again Fourier’s second law, which we now state in terms of u for temperature (rather than T): ∂u ∂ 2u =α 2 ∂t ∂x The initial and boundary conditions for Equation 5.1 taken as u(x, 0) = u0 −k
∂u(0, t) = σ (a ua4 − eu4 (0, t)) ∂x
L s ρs C ps
dus ∂u(L , t) = −k dt ∂x
(5.1)
(5.2) (5.3) (5.4)
where, in SI (MKS) units, u = insulation temperature, K t = time, s x = position in the insulation, m ua = ambient (flame) temperature, K α = insulation thermal diffusivity, m2 /s J·m k = insulation thermal conductivity, s · m2 · K σ = Stefan–Boltzmann constant J = 5.67x10−8 s · m 2 · K4 L = insulation half thickness, m a = absorptivity e = emissivity us = steel temperature, K L s = steel half thickness, m ρs = steel density, kg/m3 C ps = steel specific heat, J/kg · K Equations 5.3 and 5.4 are also energy balances, but applied at the boundaries of the insulation and steel. For Equation 5.3, we have the following: • −k∂u(0, t)/∂ x, the rate of heat conduction at the left surface of the insulation (at x = 0) according to Fourier’s first law. This term is actually a flux (a rate of heat conduction per unit area of insulation) with the units J/m2 · s. • σ (a ua4 − eu4 (0, t)), the rate of radiative heat transfer from the flame to the insulation surface according to the Stefan–Boltzmann law. Again, this is a flux with the units J/m2 · s. Note that the dependent variable (the insulation temperature) is raised to the fourth power, u4 (0, t). This is a nonlinear term in the model. Thus, although Equation 5.1 is linear, this nonlinear BC is the reason the model is considered nonlinear (and, also, the reason an analytical solution is precluded).
Copyright 2004 by Chapman & Hall/CRC
• Equation 5.3 is therefore an energy balance that equates the rate of radiative heat transfer to the rate of heat conduction at the surface of the insulation. For Equation 5.4, we have an energy balance on the steel: • L s ρs C ps (dus /dt), the rate of accumulation of energy within the steel (with thickness L s ). This term again has the units of J/m2 · s. • −k∂u(L , t)/∂ x, the rate of heat conduction from the right face of the insulation (at x = L) to the steel. Since this is a flux, it has the units of J/m2 · s. • Equation 5.4 is therefore an energy balance that equates the rate of heat transfer from the insulation to the rate of accumulation of energy within the steel. • Equation 5.4 is actually an ODE in time. Therefore, the model is a mixed ODE/PDE system. This feature does not present any particular difficulty in computing a numerical solution since the PDE, Equation 5.1, will be converted to a system of (n = 21) ODEs, as before in Chapter 4. Thus, we will merely add Equation 5.4 as another ODE for a system of 22 ODEs. • This last point illustrates an important advantage of the method of lines, i.e., it can easily accommodate mixed ODE/PDE systems. • Since Equation 5.4 is an ODE, it requires an initial condition, which we take as us (0) = u0 (5.5) To facilitate the subsequent computer analysis, dimensionless independent variables are defined as x = x/L , t = tα/L 2
(5.6)(5.7)
Substitution of Equations 5.6 and 5.7 in Equation 5.1 gives ∂ 2u ∂u = α ∂(L 2 t /α) ∂(L 2 x )2 or
∂ 2u ∂u = 2 (5.8) ∂t ∂x where the prime ( ) has been dropped and it is understood that we are working with the dimensionless independent variables of Equations 5.6 and 5.7. Equations 5.2 to 5.5 transform to (again, with the prime finally dropped). u(x, 0) = u0 k ∂u(0, t) = σ (a ua4 − eu4 (0, t)) L ∂x dus k ∂u(1, t) L s ρs C ps =− d(L 2 t /α) L ∂ x −
Copyright 2004 by Chapman & Hall/CRC
(5.9) (5.10)
or dus =− dt
kL α
1 L s ρs C ps
∂u(1, t) ∂x
us (0) = u0
(5.11) (5.12)
Equations 5.8 to 5.12 are the model equations programmed in the six languages as discussed below. The parameters are taken as: J·m s · m2 · K J 5.67x10−8 s · m 2 · K4 1.0x10−6 m2 /s 0.1 m 1.0 1.0 2000 K 25◦ C 7800.0 kg/m3 435.0 J/kg · K 0.025 m
k = 1.0 σ = α L a e ua uo ρs C ps Ls
= = = = = = = = =
These values are typical for an insulation and steel system. Their particular significance is discussed subsequently when the numerical solution to Equations 5.8 to 5.12 is discussed. The output is u(0, t), u(1/2, t), u(1, t) vs. t (in minutes; note t (minutes) = t (dimensionless) L 2 /α/60). In particular, we are interested in the steel temperature, us (t) as a function of t, which is also the temperature of the insulation at the right face, u(1, t). In constructing a program for Equations 5.8 to 5.12, we can make use of the methods of Chapter 4. In particular, we can approximate Equation 5.8 with the three point centered FD of Equation 4.9.
5.1
Programming in MATLAB
A main program for the solution of the nonlinear PDE problem, Equations 5.8 to 5.12, is the same as Program 3.1.1 and therefore is not listed here. intpar, par1, par2, inital, derv, and fprint are listed below followed by some discussion for each. function [neqn,nout,nsteps,t0,tf,abserr,relerr]=intpar % % Function intpar sets the parameters to control the
Copyright 2004 by Chapman & Hall/CRC
% integration of the nonlinear PDE problem % % Number of first order ODEs neqn=22; % % Number of output points nout=6; % % Maximum number of steps in the interval t0 to tf nsteps=1000; % % Initial, final values of independent variable t0=0.0; tf=0.2; % % Error tolerances abserr=1.0e-01; relerr=1.0e-05;
Program 5.1.1 intpar for the solution of Equations 5.8 to 5.12
We can note the following points about intpar: • The number of ODEs is 22 (21 for Equation 5.8 and 1 for Equation 5.11): % % Number of first order ODEs neqn=22;
• 1000 steps produce a stable solution, which is apparently also accurate since the low-order (euler2b) and high-order (rkc4b, rkf45b) variable step ODE algorithms give essentially the same numerical solution (recall from Chapter 1 that in addition to varying the step size, i.e., h refinement, the comparison of solutions from algorithms of different orders, i.e., p refinement, is a general way for assessing the accuracy of a numerical solution). % % Maximum number of steps in the interval t0 to tf nsteps=1000;
• The timescale for the problem is again 0 ≤ t ≤ 1 based on the dimensionless time of Equation 5.7:
Copyright 2004 by Chapman & Hall/CRC
% % Initial, final values of independent variable t0=0.0; tf=0.2;
• The error tolerances are set to abserr = 10−1 (0.1 K) and relerr = 10−5 (0.001%): % % Error tolerances abserr=1.0e-01; relerr=1.0e-05;
inital is listed below: function [u]=inital(neqn,t) % % Function inital sets the initial condition vector % for the nonlinear PDE % % Problem parameters [alpha,k,sigma,L,ui,ua]=par1; % % Inital condition for i=1:neqn u(i)=ui; end % % End of inital
Program 5.1.2 inital for the solution of Equations 5.8 to 5.12 inital is a straightforward implementation of Equations 5.9 and 5.12. Specifically, it calls function par1 to set the initial temperature, ui (= 25◦ C = 298 K), then uses this initial value for the neqn = 22 ODEs. par1 and a second routine to set parameters, par2, are listed below (MATLAB would not accept a function with 12 output parameters, so two routines, par1 and par2, are used, each with six parameters). function [alpha,k,sigma,L,ui,ua]=par1 % % Function par1 sets parameters for the nonlinear PDE alpha=1.0e-06; k=1.0;
Copyright 2004 by Chapman & Hall/CRC
sigma=5.67e-08; L=0.1; ui=298.0; ua=2000.0; % % End of par1
function [a,e,rhos,cps,ls,cs]=par2 % % Function par2 sets parameters for the nonlinear PDE a=1.0; e=1.0; rhos=7800.0; cps=435.0; ls=0.025; cs=rhos*cps*ls; % % End of par2
Program 5.1.3 par1 and par2 for the parameters of Equations 5.8 to 5.12
function [ut]=derv(neqn,t,u) % % Function derv computes the derivative vector % for the nonlinear PDE % % Problem parameters [alpha,k,sigma,L,ui,ua]=par1; [ a, e, rhos,cps,ls,cs]=par2; % % Spatial grid dx=1.0/(neqn-2); dxs=dx*dx; % % Insulation for i=1:neqn-2 if(i==1) u0=u(2)+2.0*dx*L*(sigma/k)*(a*ua^4-e*u(1)^4); ut(1)=(u(2)-2.0*u(1)+u0)/dxs; else ut(i)=(u(i+1)-2.0*u(i)+u(i-1))/dxs; end end
Copyright 2004 by Chapman & Hall/CRC
% % Steel ut(neqn)=(k*L/alpha)*(1.0/cs)*(u(neqn-2)-u(neqn-1))/dx; ut(neqn-1)=ut(neqn); % % End of derv
Program 5.1.4 derv for the solution of Equations 5.8 to 5.12 We can note the following points about derv: • The parameters in Equations 5.8 to 5.11 are first set by calls to par1 and par2: % % Problem parameters [alpha,k,sigma,L,ui,ua]=par1; [ a, e, rhos,cps,ls,cs]=par2;
• x (= d x) is then computed. Note that the length of the system (the thickness of the insulation) is unity in terms of the dimensionless length of Equation 5.6: % % Spatial grid dx=1.0/(neqn-2); dxs=dx*dx;
• The method of lines solution of the PDE, Equation 5.8, is then programmed as a system of 20 ODEs (the 21st ODE is programmed subsequently by equating its derivative to that of the steel) % % Insulation for i=1:neqn-2 if(i==1) u0=u(2)+2.0*dx*L*(sigma/k)*(a*ua^4-e*u(1)^4); ut(1)=(u(2)-2.0*u(1)+u0)/dxs; else ut(i)=(u(i+1)-2.0*u(i)+u(i-1))/dxs; end end
Copyright 2004 by Chapman & Hall/CRC
Note that for the first grid point (i = 1), BC Equation 5.10, is approximated with a FD for the spatial derivative: −
k u2 − u0 k ∂u(0, t) − = σ (a ua4 − eu4 (0, t)) = σ (a ua4 − eu41 ) L ∂x L 2x
where u0 is a fictitious value that is outside the grid (the lowest grid index is i = 1). We can solve for u0 u0 = u2 + 2(L/k)xσ (a ua4 − eu41 )
(5.13)
then use this value for u0 in the FD approximation of Equation 5.8: u2 − 2u1 + u0 du1 dt x 2
(5.14)
Equations 5.13 and 5.14 are programmed in the for loop for i = 1. • For i = 2 to i = 20, the FD approximation of Equation 4.9 can be used: ut(i)=(u(i+1)-2.0*u(i)+u(i-1))/dxs;
• For ODEs 21 and 22, the programming is % % Steel ut(neqn)=(k*L/alpha)*(1.0/cs)*(u(neqn-2) -u(neqn-1))/dx; ut(neqn-1)=ut(neqn);
The programming for ut(neqn) is Equation 5.11. Note that a FD approximation is used for the derivative ∂u(1, t)/∂ x, i.e., u21 − u20 ∂u(1, t) ∂x x The programming for ut(neq n − 1) merely sets u21 to u22 by equating their derivatives: dus ∂u(1, t) = ∂t dt or du22 du21 = dt dt In other words, this provides continuity (equality) of the temperature between the right face of the insulation (u(1, t)) and the left face of the steel (with the uniform steel temperature us (t)).
Copyright 2004 by Chapman & Hall/CRC
At the end of derv, all 22 ODEs of Equations 5.8 and 5.11 are programmed. These 22 derivatives (ut(i)) are then returned to one of the ODE integrators, euler2a to rkf45b, to take the next step along the solution. In this way, the complete solution to Equations 5.8 to 5.12 is computed through t and x; that is, we have u(x, t) and us (t) in numerical form. This numerical solution is displayed by fprint:
function [out]=fprint(ncase,neqn,t,u) % % Function fprint displays the numerical solution to the % nonlinear PDE % % Return current value of independent variable % (MATLAB requires at least one return argument) out=t; % % Problem parameters [alpha,k,sigma,L,ui,ua]=par1; [ a, e, rhos,cps,ls,cs]=par2; % % Print a heading for the solution at t = 0 if(t<=0.0) % % Label for ODE integrator % % Fixed step modified Euler if(ncase==1) fprintf('\n\n euler2a integrator\n'); % % Variable step modified Euler elseif(ncase==2) fprintf('\n\n euler2b integrator\n'); % % Fixed step classical fourth order RK elseif(ncase==3) fprintf('\n\n rkc4a integrator\n'); % % Variable step classical fourth order RK elseif(ncase==4) fprintf('\n\n rkc4b integrator\n'); % % Fixed step RK Fehlberg 45 elseif(ncase==5) fprintf('\n\n rkf45a integrator\n');
Copyright 2004 by Chapman & Hall/CRC
% %
Variable step RK Fehlberg 45 elseif(ncase==6) fprintf('\n\n rkf45b integrator\n'); end
% % Heading fprintf('\n ncase = %2d neqn = %2d\n',ncase,neqn); fprintf('\n t u(1) u(im) u(neqn)\n'); % % End of t = 0 heading end % % Grid index of midpoint im=round(neqn)/2.0; % % Display the numerical solution tmin=t*L^2/alpha/60.0; fprintf('%7.1f%10.2f%10.2f%10.2f\n',tmin,u(1),u(im), u(neqn)); % % End of fprint
Program 5.1.5 fprint for the solution of Equations 5.8 to 5.12
fprint has the same function as the previous fprint routines, i.e., • Labeling of the ODE integrator. • Display of the numerical solution. This completes the programming of Equations 5.8 to 5.12. The output is as follows: euler2a integrator ncase = t 0.0 33.3 66.7 100.0 133.3 166.7
1
neqn = 22
u(1) 298.00 1987.96 1991.05 1992.55 1993.72 1994.70
Copyright 2004 by Chapman & Hall/CRC
u(im) 298.00 1002.15 1218.09 1345.04 1447.20 1533.03
u(neqn) 298.00 394.17 622.84 834.65 1015.31 1168.10
euler2b integrator ncase = t 0.0 33.3 66.7 100.0 133.3 166.7
2
neqn = 22
u(1) 298.00 1987.93 1991.00 1992.54 1993.68 1994.65
u(im) 298.00 1002.11 1217.92 1344.68 1446.70 1532.29
u(neqn) 298.00 394.14 622.58 834.02 1014.43 1166.78
rkc4a integrator ncase = t 0.0 33.3 66.7 100.0 133.3 166.7
3
neqn = 22
u(1) 298.00 1987.96 1991.05 1992.55 1993.72 1994.70
u(im) 298.00 1002.16 1218.10 1345.05 1447.20 1533.03
u(neqn) 298.00 394.17 622.85 834.66 1015.32 1168.10
rkc4b integrator ncase = t 0.0 33.3 66.7 100.0 133.3 166.7
4
neqn = 22
u(1) 298.00 1986.00 1991.04 1992.35 1993.56 1994.69
u(im) 298.00 1002.16 1217.94 1344.91 1446.90 1532.53
u(neqn) 298.00 394.17 622.61 834.42 1014.78 1167.22
rkf45a integrator ncase = t 0.0 33.3 66.7 100.0
5
neqn = 22
u(1) 298.00 1987.96 1991.05 1992.55
Copyright 2004 by Chapman & Hall/CRC
u(im) 298.00 1002.16 1218.10 1345.05
u(neqn) 298.00 394.17 622.85 834.66
133.3 166.7
1993.72 1994.70
1447.20 1533.03
1015.32 1168.10
rkf45b integrator ncase = t 0.0 33.3 66.7 100.0 133.3 166.7
6
neqn = 22
u(1) 298.00 1988.02 1991.28 1992.55 1993.94 1994.68
u(im) 298.00 1002.16 1218.10 1345.05 1447.02 1532.63
u(neqn) 298.00 394.17 622.85 834.66 1014.99 1167.40
We can note the following points about this output: • The insulation starts at a uniform temperature of 298 K. The steel also starts at this temperature since, at the insulation–steel interface, u(1, t) = us (t) (from the coding in derv). • As expected, the left face of the insulation (at x = 0) responds rapidly to the flame and is above 1990 K for most of the transient. • The insulation at the right face (x = L), which is also the steel temperature, responds more slowly due to the low thermal conductivity of the insulation. This delay in the rise of the steel temperature is critical in the case of structural steel used in buildings that may be subject to fire. This point is discussed further at the end of this chapter. This completes the solution of Equations 5.8 to 5.12 with MATLAB. We now proceed to equivalent solutions in the five other languages.
5.2
Programming in C
Since main Program 2.2.1 is unchanged in the nonlinear PDE problem, it is not listed here. The header file, intpar, par, inital, derv, and fprint are listed below /* Definition of functions intpar, par, inital, derv, fprint for the nonlinear PDE */
Copyright 2004 by Chapman & Hall/CRC
/* Include headers */ #include <math.h> #include <stdio.h> /* Maximum number of ODES */ #define SIZE 500 /* Type variables as extern (global) */ extern int neqn, nout, nsteps; extern double t0, tf, abserr, relerr; extern double alpha, k, sigma, ui, ua, ar, rhos, cps, ls, xl, xu, dx, extern FILE *fid;
L, er, cs, dxs;
/* Integration parameters */ void intpar(); /* Problem parameters */ void par(); /* Initial condition */ void inital(double u0[]); /* Derivative vector */ void derv(double ut[], double t, double u[]); /* Output */ void fprint(int ncase, double t, double u[]);
#include "pdenon.h" #define SIZE 500 /* Type global variables */ int neqn, nout, nsteps; double t0, tf, abserr, relerr; double alpha, ui,
Copyright 2004 by Chapman & Hall/CRC
k, ua,
sigma, ar,
L, er,
rhos, xl,
cps, xu,
ls, dx,
cs, dxs;
/* Define file ID */ FILE *fid; void intpar() /* Function intpar sets the parameters to control the integration of the nonlinear PDE */ { /* Number of ODEs */ neqn=22; /* Number of output points */ nout=6; /* Maximum number of steps in the interval t0 to tf */ nsteps=1000; /* Initial, final values of the independent variable */ t0=0.0; tf=0.2; /* Error tolerances */ abserr=pow(10.0,-1); relerr=pow(10.0,-5); /* End of intpar */ }
void par()
/* Function par sets the parameters for the nonlinear PDE */ { alpha=1.0e-06; ui=298.0; rhos=7800.0; xl=0.0;
k=1.0; ua=2000.0; cps=435.0; xu=1.0;
}
void inital(double u0[])
Copyright 2004 by Chapman & Hall/CRC
sigma=5.67e-08; ar=1.0; ls=0.025; dx=(xu-xl)/(neqn-2);
L=0.1; er=1.0; cs=rhos*cps*ls; dxs=dx*dx;
/* Function inital sets the initial condition vector for the nonlinear PDE */ { /* Type variables */ int i; /* Problem parameters */ par(); /* Initial condition */ for(i=1;i<=neqn;i++) { u0[i]=ui; } /* End of inital */ }
void derv(double ut[], double t,
double u[])
/* Function derv computes the derivative vector of the nonlinear PDE */ { /* Type variables */ double u0; int i; /* Problem parameters */ par(); /* Insulation*/ for(i=1;i<=(neqn-2);i++) { if(i==1) { u0=u[2]+2.0*dx*L*(sigma/k)*(ar*pow(ua,4) -er*pow(u[1],4)); ut[1]=(u[2]-2.0*u[1]+u0)/dxs; } if(i>1) { ut[i]=(u[i+1]-2.0*u[i]+u[i-1])/dxs; } }
Copyright 2004 by Chapman & Hall/CRC
/* Steel */ ut[neqn]=(k*L/alpha)*(1.0/cs)*(u[neqn-2]-u[neqn-1])/dx; ut[neqn-1]=ut[neqn]; /* End of derv */ }
void fprint(int ncase, double t, double u[]) /* Function fprint displays the numerical solution to the nonlinear PDE */ { /* Type variables */ int im; /* Problem parameters */ par(); /* Print a heading for the solution at t = 0 */ if(t<=0.0) { /* Label for ODE integrator */ switch(ncase) { /*Fixed step modified Euler */ case 1: fprintf(fid,"\n\n euler2a integrator\n\n"); break; /* Variable step modified Euler */ case 2: fprintf(fid,"\n\n euler2b integrator\n\n"); break; /* Fixed step classical fourth order RK */ case 3: fprintf(fid,"\n\n rkc4a integrator\n\n"); break; /* Variable step classical fourth order RK */ case 4: fprintf(fid,"\n\n rkc4b integrator\n\n"); break;
Copyright 2004 by Chapman & Hall/CRC
/* Fixed step RK Fehlberg 45 */ case 5: fprintf(fid,"\n\n rkf45a integrator\n\n"); break; /* Variable step RK Fehlberg 45 */ case 6: fprintf(fid,"\n\n rkf45b integrator\n\n"); break; } /* Heading */ fprintf(fid," ncase = %1d neqn = %2d \n\n",ncase, neqn);} fprintf(fid," t u(1) u(im) u(neqn)\n"); /* End of t = 0 heading */ } /* Numerical and analytical solution output */ /* Grid index of midpoint */ im=(neqn+1)/2; /* Display the numerical solution */ fprintf(fid,"%5.0f %11.1f %11.1f %11.1f\n", t*pow(L,2)/alpha/60.0,u[1],u[im],u[neqn]); /* End of fprint */ }
Program 5.2.1 Header file, intpar, par , inital, derv and fprint for the solution of Equations 5.8 to 5.12 These routines closely parallel the MATLAB routines in Section 5.1 and therefore will not be discussed here. The output from the preceding Program 5.2.1 is listed below: euler2a integrator ncase = 1 t 0 33
neqn = 22 u(1) 298.0 1988.0
Copyright 2004 by Chapman & Hall/CRC
u(im) 298.0 1002.1
u(neqn) 298.0 394.2
67 100 133 167
1991.0 1992.6 1993.7 1994.7
1218.1 1345.0 1447.2 1533.0
622.8 834.7 1015.3 1168.1
u(im) 298.0 1002.1 1217.9 1344.7 1446.7 1532.3
u(neqn) 298.0 394.1 622.6 834.0 1014.4 1166.8
u(im) 298.0 1002.2 1218.1 1345.0 1447.2 1533.0
u(neqn) 298.0 394.2 622.8 834.7 1015.3 1168.1
u(im) 298.0 1002.2 1217.9 1344.9 1446.9 1532.5
u(neqn) 298.0 394.2 622.6 834.4 1014.8 1167.2
euler2b integrator ncase = 2 t 0 33 67 100 133 167
neqn = 22 u(1) 298.0 1987.9 1991.0 1992.5 1993.7 1994.7
rkc4a integrator ncase = 3 t 0 33 67 100 133 167
neqn = 22 u(1) 298.0 1988.0 1991.0 1992.6 1993.7 1994.7
rkc4b integrator ncase = 4 t 0 33 67 100 133 167
neqn = 22 u(1) 298.0 1986.0 1991.0 1992.3 1993.6 1994.7
Copyright 2004 by Chapman & Hall/CRC
rkf45a integrator ncase = 5 t 0 33 67 100 133 167
neqn = 22 u(1) 298.0 1988.0 1991.0 1992.6 1993.7 1994.7
u(im) 298.0 1002.2 1218.1 1345.0 1447.2 1533.0
u(neqn) 298.0 394.2 622.8 834.7 1015.3 1168.1
u(im) 298.0 1002.2 1218.1 1345.0 1447.0 1532.6
u(neqn) 298.0 394.2 622.8 834.7 1015.0 1167.4
rkf45b integrator ncase = 6 t 0 33 67 100 133 167
neqn = 22 u(1) 298.0 1988.0 1991.3 1992.5 1993.9 1994.7
This output is essentially the same as the MATLAB output in Section 5.1. We now proceed to the C++ programming of the nonlinear PDE.
5.3
Programming in C++
Again, since main Program 2.3.1 is unchanged, it is not listed here. The associated header files are listed below: /* Problem parameters */ void par(); /* Initial condition */ void inital(); /* Derivative vector */ void derv(double ut[], double t, double u[]); /* Output */ void fprint(int ncase, int neqn, double t, double u[]);
Copyright 2004 by Chapman & Hall/CRC
}; #define SIZE 500 class MOL { public: /* Variables for ODE integration */ int neqn, nout, nsteps; double t0, tf, abserr, relerr; double u[SIZE], u0[SIZE], e[SIZE]; /* Problem parameters */ double
alpha, rhos,
k, cps,
sigma, ls,
L, cs,
ui, xl,
ua, xu,
ar, dx,
er, dxs;
};
Program 5.3.1 Header files pdenon.h and MOL.h for the solution of Equations 5.8 to 5.12 intpar, par, inital, derv, and fprint are listed below: #include "DEF.h" void DEF::intpar() /* Function intpar sets the parameters to control the integration of the nonlinear PDE */ { /* Number of ODEs */ neqn=22; /* Number of output points */ nout=6; /* Maximum number of steps in the interval t0 to tf */ nsteps=1000; /* Initial, final values of the independent variable */ t0=0.0; tf=0.2;
Copyright 2004 by Chapman & Hall/CRC
/* Error tolerances */ abserr=pow(10.0,-1); relerr=pow(10.0,-5); /* End of intpar */ }
void DEF::par()
/* Function par sets the parameters for the nonlinear PDE */ { alpha=1.0e-06; ui=298.0; rhos=7800.0; xl=0.0;
k=1.0; ua=2000.0; cps=435.0; xu=1.0;
sigma=5.67e-08; ar=1.0; ls=0.025; dx=(xu-xl)/(neqn-2);
L=0.1; er=1.0; cs=rhos*cps*ls; dxs=dx*dx;
/* End of par */ }
void DEF::inital() /* Function inital sets the initial condition vector for the nonlinear PDE */ { /* Type variables */ int i; /* Initial condition */ for(i=1;i<=neqn;i++) { u0[i]=ui; } /* End of inital */ }
void DEF::derv(double ut[], double t, double u[]) /* Function derv computes the derivative vector of the nonlinear PDE problem */ { /* Type variables */ double u0; int i;
Copyright 2004 by Chapman & Hall/CRC
/* Integration parameters */ intpar(); /* Problem parameters */ par(); /* Insulation */ for(i=1;i<=(neqn-2);i++) { if(i==1) { u0=u[2]+2.0*dx*L*(sigma/k)*(ar*pow(ua,4) -er*pow(u[1],4)); ut[1]=(u[2]-2.0*u[1]+u0)/dxs; } if(i>1) { ut[i]=(u[i+1]-2.0*u[i]+u[i-1])/dxs; } } /* Steel */ ut[neqn]=(k*L/alpha)*(1.0/cs)*(u[neqn-2]-u[neqn-1])/dx; ut[neqn-1]=ut[neqn]; /* End of derv */ }
void DEF::fprint(int ncase, int neqn, double t, double u[]) /* Function fprint displays the numerical solution to the nonlinear PDE */ { /* Type variables */ int im; /* Problem parameters */ par(); /* Print a heading for the solution at t = 0 */ if(t<=0.0) { /* Label for ODE integrator */ switch(ncase)
Copyright 2004 by Chapman & Hall/CRC
{ /*Fixed step modified Euler */ case 1: fprintf(fid,"\n\n euler2a integrator\n"); break; /* Variable step modified Euler */ case 2: fprintf(fid,"\n\n euler2b integrator\n"); break; /* Fixed step classical fourth order RK */ case 3: fprintf(fid,"\n\n rkc4a integrator\n"); break; /* Variable step classical fourth order RK */ case 4: fprintf(fid,"\n\n rkc4b integrator\n"); break; /* Fixed step RK Fehlberg 45 */ case 5: fprintf(fid,"\n\n rkf45a integrator\n"); break; /* Variable step RK Fehlberg 45 */ case 6: fprintf(fid,"\n\n rkf45b integrator\n"); break; } /* Heading */ fprint(fid,"\n ncase = %1d, neqn = %2d\n\n",ncase,neqn); fprintf(fid," t u(1) u(im) u(neqn)\n");
/* End of t = 0 heading */ } /* Grid index of midpoint */ im=(neqn+1)/2; /* Display the numerical solution */
Copyright 2004 by Chapman & Hall/CRC
fprintf(fid,"%5.0f %11.1f %11.1f %11.1f\n", t*pow(L,2)/alpha/60.0,u[1],u[im],u[neqn]); /* End of fprint */ }
Program 5.3.2 intpar, par, inital, derv, and fprint for the solution of Equations 5.8 to 5.12 The output from the routines of Programs 5.3.1 and 5.3.2 is listed below: euler2a integrator ncase = 1, t 0 33 67 100 133 167
neqn = 22 u(1) 298.0 1988.0 1991.0 1992.6 1993.7 1994.7
u(im) 298.0 1002.1 1218.1 1345.0 1447.2 1533.0
u(neqn) 298.0 394.2 622.8 834.7 1015.3 1168.1
u(im) 298.0 1002.1 1217.9 1344.7 1446.7 1532.3
u(neqn) 298.0 394.1 622.6 834.0 1014.4 1166.8
u(im) 298.0 1002.2 1218.1 1345.0
u(neqn) 298.0 394.2 622.8 834.7
euler2b integrator ncase = 2, t 0 33 67 100 133 167
neqn = 22 u(1) 298.0 1987.9 1991.0 1992.5 1993.7 1994.7
rkc4a integrator ncase = 3, t 0 33 67 100
neqn = 22 u(1) 298.0 1988.0 1991.0 1992.6
Copyright 2004 by Chapman & Hall/CRC
133 167
1993.7 1994.7
1447.2 1533.0
1015.3 1168.1
u(im) 298.0 1002.2 1217.9 1344.9 1446.9 1532.5
u(neqn) 298.0 394.2 622.6 834.4 1014.8 1167.2
u(im) 298.0 1002.2 1218.1 1345.0 1447.2 1533.0
u(neqn) 298.0 394.2 622.8 834.7 1015.3 1168.1
u(im) 298.0 1002.2 1218.1 1345.0 1446.9 1532.7
u(neqn) 298.0 394.2 622.8 834.6 1014.8 1167.5
rkc4b integrator ncase = 4, t 0 33 67 100 133 167
neqn = 22 u(1) 298.0 1986.0 1991.0 1992.3 1993.6 1994.7
rkf45a integrator ncase = 5, t 0 33 67 100 133 167
neqn = 22 u(1) 298.0 1988.0 1991.0 1992.6 1993.7 1994.7
rkf45b integrator ncase = 6, t 0 33 67 100 133 167
neqn = 22 u(1) 298.0 1988.0 1990.0 1989.5 1993.7 1994.6
This output is equivalent to the output from the preceding MATLAB (Section 5.1) and C (Section 5.2). We now proceed to the Fortran programming of the nonlinear PDE.
Copyright 2004 by Chapman & Hall/CRC
5.4
Programming in Fortran
Again, since main Program 2.4.1 is unchanged, it is not listed here. intpar, par, inital, derv, and fprint are listed below:
subroutine intpar(neqn,nout,nsteps,t0,tf,abserr,relerr) C C Subroutine intpar sets the parameters to control the C integration of the nonlinear PDE problem C C Double precision coding is used implicit double precision(a-h,o-z) C C Number of ODEs neqn=22 C C Number of output points nout=6 C C Maximum number of steps in the interval t0 to tf nsteps=1000 C C Initial, final values of the independent variable t0=0.0d0 tf=0.2d0 C C Error tolerances abserr=1.0d-01 relerr=1.0d-05 return C C End of intpar end
subroutine par C C Double precision coding is used implicit double precision(a-h,o-z) C C Global variables common/p/ alpha, xk, sigma, + a, e, rhos,
Copyright 2004 by Chapman & Hall/CRC
xl, cps,
ui, xls,
ua, cs
C C Subroutine par sets the parameters for the nonlinear PDE alpha=1.0d-06 xk=1.0d0 sigma=5.67d-08 xl=0.1d0 ui=298.0d0 ua=2000.0d0 a=1.0d0 e=1.0d0 rhos=7800.0d0 cps=435.0d0 xls=0.025d0 cs=rhos*cps*xls return C C End of par end subroutine inital(neqn,t,u0) C C Subroutine inital sets the initial condition vector C for the nonlinear PDE problem C C Double precision coding is used implicit double precision(a-h,o-z) C C Global variables common/p/ alpha, xk, sigma, xl, ui, + a, e, rhos, cps, xls, C C Size the arrays dimension u0(neqn) C C Problem parameters call par C C Initial condition do i=1,neqn u0(i)=ui end do return C C End of inital end
Copyright 2004 by Chapman & Hall/CRC
ua, cs
subroutine derv(neqn,t,u,ut) C C Subroutine derv computes the derivative vector C of the nonlinear PDE problem C C Double precision coding is used implicit double precision(a-h,o-z) C C Global variables common/p/ alpha, xk, sigma, xl, ui, ua, + a, e, rhos, cps, xls, cs C C Size the arrays dimension u(neqn), ut(neqn) C C Spatial grid dx=1.0d0/dfloat(neqn-2) dxs=dx*dx C C Insulation do i=1,neqn-2 if(i.eq.1)then u0=u(2)+2.0d0*dx*xl*(sigma/xk)*(a*ua**4-e*u(1)**4) ut(i)=(u(2)-2.0d0*u(1)+u0)/dxs else ut(i)=(u(i+1)-2.0d0*u(i)+u(i-1))/dxs end if end do C C Steel ut(neqn)=(xk*xl/alpha)*(1.0d0/cs)*(u(neqn-2) -u(neqn-1))/dx ut(neqn-1)=ut(neqn) C C End of derv return end subroutine fprint(no,ncase,neqn,t,u) C C Subroutine fprint displays the numerical solution to the C nonlinear PDE problem C C Double precision coding is used implicit double precision(a-h,o-z)
Copyright 2004 by Chapman & Hall/CRC
C C Global variables common/p/ alpha, xk, sigma, xl, ui, + a, e, rhos, cps, xls, C C Size the arrays dimension u(neqn) C C Print a heading for the solution at t = 0 if(t.le.0.0d0)then C C Label for ODE integrator C C Fixed step modfied Euler if(ncase.eq.1)then write(no,11) 11 format(//,' euler2a integrator') C C Variable step modified Euler else if(ncase.eq.2)then write(no,12) 12 format(//,' euler2b integrator') C C Fixed step classical fourth order RK else if(ncase.eq.3)then write(no,13) 13 format(//,' rkc4a integrator') C C Variable step classical fourth order RK else if(ncase.eq.4)then write(no,14) 14 format(//,' rkc4b integrator') C C Fixed step RK Fehlberg 45 else if(ncase.eq.5)then write(no,15) 15 format(//,' rkf45a integrator') C C Variable step RK Fehlberg 45 else if(ncase.eq.6)then write(no,16) 16 format(//,' rkf45b integrator') end if C C Heading
Copyright 2004 by Chapman & Hall/CRC
ua, cs
2
write(no,2)ncase,neqn format(/,' ncase = ',i2,' write(no,3) format(' t u(1)
neqn = ',i2,/)
3 u(im) u(neqn)') C C End of t = 0 heading end if C C Numerical and analytical solution output C C Grid index of midpoint im=neqn/2 C C Display the numerical solution tmin=t*xl**2/alpha/60.0d0 write(no,4) tmin, u(1), u(im), u(neqn) 4 format(f6.1,3f10.2) return C C End of fprint end
Program 5.4.1 intpar, par, inital, derv, and fprint for the solution of Equations 5.8 to 5.12 The output from the routines of Program 5.4.1 is listed below: euler2a integrator ncase = t 0.0 33.3 66.7 100.0 133.3 166.7
1
neqn = 22
u(1) 298.00 1987.96 1991.05 1992.55 1993.72 1994.70
u(im) 298.00 1002.15 1218.09 1345.04 1447.20 1533.03
u(neqn) 298.00 394.17 622.84 834.65 1015.31 1168.10
euler2b integrator ncase = t 0.0
2
neqn = 22 u(1) u(im) 298.00 298.00
Copyright 2004 by Chapman & Hall/CRC
u(neqn) 298.00
33.3 66.7 100.0 133.3 166.7
1987.93 1991.00 1992.54 1993.68 1994.65
1002.11 1217.92 1344.68 1446.70 1532.29
394.14 622.58 834.02 1014.43 1166.78
rkc4a integrator ncase = t 0.0 33.3 66.7 100.0 133.3 166.7
3
neqn = 22
u(1) 298.00 1987.96 1991.05 1992.55 1993.72 1994.70
u(im) 298.00 1002.16 1218.10 1345.05 1447.20 1533.03
u(neqn) 298.00 394.17 622.85 834.66 1015.32 1168.10
rkc4b integrator ncase = t 0.0 33.3 66.7 100.0 133.3 166.7
4
neqn = 22
u(1) 298.00 1986.00 1991.04 1992.35 1993.56 1994.69
u(im) 298.00 1002.16 1217.94 1344.91 1446.90 1532.53
u(neqn) 298.00 394.17 622.61 834.42 1014.78 1167.22
rkf45a integrator ncase = t 0.0 33.3 66.7 100.0 133.3 166.7
5
neqn = 22
u(1) 298.00 1987.96 1991.05 1992.55 1993.72 1994.70
Copyright 2004 by Chapman & Hall/CRC
u(im) 298.00 1002.16 1218.10 1345.05 1447.20 1533.03
u(neqn) 298.00 394.17 622.85 834.66 1015.32 1168.10
rkf45b integrator ncase = t 0.0 33.3 66.7 100.0 133.3 166.7
6
neqn = 22
u(1) 298.00 1988.02 1991.28 1992.55 1993.94 1994.68
u(im) 298.00 1002.16 1218.10 1345.05 1447.02 1532.63
u(neqn) 298.00 394.17 622.85 834.66 1014.99 1167.40
This output is equivalent to the output from the preceding MATLAB (Section 5.1), C (Section 5.2), and C++ (Section 5.3). We now proceed to the Java programming of the nonlinear PDE.
5.5
Programming in Java
Again, since main Program 2.5.1 is unchanged in the nonlinear PDE problem, it is not listed here. An interface routine is listed below: /* Define the common (global) variables for the nonlinear PDE integration */ package mol; public class MOL { /* Maximum (default) number of ODEs */ public int SIZE=500; /* Variables for ODE integration */ public int neqn, nout, nsteps; public double t0, tf, abserr, relerr; public double u[], u0[], e[];
Copyright 2004 by Chapman & Hall/CRC
/* Parameters for PDE */ public double alpha, k, sigma, ui, ua, ar, rhos, cps, ls, xl, xu,dx=(xu-xl)/(neqn-2), public MOL() {
L, er, cs=rhos*cps*ls, dxs=dx*dx;
} }
Program 5.5.1 Interface routine for the solution of Equations 5.8 to 5.12 intpar, par, inital, derv, and fprint are listed next: /* This file is a member of the package mol */ package mol; import import import import
mol.MOL; java.math.*; java.io.*; java.text.*;
public class DEF extends MOL implements pdenoninterface { public DEF() { /* Integration parameters */ this.intpar(); /* Declare arrays */ u0=new double[SIZE]; u=new double[SIZE]; e=new double[SIZE]; /* Problem parameters */ this.par(); /* Initial condition vector */ this.inital(); }
Copyright 2004 by Chapman & Hall/CRC
public void intpar() /* Function intpar sets the parameters to control the integration of the nonlinear PDE */ { /* Number of ODEs */ neqn=22; /* Size of arrays in MOL library */ SIZE=neqn+1; /* Number of output points */ nout=6; /* Maximum number of steps in the interval t0 to tf */ nsteps=1000; /* Initial, final values of the independent variable */ t0=0.0; tf=0.2; /* Error tolerances */ abserr=Math.pow(10.0,-1.0); relerr=Math.pow(10.0,-5.0); /* End of intpar */ }
public void par()
/* Function par sets the parameters for the nonlinear PDE */ { alpha=1.0e-06; ui=298.0; rhos=7800.0; xl=0.0;
k=1.0; ua=2000.0; cps=435.0; xu=1.0;
sigma=5.67e-08; ar=1.0; ls=0.025; dx=(xu-xl)/(neqn-2);
L=0.1; er=1.0; cs=rhos*cps*ls; dxs=dx*dx;
/* End of par */ }
public void inital() /* Function inital sets the initial condition vector for the nonlinear PDE */
Copyright 2004 by Chapman & Hall/CRC
{ /* Type variables */ int i; /* Initial condition */ for(i=1;i<=neqn;i++) { u0[i]=ui; } /* End of inital */ }
public void derv(double ut[], double t, double u[]) /* Function derv computes the derivative vector of the nonlinear PDE */ { /* Type variables */ double u0; int i; /* Problem parameters */ par(); /* Insulation */ for(i=1;i<=(neqn-1);i++) { if(i==1) { u0=u[2]+2.0*dx*L*(sigma/k)*(ar*Math.pow(ua,4) -er*Math.pow(u[1],4)); ut[1]=(u[2]-2.0*u[1]+u0)/dxs; } if(i>1) { ut[i]=(u[i+1]-2.0*u[i]+u[i-1])/dxs; } } /* Steel */ ut[neqn]=(k*L/alpha)*(1.0/cs)*(u[neqn-2]-u[neqn-1])/dx; ut[neqn-1]=ut[neqn];
Copyright 2004 by Chapman & Hall/CRC
/* End of derv */ }
public void fprint(PrintWriter f, int ncase, int neqn, double t, double u[]) /* Function fprint displays the numerical solution to the nonlinear PDE */ { /* Type variables */ int im; /* Print a heading for the solution at t = 0 */ if(t<=0.0) { /* Label for ODE integrator */ switch(ncase) { /*Fixed step modified Euler */ case 1: f.println("\n\n euler2a integrator\n"); break; /* Variable step modified Euler */ case 2: f.println("\n\n euler2b integrator\n"); break; /* Fixed step classical fourth order RK */ case 3: f.println("\n\n rkc4a integrator\n"); break; /* Variable step classical fourth order RK */ case 4: f.println("\n\n rkc4b integrator\n"); break; /* Fixed step RK Fehlberg 45 */ case 5: f.println("\n\n rkf45a integrator\n"); break; /* Variable step RK Fehlberg 45 */
Copyright 2004 by Chapman & Hall/CRC
case 6: f.println("\n\n rkf45b integrator\n"); break; } /* Heading */ f.println(" neqn = "+neqn+ " f.println(" t u(1)
ncase = "+ncase+" \n"); u(im) u(neqn)");
/* End of t = 0 heading */ } /* Grid index of midpoint */ im=(neqn+1)/2; /* Display format for floating numbers */ DecimalFormat df1=new DecimalFormat("0.00"); DecimalFormat df2=new DecimalFormat("0.0"); /* Display the numerical solution */ f.println(df1.format(t) +"\t " +df2.format(u[1]) +"\t "+df2.format(u[im]) +"\t "+df2.format(u[neqn])); /* End of fprint */ } /* End of DEF */ }
Program 5.5.2 intpar, par, inital, derv, and fprint for the solution of Equations 5.8 to 5.12 The output from the preceding routines is listed below: euler2a integrator neqn = 22 t 0.00 0.20 0.40
ncase = 1 u(1) 298.0 1988.0 1991.0
Copyright 2004 by Chapman & Hall/CRC
u(im) 298.0 1002.1 1218.1
u(neqn) 298.0 394.2 622.8
0.60 0.80 1.00
1992.6 1993.7 1994.7
1345.0 1447.2 1533.0
834.7 1015.3 1168.1
u(im) 298.0 1002.1 1217.9 1344.7 1446.7 1532.3
u(neqn) 298.0 394.1 622.6 834.0 1014.4 1166.8
u(im) 298.0 1002.2 1218.1 1345.0 1447.2 1533.0
u(neqn) 298.0 394.2 622.8 834.7 1015.3 1168.1
u(im) 298.0 1002.2 1217.9 1344.9 1446.9 1532.5
u(neqn) 298.0 394.2 622.6 834.4 1014.8 1167.2
euler2b integrator neqn = 22 t 0.00 0.20 0.40 0.60 0.80 1.00
ncase = 2 u(1) 298.0 1987.9 1991.0 1992.5 1993.7 1994.7
rkc4a integrator neqn = 22 t 0.00 0.20 0.40 0.60 0.80 1.00
ncase = 3 u(1) 298.0 1988.0 1991.0 1992.6 1993.7 1994.7
rkc4b integrator neqn = 22 t 0.00 0.20 0.40 0.60 0.80 1.00
ncase = 4 u(1) 298.0 1986.0 1991.0 1992.3 1993.6 1994.7
Copyright 2004 by Chapman & Hall/CRC
rkf45a integrator neqn = 22 t 0.00 0.20 0.40 0.60 0.80 1.00
ncase = 5 u(1) 298.0 1988.0 1991.0 1992.6 1993.7 1994.7
u(im) 298.0 1002.2 1218.1 1345.0 1447.2 1533.0
u(neqn) 298.0 394.2 622.8 834.7 1015.3 1168.1
u(im) 298.0 1002.2 1218.1 1345.0 1447.0 1532.6
u(neqn) 298.0 394.2 622.8 834.7 1015.0 1167.4
rkf45b integrator neqn = 22 t 0.00 0.20 0.40 0.60 0.80 1.00
ncase = 6 u(1) 298.0 1988.0 1991.3 1992.5 1993.9 1994.7
This output is equivalent to the output from the preceding MATLAB (Section 5.1), C (Section 5.2), C++ (Section 5.3), and Fortran (Section 5.4). We now proceed to the Maple programming of the nonlinear PDE.
5.6
Programming in Maple
Since main Program 5.6.1 (and subordinate routines) accesses specific files by read statements, it is listed first: > restart: > read "c:\\odelib\\maple\\pdenon\\pdenon.txt"; > pdenon();
Program 5.6.1 Maple main program pdenon.mws for the numerical integration of Equations 5.8 to 5.12
Copyright 2004 by Chapman & Hall/CRC
pdenon:=proc() # # Main program pdenon computes the numerical # solution to the nonlinear PDE system by one of # six integrators # # Type variables global neqn, nout, nsteps, t0, tf, abserr, relerr: local u0, u, tp, ncase, i, j: # # Step through six integrators for ncase from 1 to 6 do # # Integration parameters read "c:\\odelib\\maple\\pdenon\\intpar.txt": intpar(): # # Size arrays u0:=array(1..neqn): u:=array(1..neqn): # # Initial condition vector read "c:\\odelib\\maple\\pdenon\\inital.txt": inital(neqn,t0,u0): # # Output interval tp:=tf-t0: # # Compute solution at nout output points for j from 1 to nout do # # Print current solution read "c:\\odelib\\maple\\pdenon\\fprint.txt": fprint(ncase,neqn,t0,u0): # # Fixed step modified Euler integrator if (ncase = 1) then read "c:\\odelib\\maple\\pdenon\\euler2a.txt": euler2a(neqn,t0,tf,u0,nsteps,u): end if: # # Variable step modified Euler integrator if (ncase = 2) then read "c:\\odelib\\maple\\pdenon\\euler2b.txt": euler2b(neqn,t0,tf,u0,nsteps,abserr,relerr,u): end if:
Copyright 2004 by Chapman & Hall/CRC
# #
# #
# #
# #
# #
Fixed step classical fourth order RK integrator if (ncase = 3) then read "c:\\odelib\\maple\\pdenon\\rkc4a.txt": rkc4a(neqn,t0,tf,u0,nsteps,u): end if: Variable step classical fourth order RK integrator if (ncase = 4) then read "c:\\odelib\\maple\\pdenon\\rkc4b.txt": rkc4b(neqn,t0,tf,u0,nsteps,abserr,relerr,u): end if: Fixed step RK Fehlberg (RKF45) integrator if (ncase = 5) then read "c:\\odelib\\maple\\pdenon\\rkf45a.txt": rkf45a(neqn,t0,tf,u0,nsteps,u): end if: Variable step RK Fehlberg (RKF45) integrator if (ncase = 6) then read "c:\\odelib\\maple\\pdenon\\rkf45b.txt": rkf45b(neqn,t0,tf,u0,nsteps,abserr,relerr,u): end if: Advance solution t0:=tf: tf:=tf+tp: for i from 1 to neqn do u0[i]:=u[i]: end do:
# # Next output end do: # # Next integrator end do: # # End of pdenon end:
Program 5.6.2 Maple main program pdenon.txt for the numerical integration of Equations 5.8 to 5.12
Copyright 2004 by Chapman & Hall/CRC
Note the reference to specific files by read statements, e.g., # # Initial condition vector read "c:\\odelib\\maple\\pdenon\\inital.txt": inital(neqn,t0,u0):
intpar, inital, derv, and fprint are listed below: intpar:=proc() # # Function intpar sets the parameters to control the # integration of the nonlinear PDE problem # # Type variables global neqn, nout, nsteps, t0, tf, abserr, relerr: # # Number of first order ODEs neqn:=22: # # Number of output points nout:=6: # # Maximum number of steps in the interval t0 to tf nsteps:=1000: # # Initial, final values of independent variable t0:=0.0: tf:=0.2: # # Error tolerances abserr:=1.0e-01: relerr:=1.0e-05: # # End of intpar end:
inital:=proc(neqn,t,u0) # # Function inital sets the initial condition vector # of the nonlinear PDE problem # # Type variables local ui, i: #
Copyright 2004 by Chapman & Hall/CRC
# Problem parameters ui:=298.0: # # Initial condition for i from 1 to neqn do u0[i]:=ui: end do: # # End of inital end:
derv:=proc(neqn,t,u,ut) # # Function derv computes the derivative vector # of the nonlinear PDE problem # # Type variables local u0, dx, dxs, i, alpha, k, sigma, L, ui, ua, a, e, rhos, cps, ls, cs: # # Problem parameters alpha:=1.0e-06: k:=1.0: sigma:=5.67e-08: L:=0.1: ui:=298.0: ua:=2000.0: a:=1.0: e:=1.0: rhos:=7800.0: cps:=435.0: ls:=0.025: cs:=rhos*cps*ls: # # Spatial grid dx:=1.0/(neqn-2): dxs:=dx*dx: # # Insulation for i from 1 to neqn-2 do if(i=1)then u0:=u[2]+2.0*dx*L*(sigma/k)*(a*ua^4-e*u[1]^4): ut[1]:=(u[2]-2.0*u[1]+u0)/dxs: else ut[i]:=(u[i+1]-2.0*u[i]+u[i-1])/dxs: end if: end do:
Copyright 2004 by Chapman & Hall/CRC
# # Steel ut[neqn]:=(k*L/alpha)*(1.0/cs)*(u[neqn-2]-u[neqn-1])/dx: ut[neqn-1]:=ut[neqn]: # # End of derv end:
fprint:=proc(ncase,neqn,t,u) # # Function fprint displays the numerical solution # to the nonlinear PDE problem # # Declare global variables global nsteps: # # Type variables local im, tmin, dx, dxs, i, alpha, k, sigma, L, ui, ua, a, e, rhos, cps, ls, cs: # # Problem parameters alpha:=1.0e-06: k:=1.0: sigma:=5.67e-08: L:=0.1: ui:=298.0: ua:=2000.0: a:=1.0: e:=1.0: rhos:=7800.0: cps:=435.0: ls:=0.025: cs:=rhos*cps*ls: # # Print a heading for the solution at t = 0 if (t <= 0.0) then # # Label for ODE integrator # # Fixed step modified Euler if (ncase = 1) then printf(`\n\n euler2a integrator\n\n`); # # Variable step modified Euler elif (ncase = 2) then printf(`\n\n euler2b integrator\n\n`);
Copyright 2004 by Chapman & Hall/CRC
# #
# #
# #
# #
Fixed step classical fourth order RK elif (ncase = 3) then printf(`\n\n rkc4a integrator\n\n`); Variable step classical fourth order RK elif (ncase = 4) then printf(`\n\n rkc4b integrator\n\n`); Fixed step RK Fehlberg 45 elif (ncase = 5) then printf(`\n\n rkf45a integrator\n\n`); Variable step RK Fehlberg 45 elif (ncase = 6) then printf(`\n\n rkf45b integrator\n\n`); end if:
# # Heading printf(` ncase = %2d neqn = %2d nsteps = %3d\n\n`,ncase,neqn,nsteps); printf(` t u(1) u(im) u(neqn)\n`); # # End of t = 0 heading end if: # # Numerical solution output # # Grid index of midpoint im:=(neqn/2); # # Display the numerical solution tmin:=t*L^2/alpha/60.0; printf(`%7.1f%10.2f%10.2f%10.2f\n`,tmin,u[1],u[im], u[neqn]); # # End of fprint end:
Program 5.6.3 intpar, inital, derv, and fprint for the solution of Equations 5.8 to 5.12 Programs 5.6.1, 5.6.2, and 5.6.3 closely parallel Programs 4.6.1, 4.6.2, and 4.6.3 so they will not be discussed here. The output from the preceding routines is listed below:
Copyright 2004 by Chapman & Hall/CRC
euler2a integrator ncase = t 0.0 33.3 66.7 100.0 133.3 166.7
1
neqn = 22
u(1) 298.00 1987.96 1991.05 1992.55 1993.72 1994.70
nsteps = 1000
u(im) 298.00 1002.15 1218.09 1345.04 1447.20 1533.03
u(neqn) 298.00 394.17 622.84 834.65 1015.31 1168.10
euler2b integrator ncase = t 0.0 33.3 66.7 100.0 133.3 166.7
2
neqn = 22
u(1) 298.00 1987.93 1991.00 1992.54 1993.68 1994.65
nsteps = 1000
u(im) 298.00 1002.11 1217.92 1344.68 1446.70 1532.29
u(neqn) 298.00 394.14 622.58 834.02 1014.43 1166.78
rkc4a integrator ncase = t 0.0 33.3 66.7 100.0 133.3 166.7
3
neqn = 22
u(1) 298.00 1987.96 1991.05 1992.55 1993.72 1994.70
nsteps = 1000
u(im) 298.00 1002.16 1218.10 1345.05 1447.20 1533.03
u(neqn) 298.00 394.17 622.85 834.66 1015.32 1168.10
rkc4b integrator ncase = t 0.0
4
neqn = 22 u(1) 298.00
Copyright 2004 by Chapman & Hall/CRC
nsteps = 1000
u(im) 298.00
u(neqn) 298.00
33.3 66.7 100.0 133.3 166.7
1987.94 1990.90 1992.54 1993.70 1994.65
1001.97 1217.84 1344.63 1446.55 1532.15
394.06 622.46 833.92 1014.16 1166.53
rkf45a integrator ncase = t 0.0 33.3 66.7 100.0 133.3 166.7
5
neqn = 22
u(1) 298.00 1987.96 1991.05 1992.55 1993.72 1994.70
nsteps = 1000
u(im) 298.00 1002.16 1218.10 1345.05 1447.20 1533.03
u(neqn) 298.00 394.17 622.85 834.66 1015.32 1168.10
rkf45b integrator ncase = t 0.0 33.3 66.7 100.0 133.3 166.7
6
neqn = 22
u(1) 298.00 1988.01 1991.05 1992.54 1993.73 1994.70
nsteps = 1000
u(im) 298.00 1002.16 1217.81 1344.61 1446.47 1532.10
u(neqn) 298.00 394.17 622.40 833.89 1014.02 1166.45
This output is equivalent to the output from the preceding MATLAB (Section 5.1), C (Section 5.2), C++ (Section 5.3), Fortran (Section 5.4), and Java (Section 5.5). This completes the discussion of the nonlinear PDE problem programmed in the six languages. We now consider an interpretation of the application of the six programs and the outputs. The collapse of the World Trade Center (WTC) towers on September 11, 2001 is generally attributed to the effect of the fires that resulted from burning jet fuel. Specifically, the structural steel was heated by the fires until it was weakened to the point that it could no longer support the towers; the four to six floors that suffered the most direct impact of the fires collapsed onto the
Copyright 2004 by Chapman & Hall/CRC
floors below, which in turn were overloaded and collapsed. This successive floor collapse propagated down the towers until they completely collapsed onto the ground. The structural steel was insulated to protect it from fire (a standard practice in the construction of buildings with steel frames). The question of how well this insulation protected the columns from the fires is complicated by two additional factors: • Some of the vertical columns in the outer walls of the towers were sheared off by the planes; thus, vertical support from these columns was lost. • The insulation at the point of impact of the planes was damaged and, in the case of some columns, was dislodged so that the steel in those columns was unprotected from the fire. Thus, the situation surrounding the impact of the planes and the resulting fires was complicated, and a detailed analysis is certainly beyond the scope of this discussion. However, a central question remains of how much the insulation that remained intact protected the steel columns from the burning jet fuel. The fact that the buildings did not collapse immediately suggests that the insulation was effective; the delayed collapse, summarized below, may have saved up to as many as 25,000 lives of the people who were able to escape the towers:
Tower
Time to Collapse (min)
North South
143 56
The preceding model of steel covered with insulation and subject to a high temperature through thermal radiation gives some insight into how the insulation might have performed. The numerical parameters that were used in the calculations are representative of the values for steel, insulation, and thermal radiation, but they cannot be considered accurate in the sense that they apply with certainty to the situation in the towers immediately after the time of impact of the planes. This is particularly true because of the complexity of the devastation that occurred at the points of impact. However, solutions to the model Equations 5.8 to 5.12 give an indication of how the structural steel temperatures may have increased with time. Specifically, if we consider the last output (from Programs 5.6.1, 5.6.2, and 5.6.3), we first observe that the temperature of the insulation directly facing the flame, u(1), increases rapidly:
Copyright 2004 by Chapman & Hall/CRC
ncase = t 0.0 33.3 66.7 100.0 133.3 166.7
6
neqn = 22
nsteps = 1000
u(1) 298.00 1988.01 1991.05 1992.54 1993.73 1994.70
This numerical output also gives some indication of what might have happened to the steel from which the insulation was dislodged. Clearly in a matter of a few minutes the temperatures reached values that would cause the steel to soften. The interior insulation temperatures (at the midpoint of the insulation, u(im) = u(11), and at the insulation/steel interface, u(neqn) = u(22)) were substantially delayed in time: ncase = t 0.0 33.3 66.7 100.0 133.3 166.7
6
neqn = 22
u(im) 298.00 1002.16 1217.81 1344.61 1446.47 1532.10
nsteps = 1000
u(neqn) 298.00 394.17 622.40 833.89 1014.02 1166.45
At the insulation/steel interface (neqn = 22), the steel temperature eventually enters a temperature range that would lead to substantial weakening of the steel, Steel Time (min) 66.7 100.0 133.3
K 622.40 833.89 1014.02
Temperature F 660 1041 1365
The time delay of about 60 to 120 min is approximately in agreement with the time of collapse of the towers, which suggests that the insulation was effective in delaying the collapse.
Copyright 2004 by Chapman & Hall/CRC
However, there is substantial uncertainty in the model. Therefore, the reader may wish to study the effect of parameter changes on the model output. The parameter values are set in the following code taken from Program 5.6.3: # # Problem parameters alpha:=1.0e-06: k:=1.0: L:=0.1: ui:=298.0: a:=1.0: e:=1.0: cps:=435.0: ls:=0.025:
sigma:=5.67e-08: ua:=2000.0: rhos:=7800.0: cs:=rhos*cps*ls:
Here are some suggested variations in the parameter values:
Parameter Thermal conductivity, k Thermal diffusivity, α Insulation thickness, L Steel thickness, ls Absorptivity, a Emissivity, e Flame temperature, ua
Current Value
Variation
1.0 J/s·m ·K 10−6 m2 /s 0.1 m 0.025 m 1.0 (dimensionless) 1.0 (dimensionless) 2000 (K)
0.5–2.0 0.8 × 10−6 –1.2 × 10−6 0.1–0.2 0.025–0.1 0.25–1.0 0.25–1.0 1800–2000
2
The reader will observe when making the suggested variations in the parameters that: • Some parameters are more sensitive than others in affecting the steel temperature variations with time. • The heating of the steel is substantially affected by the parameters. Thus, the conclusions drawn from the model (e.g., when the steel begins to weaken substantially) are determined significantly by the values of the model paramaters. • The preceding conclusion indicates the need for reliable parameter values, and the increased uncertainty of the conclusions drawn from the model with increased uncertainty in the model parameters. Thus, experimental measurements of the model parameters (in a laboratory) would be well worth the effort if models such as the one outlined in this chapter are to be used. Additionally, laboratory measurements of the strength of steel as a function of temperature would be essential for the interpretation of the solutions computed from the mathematical model of Equations 5.8 to 5.12. • Although the uncertainty in the model output is substantial, we would look to a refinement of the model as the next logical step for better
Copyright 2004 by Chapman & Hall/CRC
understanding of the basic problem (the effect of fire on structural steel); that is, we would refine the equations and parameters. The alternative is to design without an analysis that serves as a quantitative guide; this would be unacceptable for the design of important physical and chemical systems. Thus, we conclude with an indication of the following: • How ODE/PDE models might be used in important applications • Some numerical methods and associated software that can produce reliable (accurate) numerical solutions with reasonable effort In general, computation has become an essential part of scientific and engineering analysis, particularly for situations that can only be studied mathematically, and perhaps augmented by laboratory data, such as the effect of fire on structural steel, and its implications for the safety of large structures. Another example is the growth of tumors modeled by the 1x1 ODE system. The insights gained by computational analysis are indispensable for the understanding and design of scientific and engineering systems. We hope the preceding material in Chapters 1 to 5, and the supplementary material in the following Appendices A to F, will be of assistance in this type of analysis.
Copyright 2004 by Chapman & Hall/CRC
Appendix A Embedded Runge Kutta Pairs
The following 17 embedded RK pairs can be used for practice programming (Pairs 1, 9, and 11 are already programmed in Chapters 1 and 2 and the programs can therefore be used as templates). The performance of the error estimators can also be studied by applying the algorithms to problems with known analytical solutions as was done in Chapters 1 to 4. Of course, the 17 algorithms can also be applied to general nxn nonstiff ODE problems, with extensions to PDEs by the method of lines. Stiff ODEs can be studied by the methods discussed in Appendix C. The equations of each algorithm are listed in the order in which they would be programmed; i.e., they are listed in an executable sequence. Three pairs appear in routines discussed in Chapter 2:
Pair 1 9 11
Routine euler2a (fixed step) euler2b (variable step) rkf45a (fixed step) rkf45b (variable step) rkc4a (fixed step) rkc4b (variable step)
Pair 1 Calculation Stage 1 Stage 2
k1 = f (yi , ti )h k2 = f (yi + k1 , ti + h)h
Step O(h 1 )
(1) yi+1 = yi + k1
Est error Step O(h 2 ) Step t
Copyright 2004 by Chapman & Hall/CRC
(1,2) Chap 1 Equation
(1) e i+1 = 12 (k2 − k1 )
(2) (1) (1) yi+1 = yi+1 + e i+1 ti+1 = ti + h
Pair 2 Calculation
(2,3) Chap 1 Equation k1 = f (yi , ti )h k2 = f (yi + 23 k1 , ti + 23 h)h k3 = f (yi + 23 k2 , ti + 23 h)h
Stage 1 Stage 2 Stage 3
(2) yi+1 = yi + 14 k1 + 34 k2
Step O(h 2 )
(3) yi+1 = yi + 28 k1 + 38 k2 + 38 k3
Step O(h 3 )
(2) (3) (2) e i+1 = yi+1 − yi+1 ti+1 = ti + h
Est error Step t
Pair 3 Calculation
(2,3) Equation
Stage 1 Stage 2 Stage 3
k1 = f (yi , ti )h k2 = f (yi + k21 , ti + h2 )h k3 = f (yi − k1 + 2k2 , ti + h)h
Step O(h 2 )
(2) yi+1 = yi + 12 (k1 + k3 )
Est error Step O(h 3 ) Step t
Pair 4 Calculation
(2) = 13 (−k1 + 2k2 − k3 ) e i+1
(3) (2) (2) yi+1 = yi+1 + e i+1 ti+1 = ti + h
(3,4) Equation
Stage 1 Stage 2
k1 = f (yi , ti ) h3 k2 = f (yi + k1 , ti + h3 ) h3
Stage 3
k3 = f (yi + 12 (k1 + k2 ), ti + h3 ) h3
Stage 4
k4 = f (yi + 38 k1 + 98 k3 , ti + h2 ) h3
Stage 5
k5 = f (yi + 32 k1 − 92 k3 + 6k4 , ti + h) h3
Step O(h 3 ) Est error Step O(h 4 ) Step t
Copyright 2004 by Chapman & Hall/CRC
(3) yi+1 = yi + 12 (k1 + 4k4 + k5 )
(3) = 15 (−k1 + 92 k3 − 4k4 + 12 k5 ) e i+1 (4) (3) (3) yi+1 = yi+1 + e i+1 ti+1 = ti + h
Pair 5 Calculation
(3,4) Equation f (yi , ti )h f (yi − 0.4k1 , ti − 0.4h)h f (yi + 0.6684895833k1 − 0.2434895833k2 , ti + 0.425h)h f (yi − 2.323685857k1 + 1.125483559k2 + 2.198202298k3 , ti + h)h (3) yi+1 = yi + 0.03968253968k2 + 0.7729468599k3 + 0.18737060041k4 (3) (3) e i+1 = −yi+1 + yi + 0.03431372549k1 + 0.02705627706k2 + 0.7440130202k3 + 0.1946169772k4 (4) (3) (3) yi+1 = yi+1 + e i+1 ti+1 = ti + h
Stage 1 Stage 2 Stage 3 Stage 4
k1 k2 k3 k4
Step O(h 3 ) Est error Step O(h 4 ) Step t
= = = =
Pair 6 Calculation Stage 1 Stage 2 Stage 3 Stage 4 Stage 5 Step O(h 4 ) Est error Step O(h 5 ) Step t
(4,5) Equation f (yi , ti )h f (yi + 0.0005k1 , ti + 0.0005h)h f (yi − 80.89939470k1 + 81.18439470k2 , ti + 0.285h)h f (yi + 2113.327899k1 − 2117.778035k2 +5.442136522k3 , ti + 0.992h)h k5 = f (yi + 2249.757677k1 − 2254.489040k2 + 5.739991965k3 −0.008629230728k4 , ti + h)h (4) yi+1 = yi − 131.2823524k1 + 131.4998223k2 + 0.4837620276k3 +0.2987680554k4 (4) (4) e i+1 = −yi+1 + yi + 65.80784286k1 − 65.94767173k2 +0.7959885276k3 + 4.715404915k4 − 4.371564570k5 (5) (4) (4) yi+1 = yi+1 + e i+1 ti+1 = ti + h
k1 k2 k3 k4
= = = =
Pair 7 Calculation
(3,4) Equation = = = = =
f (yi , ti )h f (yi + 13 k1 , ti + 13 h)h f (yi + 16 (k1 + k2 ), ti + 13 h)h f (yi + 18 (k1 + 3k3 ), ti + 12 h)h f (yi + 12 k1 − 32 k3 + 2k4 , ti + h)h
Stage 1 Stage 2 Stage 3 Stage 4 Stage 5
k1 k2 k3 k4 k5
Step O(h 3 )
(3) yi+1 = yi + 12 k1 − 32 k3 + 2k4
Est error Step O(h 4 ) Step t
Copyright 2004 by Chapman & Hall/CRC
(3) e i+1 = − 13 k1 + 32 k3 − 43 k4 + 16 k5 (4) (3) (3) yi+1 = yi+1 + e i+1 ti+1 = ti + h
Pair 8 Calculation
(4,5) Equation
Stage 1
k1 = f (yi , ti )h
Stage 2
k2 = f (yi + 12 k1 , ti + 12 h)h
Stage 3
k3 = f (yi + 14 (k1 + k2 ), ti + 12 h)h
Stage 4
k4 = f (yi − k2 + 2k3 , ti + h)h
Stage 5
k5 = f (yi +
Stage 6
k6 = f (yi +
Step O(h 4 )
(4) yi+1 = yi + 16 (k1 + 4k3 + 4k4 )
Est error
(4) = e i+1
1 (−42k1 − 336 (4) (4) yi+1 + e i+1
Step O(h )
(5) yi+1
Step t
ti+1 = ti + h
5
=
1 (7k1 + 10k2 + k4 ), ti + 23 h)h 27 1 (28k1 − 125k2 + 546k3 + 54k4 625
224k3 − 21k4 + 162k5 + 125k6 )
Pair 9 Calculation
(4,5) Chap 1 Equation
Stage 1
k1 = f (yi , ti )h
Stage 2
k2 = f (yi + 14 k1 , ti + 14 h)h
Stage 3
k3 = f (yi +
Stage 4
k4 = f (yi +
Stage 5
k5 = f (yi + k6 = f (yi −
Stage 6 Step O(h )
(4) yi+1 = yi +
Step O(h 5 )
(5) yi+1 = yi +
Est error
(4) e i+1 =
Step t
ti+1 = ti + h
4
Copyright 2004 by Chapman & Hall/CRC
− 378k5 ), ti + 15 h)h
1 (3k1 + 9k2 ), ti + 38 h)h 32 1 (1932k1 − 7200k2 + 7296k3 ), ti + 12 h)h 2197 13 439 3680 845 k − 8k2 + 513 k3 − 4104 k4 , ti + h)h 216 1 8 11 k + 2k2 − 3544 k + 1859 k − 40 k5 , ti + 12 h)h 27 1 2565 3 4104 4 25 k 216 1
+
1408 k 2565 3
+
16 6656 k + 12825 k3 135 1 (5) (4) yi+1 − yi+1
+
2197 k 4104 4
− 15 k5
28561 k 56430 4
−
9 k 50 5
+
2 k 55 6
Pair 10 Calculation
(4,5) Equation
Stage 1 Stage 2
k1 = f (yi , ti )h k2 = f (yi + 15 k1 , ti + 15 h)h
Stage 3
k3 = f (yi +
Stage 4
k4 = f (yi +
Stage 5
k5 = f (yi −
Stage 6
k6 = f (yi +
3 k + 40 1 3 k − 10 1 11 k + 54 1 1631 k 55296 1
9 3 k , t + 10 h)h 40 2 i 9 6 k + 5 k3 , ti + 35 h)h 10 2 5 k − 70 k + 35 k , t + h)h 2 2 27 3 27 4 i 175 575 44275 + 512 k2 + 13824 k3 + 110592 k4
253 + 4096 k5 , ti + 78 h)h
Step O(h 4 )
(4) yi+1 = yi +
Step O(h 5 )
(5) yi+1 = yi +
Est error
(4) e i+1 =
Step t
ti+1 = ti + h
2825 k 27648 1
+
18575 k 48384 3
37 k + 250 k 378 1 621 3 (5) (4) yi+1 − yi+1
Pair 11 Calculation
+
+
13525 k 55296 4
125 k 594 4
+
+
277 k 14336 5
512 k 1771 6
(2,4) Chap 1 Equation = = = =
f (yi , ti )h f (yi + 12 k1 , ti + 12 h)h f (yi + 12 k2 , ti + 12 h)h f (yi + k3 , ti + h)h
Stage 1 Stage 2 Stage 3 Stage 4
k1 k2 k3 k4
Step O(h 2 )
(2) yi+1 = yi + k2
Est error
(2) e i+1 = 16 (k1 − 4k2 + 2k3 + k4 )
Step O(h 4 ) Step t
(4) (2) (2) yi+1 = yi+1 + e i+1 ti+1 = ti + h
Copyright 2004 by Chapman & Hall/CRC
+ 14 k6
Pair 12 Calculation
(2,4) Equation = = = =
f (yi , ti )h f (yi + 13 k1 , ti + 13 h)h f (yi − 13 k1 + k2 , ti + 23 h)h f (yi + k1 − k2 + k3 , ti + h)h
Stage 1 Stage 2 Stage 3 Stage 4
k1 k2 k3 k4
Step O(h 2 )
(2) yi+1 = yi − 12 k1 + 32 k2
Est error
(2) e i+1 = 18 (5k1 − 9k2 + 3k3 + k4 )
Step O(h 4 ) Step t
(4) (2) (2) yi+1 = yi+1 + e i+1 ti+1 = ti + h
Pair 13 Calculation
(2,4) Equation
Stage 1 Stage 2
k1 = f (yi , ti )h k2 = f (yi + 12 k1 , ti + 12 h)h
Stage 3
k3 = f (yi − ( 12 −
√1 )k 1 2
Stage 4
k4 = f (yi −
+ (1 +
√1 k2 2
Step O(h )
(2) yi+1
= yi + k2
Est error
(2) e i+1
= 16 (k1 − 2(2 +
2
Step O(h )
(4) yi+1
Step t
ti+1 = ti + h
4
=
Pair 14 Calculation
(2) yi+1
+
+ (1 −
√1 )k 2 , ti 2
√1 )k 3 , ti 2
√1 )k2 2
+ h)h
+ 2(1 +
√1 )k 3 2
(2) e i+1
(2,4) Equation
Stage 1
k1 = f (yi , ti )h
Stage 2
k2 = f (yi + 12 k1 , ti + 12 h)h
Stage 3
k3 = f (yi − 12 k1 + k2 , ti + 12 h)h k4 = f (yi + 12 k2 + 12 k3 , ti + h)h
Stage 4 Step O(h )
(2) yi+1 = yi + k2
Est error
(2) e i+1 = 16 (k1 − 3k2 + k3 + k4 )
2
Step O(h 4 ) Step t
Copyright 2004 by Chapman & Hall/CRC
(4) (2) (2) yi+1 = yi+1 + e i+1 ti+1 = ti + h
+ 12 h)h
+ k4 )
Pair 15 Calculation
(2,4) Equation
Stage 1
k1 = f (yi , ti )h
Stage 2
k2 = f (yi + 25 k1 , ti + 25 h)h
Stage 3
k3 = f (yi + 0.29697760924775360k1 + 0.15875964497103583k2 , ti + 0.45573725421878943h)h k4 = f (yi + 0.21810038822592047k1 − 3.0509651486929308k2 +3.8328647604670103k3 , ti + h)h
Stage 4 Step O(h 2 ) Est error Step O(h 4 ) Step t
(2) yi+1 = yi − 14 k1 + 1.25k2 (2) e i+1 = 0.42476028226269037k1 − 1.8014806628787329k2 + 1.2055355993965235k3 + 0.17118478121951903k4 (4) (2) (2) yi+1 = yi+1 + e i+1 ti+1 = ti + h
Pair 16 Calculation
(2,5) Equation
Stage 1
k1 = f (yi , ti )h
Stage 2
k2 = f (yi + 12 k1 , ti + 12 h)h
Stage 3
k3 = f (yi + 14 (k1 + k2 ), ti + 12 h)h
Stage 4
k4 = f (yi − k2 + 2k3 , ti + h)h
Stage 5
k5 = f (yi +
Stage 6
k6 = f (yi +
Step O(h 2 )
(2) yi+1 = yi + 12 k2
Est error
(2) e i+1 =
Step O(h 5 )
(5) yi+1
Step t
ti+1 = ti + h
Copyright 2004 by Chapman & Hall/CRC
=
1 (7k1 + 10k2 + k4 ), ti + 23 h)h 27 1 (28k1 − 125k2 + 546k3 + 54k4 625
1 (14k1 − 336 (2) (2) yi+1 + e i+1
− 378k5 ), ti + 15 h)h
336k2 + 35k4 + 162k5 + 125k6 )
Pair 17 Calculation
(2,5) Equation
Stage 1
k1 = f (yi , ti )h
Stage 2
k2 = f (yi + 13 k1 , ti + 13 h)h
Stage 3
k3 = f (yi +
Stage 4
k4 = f (yi +
Stage 5
k5 = f (yi + k6 = f (yi +
Stage 6 Step O(h ) 2
Est error Step O(h 5 ) Step t
(2) yi+1 (2) e i+1 (5) yi+1
ti+1
= yi − =
1 (4k1 + 6k2 ), ti + 25 h)h 25 1 (k − 12k2 + 15k3 ), ti + h)h 4 1 1 (6k1 + 90k2 − 50k3 + 8k4 ), ti 81 1 (6k1 + 36k2 + 10k3 + 8k4 ), ti 75 1 k 2 1
+
+ 23 h)h + 45 h)h
3 k 2 2
1 (119k1 − 192 (2) (2) yi+1 + e i+1
288k2 + 125k3 − 81k5 + 125k6 )
= = ti + h
Note that these 17 integrators have error estimates based on varying numbers of terms in the associated Taylor series. The number of terms in each error estimate, which just equals the difference in the orders of the pairs, is summarized below. For example, a (2, 4) pair has 4−2 = 2 terms in the error estimate.
Pairs 1,2,3,4,5,6,7,8,9,10 11,12,13,14,15 16,17
Terms in the Error Estimate 1 2 3
The algorithms with 3 term error estimates (Pairs 16 and 17) are recommended for ODE problems with particularly stringent accuracy requirements. In addition to the references for Pairs 1, 2, 9, and 11 given in Chapter 1, references for the other pairs (except 10), are given in Reference 1. This reference also provides Fortran routines for the ODE integrators summarized above (except 2 and 10). Pair 10 is taken from Reference 2.
References 1. Silebi, C.A., and W.E. Schiesser, Dynamic Modeling of Transport Process Systems, Academic Press, San Diego, CA, 1992. 2. Press, W.H., S.A. Teukolsky, W.T. Vetterling, and B.P. Flannery, Numerical Recipes in Fortran, 2nd ed., Cambridge University Press, Cambridge, U.K., 1992.
Copyright 2004 by Chapman & Hall/CRC
Appendix B Integrals from ODEs
The ODE algorithms discussed in Chapter 1 can be used to compute onedimensional integrals. Specifically, the special case ODE (with the derivative function f (t) a function of the independent variable t and not the dependent variable y) dy (B.1) = f (t), y(t0 ) = y0 dt has the solution t
y(t) = y0 +
f (τ )dτ
(B.2)
t0
Thus, we can integrate Equation B.1 to produce a numerical value of the integral of Equation B.2. To illustrate this procedure, consider the ODE 2 dy 2 = √ e −t , y(0) = 0 dt π which has the solution
2 y(t) = √ π
t
e −τ dτ 2
(B.3)
(B.4)
0
y(t) of Equation B.4 is the well-known error function, erf(t), which has broad application in science and engineering, and is tabulated extensively. Functions intpar, inital, derv, and fprint, which follow directly from the preceding discussion (for the solution of Equation B.3), are listed below: function [neqn,nout,nsteps,t0,tf,abserr,relerr]=intpar % % Function intpar sets the parameters to control the % integration of the ODE with erf as a solution % % Number of first order ODEs neqn=1;
Copyright 2004 by Chapman & Hall/CRC
% % Number of output points nout=6; % % Maximum number of steps in the interval t0 to tf nsteps=100; % % Initial, final values of independent variable t0=0.0; tf=0.1; % % Error tolerances abserr=1.0e-05; relerr=1.0e-05;
function [y]=inital(neqn,t) % % Function inital sets the initial condition vector % for the ODE with erf as the solution % % Initial condition y(1)=0.0;
function [yt]=derv(neqn,t,y) % % Function derv computes the derivative vector % of the ODE with erf as the solution % % Declare global variables global nsteps; % % ODE yt(1)=exp(-t^2);
function [out]=fprint(ncase,neqn,t,y) % % Function fprint displays the numerical and % tabulated solutions to the ODE with erf as % a solution % % Define global variables global nsteps;
Copyright 2004 by Chapman & Hall/CRC
% % Return current value of independent variable % (MATLAB requires at least one return argument) out=t; % % Print a heading for the solution at t = 0 if(t<=0.0) % % Label for ODE integrator % % Fixed step modified Euler if(ncase==1) fprintf('\n\n euler2a integrator\n\n'); % % Variable step modified Euler elseif(ncase==2) fprintf('\n\n euler2b integrator\n\n'); % % Fixed step classical fourth order RK elseif(ncase==3) fprintf('\n\n rkc4a integrator\n\n'); % % Variable step classical fourth order RK elseif(ncase==4) fprintf('\n\n rkc4b integrator\n\n'); % % Fixed step RK Fehlberg 45 elseif(ncase==5) fprintf('\n\n rkf45a integrator\n\n'); % % Variable step RK Fehlberg 45 elseif(ncase==6) fprintf('\n\n rkf45b integrator\n\n'); end % % Heading fprintf(' ncase = %2d nsteps = %3d\n\n',ncase,nsteps); fprintf(' t y(num) y(tab) diff\n'); % % End of t = 0 heading end % % Tabulated solution if(t<0.001) ytab=0.0; end if((t>0.0999)&(t<0.1001)) ytab=0.112463; end
Copyright 2004 by Chapman & Hall/CRC
if((t>0.1999)&(t<0.2001)) if((t>0.2999)&(t<0.3001)) if((t>0.3999)&(t<0.4001)) if((t>0.4999)&(t<0.5001))
ytab=0.222703; ytab=0.328627; ytab=0.428392; ytab=0.520500;
end end end end
% % Numerical and tabulated solutions and their difference % as output y1=(2.0/sqrt(pi))*y(1); diff=y1-ytab; % % Display the numerical and exact solutions, and their % difference fprintf('%5.2f %11.6f %11.6f %11.6f\n',t,y1,ytab,diff);
Program B.1 intpar, inital, derv, and fprint for the solution of Equation B.3
The main program is again Program 3.1.1. The output from the preceding program is listed below euler2a integrator ncase = t 0.00 0.10 0.20 0.30 0.40 0.50
1
nsteps = 100
y(num) 0.000000 0.112463 0.222703 0.328627 0.428392 0.520500
y(tab) 0.000000 0.112463 0.222703 0.328627 0.428392 0.520500
diff 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
euler2b integrator ncase = t 0.00 0.10 0.20 0.30 0.40 0.50
2
nsteps = 100
y(num) 0.000000 0.112461 0.222700 0.328623 0.428388 0.520496
Copyright 2004 by Chapman & Hall/CRC
y(tab) 0.000000 0.112463 0.222703 0.328627 0.428392 0.520500
diff 0.000000 -0.000002 -0.000003 -0.000004 -0.000004 -0.000004
rkc4a integrator ncase = t 0.00 0.10 0.20 0.30 0.40 0.50
3
nsteps = 100
y(num) 0.000000 0.112463 0.222703 0.328627 0.428392 0.520500
y(tab) 0.000000 0.112463 0.222703 0.328627 0.428392 0.520500
diff 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
rkc4b integrator ncase = t 0.00 0.10 0.20 0.30 0.40 0.50
4
nsteps = 100
y(num) 0.000000 0.112463 0.222703 0.328627 0.428392 0.520500
y(tab) 0.000000 0.112463 0.222703 0.328627 0.428392 0.520500
diff 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
rkf45a integrator ncase = t 0.00 0.10 0.20 0.30 0.40 0.50
5
nsteps = 100
y(num) 0.000000 0.112463 0.222703 0.328627 0.428392 0.520500
y(tab) 0.000000 0.112463 0.222703 0.328627 0.428392 0.520500
diff 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
rkf45b integrator ncase = t 0.00 0.10
6
nsteps = 100
y(num) 0.000000 0.112463
Copyright 2004 by Chapman & Hall/CRC
y(tab) 0.000000 0.112463
diff 0.000000 0.000000
0.20 0.30 0.40 0.50
0.222703 0.328627 0.428392 0.520500
0.222703 0.328627 0.428392 0.520500
0.000000 0.000000 0.000000 0.000000
The error tolerances set in intpar are satisfied by all six integrators. Of course, this problem tests only the stepping in t, and not y, because of the special form of Equation B.1.
Copyright 2004 by Chapman & Hall/CRC
Appendix C Stiff ODE Integration
The explicit integrators discussed in Chapters 1 and 2, and applied to ODE and PDE systems in Chapters 2 to 5, can be used for a broad spectrum of applications. However, as discussed in Section 1.7, these explicit (nonstiff) integrators will require lengthy calculations for stiff systems. Again, this difficulty with stiff systems results from having to take small integration steps to maintain stability of the numerical integration (because of the stability constraints of explicit algorithms) while having to cover a large interval in the independent variable to compute a complete solution. An example is the 2x2 system of Chapter 3 for which the maximum integration step to maintain stability is set by the largest eigenvalue, while the timescale of the solution is set by the smallest eigenvalue. If the eigenvalues are widely separated, the problem is stiff, and an implicit integrator is required to compute a solution efficiently, as we will demonstrate. We now illustrate some of the properties of stiff systems using the 2x2 system. Specifically, we will consider: • The BDF formulas of Section 1.7 applied to the 2x2 ODE system • A MATLAB program for the first-order BDF method (implicit Euler method) • The 2x2 ODE system integrated by the MATLAB stiff integrators ode23s and ode15s
C.1
The BDF Formulas Applied to the 2x2 ODE System
The BDF method is first applied to the general (possibly nonlinear) 2x2 ODE system dy1 /dt = f 1 (y1 , y2 , t) (C.1) dy2 /dt = f 2 (y1 , y2 , t)
Copyright 2004 by Chapman & Hall/CRC
The BDF equations are α0 yi+1,1 + α1 yi,1 + · · · + αν yi−ν+1,1 = h f 1 (yi+1,1 , yi+1,2 , ti+1 ) α0 yi+1,2 + α1 yi,2 + · · · + αν yi−ν+1,2 = h f 2 (yi+1,1 , yi+1,2 , ti+1 ) or h f 1 (yi+1,1 , yi+1,2 , ti+1 ) − [α0 yi+1,1 + α1 yi,1 + · · · + αν yi−ν+1,1 ] = g1 (yi+1,1 , yi+1,2 ti+1 ) = 0 h f 2 (yi+1,1 , yi+1,2 , ti+1 ) − [α0 yi+1,2 + α1 yi,2 + · · · + αν yi−ν+1,2 ]
(C.2)
= g2 (yi+1,1 , yi+1,2 , ti+1 ) = 0 Equations C.2 are a 2x2 nonlinear system (nonlinear if the original ODE system, Equations C.1, is nonlinear), for the two unknowns yi+1,1 , yi+1,2 (the ODE solution at the advanced point i +1). We need to apply a nonlinear solver to Equations C.2 such as Newton’s method to compute the two unknowns, yi+1,1 , yi+1,2 . Jδy = −g(y)
(C.3)
where for the nxn problem y1 y2 y = . , ..
yn
δy1 δy2 δy = . ..
(C.4)(C.5)
δyn
g1 (y1, y2 , · · · yn , t) g1 g2 g2 (y1, y2 , · · · yn , t) g= . = .. .. .
gn (y1, y2 , · · · yn , t)
gn ∂g
1
∂ y1 ∂g 2 J= ∂ y1 . . . ∂gn ∂ y1
Copyright 2004 by Chapman & Hall/CRC
∂g1 ∂ y2 ∂g2 ∂ y2 .. . ∂gn ∂ y2
··· ··· ..
(C.6)
.
···
∂g1 ∂ yn J 11 ∂g2 J 21 . ∂ yn = .. .. . J n1 ∂gn ∂ yn
J 12 J 22 .. . J n2
··· ··· .. . ···
J 1n J 2n .. . J nn
(C.7)
J is the nxn Jacobian matrix, consisting of all first-order partial derivatives of the functions, [g1 g2 . . . gi . . . gn ]T with respect to the dependent variables [y1 y2 . . . y j . . . yn ]T , i.e., ∂gi Ji j = (C.8) ∂ yj δy is the vector of Newton corrections, which should decrease below a specified tolerance or threshold as the Newton iteration proceeds. Application of the preceding equations to the 2x2 ODE system of Equations 1.6 of Chapter 1: dy1 = −a y1 + by2 dt (C.9) dy2 = by1 − a y2 dt gives
y=
g= =
g1 (y1, y2 , t)
y1 , y2
δy1 δy = δy2
(C.10)(C.11)
g2 (y1, y2 , t) h [−a yi+1,1 + byi+1,2 ] − [α0 yi+1,1 + α1 yi,1 + · · · + αν yi−ν+1,1 ]
h [+byi+1,1 − a yi+1,2 ] − [α0 yi+1,2 + α1 yi,2 + · · · + αν yi−ν+1,2 ] ∂g
1
∂ y1 J= ∂g
2
∂ y1
∂g1 ∂ y2 J 11 = J ∂g
21
2
J 12 J 22
=
−a h − α0
bh −a h − α0
bh
(C.12)
(C.13)
∂ y2
Thus, Equation C.3 becomes
=−
−a h − α0 bh
bh −a h − α0
δy1 δy2
h [−a yi+1,1 + byi+1,2 ] − [α0 yi+1,1 + α1 yi,1 + · · · + αν yi−ν+1,1 ] h [+byi+1,1 − a yi+1,2 ] − [α0 yi+1,2 + α1 yi,2 + · · · + αν yi−ν+1,2 ]
(C.14)
For the first-order BDF (first-order implicit RK or implicit Euler method), ν = 1, α0 = 1, α1 = −1, α2 , . . . αν = 0. Thus, Equations C.14 require only the preceding values yi,1 , yi,2 (generally RK methods are self-starting while higher-order BDF methods require a history of the solution at the points i −1, i −2, . . . i −ν +1
Copyright 2004 by Chapman & Hall/CRC
to take the step from i to i + 1; but, of course, the higher-order BDF methods have better accuracy than the first-order BDF). Equations C.14 are programmed in the following MATLAB program. The numerical integration continues until the condition |δy1 | < eps |δy2 | < eps is satisfied where eps is a tolerance set in the program.
C.2
MATLAB Program for the Solution of the 2x2 ODE System
Equations C.14 with ν = 1 (first-order BDF or the implicit Euler method) are solved by Newton’s method in the following program: % % 2 x 2 ODE system by first order BDF % % Open a file for output fid=fopen('appc1.out','w'); % % Number of ODEs neqn=2; % % Convergence tolerance, maximum number of % iterations to compute Newton corrections eps=0.00001; maxiter=20; % % Initial condition y10=0.0; y20=2.0; y(1)=y10; y(2)=y20; t=0.0; % % Values just for initial output dy(1)=0.0; dy(2)=0.0; niter=0; % % Integration step, steps/output, number of outputs h=0.01;
Copyright 2004 by Chapman & Hall/CRC
nsteps=100; nout=11; % % Problem parameters a=500000.5; b=499999.5; % % Jacobian matrix J(1,1)=-a*h-1.0; J(1,2)=b*h; J(2,1)=b*h; J(2,2)=-a*h-1.0; % % Print a heading fprintf( ' t dy(1) dy(2) y(1) y(2) erry(1) erry(2) iter\n'); % % nout outputs for i=1:nout % % Initial output or after nstep integration steps % completed; display Newton corrections, numerical and % exact solutions for output lambda1=-(a-b); lambda2=-(a+b); exp1=exp(lambda1*t); exp2=exp(lambda2*t); y1e=exp1-exp2; y2e=exp1+exp2; erry1=y1e-y(1); erry2=y2e-y(2); fprintf(fid,'%10.5f%10.5f%10.5f%10.5f%10.5f%10.5f %10.5f%5d\n',...t,dy(1),dy(2),y(1),y(2), erry1,erry2,niter); fprintf( '%10.5f%10.5f%10.5f%10.5f%10.5f%10.5f %10.5f%5d\n',...t,dy(1),dy(2),y(1),y(2), erry1,erry2,niter); % % nsteps steps/output for is=1:nsteps % % Initialize iteration counter niter=1; stop=0; %
Copyright 2004 by Chapman & Hall/CRC
% % %
% % % % % % % % %
% %
% %
% % % % % %
% %
Test for the end of the current step while stop==0 Functions g1, g2 g=[h*(-a*y(1)+b*y(2))-y(1)+y10 h*( b*y(1)-a*y(2))-y(2)+y20]; Solve for Newton corrections Gaussian elimination dy=-J\g; Jacobian inverse dy=-J^(-1)*g; Update solution for ic=1:neqn y(ic)=y(ic)+dy(ic); end stop=1; Check if the corrections are within the tolerance eps for ic=1:neqn if abs(dy(ic))>eps Convergence not achieved; continue calculation niter=niter+1; stop=0; break; end end If maximum iterations reached, accept current step if (niter==maxiter) stop=1; end Continue integration step end Integration step completed y10=y(1); y20=y(2); t=t+h; Continue nstep integration steps end
Copyright 2004 by Chapman & Hall/CRC
% % Continue integration for next output interval end
Program C.1 Solution of Equations C.14 by Newton’s method We can note the following points about Program C.1: • An output file is defined, then the number of nonlinear equations to be solved, in this case 2 for Equations C.14: % % 2 x 2 ODE system by first order BDF % % Open a file for output fid=fopen('appc1.out','w'); % % Number of ODEs neqn=2;
• The tolerance for the Newton corrections and the maximum number of Newton iterations are then defined numerically: % % Convergence tolerance, maximum number of % iterations to compute Newton corrections eps=0.00001; maxiter=20;
• An initial estimate of the solution, required by Newton’s method is defined, which is taken as the initial condition for Equations C.9 (y1 (0) = 0, y2 (0) = 2). Also, the independent variable, t, in Equations C.9 is initialized: % % Initial condition y10=0.0; y20=2.0; y(1)=y10; y(2)=y20; t=0.0;
• The Newton corrections are zeroed and the counter for the Newton iterations is initialized:
Copyright 2004 by Chapman & Hall/CRC
% % Values just for initial output dy(1)=0.0; dy(2)=0.0; niter=0;
• The variables that control the BDF integration are set, specifically, the integration step in Equations C.14 (this will be for a fixed step BDF integration), the maximum number of integration steps for each output interval, and the number of outputs: % % Integration step, steps/output, number of outputs h=0.01; nsteps=100; nout=11;
• The parameters in Equations C.9 are set: % % Problem parameters a=500000.5; b=499999.5;
These values correspond to the stiff case discussed in Section 1.7 for which the eigenvalues of Equations 1.6 (or C.9) are λ1 = −(a − b) = −1, λ2 = −(a +b) = −1, 000, 000. Thus, there will be a total of 100×(11−1) = 1000 Newton steps, each of length 0.01 so that the final value of t is 0.01 × 1000 = 10. • The elements of the nxn = 2x2 Jacobian matrix of Equation C.13 are programmed: % % Jacobian matrix J(1,1)=-a*h-1.0; J(1,2)=b*h; J(2,1)=b*h; J(2,2)=-a*h-1.0;
Note that since Equations C.9 are linear (constant coefficient ODEs), the Jacobian matrix is constant and therefore has to be evaluated only once. More generally, if the ODEs are nonlinear, the Jacobian matrix would have to be updated at each Newton iteration (which is a major portion of the calculation in using Newton’s method).
Copyright 2004 by Chapman & Hall/CRC
• A heading for the numerical solution is then printed: % % Print a heading fprintf( ' y(1) y(2)
t erry(1)
dy(1) erry(2)
dy(2) iter\n');
(this output statement has been put in two lines to fit within the available printed space; they should be returned to single lines if Equation C.1 is executed). • An outer loop is used to output the solution at the nout output points: % % nout outputs for i=1:nout % % Initial output or after nstep integration steps % completed; display Newton corrections, numerical % and exact solutions for output lambda1=-(a-b); lambda2=-(a+b); exp1=exp(lambda1*t); exp2=exp(lambda2*t); y1e=exp1-exp2; y2e=exp1+exp2; erry1=y1e-y(1); erry2=y2e-y(2); fprintf(fid,'%10.5f%10.5f%10.5f%10.5f%10.5f%10.5f %10.5f%5d\n',...t,dy(1),dy(2),y(1), y(2),erry1,erry2,niter); fprintf( '%10.5f%10.5f%10.5f%10.5f%10.5f%10.5f %10.5f%5d\n',...t,dy(1),dy(2),y(1), y(2),erry1,erry2,niter);
The first task in this loop is to compute the exact solution to Equations 1.6 (or Equations C.9), i.e., Equations 1.17. The difference between the numerical and exact solution is then computed, followed by printing of the independent variable, t, the two Newton corrections, the numerical solution, the error in the numerical solution, and the number of iterations required to produce the numerical solution. • An intermediate loop performs the calculations through nsteps integration steps of length h; the iteration counter is initialized for each integration step and a variable is initialized, which will indicate when the Newton iterations are stopped:
Copyright 2004 by Chapman & Hall/CRC
% % % %
nsteps steps/output for is=1:nsteps Initialize iteration counter niter=1; stop=0;
• An inner loop then performs the Newton iterations while nstop= 0; first the vector of functions to be zeroed is computed according to Equation C.12 (with ν = 1): % % % %
Test for the end of the current step while stop==0 Functions g1, g2 g=[h*(-a*y(1)+b*y(2))-y(1)+y10 h*( b*y(1)-a*y(2))-y(2)+y20];
• The linear Newton Equations C.3 are then solved for the Newton corrections: % % % % % % %
Solve for Newton corrections Gaussian elimination dy=-J\g; Jacobian inverse dy=-J^(-1)*g;
Note that two methods of solution are programmed: — Gaussian elimination, which is the preferred method of solution — Inverse Jacobian matrix, i.e., δy = −J−1 g(y)
(C.15)
Although Equation C.15 is formally (mathematically) correct, it is not used in practice because computing the inverse Jacobian matrix is inefficient; rather, some form of Gaussian elimination is generally used. Note, however, how easily either method is programmed in MATLAB; this is due to the facility of MATLAB to handle matrices (arrays) without subscripting, plus the definition of basic matrix operations, e.g., multiplication, inverse, Gaussian reduction. This step (for computing the Newton corrections) will fail if the Jacobian matrix is singular or near singular (i.e., ill-conditioned). Thus, the calculation of the condition of J at
Copyright 2004 by Chapman & Hall/CRC
this point would be a good idea. MATLAB has a utility for calculating the condition of a matrix which can then be compared with the machine epsilon discussed in Section 1.7; specifically, if the condition number exceeds the reciprocal of the machine epsilon, the linear algebraic system (in this case, Equation C.3) is numerically singular. • The Newton corrections are then applied to the current solution vector to produce (one hopes) an improved solution: % %
Update solution for ic=1:neqn y(ic)=y(ic)+dy(ic); end stop=1;
If the Newton corrections are small enough (to be tested next), the iterations are terminated by setting stop= 1. • Each Newton correction is tested against the convergence tolerance: % % %
% % %
Check if the corrections are within the tolerance eps for ic=1:neqn if abs(dy(ic))>eps Convergence not achieved; continue calculation niter=niter+1; stop=0; break; end end
If any Newton correction exceeds the tolerance, the iteration counter is incremented, the iterations are continued (stop = 0) and the testing is ended (break from the for loop) • If the maximum number of iterations is reached, the iterations are stopped. Otherwise, if the iterations are to be continued (stop = 0), the next pass through the while loop (based on stop == 0) is initiated % % % %
If maximum iterations reached, accept current step if (niter==maxiter) stop=1; end Continue integration step end
Copyright 2004 by Chapman & Hall/CRC
• Convergence has been achieved (stop = 1) so the new solution is now used as the old (base) solution in the next step of the ODE integration; the independent variable is incremented for the next step along the solution: % %
Integration step completed y10=y(1); y20=y(2); t=t+h;
• Finally, the nstep integration steps are completed, and the next output interval is covered until all nout output intervals are completed. % %
Continue nstep integration steps end
% % Continue integration for next output interval end
Note that Program C.1 contains a general Newton solver that can be applied to an nxn system of nonlinear equations; all that really is required is to reprogram: (1) the Jacobian matrix (which generally will be inside the loop for the Newton iterations rather than outside) and (2) the vector of functions to be zeroed. Also, some tuning of the parameters will generally be required (e.g., the tolerance eps and the maximum number of iterations maxiter). The output from the preceding program is as follows: t dy(1) dy(2) y(1) y(2) 0.00000 0.00000 0.00000 0.00000 2.00000 1.00000 0.00000 0.00000 0.36971 0.36971 2.00000 0.00000 0.00000 0.13669 0.13669 3.00000 0.00000 0.00000 0.05053 0.05053 4.00000 0.00000 0.00000 0.01868 0.01868 5.00000 0.00000 0.00000 0.00691 0.00691 6.00000 0.00000 0.00000 0.00255 0.00255 7.00000 -0.00001 -0.00001 0.00094 0.00094 8.00000 0.00000 0.00000 0.00035 0.00035 9.00000 0.00000 0.00000 0.00013 0.00013 10.00000 0.00000 0.00000 0.00005 0.00005
erry(1) 0.00000 -0.00183 -0.00135 -0.00075 -0.00037 -0.00017 -0.00007 -0.00003 -0.00001 -0.00001 0.00000
erry(2) iter 0.00000 0 -0.00183 2 -0.00135 2 -0.00075 2 -0.00037 2 -0.00017 2 -0.00007 2 -0.00003 1 -0.00001 1 -0.00001 1 0.00000 1
We can note the following points about this example: • Only 1000 implicit Euler steps were used. This contrasts with the 5 × 106 steps estimated for the explicit Euler method in Section 1.7; thus there was a reduction of 1/5000 in the number of steps required by an explicit integrator, which clearly shows the advantage of using an implicit integrator for stiff ODEs. Again, as discussed in Section 1.7, if this
Copyright 2004 by Chapman & Hall/CRC
conclusion is not convincing, using a = 500,000,000.5, b = 499,999,999.5 would result in a reduction of 1/5 × 106 steps! • The two Newton corrections, δy1 and δy2 , met the tolerance eps= 0.00001 with no more than two iterations, which is an indication of the quadratic converegnce of Newton’s method, i.e., when Newton’s method works, it generally is very efficient. • Accuracy (not stability) was limited by the step size h = 0.01; this suggests a higher-order BDF method could be used to good advantage (to increase the accuracy, while maintaining stability). Specifically, the BDF methods are stable along the entire negative real axis, and therefore would be stable for the 2 × 2 linear problem of Equations C.9 (since the two eigenvalues are real and negative, e.g., λ1 = −(a − b) = −1, λ2 = −(a + b) = −1,000,000). An extension of Program C.1 for BDF methods of order 2 and 3 is available from the authors (W.E.S.). To investigate this last point (the possible advantage of using higher-order BDF methods), we now consider the use of a stiff ODE integrator in MATLAB, which varies the order of the BDF method.
C.3
MATLAB Program for the Solution of the 2x2 ODE System Using ode23s and ode15s
A MATLAB program that calls ode23s and ode15s for the solution of the 2x2 ODE system of Equations C.9 is listed below: % % 2 x 2 ODE system by variable order BDF % % Global variables global a b ncall; % % Model parameters a=500000.5; b=499999.5; % % Select method for mf=1:2 % % Error tolerances reltol=1.0e-02; abstol=1.0e-02; %
Copyright 2004 by Chapman & Hall/CRC
% Cases for changes in error tolerances for ncase=1:2 reltol=1.0e-02*reltol; abstol=1.0e-02*abstol; % % Variables for ODE integration t0=0.0; tf=10.0; tout=[t0:1.0:tf]'; nout=11; % % Initialize number of calls to derivative % subroutine ncall=0; % % Initial condition y10=0.0; y20=2.0; y0=[y10 y20]'; % % Call ODE integrator options=odeset('RelTol',reltol,'AbsTol',abstol); if(mf==1)[t,y]=ode23s('ode2x2',tout,y0,options); end if(mf==2)[t,y]=ode15s('ode2x2',tout,y0,options); end % % Display solution and error fprintf('\n\n mf = %1d\n case = %1d \n reltol = %6.2e\n abstol = %6.2e\n\n',... mf,ncase,reltol,abstol); fprintf(' t y1e y1 erry1 \n y2e y2 erry2\n'); for i=1:nout lambda1=-(a-b); lambda2=-(a+b); exp1=exp(lambda1*t(i)); exp2=exp(lambda2*t(i)); y1e=(y10+y20)/2.0*exp1-(y20-y10)/2.0*exp2; y2e=(y10+y20)/2.0*exp1+(y20-y10)/2.0*exp2; erry1=y1e-y(i,1); erry2=y2e-y(i,2); fprintf('%5.1f%9.4f%9.4f%15.10f\n %9.4f%9.4f%15.10f\n\n',... t(i),y1e,y(i,1),erry1,y2e,y(i,2),erry2); end %
Copyright 2004 by Chapman & Hall/CRC
% Next case fprintf(' ncall = %5d\n',ncall); end % % Next method end % % Plot last solution plot(t,y); xlabel('t') ylabel('y1(t),y2(t)') title(' Appendix C, 2 x 2 Linear System') gtext('y1(t)'); gtext('y2(t)'); print appc.ps
Program C.2 Solution of Equations C.9 by ode23s and ode15s We can note the following points about Program C.2: • The beginning of the program is similar to that of Program C.1; three variables are declared global so that they can be shared with other routines: % % 2 x 2 ODE system by variable order BDF % % Global variables global a b ncall; % % Model parameters a=500000.5; b=499999.5;
• An outer loop executes two times; for the first (m f = 1), ode23s is called, while for the second, ode15s is called: % % Select method for mf=1:2 % % Error tolerances reltol=1.0e-02; abstol=1.0e-02;
For each of the two passes through this for loop, the relative and absolute error tolerances for the two ODE integrators are set to 10−2 .
Copyright 2004 by Chapman & Hall/CRC
• In a subordinate loop, the error tolerances are reduced by 10−2 (so that for the two passes through this loop, the error tolerances are set to 10−4 and 10−6 ); in this way, the performance of the integrators can be assessed using the exact solution of the 2x2 ODE problem, Equation 1.17: % % Cases for changes in error tolerances for ncase=1:2 reltol=1.0e-02*reltol; abstol=1.0e-02*abstol;
• The variables controlling the integration are set: % %
Variables for ODE integration t0=0.0; tf=10.0; tout=[t0:1.0:tf]'; nout=11;
Note that the solution is computed to a final time t f = 10 with 11 outputs at an interval of 1. • A counter for the number of calls to the derivative routine (discussed subsequently) is initialized; also, the initial conditions for the two ODEs (Equations C.9) are set (these are a 2 × 1 column vector, which is defined as the transpose of a 1 × 2 row vector): % % % % %
Initialize number of calls to derivative subroutine ncall=0; Initial condition y10=0.0; y20=2.0; y0=[y10 y20]';
• The MATLAB utility odeset is called to set the error tolerances for the subsequent calls to ode23s and ode15s: % %
Call ODE integrator options=odeset('RelTol',reltol,'AbsTol',abstol); if(mf==1)[t,y]=ode23s('ode2x2',tout,y0,options); end if(mf==2)[t,y]=ode15s('ode2x2',tout,y0,options); end
Note that the two integrators call the function ode2x2 to define the derivatives (RHS functions) of Equations C.9. Also, the coding to call ode23s and ode15s is straightforward (which facilitates their use).
Copyright 2004 by Chapman & Hall/CRC
• The numerical and exact solutions, and their differences, are displayed in the same way as Program C.1: % %
Display solution and error fprintf('\n\n mf = %1d\n case = %1d \n reltol = %6.2e\n abstol = %6.2e\n\n', ...mf,ncase,reltol,abstol); fprintf(' t y1e y1 erry1 \n y2e y2 erry2\n'); for i=1:nout lambda1=-(a-b); lambda2=-(a+b); exp1=exp(lambda1*t(i)); exp2=exp(lambda2*t(i)); y1e=(y10+y20)/2.0*exp1-(y20-y10)/2.0*exp2; y2e=(y10+y20)/2.0*exp1+(y20-y10)/2.0*exp2; erry1=y1e-y(i,1); erry2=y2e-y(i,2); fprintf('%5.1f%9.4f%9.4f%15.10f\n %9.4f%9.4f%15.10f\n\n',... t(i),y1e,y(i,1),erry1,y2e,y(i,2),erry2); end
% % Next case fprintf(' ncall = %5d\n',ncall); end % % Next method end
Also, the number of calls to the derivative routine ode2x2 is displayed at the end of the solution. In this way, we can compare the number of derivative evaluations for the two integrators, and the fixed step integrator of Program C.1. • Plotting is added at the end to display the solutions from ode15s (which, graphically, is identical to the solution from ode23s): % % Plot last solution plot(t,y); xlabel('t') ylabel('y1(t),y2(t)') title(' Appendix C, 2 x 2 Linear System')
Copyright 2004 by Chapman & Hall/CRC
gtext('y1(t)'); gtext('y2(t)'); print appc.ps
Function ode2x2 to define the RHS functions of the ODEs (Equations C.9) is listed below: function yt=ode2x2(t,y) % % Global variables global a b ncall; % % ODEs yt(1)=-a*y(1)+b*y(2); yt(2)= b*y(1)-a*y(2); yt=yt'; % % Increment number of calls to ode2x2 ncall=ncall+1;
Program C.3 Function ode2x2.m called by Program C.2 We can note the following points about ode2x2: • The global variables are available for use in calculating the derivatives; also, the counter for derivative evaluations is incremented by 1 each time ode2x2 is called. • The derivative row vector is transposed into a column vector, which is the required format for ode23s and ode15s. The output from Programs C.2 and C.3 is listed in abbreviated form below (the output for t = 2, 3, . . . , 9 is deleted to reduce the output to reasonable length): mf = 1 case = 1 reltol = 1.00e-004 abstol = 1.00e-004 t 0.0
1.0
y1e y2e 0.0000 2.0000
y1 y2 0.0000 2.0000
erry1 erry2 0.0000000000 0.0000000000
0.3679 0.3679
0.3684 0.3684
-0.0004764099 -0.0004764099
Copyright 2004 by Chapman & Hall/CRC
10.0
. . . 0.0000 0.0000
ncall =
0.0000 0.0000
. . . -0.0000030593 -0.0000030594
324
mf = 1 case = 2 reltol = 1.00e-006 abstol = 1.00e-006 t 0.0
1.0
10.0
y1e y2e 0.0000 2.0000
y1 y2 0.0000 2.0000
erry1 erry2 0.0000000000 0.0000000000
0.3679 0.3679 . . . 0.0000 0.0000
0.3678 0.3678
0.0000421012 0.0000420695 . . . -0.0000017745 -0.0000017747
ncall =
0.0000 0.0000
1591
mf = 2 case = 1 reltol = 1.00e-004 abstol = 1.00e-004 t 0.0
1.0
y1e y2e 0.0000 2.0000
y1 y2 0.0000 2.0000
erry1 erry2 0.0000000000 0.0000000000
0.3679 0.3679 . . .
0.3680 0.3680
-0.0001104769 -0.0001104769 . . .
Copyright 2004 by Chapman & Hall/CRC
10.0
0.0000 0.0000
ncall =
0.0000 0.0000
0.0000057390 0.0000057390
139
mf = 2 case = 2 reltol = 1.00e-006 abstol = 1.00e-006 t 0.0
1.0
10.0
y1e y2e 0.0000 2.0000
y1 y2 0.0000 2.0000
erry1 erry2 0.0000000000 0.0000000000
0.3679 0.3679 . . . 0.0000 0.0000
0.3679 0.3679
0.0000002033 0.0000002033 . . . -0.0000002999 -0.0000002999
ncall =
0.0000 0.0000
233
We can note the following points about this output: • ode23s did not meet the error tolerances, even with 1591 calls to ode2x2, e.g., 1.00e—004 vs. −0.0004764099: mf = 1 case = 1 reltol = 1.00e-004 abstol = 1.00e-004 t 1.0
y1e y2e 0.3679 0.3679
ncall =
mf = 1 case = 2
Copyright 2004 by Chapman & Hall/CRC
324
y1 y2 0.3684 0.3684
erry1 erry2 -0.0004764099 -0.0004764099
reltol = 1.00e-006 abstol = 1.00e-006 t 1.0
y1e y2e 0.3679 0.3679
ncall =
y1 y2 0.3678 0.3678
erry1 erry2 0.0000421012 0.0000420695
1591
However, we should keep in mind that Program C.1 did not produce solutions that were any better than about 2+ figures, e.g., −0.00183, with 1000 derivative evaluations (due to the use of a fixed step, first-order BDF): t dy(1) dy(2) y(1) y(2) erry(1) erry(2) iter 1.00000 0.00000 0.00000 0.36971 0.36971 -0.00183 -0.00183 2
• ode15s did come close to meeting, or exceeded, the error tolerances, e.g., 1.00e − 004 vs. −0.0001104769: mf = 2 case = 1 reltol = 1.00e-004 abstol = 1.00e-004 t 1.0
y1e y2e 0.3679 0.3679
ncall =
y1 y2 0.3680 0.3680
erry1 erry2 -0.0001104769 -0.0001104769
139
mf = 2 case = 2 reltol = 1.00e-006 abstol = 1.00e-006 t 1.0
y1e y2e 0.3679 0.3679
ncall =
Copyright 2004 by Chapman & Hall/CRC
233
y1 y2 0.3679 0.3679
erry1 erry2 0.0000002033 0.0000002033
• The number of derivative evaluations by ode15s was substantially lower than for ode23s, which infers better performance of the higher-order methods in ode15s. For ode23s: mf = 1 case = 1 reltol = 1.00e-004 abstol = 1.00e-004 ncall =
324
mf = 1 case = 2 reltol = 1.00e-006 abstol = 1.00e-006 ncall =
1591
and for ode15s: mf = 2 case = 1 reltol = 1.00e-004 abstol = 1.00e-004 ncall =
139
mf = 2 case = 2 reltol = 1.00e-006 abstol = 1.00e-006 ncall =
233
ode23s and ode15s vary the order of the BDF method (in accordance with the table of coefficients in Section 1.7) as well as the integration step in attempting to meet the specified error tolerance. Thus, they are variable step–variable order implementations of the BDF method; i.e., they perform h refinement and p refinement simultaneously. To conclude this appendix, we have observed the effectiveness (superior efficiency) of implicit methods, such as BDF, for stiff ODE problems. This improved performance, however, involves greater computational complexity (generally the solution of linear or nonlinear algebraic or transcendental equations (Equations C.2), depending on whether the ODE system is linear
Copyright 2004 by Chapman & Hall/CRC
or nonlinear); therefore implicit methods should be used only if the ODE system is stiff. Also, we should not conclude that a nonlinear ODE system is necessarily stiff, and therefore an implicit integrator is required. This final discussion suggests a fundamental question: “How do we know if an ODE system is stiff and therefore an implicit integrator should be used?” In the case of linear ODE systems, we can look at the spread in the eigenvalues, as we did for Equations C.9. However, in the case of nonlinear ODEs, eigenvalues are not defined (and therefore cannot be studied for possible stiffness). For this more general case (of nonlinear ODEs), we suggest the following criterion for determining if an implicit integrator should be used: maximum stable step << problem timescale In other words, observe if the ODE problem timescale is much greater than the largest integration step (h) that can be taken while still maintaining a stable solution, i.e., this suggests that stability is the limiting condition on h, and therefore the ODE system is effectively stiff so that an implicit integrator should be used. To illustrate the application of this criterion, for the 2x2 ODE system of Equations C.9 with a = 500000.5, b = 499999.5, and λ1 = −(a − b) = −1, λ2 = −(a + b) = −1,000,000, we found in Section 1.7 that (1) the maximum step for a stable explicit solution is 2/1,000,000, and (2) the timescale for the ODE system is 10 (so that e −1(10) has decayed to insignificance). Thus, application of the preceding criterion gives 2 << 10 1, 000, 000 which implies that an implicit integrator should be used. To confirm the preceding analysis, Programs c.2 and c.3 were executed with the two MATLAB explicit integrators, ode23 and ode45. This was easily accomplished by changing the following lines: % %
Call ODE integrator options=odeset('RelTol',reltol,'AbsTol',abstol); if(mf==1)[t,y]=ode23s('ode2x2',tout,y0,options); end if(mf==2)[t,y]=ode15s('ode2x2',tout,y0,options); end
to % %
Call ODE integrator options=odeset('RelTol',reltol,'AbsTol',abstol); if(mf==1)[t,y]=ode23('ode2x2',tout,y0,options); end if(mf==2)[t,y]=ode45('ode2x2',tout,y0,options); end
The resulting change in the numbers of calls to the ODE routine ode2x2 is summarized below:
Copyright 2004 by Chapman & Hall/CRC
Integrator
Tolerance
Calls to ode2x2
ode23s ode23 ode23s ode23 ode15s ode45 ode15s ode45
−4
324 11, 939, 197 1591 11, 939, 440 139 19, 283, 629 233 19, 283, 713
10 10−4 10−6 10−6 10−4 10−4 10−6 10−6
Clearly, the MATLAB stiff integrators, and even the basic BDF integrator of Program C.1, are substantially more efficient than the MATLAB explicit (nonstiff) integrators (and the same conclusion would be true for the explicit integrators discussed in Chapters 1 and 2). However, we now have two additional questions to answer in applying the preceding criterion for stiffness (involving the maximum integration step and problem timescale): 1. How do we determine the maximum integration step for an explicit integrator that still produces a stable solution? Answer: In general, by trial and error using a computer program with an explicit integrator. Or, if the computer run times for a stable explicit solution are large, or an excessive number of derivative evaluations is required to maintain a stable solution, an implicit integrator may possibly be used to good advantage. 2. How do we determine the timescale for the ODE problem? Answer: Either from some knowledge of the characteristics of the problem such as physical reasoning, or again, by trial and error to observe when a complete solution appears to have been computed. In other words, some trial and error with an explicit integrator is generally required. If the computational effort required to compute a complete solution appears to be excessive, switch to an implicit integrator. Admittedly, this procedure is rather vague (a general, easily applied mathematical test is not available, especially for nonlinear problems), and some trial and error with explicit integrators first, followed possibly by a switch to implicit integrators, may be required (this is the procedure we generally follow for a new ODE problem). The preceding discussion (in this appendix and Chapters 1 to 5) indicates that a spectrum of ODE/PDE problems can be handled with explicit integrators (e.g., the 1x1 and 2x2 ODE problems, and the linear and nonlinear PDE problems), and that only under the condition of stiffness (or stability constraints) is an implicit integrator required. Thus, some judgment based on direct computational experience is required.
Copyright 2004 by Chapman & Hall/CRC
Appendix D Alternative Forms of ODEs
The ODE systems considered previously (in Chapters 1 to 5 and Appendices A to C) all defined the derivatives (RHS of the ODEs) explicitly; that is, only one derivative appeared in each ODE; Equations 1.6 for the 2x2 linear, constant coefficient ODE system are an example (renumbered here as Equations D.1) dy1 = a 11 y1 + a 12 y2 dt dy2 = a 21 y1 + a 22 y2 dt
y1 (0) = y10 (D.1) y2 (0) = y20
Equations D.1 are an example of explicit ODEs (not to be confused with explicit ODE integration algorithms); this designation comes from the characteristic that each ODE defines one derivative explicitly. However, we could consider ODEs in which each ODE contains more than one derivative, e.g., dy1 dy2 + c 12 = −a y1 + by2 dt dt dy1 dy2 c 21 + c 22 = by1 − a y2 dt dt y1 (0) = y10 , y2 (0) = y20
c 11
or in matrix form
c 11 c 21
c 12 c 22
dy1 dt = −a · dy2 b dt
Copyright 2004 by Chapman & Hall/CRC
y y1 (0) = 10 y2 (0) y20
b −a
y · 1 y2
(D.2)
For the special case c 11 = 1, c 12 = 0, c 21 = 0, c 22 = 1, is the explicit ODE system (not to be confused with an explicit integrator): dy1 = −a y1 + by2 dt dy2 = by1 − a y2 dt y1 (0) = y10 , y2 (0) = y20
(D.3)
which can be written in matrix form (for the general nxn case) as I
dy = Ay dt
(D.4)
where I = identity matrix A = ODE coefficient matrix y = dependent variable vector Equations D.2 are an example of a linearly coupled implicit ODE system, with the coupling matrix M dy M = Ay (D.5) dt The term linearly coupled comes from the linear coupling (or linear combinations) of the derivatives on the LHS of Equations D.2. This form of coupled ODEs is common in applications, and therefore library integrators are available to handle such systems. For example, the MATLAB ODE integrators can accept a coupling matrix that is not the identity matrix; L SODI ,1,4 DASSL,2,4,5 RADAU5,3,5 and MEBDFDAE5 can also accommodate such coupled ODE systems. One approach to the solution of Equations D.2 would be to consider them as linear algebraic equations in the derivatives dy1 /dt and dy2 /dt. Then these equations can be solved in the usual ways for linear algebraic equations to arrive at the derivatives explicitly. For example, eliminating dy1 /dt from Equations D.2 gives dy2 c 21 a y1 − c 21 by2 + c 11 by1 − c 11 a y2 = dt c 11 c 22 − c 21 c 12
(D.6)
Higher-order (nxn) linearly coupled ODE systems can be uncoupled by Gaussian elimination or any other established method for simultaneous algebraic equations. Of course, this presupposes that the coupling matrix M is not sin 0 in Equation D.6). If this gular or ill-conditioned (e.g., that c 11 c 22 − c 21 c 12 = is the case, which is common in applications, more sophisticated methods must be applied to perform the numerical integration of the equations, as subsequently discussed briefly.
Copyright 2004 by Chapman & Hall/CRC
For example, if in Equations D.2 c 11 = c 12 = 1, c 21 = c 22 = 0, the coupling matrix
c 11 c 12 (D.7) c 21 c 22 is singular. Note that the second ODE is actually an algebraic equation, i.e., dy1 dy2 + = −a y1 + by2 dt dt dy1 dy2 0 +0 = by1 − a y2 dt dt
(D.8)
Thus, Equations D.8 are actually an example of a differential algebraic or DAE system. Equation D.5 can be generalized to M(y)
dy = Ay dt
(D.9)
where now the coupling matrix M(y) is a function of y; thus, Equation D.9 defines a nonlinearly coupled implicit ODE system. Finally, if the ODE system is of the form dy ,t =0 f y, dt
(D.10)
Equation D.10 is a fully implicit ODE system; it is also frequently designated as a DAE system since some of the equations defined by f can be algebraic. All of the preceding ODEs are a special case of Equation D.10 (depending on the form of f in Equation D.10). Library integrators are available, in principle, for all of the preceding forms of ODE systems. In particular, DASSL 2,4 and RADAU53 will accommodate Equations D.10 for certain cases of coupling between the ODEs and algebraic equations. The solution of Equation D.10 for completely general forms of f is still an open and active area of research. In summary, we list these alternate forms of ODEs (beyond the explicit ODEs illustrated by Equations D.4 to indicate that (1) all of these forms occur in applications in science and engineering, and (2) library solvers are available for most of these forms (but without a guarantee for the successful calculation of an accurate solution, especially for Equation D.10, depending on the form of f). A detailed discussion of these alternate ODE forms and the available solvers is beyond the scope of this book. However, the following mathematical software libraries are a good starting point and source of solvers: netlib,4 gams,4 and mebdfdae.5
Copyright 2004 by Chapman & Hall/CRC
References 1. Hindmarsh, A.C., ODEPACK, a systematized collection of ODE solvers, in Scientific Computing, R.S. Stepleman et al., Eds., North-Holland, Amsterdam, 1983, 55–64. 2. Brenan, K.E., S.L. Campbell, and L.R. Petzold, Numerical Solution of Initial-Value Problems in Differential-Algebraic Equations, SIAM, Philadelphia, 1996. 3. Hairer, E., and G. Wanner, Solving Ordinary Differential Equations II: Stiff and Differential-Algebraic Problems, Springer-Verlag, Berlin, 1991. 4. Library mathematical software is available from http://www.netlib.org/index.html; http://gams.nist.gov. 5. Library ODE/DAE integrators are available from http://hilbert.dm. uniba.it/˜ testset/software.htm
Copyright 2004 by Chapman & Hall/CRC
Appendix E Spatial p Refinement
In Section 4.1 and Section 5.1 we considered the numerical integration of a PDE (Equation 4.1) in which the finite difference approximation of the second-order spatial derivative ∂u2 /∂ x 2 was programed in derv. Since firstand second-order derivatives in space, with their associated boundary conditions, are so commonplace in applications, the calculation of these derivatives can be facilitated by using library routines. We consider here a few library routines for this purpose. For example, a derv is listed below that can be used in place of the derv in Section 4.1 (Program 4.1.3):
function [ut]=derv(n,t,u) % % Function derv computes the derivative vector % of the linear PDE problem % % Declare global variables global nsteps ndss; % % Problem parameters xl=0.0; xu=1.0; % % BC at x = 0 u(1)=0.0; % % BC at x = 1 u(n)=0.0; %
Copyright 2004 by Chapman & Hall/CRC
% ux if ndss == 2 [ux]=dss002(xl,xu,n,u); end if ndss == 4 [ux]=dss004(xl,xu,n,u); end % % uxx if ndss == 2 [uxx]=dss002(xl,xu,n,ux); end if ndss == 4 [uxx]=dss004(xl,xu,n,ux); end if ndss == 42 nl=1; nu=1; ux=zeros(n,1); [uxx]=dss042(xl,xu,n,u,ux,nl,nu); end if ndss == 44 nl=1; nu=1; ux=zeros(n,1); [uxx]=dss044(xl,xu,n,u,ux,nl,nu); end % % pdelin for i=1:n ut(i)=uxx(i); end
Program E.1 derv for the solution of Equations 4.1 to 4.4 We can note the following points about derv: • After setting the boundary values in x, xl = 0.0, xu = 1.0, boundary conditions (Equations 4.3 and 4.4) are programmed: % % BC at x = 0 u(1)=0.0; % % BC at x = 1 u(n)=0.0;
Note that here we have zeroed the dependent variables, u(1) and u(n), rather than the time derivatives, ut(1) and ut(n) as in Program 4.1.3. This difference is due to the way the PDE, Equation 4.1, is programmed at the end of derv, as explained below. • The first derivative ∂u/∂ x is computed by one of two spatial differentiation (DSS) routines, dss002 or dss004:
Copyright 2004 by Chapman & Hall/CRC
% % ux if ndss == 2 [ux]=dss002(xl,xu,n,u); end if ndss == 4 [ux]=dss004(xl,xu,n,u); end
dss002 implements three point (O(x 2 )) finite difference approximations for ∂u/∂ x whereas dss004 implements five point (O(x 4 )) approximations for this derivative. The vector of dependent variables to be differentiated, u, is an input to dss002 and dss004, and ∂u/∂ x is returned in the vector ux. The choice of the differentiator is through ndss, which is set in intpar (ndss is added as another parameter to intpar of Program 4.1.1, with ndss = 2 to call dss002 and ndss = 4 to call dss004). • The second derivative, ∂ 2 u/∂ x 2 , is then computed by differentiating the first derivative ∂u/∂ x (i.e., by stagewise differentiation): % % uxx if ndss == 2 [uxx]=dss002(xl,xu,n,ux); end if ndss == 4 [uxx]=dss004(xl,xu,n,ux); end
The second derivative is returned in vector uxx. • For ndss = 42 or ndss = 44, two differentiation routines, dss042 and dss044, are called that calculate the second derivative, ∂ 2 u/∂ x 2 , directly: if ndss == 42 nl=1; nu=1; ux=zeros(n,1); [uxx]=dss042(xl,xu,n,u,ux,nl,nu); end if ndss == 44 nl=1; nu=1; ux=zeros(n,1); [uxx]=dss044(xl,xu,n,u,ux,nl,nu); end
nl = 1 and nu = 1 specify that Dirichlet boundary conditions are used (according to Equations 4.3 and 4.4); if nl = 2 and/or nu = 2, Neumann boundary conditions are used. For the latter, the first derivative, ux, is required and it is therefore an input to dss042 and dss044. In the present case (with Dirichlet boundary conditions), the first derivative is not required and it is therefore zeroed.
Copyright 2004 by Chapman & Hall/CRC
• The finite difference approximations in dss042 and dss044 are O(x 2 ) and O(x 4 ), respectively. For example, for dss042, the calculation of the second derivative ∂ 2 u/∂ x 2 is done with the following code (taken from dss042): %... %... %... %...
%... %...
%... %...
%... %...
%... %...
Grid spacing dx=(xu-xl)/(n-1); Calculate uxx at the left boundary, without ux if nl==1 uxx(1)=(( 2.)*u( 1)... +( -5.)*u( 2)... +( 4.)*u( 3)... +( -1.)*u( 4))/(dx^2); Calculate uxx at the left boundary, including ux elseif nl==2 uxx(1)=(( -7.)*u( 1)... +( 8.)*u( 2)... +( -1.)*u( 3))/(2.*dx^2)... +( -6.)*ux( 1) /(2.*dx); end Calculate uxx at the right boundary, without ux if nu==1 uxx(n)=(( 2.)*u(n )... +( -5.)*u(n-1)... +( 4.)*u(n-2)... +( -1.)*u(n-3))/(dx^2); Calculate uxx at the right boundary, including ux elseif nu==2 uxx(n)=(( -7.)*u(n )... +( 8.)*u(n-1)... +( -1.)*u(n-2))/(2.*dx^2)... +( 6.)*ux(n ) /(2.*dx); end Calculate uxx at the interior grid points for i=2:n-1 uxx(i)=(u(i+1)-2.*u(i)+u(i-1))/dx^2; end
This code involves several finite difference approximations at the boundaries for Dirichlet and Neumann boundary conditions. To keep this
Copyright 2004 by Chapman & Hall/CRC
discussion to reasonable length, the details are not discussed (they are available in Reference 1). However, the calculation of the second derivative at the interior points by %... %...
Calculate uxx at the interior grid points for i=2:n-1 uxx(i)=(u(i+1)-2.*u(i)+u(i-1))/dx^2; end
directly parallels the coding in derv of Program 4.1.3: % % Interior points dx=(xu-xl)/(n-1); dxs=dx*dx; for i=2:n-1 ut(i)=(u(i+1)-2.0*u(i)+u(i-1))/dxs; end
• The corresponding code in dss044 for the second derivative at the interior points is %... %...
i = 3, 4,..., n-2 for i=3:n-2 uxx(i)=r12dxs*... ( -1.0*u(i-2)... +16.0*u(i-1)... -30.0*u(i )... +16.0*u(i+1)... -1.0*u(i+2));
Note that five points (or values of u) are used to calculate the second derivative, while in dss042, only three points are used. This explains the greater accuracy for dss044 (O(x 4 ) for dss044 and O(x 2 ) for dss042). Again, the details of the finite difference approximations in dss042 and dss044 are given in Reference 1. • Finally, the PDE, Equation 4.1 is programmed: % % pdelin for i=1:n ut(i)=uxx(i); end
Copyright 2004 by Chapman & Hall/CRC
The close correspondence between this coding and the PDE is clear; this is possible through the use of the vector uxx computed by the preceding differentiation (DSS) routines. Functions inital of Program 4.1.2 and fprint of Program 4.1.4 are essentially the same (the value of ndss is printed in fprint). The output from this combination of functions, plus a main program that is also the same as Program 3.1.1, is discussed below. Since this output is quite voluminous, only a summary with key points is given. For ndss = 2, n = 21, the output is as follows: euler2a integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
1
neqn = 21
u(num) 1.000000 0.141180 0.019932 0.002814 0.000397 0.000056
nsteps = 250
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
ndss =
2
diff 0.0000e+000 2.2689e-003 6.3550e-004 1.3350e-004 2.4930e-005 4.3644e-006
euler2b integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
2
neqn = 21
u(num) 1.000000 0.141232 0.019940 0.002817 0.000398 0.000056
nsteps = 250
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
ndss =
2
diff 0.0000e+000 2.3208e-003 6.4378e-004 1.3634e-004 2.5813e-005 4.5242e-006
rkc4a integrator ncase = t 0.00 0.20 0.40
3
neqn = 21
u(num) 1.000000 0.141177 0.019931
Copyright 2004 by Chapman & Hall/CRC
nsteps = 250
u(exact) 1.000000 0.138911 0.019296
ndss =
diff 0.0000e+000 2.2661e-003 6.3470e-004
2
0.60 0.80 1.00
0.002814 0.000397 0.000056
0.002680 0.000372 0.000052
1.3333e-004 2.4898e-005 4.3588e-006
rkc4b integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
4
neqn = 21
u(num) 1.000000 0.141177 0.019931 0.002814 0.000397 0.000056
nsteps = 250
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
ndss =
2
diff 0.0000e+000 2.2661e-003 6.3471e-004 1.3333e-004 2.4898e-005 4.3589e-006
rkf45a integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
5
neqn = 21
u(num) 1.000000 0.141177 0.019931 0.002814 0.000397 0.000056
nsteps = 250
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
ndss =
2
diff 0.0000e+000 2.2661e-003 6.3470e-004 1.3333e-004 2.4898e-005 4.3588e-006
rkf45b integrator ncase = t 0.00 0.20 0.40 0.60 0.80 1.00
6
neqn = 21
u(num) 1.000000 0.141177 0.019931 0.002814 0.000397 0.000056
nsteps = 250
u(exact) 1.000000 0.138911 0.019296 0.002680 0.000372 0.000052
ndss =
2
diff 0.0000e+000 2.2661e-003 6.3470e-004 1.3333e-004 2.4898e-005 4.3587e-006
For this case (n = 21), the errors are relatively large, e.g., for rkf45a at t = 0.20 the error at x = 0.5 is 2.2661e–003:
Copyright 2004 by Chapman & Hall/CRC
rkf45a integrator ncase = t 0.00 0.20
5
neqn = 21
u(num) 1.000000 0.141177
nsteps = 250
u(exact) 1.000000 0.138911
ndss =
2
diff 0.0000e+000 2.2661e-003
Thus, we would expect that as more points are added (h refinement) and a higher-order spatial differentiator is used (dss044 in place of dss042), which is a form of p refinement in space, the accuracy of the solution would improve. This improvement is demonstrated in the following summary of results from rkf45a at t = 0.2: ncase = 5 neqn = 21 nsteps = 250 ndss = 0.20 0.141177 0.138911 2.2661e-003
2
ncase = 5 neqn = 31 nsteps = 250 ndss = 0.20 0.139828 0.138911 9.1682e-004
2
ncase = 5 neqn = 41 nsteps = 250 ndss = 0.20 0.139476 0.138911 5.6448e-004
2
ncase = 5 neqn = 21 nsteps = 500 ndss = 0.20 0.138921 0.138911 9.6684e-006
4
ncase = 5 neqn = 31 nsteps = 500 ndss = 0.20 0.138913 0.138911 2.1371e-006
4
ncase = 5 neqn = 41 nsteps = 500 ndss = 0.20 0.138912 0.138911 6.5075e-007
4
ncase = 5 neqn = 21 nsteps = 500 ndss = 42 0.20 0.139476 0.138911 5.6448e-004 ncase = 5 neqn = 31 nsteps = 500 ndss = 42 0.20 0.139162 0.138911 2.5071e-004 ncase = 5 neqn = 41 nsteps = 1000 ndss = 42 0.20 0.139052 0.138911 1.4099e-004 ncase = 5 neqn = 21 nsteps = 500 ndss = 44 0.20 0.138913 0.138911 1.6930e-006
Copyright 2004 by Chapman & Hall/CRC
ncase = 5 neqn = 31 nsteps = 500 ndss = 44 0.20 0.138911 0.138911 3.5667e-007 ncase = 5 neqn = 41 nsteps = 1000 ndss = 44 0.20 0.138911 0.138911 1.1461e-007
We can note the following points about this output: • The number of steps in t to maintain stability changed through the values 250, 500, 1000. Generally this number had to be increased with increasing numbers of grid points, neqn (as h refinement was used), and increasing order of the finite difference approximations (as p refinement was used). • Generally, the order conditions were maintained when changing the number of grid points, neqn. For example, with ndss = 44 (dss044 was called), O(x 4 ), and comparing the neqn = 21 and neqn = 41 solutions, 1.6930 × 10−6 (1/2)4 = 1.0581 × 10−7 1.1461 × 10−7 These order conditions will not be maintained exactly because of the additional error introduced by the integration in t, i.e., errors occur because of the approximate spatial and time discretizations. Note that nsteps = 500 and 1000 in the preceding solutions so that the change in the number of steps in t complicates the comparisons in x. • Also, the order conditions were maintained when changing the order of the approximations, e.g., dss042 (with O(x 2 )) to dss044 (with O(x 4 )). For example, for neqn = 21, 5.6448 × 10−4
(1/20)4 = 1.4112 × 10−6 1.6930 × 10−6 (1/20)2
This result clearly indicates the advantage of using higher-order approximations, e.g., O(x 4 ) rather than O(x 2 ). Finally, in addition to the routines for ∂u/∂ x and ∂ 2 u/∂ x 2 , i.e., three point (O(x 2 )) and five point (O(x 4 )) approximations in the six languages, routines with seven point (O(x 6 )), nine point (O(x 8 )) and 11 point (O(x 10 )) approximations in Fortran and MATLAB are available from the authors; the translation of these routines to the other four languages is straightforward.
References 1. Schiesser, W.E. The Numerical Method of Lines Integration of Partial Differential Equations, Academic Press, San Diego, CA, 1991.
Copyright 2004 by Chapman & Hall/CRC
Appendix F Testing ODE/PDE Codes
The development of a new ODE/PDE application typically requires some trial-and-error analysis until the application is running correctly. The errors that can occur during this development process are generally of two types: 1. Compiler errors resulting from incorrect syntax for the particular compiler (language) 2. Execution errors after successful compilation of the source code We cannot give any specific help with compiler errors since they must be corrected by repeated attempts at compiling the source code until the compiler accepts the source code with no reported errors. The success in the elimination of compiler errors is directly tied to the programmer’s experience with the language. Execution errors can generally occur for two reasons: 1. The ODE/PDE mathematical model has fundamental flaws that eventually cause arithmetic problems. For example, if the model involves division by zero, or if the model equations are unstable so that eventually the calculations cause an overflow (the calculations produce numbers that exceed the largest number the computer can handle), an execution error will eventually be reported. 2. Errors were made in the programming of the model equations; this could be something as simple as a sign error. Execution errors are generally the more difficult to correct (compiler errors will be rather explicit and can generally be corrected by reading the compiler error messages and making corrections in the source code). Thus, we present here a method for detecting and correcting execution errors when developing an ODE/PDE application.
Copyright 2004 by Chapman & Hall/CRC
Here, then, are the steps that should be most relevant to finding execution errors: • We assume that some form of numerical integration of an ODE system is a fundamental part of the calculations and the associated source code (PDEs will generally be approximated as a system of ODEs by the method of lines (MOL) as discussed in Chapters 4 and 5). • Next, we assume that the execution errors are most likely the result of errors in programming the RHSs of the ODEs. Thus, we concentrate on the programming of the ODEs to look for execution errors. • In all the programming of initial value ODEs we have considered previously, a vector of dependent variables is to be computed (starting with the vector defined as initial conditions). Thus, the principal output of the calculations is the vector of dependent variables as a function of the independent variable (which is the solution to a system of ODEs), and we therefore examine the vector of dependent variables in detail to look for results that are obviously in error, e.g., some “bad numbers.” • However, the vector of dependent variables is generated by numerically integrating the associated vector of derivatives. So we also should examine in detail the vector of derivatives computed from the RHSs of the ODEs. In fact, it is the vector of derivatives that determines the vector of dependent variables (i.e., that determines the solution). In this sense, the derivatives are as interesting as the dependent variables since they define the solution (through the ODEs). • To organize these ideas, we can output the dependent variable vector at the beginning of the derivative code or routine (since this vector defines the vector of derivatives through the ODEs). • We can then output the vector of derivatives at the end of the derivative calculations (the end of the code for the ODEs) to look for possible errors in the programming of the ODEs. To illustrate this procedure for finding the source of execution errors, we consider again the 2x2 linear ODE system of Equations 1.6. Function inital now includes initialization of a counter for the number of times the derivative routine is called: function [u0]=inital(n,t) % % Function inital sets the initial condition vector % for the 2 x 2 ODE problem % % Define global variables global ncall; %
Copyright 2004 by Chapman & Hall/CRC
% Initialize counter for calls to the dervative % routine (derv) ncall=0; % % Initial condition vector u0(1)=0; u0(2)=2;
Program F.1 inital with a counter for the derivative evaluations Note that ncall is declared global so that it can be passed to other routines, in this case derv. function [ut]=derv(n,t,u) % % Function derv computes the derivative vector % of the 2 x 2 ODE problem % % Define global variables global ncall; % % Heading during first call to derv if ncall == 0 fprintf('\n\n Output from derv\n\n'); fprintf(' t u(1) u(2)\n'); fprintf(' ut(1) ut(2)\n\n'); end % % Display dependent variable vector fprintf('%10.4f%10.4f%10.4f\n',t,u(1),u(2)); % % Problem parameters a=5.5; b=4.5; % % Derivative vector ut(1)=-a*u(1)+b*u(2); ut(2)= b*u(1)-a*u(2); % % Display derivative vector fprintf('%20.4f%10.4f\n\n',ut(1),ut(2)); % % Increment counter for calls to derv ncall=ncall+1;
Copyright 2004 by Chapman & Hall/CRC
% % Terminate execution after five calls to derv if ncall==5 dbstop in derv; end
Program F.2 derv with output of the dependent variable vector and derivative vector We can note the following points about derv: • The first time derv is executed (with ncall = 0), a heading for the output from derv is displayed: % % Heading during first call to derv if ncall == 0 fprintf('\n\n Output from derv\n\n'); fprintf(' t u(1) u(2)\n'); fprintf(' ut(1) ut(2)\n\n'); end
• Since derv has the central function of using the dependent variable vector, in this case [u(1) u(2)]T , to compute the derivative vector [ut(1) ut(2)]T , two output ( fprintf ) statements are used: — The dependent variable vector is displayed at the beginning of derv so that the state of the dependent variables coming into derv can be judged. If they do not look reasonable, then there is probably an error in the numerical integration; e.g., perhaps some of the dependent variables are moving in the wrong direction (due possibly to an error in the programming of the derivatives that follows in derv), or are becoming large due to an instability in the numerical integration (for example, if an explicit ODE integrator is being used and the step h is too large). % % Display dependent variable vector fprintf('%10.4f%10.4f%10.4f\n',t,u(1),u(2)); — The derivative vector is then programmed (the central function of derv) using the dependent variables, i.e., the RHSs of the ODEs are programmed. Once all of the derivatives are computed, they are displayed by an output statement (generally close to the end of the derivative routine) % % Derivative vector ut(1)=-a*u(1)+b*u(2); ut(2)= b*u(1)-a*u(2);
Copyright 2004 by Chapman & Hall/CRC
% % Display derivative vector fprintf('%20.4f%10.4f\n\n',ut(1),ut(2));
The purpose of this output statement for the derivatives is to check if the derivatives appear reasonable (not of the wrong sign, e.g., a dependent variable that should be decreasing should have a negative derivative or too large because units in the model equations are not correct or the numerical integration is becoming unstable). • Once the input dependent variable vector and output derivative vector are checked, the number of times the output appears should be limited (because the derivative routine typically can be called hundreds or thousands of times during the computation of a complete numerical solution to an ODE problem). In this case, execution of the program is terminated when ncall reaches a value of 5 (five calls to derv): % % Increment counter for calls to derv ncall=ncall+1; % % Terminate execution after five calls to derv if ncall==5 dbstop in derv; end
Note that the MATLAB command dbstop is used when ncall = 5 to terminate execution. The output from derv is listed below: Output from derv t
u(1) ut(1)
u(2) ut(2)
0.0000
0.0000 9.0000
2.0000 -11.0000
0.0100
0.0900 8.0100
1.8900 -9.9900
0.0100
0.0850 8.0600
1.8950 -10.0400
0.0200
0.1656 7.1649
1.7946 -9.1251
0.0200
0.1612 7.2101
1.7992 -9.1704
Copyright 2004 by Chapman & Hall/CRC
Not too surprisingly, the output looks reasonable (this is a small, simple ODE problem). As expected, u1 (t) has a positive derivative (it increases from the initial condition u1 (0) = 0) and u2 (t) has a negative derivative (it decreases from the initial condition u2 (0) = 2). If these signs in the derivatives were not observed, we would know something is wrong, probably in the preceding programming of the derivatives. Also, note that the initial conditions can be checked (in the t = 0 output). This is an important check (the solution must start out at the right values). Although this check is obvious in this small problem (there are only two initial conditions, u1 (0) = 0, u2 (0) = 2), for larger problems, e.g., hundreds or thousands of ODEs, overlooking even one initial condition or using an incorrect value will most likely guarantee that the numerical solution will be incorrect. The same is true for the derivative calculations in derv. If we are integrating n first-order ODEs, we need to program n derivatives. If we overlook even one derivative, which is easy to do in a large ODE system, the solution will be incorrect. Thus, at the end of derv we must have n good numerical values for the derivative vector. Also, intermediate calculations before the final calculation of the derivatives are quite common. For example, we might have to solve a set of nonlinear equations, using the ODE dependent variables as inputs. Once these intermediate variables, such as from the solution of a set of nonlinear equations, are computed in derv, they can be used in the calculation of the derivatives. Of course, errors can occur in these intermediate calculations, and they can be checked by using additional output statements. Generally, a complete output of all of the variables used in the calculation of the derivative vector can be included in derv to ensure that the calculations are done correctly (according to the model equations). This is particularly true for PDE systems in which spatial (boundary value) derivatives are computed in the method of lines. These spatial derivatives can be displayed from derv to check their calculation. Eventually, after detailed checking of the output from derv, the derivative vector (that is the input to the ODE integrator) will be correct; the output statements in derv and the derivative counter can then be removed to compute a complete solution (or these statements can be “commented out” in case they have to be subsequently reactivated for more checking). Although the preceding method of checking the initial derivative calculations is usually effective in finding and correcting errors, it will not be effective for the case when the initial calculations appear to be correct, but later, the numerical solution develops an obvious problem; e.g., the dependent variables become excessively large, or the compiler reports a NAN (not a number). This may be due to integrator instability, or possibly to an error that grows slowly, but eventually causes a calculational failure. The difficulty in finding the cause of such problems stems from the uncertainty in knowing when it occurs (in order to produce some output from derv, for example), and why. That is why
Copyright 2004 by Chapman & Hall/CRC
some knowledge of how numerical ODEs integrators function is important, such as issues of stability, and error monitoring and step size control. For the latter, a common source of failures is to specify unreasonable error tolerances, as discussed in Chapter 1. But in any case, there is no substitute for thorough testing and scrutiny of the computed output. Finally, two other points can be considered to assist in the checking of computer codes for ODE/PDE problems: First, we can consider the question of the units of the timescale, e.g., in an application, when we compute the dependent variables of an ODE system as a function of the independent variable t, is the timescale (units of t) in microseconds, milliseconds, seconds, hours, days, years, etc.? The answer generally is that the units of the timescale are the same as the units of the derivative vector. In order words, the derivatives will have the units of reciprocal time, and whatever those units are will be the units of the timescale. Additionally, the time units must be the same for all of the computed derivatives. Thus, we cannot calculate the derivative for one dependent variable with the units of seconds, and another derivative with the units of hours. The simultaneous integration of the two derivatives will lead to an incorrect solution. To avoid this problem, the time units of all of the derivatives should be checked to ensure that they are the same. The same reasoning can be applied to the units of the dependent variables. For example, if an energy balance produces a time derivative with the units of K/min, integration of this derivative will produce a solution with the temperature in K and the timescale in min. Second, as a word of advice based on experience, we suggest that the development of a new ODE/PDE application should not necessarily be initiated with all of the details of the mathematical model included. This might seem like an inexplicable approach since we eventually want to solve the model equations with the full and complete details we think are necessary to describe the physical system. However, for a relatively complicated model, putting all the mathematical details into the code at the beginning very often guarantees that the code will not execute correctly. If this occurs, the question then that is often difficult or impossible to answer is “What caused the code to fail?” In other words, there are so many details that could be the source of the calculational failure, the identification of which particular detail(s) are the cause of the failure is difficult (although the testing with intermediate output as described previously can help in identifying the cause of the failure). As an alternate approach, we suggest that the code be built up a little at a time. For example, very simple ODEs can be coded first which can be numerically integrated and the solution checked. The ODEs might even be coded with constant derivatives (and therefore their solutions should be linear in the independent variable); if the derivatives of some of the dependent variables are set to zero, those dependent variables should remain constant during the testing. Then an ODE can be added while the other dependent variables are computed from derivatives that are constant. The code is then
Copyright 2004 by Chapman & Hall/CRC
built by adding more ODEs. As this process continues, if a failure occurs, it must be due to the ODEs or mathematical relationships just added, and these can be examined in detail, or they can be reset to the previous condition for which the code executed and some additional testing can be performed to determine why the last step failed. In this way, mathematical details are added until the entire model is coded. The source of any failure along the way can be identified and corrected, particularly by using detailed output from the derivative routine as discussed previously. In other words, the development of an ODE/PDE code is an experimental, trial-and-error, evolutionary process. Thus, as much as we might like to proceed directly to the solution of the complete mathematical model with all of the details that we think are relevant and should be included, starting out with a simpler ODE/PDE system that has a solution that can be checked, then extending the system through a series of steps, each of which can be checked before going on to the next step, we think is the best way to arrive at a final working code for the problem system of interest.
Copyright 2004 by Chapman & Hall/CRC