Thompson - Computing for Scientists and Engineers (523188), страница 18
Текст из файла (страница 18)
nThe sine function, PSsin, can easily be checked against values from tables orfrom a pocket calculator, with a similar suggestion to that for the cosine series thatmultiples of /2 (with appropriate sign changes) can be removed. The convergenceof the sine series as a function of kmax should be similar to that for the cosine series. Also, for any kmax the sine series should be an odd function of x.In Figure 3.10 we have the power series compared against the sine function forkm a x values of 1 and 5.
Just as for the cosine series, the convergence is very rapidbecause of the decrease as roughly l/k2 of successive terms in the series.FIGURE 3.10 Maclaurin series expansions for k max = 1 (short dashed curve) and the scalederror for k max = 5 (long dashed curve) for the sine function (solid curve).88POWER SERIESIn Figure 3.10, rather than showing the series approximation for k m a x = 5,which could not be visually resolved from the converged series, we show the error(exact minus series) scaled up by a factor of 107. Clearly such a truncated series expansion is very accurate, especially for x < 1.
This leads to the following improvement in accuracy of the power-series expansions.Given both the cosine and the sine series, these two functions need be used onlywith arguments that are less in magnitude than /4, since if x lies between this valueand /2 one may use the identity(3.68)and vice versa for the sine function. This is a great advantage even over the removalof multiples of /2 for the cosine and sine function arguments. Since in the powerseries for the cosine or sine, successive terms have x 2 factors as large as( /2)* = 2.47 > 1, or only as large as ( /4)2 = 0.62 < 1, the latter (being lessthan unity) is clearly preferable for producing rapid convergence.Exercise 3.32(a) Explain, in terms of the recurrence formulas (3.63) and (3.66), why it takesonly about half as many terms (k m a x is halved) to obtain the same accuracy inthe powers-series expansions of the cosine and sine if the relation (3.68) is usedto limit the function arguments to less than /4 than if arguments up to /2 areallowed.(b) Verify the analytical estimate in (a) by numerical examples, using the functions PScos and PSsin that you have programmed.
n4. Arccosine function. The arccosine function converges rapidly only for x valueswhere the cosine function is small in magnitude. Therefore, it has much better convergence when it produces angles near /2 than angles near zero. We may write(3.69)which is suitable for x <For x >one may use= 0.707, that is, for obtaining angles less than /4.(3.70)Exercise 3.33In order to verify (3.69) and (3.70), sketch a right-angled triangle with hypotenuse of length unity and base of length x.
Use geometry and the theorem of Pythagoras to derive these two relations. nThus, in the function PSarccos we do not evaluate a power series for the arccosine (in spite of the initial letters), but use (3.69) or (3.70) instead. As with any3.5 TESTING THE CONVERGENCE OF SERIES89inverse trigonometric function, there are quadrant ambiguities in the arccosine function, as shown in Figure 2.7, Equations (3.69) and (3.70) place the angles in thefirst quadrant for positive x. Note that the magnitude of x must be less than unity,else the arcsine power series does not converge. Such a check on x is made in bothprogram functions.Testing of Psarccos is best done after Psarcsin has been coded and tested.For large kmax the two functions should return complementary angles (summing to/2) if they are given the same argument value x.5.
Arcsine function. The Maclaurin series for this inverse circular function wasderived in Section 3.2. We may write the power series as(3.7 1)with successive terms being obtained from the recurrence relation(3.72)(3.73)Exercise 3.34Show that formulas (3.72) and (3.73) give the expansion coefficients for theMaclaurin expansion of the arcsine function (3.71).
nFigure 3.11 shows the arcsin function and power series approximations to it.FIGURE 3.11 Arcsine function (solid curve) and its Maclaurin expansions for k max = 1 (dottedcurve) and the scaled error for k max = 20.90POWER SERIESIn the function PSarcsin the power series is evaluated after checking that theabsolute value of x is less than unity. The function can be tested by using arguments with simple angles, such as x = 0 for zero radians, x = 1/2 for angle π/6,x = 1/ for angle π/4, and x =for π/3.
Note that the angles are obtainedin radians. The values of kmax needed to obtain a close approximation to theseangles are large, because the series is not quickly damped out by the k dependencein (3.72).Notice in Figure 3.11 that the convergence is rapid until x is near unity. Thus,in the recurrence relation for the arcsine series, (3.72), it is the x2 powers that produce the convergence, rather than the decrease with k (which is rather weak). Evenfor kmax = 20 the error for the largest x values is of order 1%.6. Natural logarithm.
For this function the power series that is expanded is for thenatural logarithm ln (1 + x), with convergence requiring an absolute value of xthat is less than unity. The power series given by (3.31) can be written for the numerical algorithm as(3.74)with the recurrence relation being(3.75)(3.76)Exercise 3.35Show that formulas (3.75) and (3.76), with the denominator in (3.74), give theexpansion coefficients for the series expansion of the natural logarithm function(3.31). nProgram function PSln implements the formulas (3.74) through (3.76), afterchecking that |x| < 1. The function may be tested by comparing its values withthose from tables or pocket calculators.
Figure 3.12 shows the logarithm functionand its power-series approximations for three values of k max.Notice from Figure 3.12 that convergence of the logarithm series is very slow,as discussed in Section 3.2 for the function x ln (x). For x larger than about 0.8there are power-series truncation errors of at least 10-4 even after 50 terms havebeen included in the sum.
A similar difficulty was encountered with the power-series expansion of the function x ln (x) in Section 3.2.Having completed developing the algorithms, the coding, and the testing of eachof the six functions, they can be assembled into a program complete with file-handling or graphics output options, as we suggest in the next subsections.3.5 TESTING THE CONVERGENCE OF SERIES91FIGURE 3.12 Logarithm function (solid curve) expanded in Taylor series about unity for 2, 5,and 50 terms in the expansion.Including the hyperbolic functionsThe hyperbolic cosine and sine, cosh and sinh, whose analytical properties are covered extensively in Sections 2.3 and 3.2, are also interesting to study in terms oftheir power-series expansions.
Their Maclaurin series are almost the same (apartfrom signs) as those for the circular functions, cosine and sine. Deriving the recurrence relations for their series terms, then coding the formulas, is therefore straightforward after you have completed the circular functions.Exercise 3.36(a) By analogy with the cosine, or by the connection (2.53), show that the Maclaurin series for the hyperbolic cosine can be written as(3.77)where the successive terms are related by the recurrence relation(3.78)(3.79)(b) Similarly to (a), but for the hyperbolic sinh, show that92POWER SERIES(3.80)with the recurrence relation(3.8 1)(3.82)(c) Code and test functions PScosh and PSsinh for the hyperbolic cosine andhyperbolic sine, respectively. These hyperbolic functions can be obtained fromthe exponential functions through the definitions (2.48) and (2.49).
nThese two functions can then be incorporated in the complete program that is suggested below.File output and graphics optionsAs you have seen from the preceding four figures, it is usually much easier to interpret numerical results, especially comparison of values, if the comparison is displayed graphically as well as numerically. At the debugging stage numerical output wasprobably sufficient for you. In order to see both the dependence of the power seriesapproximations on x and k max, graphics are essential.
To appreciate this, realizethat about 100 numerical values are used to construct each of the figures.In the composite program in the next subsection, I have included a simple section for file output . One file is provided for each of the six functions, and this filemay either be written over or added to at the start of program execution. By usingsix files you will not be constrained to calculate the series in any particular order.After you have programmed the file output sections, perhaps testing your ideasbeforehand on a short program, you may either include a graphics section in thisprogram or interface the output in the file to a separate graphics program. The latterwas my choice, so there are no graphics commands in the following program.The composite program for the functionsAfter the considerable preliminaries in the two preceding subsections, you should beready to join the function modules together to form the program Power SeriesConvergence.