5.10 Polynomial Approximation from Chebyshev Coefficients 197 5.10 Polynomial Approximation from Chebyshev Coefficients You may well ask after reading the preceding two sections,"Must I store and evaluate my Chebyshev approximation as an array of Chebyshev coefficients for a transformed variable y?Can't I convert the ck's into actual polynomial coefficients in the original variable z and have an approximation of the following form?" f(x (5.10.1) nted for Yes,you can do this (and we will give you the algorithm to do it),but we caution you against it:Evaluating equation(5.10.1),where the coefficient g's reflect an underlying Chebyshev approximation,usually requires more significant figures than evaluation of the Chebyshev sum directly (as by chebev).This is because the Chebyshev polynomials themselves exhibit a rather delicate cancellation:The leading coefficient of T(),for example,is 2-1;other coefficients of T()are (Nort serve even bigger:yet they all manage to combine into a polynomial that lies between+1. America computer, make one paper e University Press. THE Only when m is no larger than 7 or 8 should you contemplate writing a Chebyshev ART fit as a direct polynomial,and even in those cases you should be willing to tolerate two or so significant figures less accuracy than the roundoff limit of your machine. You get the g's in equation(5.10.1)from the c's output from chebft(suitably ictly proh Programs truncated at a modest value ofm)by calling in sequence the following two procedures: #include "nrutil.h" to dir void chebpc(f1oatc],float d[☐,intn) Chebyshev polynomial coefficients.Given a coefficient array c[..n-1],this routine generates coefficient array d[.1]such that)-co/2.The method OF SCIENTIFIC COMPUTING(ISBN is Clenshaw's recurrence (5.8.11),but now applied algebraically rather than arithmetically. int k,j; float sv,*dd; dd=vector(0,n-1); v@cambridge.org 胸之 1988-1992 by Numerical Recipes for(j=0;j=1;j--)[ for (k=n-j;k>=1;k--){ sv=d[k]; Software. d[k]=2.0*dk-1]-dd[k]; dd[k]=sv; (outside North America) sv=d[o]; d[o]=-dd[o]+c[j] dd[o]=sv; for(j=n-1;j>=1;j-) d[j]=d[j-1]-dd[j]; d[0]=-dd[0]+0.5*c[0]; free_vector(dd,0,n-1);
5.10 Polynomial Approximation from Chebyshev Coefficients 197 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). 5.10 Polynomial Approximation from Chebyshev Coefficients You may well ask after reading the preceding two sections, “Must I store and evaluate my Chebyshev approximation as an array of Chebyshev coefficients for a transformed variable y? Can’t I convert the ck’s into actual polynomial coefficients in the original variable x and have an approximation of the following form?” f(x) ≈ m −1 k=0 gkxk (5.10.1) Yes, you can do this (and we will give you the algorithm to do it), but we caution you against it: Evaluating equation (5.10.1), where the coefficient g’s reflect an underlying Chebyshev approximation, usually requires more significant figures than evaluation of the Chebyshev sum directly (as by chebev). This is because the Chebyshev polynomials themselves exhibit a rather delicate cancellation: The leading coefficient of Tn(x), for example, is 2n−1; other coefficients of Tn(x) are even bigger; yet they all manage to combine into a polynomial that lies between ±1. Only when m is no larger than 7 or 8 should you contemplate writing a Chebyshev fit as a direct polynomial, and even in those cases you should be willing to tolerate two or so significant figures less accuracy than the roundoff limit of your machine. You get the g’s in equation (5.10.1) from the c’s output from chebft (suitably truncated at a modest value of m) by calling in sequence the following two procedures: #include "nrutil.h" void chebpc(float c[], float d[], int n) Chebyshev polynomial coefficients. Given a coefficient array c[0..n-1], this routine generates a coefficient array d[0..n-1] such that n-1 k=0 dkyk = n-1 k=0 ckTk(y) − c0/2. The method is Clenshaw’s recurrence (5.8.11), but now applied algebraically rather than arithmetically. { int k,j; float sv,*dd; dd=vector(0,n-1); for (j=0;j=1;j--) { for (k=n-j;k>=1;k--) { sv=d[k]; d[k]=2.0*d[k-1]-dd[k]; dd[k]=sv; } sv=d[0]; d[0] = -dd[0]+c[j]; dd[0]=sv; } for (j=n-1;j>=1;j--) d[j]=d[j-1]-dd[j]; d[0] = -dd[0]+0.5*c[0]; free_vector(dd,0,n-1); }
198 Chapter 5.Evaluation of Functions void pcshft(float a,float b,float d[],int n) Polynomial coefficient shift.Given a coefficient array d[..n-1],this routine generates a coefficient array[..1]such that where z and yare related by (5.8.10).i.e.,the interval-1 =j;k--) division is a miracle of high-school algebra.If you NUMERICAL d[k]-=cnst*d[k+1]; never learned it,go do so.You won't be sorry. RECIPES CITED REFERENCES AND FURTHER READING: Acton,F.S.1970,Numerica/Methods That Work;1990,corrected edition (Washington:Mathe- 号 Press. matical Association of America),pp.59.182-183 [synthetic division]. Programs 5.11 Economization of Power Series IENTIFIC One particular application of Chebyshev methods,the economization of power series,is an occasionally useful technique,with a flavor of getting something for nothing. Suppose that you are already computing a function by the use of a convergent power series,for example 9 (ISBN f)三1-+京-7+ (5.11.1) Numerica 10621 (This function is actually sin()/v,but pretend you don't know that.)You might be 43106 doing a problem that requires evaluating the series many times in some particular interval,say [0,(2)2].Everything is fine,except that the series requires a large number of terms before S Recipes its error (approximated by the first neglected term,say)is tolerable.In our example,with =(2)2,the first term smaller than 10-7 is/(27!).This then approximates the error of the finite series whose last term is 12/(25!). Software. Notice that because of the large exponent in the error is much smaller than 10-7 everywhere in the interval except at the very largest values of r.This is the feature that allows "economization":if we are willing to let the error elsewhere in the interval rise to about the same value that the first neglected term has at the extreme end of the interval,then we can replace the 13-term series by one that is significantly shorter. Here are the steps for doing so: 1.Change variables from x to y,as in equation (5.8.10),to map the z interval into -1≤y≤1. 2.Find the coefficients of the Chebyshev sum(like equation 5.8.8)that exactly equals your truncated power series (the one with enough terms for accuracy). 3.Truncate this Chebyshev series to a smaller number of terms,using the coefficient of the first neglected Chebyshev polynomial as an estimate of the error
198 Chapter 5. Evaluation of Functions 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). void pcshft(float a, float b, float d[], int n) Polynomial coefficient shift. Given a coefficient array d[0..n-1], this routine generates a coefficient array g[0..n-1] such that n-1 k=0 dkyk = n-1 k=0 gkxk, where x and y are related by (5.8.10), i.e., the interval −1 =j;k--) d[k] -= cnst*d[k+1]; } CITED REFERENCES AND FURTHER READING: Acton, F.S. 1970, Numerical Methods That Work; 1990, corrected edition (Washington: Mathematical Association of America), pp. 59, 182–183 [synthetic division]. 5.11 Economization of Power Series One particular application of Chebyshev methods, the economization of power series, is an occasionally useful technique, with a flavor of getting something for nothing. Suppose that you are already computing a function by the use of a convergent power series, for example f(x) ≡ 1 − x 3! + x2 5! − x3 7! + ··· (5.11.1) (This function is actually sin(√x)/ √x, but pretend you don’t know that.) You might be doing a problem that requires evaluating the series many times in some particular interval, say [0,(2π) 2]. Everything is fine, except that the series requires a large number of terms before its error (approximated by the first neglected term, say) is tolerable. In our example, with x = (2π) 2, the first term smaller than 10−7 is x13/(27!). This then approximates the error of the finite series whose last term is x12/(25!). Notice that because of the large exponent in x13, the error is much smaller than 10−7 everywhere in the interval except at the very largest values of x. This is the feature that allows “economization”: if we are willing to let the error elsewhere in the interval rise to about the same value that the first neglected term has at the extreme end of the interval, then we can replace the 13-term series by one that is significantly shorter. Here are the steps for doing so: 1. Change variables from x to y, as in equation (5.8.10), to map the x interval into −1 ≤ y ≤ 1. 2. Find the coefficients of the Chebyshev sum (like equation 5.8.8) that exactly equals your truncated power series (the one with enough terms for accuracy). 3. Truncate this Chebyshev series to a smaller number of terms, using the coefficient of the first neglected Chebyshev polynomial as an estimate of the error.