734 Chapter 16.Integration of Ordinary Differential Equations Note that for compatibility with bsstep the arrays y and d2y are of length 2n for a system of n second-order equations.The values of y are stored in the first n elements of y, while the first derivatives are stored in the second n elements.The right-hand side f is stored in the first n elements of the array d2y,the second n elements are unused.With this storage arrangement you can use bsstep simply by replacing the call to mmid with one to stoerm using the same arguments;just be sure that the argument nv of bsstep is set to 2n.You should also use the more efficient sequence of stepsizes suggested by Deufhard: n=1,2,3,4,5,… (16.5.6) and set KMAXX 12 in bsstep. ://www.nr CITED REFERENCES AND FURTHER READING: Deuflhard,P.1985,SIAM Review,vol.27,pp.505-535. 八g2 1.800 NUMERICAL RECIPES 16.6 Stiff Sets of Equations (North America to make one paper server computer, University Press. THE As soon as one deals with more than one first-order differential equation,the ART possibility of a stiff set of equations arises.Stiffness occurs in a problem where there are two or more very different scales of the independent variable on which 9 Program the dependent variables are changing.For example,consider the following set of equations [1]: 6 、三 OF SCIENTIFIC( u'=998u+1998U (16.6.1) v'=-999u-1999u with boundary conditions 10621 u(0)=1 u(0)=0 (16.6.2) Numerica 431 By means of the transformation Recipes u=2y-z v=-y+z (16.6.3) North we find the solution u=2e-z-e-1000z v=-e-z+e-1000z (16.6.4) If we integrated the system (16.6.1)with any of the methods given so far in this chapter,the presence of the e-1000 term would require a stepsize h1/1000 for the method to be stable (the reason for this is explained below).This is so even
734 Chapter 16. Integration of Ordinary Differential Equations Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copyin Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) g of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America). Note that for compatibility with bsstep the arrays y and d2y are of length 2n for a system of n second-order equations. The values of y are stored in the first n elements of y, while the first derivatives are stored in the second n elements. The right-hand side f is stored in the first n elements of the array d2y; the second n elements are unused. With this storage arrangement you can use bsstep simply by replacing the call to mmid with one to stoerm using the same arguments; just be sure that the argument nv of bsstep is set to 2n. You should also use the more efficient sequence of stepsizes suggested by Deuflhard: n = 1, 2, 3, 4, 5,... (16.5.6) and set KMAXX = 12 in bsstep. CITED REFERENCES AND FURTHER READING: Deuflhard, P. 1985, SIAM Review, vol. 27, pp. 505–535. 16.6 Stiff Sets of Equations As soon as one deals with more than one first-order differential equation, the possibility of a stiff set of equations arises. Stiffness occurs in a problem where there are two or more very different scales of the independent variable on which the dependent variables are changing. For example, consider the following set of equations [1]: u = 998u + 1998v v = −999u − 1999v (16.6.1) with boundary conditions u(0) = 1 v(0) = 0 (16.6.2) By means of the transformation u = 2y − z v = −y + z (16.6.3) we find the solution u = 2e−x − e−1000x v = −e−x + e−1000x (16.6.4) If we integrated the system (16.6.1) with any of the methods given so far in this chapter, the presence of the e−1000x term would require a stepsize h 1/1000 for the method to be stable (the reason for this is explained below). This is so even
16.6 Stiff Sets of Equations 735 83 granted for 1100 Figure 16.6.1.Example of an instability encountered in integrating a stiff equation (schematic).Here it is supposed that the equation has two solutions,shown as solid and dashed lines.Although the initial conditions are such as to give the solid solution,the stability of the integration (shown as the unstable NUMERICAL RECIPES I dotted sequence of segments)is determined by the more rapidly varying dashed solution,even after that solution has effectively died away to zero.Implicit integration methods are the cure. 令 though the e-1000x term is completely negligible in determining the values of u and v as soon as one is away from the origin (see Figure 16.6.1). 2名分 Press. This is the generic disease of stiff equations:we are required to follow the variation in the solution on the shortest length scale to maintain stability of the integration,even though accuracy requirements allow a much larger stepsize. To see how we might cure this problem,consider the single equation IENTIFIC y=-cy (16.6.5) 6 where c>0 is a constant.The explicit (or forward)Euler scheme for integrating this equation with stepsize h is Un+1 yn +hyn =(1-ch)yn (16.6.6 The method is called explicit because the new value yn+is given explicitly in Numerical 10521 terms of the old value yn.Clearly the method is unstable if h >2/c,for then 43126 yn+oasn→o. The simplest cure is to resort to implicit differencing,where the right-hand side is evaluated at the new y location.In this case,we get the backward Euler scheme: Un+1=Un hyn+1 (16.6.7) Or n+1=1+ch (16.6.8) The method is absolutely stable:even as hoo,yn+1-0,which is in fact the correct solution of the differential equation.If we think of as representing time, then the implicit method converges to the true equilibrium solution (i.e.,the solution at late times)for large stepsizes.This nice feature of implicit methods holds only for linear systems,but even in the general case implicit methods give better stability
16.6 Stiff Sets of Equations 735 Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copyin Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) g of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America). x y Figure 16.6.1. Example of an instability encountered in integrating a stiff equation (schematic). Here it is supposed that the equation has two solutions, shown as solid and dashed lines. Although the initial conditions are such as to give the solid solution, the stability of the integration (shown as the unstable dotted sequence of segments) is determined by the more rapidly varying dashed solution, even after that solution has effectively died away to zero. Implicit integration methods are the cure. though the e−1000x term is completely negligible in determining the values of u and v as soon as one is away from the origin (see Figure 16.6.1). This is the generic disease of stiff equations: we are required to follow the variation in the solution on the shortest length scale to maintain stability of the integration, even though accuracy requirements allow a much larger stepsize. To see how we might cure this problem, consider the single equation y = −cy (16.6.5) where c > 0 is a constant. The explicit (or forward) Euler scheme for integrating this equation with stepsize h is yn+1 = yn + hy n = (1 − ch)yn (16.6.6) The method is called explicit because the new value yn+1 is given explicitly in terms of the old value yn. Clearly the method is unstable if h > 2/c, for then |yn|→∞ as n → ∞. The simplest cure is to resort to implicit differencing, where the right-hand side is evaluated at the new y location. In this case, we get the backward Euler scheme: yn+1 = yn + hy n+1 (16.6.7) or yn+1 = yn 1 + ch (16.6.8) The method is absolutely stable: even as h → ∞, yn+1 → 0, which is in fact the correct solution of the differential equation. If we think of x as representing time, then the implicit method converges to the true equilibrium solution (i.e., the solution at late times) for large stepsizes. This nice feature of implicit methods holds only for linear systems, but even in the general case implicit methods give better stability
736 Chapter 16.Integration of Ordinary Differential Equations Of course,we give up accuracy in following the evolution towards equilibrium if we use large stepsizes,but we maintain stability. These considerations can easily be generalized to sets of linear equations with constant coefficients: y=-C·y (16.6.9) where C is a positive definite matrix.Explicit differencing gives yn+1=(1-Ch)·yn (16.6.10) Now a matrix An tends to zero as n-oo only if the largest eigenvalue of A has magnitude less than unity.Thus y is bounded as n-oo only if the largest 菲 eigenvalue of 1-Ch is less than 1,or in other words 爱州 ICAL 2 h< (16.6.11) 入max where Amax is the largest eigenvalue of C. On the other hand,implicit differencing gives yn+1 =yn +hyn+1 (16.6.12) or 9 yn+1=(1+Ch)-1·yn (16.6.13) 9 If the eigenvalues of C are A,then the eigenvalues of(1+Ch)-1 are (1+h)-1 which has magnitude less than one for all h.(Recall that all the eigenvalues of a IENTIFIC( positive definite matrix are nonnegative.)Thus the method is stable for all stepsizes 61 h.The penalty we pay for this stability is that we are required to invert a matrix at each step. Not all equations are linear with constant coefficients,unfortunately!For the system y=f(y) (16.6.14) Numerical 10521 implicit differencing gives 43106 yn+1 yn+hf(yn+1) (16.6.15) In general this is some nasty set of nonlinear equations that has to be solved iteratively at each step.Suppose we try linearizing the equations,as in Newton's method: Yn+1=yn +h + (16.6.16) Here of/Oy is the matrix of the partial derivatives of the right-hand side(the Jacobian matrix).Rearrange equation(16.6.16)into the form Yn+1=yn+h 1-hof] ·fyn) (16.6.17
736 Chapter 16. Integration of Ordinary Differential Equations Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copyin Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) g of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America). Of course, we give up accuracy in following the evolution towards equilibrium if we use large stepsizes, but we maintain stability. These considerations can easily be generalized to sets of linear equations with constant coefficients: y = −C · y (16.6.9) where C is a positive definite matrix. Explicit differencing gives yn+1 = (1 − Ch) · yn (16.6.10) Now a matrix An tends to zero as n → ∞ only if the largest eigenvalue of A has magnitude less than unity. Thus yn is bounded as n → ∞ only if the largest eigenvalue of 1 − Ch is less than 1, or in other words h < 2 λmax (16.6.11) where λmax is the largest eigenvalue of C. On the other hand, implicit differencing gives yn+1 = yn + hy n+1 (16.6.12) or yn+1 = (1 + Ch) −1 · yn (16.6.13) If the eigenvalues of C are λ, then the eigenvalues of (1 + Ch) −1 are (1 + λh)−1, which has magnitude less than one for all h. (Recall that all the eigenvalues of a positive definite matrix are nonnegative.) Thus the method is stable for all stepsizes h. The penalty we pay for this stability is that we are required to invert a matrix at each step. Not all equations are linear with constant coefficients, unfortunately! For the system y = f(y) (16.6.14) implicit differencing gives yn+1 = yn + hf(yn+1) (16.6.15) In general this is some nasty set of nonlinear equations that has to be solved iteratively at each step. Suppose we try linearizing the equations, as in Newton’s method: yn+1 = yn + h f(yn) + ∂f ∂y yn · (yn+1 − yn) (16.6.16) Here ∂f/∂y is the matrix of the partial derivatives of the right-hand side (the Jacobian matrix). Rearrange equation (16.6.16) into the form yn+1 = yn + h 1 − h ∂f ∂y −1 · f(yn) (16.6.17)
16.6 Stiff Sets of Equations 737 If h is not too big,only one iteration of Newton's method may be accurate enough to solve equation (16.6.15)using equation(16.6.17).In other words,at each step we have to invert the matrix 1-hof (16.6.18) to find y Solving implicit methods by linearization is called a"semi-implicit" method,so equation(16.6.17)is the semi-implicit Euler method.It is not guaranteed to be stable,but it usually is,because the behavior is locally similar to the case of a constant matrix C described above. So far we have dealt only with implicit methods that are first-order accurate. While these are very robust,most problems will benefit from higher-order methods. There are three important classes of higher-order methods for stiff systems: Generalizations of the Runge-Kutta method,of which the most useful are the Rosenbrock methods.The first practical implementation of these ICAL ideas was by Kaps and Rentrop,and so these methods are also called Kaps-Rentrop methods. Generalizations of the Bulirsch-Stoer method,in particular a semi-implicit RECIPES extrapolation method due to Bader and Deufhard. Predictor-corrector methods,most of which are descendants of Gear's 9 backward differentiation method. We shall give implementations of the first two methods.Note that systems where the right-hand side depends explicitly on z,f(y,z),can be handled by adding x to 9 the list of dependent variables so that the system to be solved is 9 (16.6.19) In both the routines to be given in this section,we have explicitly carried out this 61 replacement for you,so the routines can handle right-hand sides of the form f(y, without any special effort on your part. We now mention an important point:It is absolutely crucial to scale your vari- ables properly when integrating stiff problems with automatic stepsize adjustment. 言m As in our nonstiff routines,you will be asked to supply a vector ysca with which the error is to be scaled.For example,to get constant fractional errors,simply set yscal =ly.You can get constant absolute errors relative to some maximum values Numerica 10621 by setting yscal equal to those maximum values.In stiff problems,there are often 43106 strongly decreasing pieces of the solution which you are not particularly interested in following once they are small.You can control the relative error above some threshold C and the absolute error below the threshold by setting yscal=max(C,lyl) (16.6.20) Ifyou are using appropriate nondimensional units,then each component of C should be of order unity.If you are not sure what values to take for C,simply try setting each component equal to unity.We strongly advocate the choice (16.6.20) for stiff problems. One final warning:Solving stiff problems can sometimes lead to catastrophic precision loss.Be alert for situations where double precision is necessary
16.6 Stiff Sets of Equations 737 Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copyin Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) g of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America). If h is not too big, only one iteration of Newton’s method may be accurate enough to solve equation (16.6.15) using equation (16.6.17). In other words, at each step we have to invert the matrix 1 − h ∂f ∂y (16.6.18) to find yn+1. Solving implicit methods by linearization is called a “semi-implicit” method, so equation (16.6.17) is the semi-implicit Euler method. It is not guaranteed to be stable, but it usually is, because the behavior is locally similar to the case of a constant matrix C described above. So far we have dealt only with implicit methods that are first-order accurate. While these are very robust, most problems will benefit from higher-order methods. There are three important classes of higher-order methods for stiff systems: • Generalizations of the Runge-Kutta method, of which the most useful are the Rosenbrock methods. The first practical implementation of these ideas was by Kaps and Rentrop, and so these methods are also called Kaps-Rentrop methods. • Generalizations of the Bulirsch-Stoer method, in particular a semi-implicit extrapolation method due to Bader and Deuflhard. • Predictor-corrector methods, most of which are descendants of Gear’s backward differentiation method. We shall give implementations of the first two methods. Note that systems where the right-hand side depends explicitly on x, f(y, x), can be handled by adding x to the list of dependent variables so that the system to be solved is y x = f 1 (16.6.19) In both the routines to be given in this section, we have explicitly carried out this replacement for you, so the routines can handle right-hand sides of the form f(y, x) without any special effort on your part. We now mention an important point: It is absolutely crucial to scale your variables properly when integrating stiff problems with automatic stepsize adjustment. As in our nonstiff routines, you will be asked to supply a vector y scal with which the error is to be scaled. For example, to get constant fractional errors, simply set yscal = |y|. You can get constant absolute errors relative to some maximum values by setting yscal equal to those maximum values. In stiff problems, there are often strongly decreasing pieces of the solution which you are not particularly interested in following once they are small. You can control the relative error above some threshold C and the absolute error below the threshold by setting yscal = max(C, |y|) (16.6.20) If you are using appropriate nondimensional units, then each component of C should be of order unity. If you are not sure what values to take for C, simply try setting each component equal to unity. We strongly advocate the choice (16.6.20) for stiff problems. One final warning: Solving stiff problems can sometimes lead to catastrophic precision loss. Be alert for situations where double precision is necessary
738 Chapter 16.Integration of Ordinary Differential Equations Rosenbrock Methods These methods have the advantage of being relatively simple to understand and imple- ment.For moderate accuracies(10-4-10-5 in the error criterion)and moderate-sized systems (N S 10),they are competitive with the more complicated algorithms.For more stringent parameters,Rosenbrock methods remain reliable;they merely become less efficient than competitors like the semi-implicit extrapolation method (see below). A Rosenbrock method seeks a solution of the form yro+)=o+∑ck (16.6.21) i= where the corrections k;are found by solving s linear equations that generalize the structure 8 in(16.6.17)月 1-h)k=o+ak+hr·∑与 i=1,..,s (16.6.22) Here we denote the Jacobian matrix by f'.The coefficients ca,,and Yj are fixed constants independent of the problem.Ify =;=0,this is simply a Runge-Kutta scheme. RECIPES I Equations (16.6.22)can be solved successively for ki,k2,.. 令 Crucial to the success of a stiff integration scheme is an automatic stepsize adjustment algorithm.Kaps and Rentrop [2]discovered an embedded or Runge-Kutta-Fehlberg method as described in $16.2:Two estimates of the form (16.6.21)are computed,the "real"one y and a lower-order estimate y with different coefficients ,i=1,...,s,where s <s but the ki 六是 Press. are the same.The difference between y and y leads to an estimate of the local truncation error, which can then be used for stepsize control.Kaps and Rentrop showed that the smallest value of s for which embedding is possible is s =4,s 3,leading to a fourth-order method. Programs To minimize the matrix-vector multiplications on the right-hand side of(16.6.22),we rewrite the equations in terms of quantities SCIENTIFIC i-1 g1= Yigkj +yk (16.623) 61 The equations then take the form COMPUTING (1/h-f)g1=f(yo) 19200 (ISBN (1/h-f')·g2=fyo+a21g1)+c21g/h Numerical Recipes 21 (1/h-f')g3=f(yo+a31g1+a32g2)+(c31g1+c32g2)/h (1/hh-f)·g4=fyo+a41g1+a42g2+a43g3)+(c41g1+c42g2+c43g3)/h 43106 (16.6.24) In our implementation stiff of the Kaps-Rentrop algorithm,we have carried out the (outside replacement (16.6.19)explicitly in equations (16.6.24),so you need not concern yourself about it.Simply provide a routine (called derivs in stiff)that returns f(called dydx)as a North Software. function of x and y.Also supply a routine jacobn that returns f(dfdy)and of/ox(dfdx) as functions of and y.If does not occur explicitly on the right-hand side,then dfdx will be zero.Usually the Jacobian matrix will be available to you by analytic differentiation of the right-hand side f.If not,your routine will have to compute it by numerical differencing with appropriate increments Ay. Kaps and Rentrop gave two different sets of parameters,which have slightly different stability properties.Several other sets have been proposed.Our default choice is that of Shampine [3],but we also give you one of the Kaps-Rentrop sets as an option.Some proposed parameter sets require function evaluations outside the domain of integration;we prefer to avoid that complication. The calling sequence of stiff is exactly the same as the nonstiff routines given earlier in this chapter.It is thus"plug-compatible"with them in the general ODE integrating routine
738 Chapter 16. Integration of Ordinary Differential Equations Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copyin Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) g of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America). Rosenbrock Methods These methods have the advantage of being relatively simple to understand and implement. For moderate accuracies ( <∼ 10−4 – 10−5 in the error criterion) and moderate-sized systems (N <∼ 10), they are competitive with the more complicated algorithms. For more stringent parameters, Rosenbrock methods remain reliable; they merely become less efficient than competitors like the semi-implicit extrapolation method (see below). A Rosenbrock method seeks a solution of the form y(x0 + h) = y0 +s i=1 ciki (16.6.21) where the corrections ki are found by solving s linear equations that generalize the structure in (16.6.17): (1 − γhf ) · ki = hf y0 +i−1 j=1 αijkj + hf · i−1 j=1 γijkj , i = 1,...,s (16.6.22) Here we denote the Jacobian matrix by f . The coefficients γ, ci, αij , and γij are fixed constants independent of the problem. If γ = γij = 0, this is simply a Runge-Kutta scheme. Equations (16.6.22) can be solved successively for k1, k2,... . Crucial to the success of a stiff integration scheme is an automatic stepsize adjustment algorithm. Kaps and Rentrop [2] discovered an embedded or Runge-Kutta-Fehlberg method as described in §16.2: Two estimates of the form (16.6.21) are computed, the “real” one y and a lower-order estimate y with different coefficients cˆi, i = 1,..., sˆ, where s<s ˆ but the ki are the same. The difference between y and y leads to an estimate of the local truncation error, which can then be used for stepsize control. Kaps and Rentrop showed that the smallest value of s for which embedding is possible is s = 4, sˆ = 3, leading to a fourth-order method. To minimize the matrix-vector multiplications on the right-hand side of (16.6.22), we rewrite the equations in terms of quantities gi = i−1 j=1 γijkj + γki (16.6.23) The equations then take the form (1/γh − f ) · g1 = f(y0) (1/γh − f ) · g2 = f(y0 + a21g1) + c21g1/h (1/γh − f ) · g3 = f(y0 + a31g1 + a32g2)+(c31g1 + c32g2)/h (1/γh − f ) · g4 = f(y0 + a41g1 + a42g2 + a43g3)+(c41g1 + c42g2 + c43g3)/h (16.6.24) In our implementation stiff of the Kaps-Rentrop algorithm, we have carried out the replacement (16.6.19) explicitly in equations (16.6.24), so you need not concern yourself about it. Simply provide a routine (called derivs in stiff) that returns f (called dydx) as a function of x and y. Also supply a routine jacobn that returns f (dfdy) and ∂f/∂x (dfdx) as functions of x and y. If x does not occur explicitly on the right-hand side, then dfdx will be zero. Usually the Jacobian matrix will be available to you by analytic differentiation of the right-hand side f. If not, your routine will have to compute it by numerical differencing with appropriate increments ∆y. Kaps and Rentrop gave two different sets of parameters, which have slightly different stability properties. Several other sets have been proposed. Our default choice is that of Shampine [3], but we also give you one of the Kaps-Rentrop sets as an option. Some proposed parameter sets require function evaluations outside the domain of integration; we prefer to avoid that complication. The calling sequence of stiff is exactly the same as the nonstiff routines given earlier in this chapter. It is thus “plug-compatible” with them in the general ODE integrating routine