正在加载图片...
5.7 Numerical Derivatives 187 With h an "exact"number,the roundoff error in equation (5.7.1)is er eflf()/h.Here ef is the fractional accuracy with which f is computed;for a simple function this may be comparable to the machine accuracy,efem,but for a complicated calculation with additional sources of inaccuracy it may be larger.The truncation error in equation (5.7.3)is on the order of ehf"().Varying h to minimize the sum e,+et gives the optimal choice of h, (5.7.5) 三 where(f/f")1/2 is the"curvature scale"of the function f,or"characteristic com or scale"over which it changes.In the absence of any other information,one often assumes re =x (except near z=0 where some other estimate of the typical x 人 scale should be used). With the choice of equation(5.7.5),the fractional accuracy of the computed derivative is (er+et)/f've(ff"/f)2v (5.7.6) 景墨9州 2 Here the last order-of-magnitude equality assumes that f,f',and f"all share the same characteristic length scale,usually the case.One sees that the simple finite-difference equation(5.7.1)gives at best only the square root of the machine accuracy Em. If you can afford two function evaluations for each derivative calculation,then 9名。∽ it is significantly better to use the symmetrized form f'(e)≈fc+)-fz-h) 6 (5.7.7) 2h In this case,by equation(5.7.2),the truncation error is e~h2f".The roundoff error er is about the same as before.The optimal choice ofh,by a short calculation analogous to the one above,is now 1/3 (5.7.8) Numerica 10.621 43126 and the fractional error is (e,+e)/If1~(ef)23f23(f"/3/f'~(e)23 (5.7.9) which will typically be an order of magnitude (single precision)or two orders of magnitude(double precision)better than equation(5.7.6).We have arrived at Lesson 2:Choose h to be the correct power of er or em times a characteristic scale e. You can easily derive the correct powers for other cases [1].For a function of two dimensions,for example,and the mixed derivative formula 82f [f(x+h,y+h)-f(x+h,y-h)]-[f(x-h,y+h)-f(x-h,y-h)] oxoy 4h2 (5.7.10)5.7 Numerical Derivatives 187 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 machine￾readable 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). With h an “exact” number, the roundoff error in equation (5.7.1) is e r ∼ f |f(x)/h|. Here f is the fractional accuracy with which f is computed; for a simple function this may be comparable to the machine accuracy,  f ≈ m, but for a complicated calculation with additional sources of inaccuracy it may be larger. The truncation error in equation (5.7.3) is on the order of e t ∼ |hf(x)|. Varying h to minimize the sum er + et gives the optimal choice of h, h ∼  f f f ≈ √fxc (5.7.5) where xc ≡ (f /f)1/2 is the “curvature scale” of the function f, or “characteristic scale” over which it changes. In the absence of any other information, one often assumes xc = x (except near x = 0 where some other estimate of the typical x scale should be used). With the choice of equation (5.7.5), the fractional accuracy of the computed derivative is (er + et)/|f | ∼ √f (ff/f2 ) 1/2 ∼ √f (5.7.6) Here the last order-of-magnitude equality assumes that f, f  , and f  all share the same characteristic length scale, usually the case. One sees that the simple finite-difference equation (5.7.1) gives at best only the square root of the machine accuracy m. If you can afford two function evaluations for each derivative calculation, then it is significantly better to use the symmetrized form f (x) ≈ f(x + h) − f(x − h) 2h (5.7.7) In this case, by equation (5.7.2), the truncation error is e t ∼ h2f. The roundoff error er is about the same as before. The optimal choice of h, by a short calculation analogous to the one above, is now h ∼ f f f 1/3 ∼ (f ) 1/3xc (5.7.8) and the fractional error is (er + et)/|f | ∼ (f ) 2/3f 2/3(f) 1/3/f ∼ (f ) 2/3 (5.7.9) which will typically be an order of magnitude (single precision) or two orders of magnitude (double precision) better than equation (5.7.6). We have arrived at Lesson 2: Choose h to be the correct power of f or m times a characteristic scale xc. You can easily derive the correct powers for other cases [1]. For a function of two dimensions, for example, and the mixed derivative formula ∂2f ∂x∂y = [f(x + h, y + h) − f(x + h, y − h)] − [f(x − h, y + h) − f(x − h, y − h)] 4h2 (5.7.10)
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有