Thompson - Computing for Scientists and Engineers (523188), страница 17
Текст из файла (страница 17)
nIn Figure 3.7 we compare exponential interest, which accrues continuously withtime, for an interest rate r = 0.2. Although it accumulates to nearly 20% more than3.4 REPETITION IN MATHEMATICS AND COMPUTING83simple interest, consistently with the result in Exercise 3.29 (d), after two years it isonly about 6% greater than interest compounded quarterly.The mathematics of interest schemes is developed in an interesting book by Kellison and in a module for teaching mathematics by Lindstrom. From our excursioninto schemes for calculating financial interest, you should be richer in your understanding of power series, the exponential function, and finite step sizes in approximating functions. This last topic is resumed in Chapter 4.3.4 DIVERSION: REPETITION IN MATHEMATICSAND COMPUTINGIt is interesting and useful to make a distinction between three different kinds of repetition patterns in mathematics and (especially) in computing.
We distinguish iteration, recurrence, and recursion. Although I have been careful with the usage ofthese three terms in the preceding sections of this chapter, I have not yet explainedwhat I understand by each of them. In the following their commonalities and differences will be discussed. Figure 3.8 illustrates schematically the three types of repetition.FIGURE 3.8 Three kinds of repetition in mathematics and computing.84POWER SERIESWe now describe, sequentially, how I intend these three kinds of repetition to beinterpreted.IterationBy iteration is understood repetition of some parts of an analytical or numerical calculation, reusing the same algorithm and parameters but repeating the calculationuntil some criterion, such as a convergence limit, is satisfied.
Summation of seriesis usually done by iteration over the partial sums. Examples from programming arethe C language for and whi1e statements, the Pascal FOR statement, and theFortran DO loop.The process of iteration can be indicated pictorially by the left-hand diagram inFigure 3.8. In the iteration diagram in Figure 3.8 the elongated rectangle indicatesa sequence of program steps. The sequence is entered (top, left arrow), it is executed, then it is repeated (broken arrow re-entering at the top). Eventually, some criterion is satisfied, so the iteration terminates (bottom arrow).RecurrenceBy recurrence is understood a formula to be used repeatedly to related successivevalues of a function when one of its parameters changes by uniform steps.
For example, the power-series terms in Section 3.3 and 3.5 are obtained by recurrencewith respect to the parameter k. The recurrence process is indicated pictorially inFigure 3.8. In each rectangle there is a procedure for producing this value of afunction from the preceding values. For example, the first box may have k = 1,the second box k = 2, and so on. The procedure within each box is often thesame, only the value of the control parameter (such as k) would be changing. Aswith iteration, recurrence requires a stopping criterion. Indeed, iteration and recurrence are very similar and so they are often not distinguished.In mathematical analysis recurrence is logically related to the method of proof byinduction, as used for the geometric series and the proof of Taylor’s theorem in Section 3.1. In his book on induction and analogy in mathematics, Polya discussesclearly the inductive method.RecursionIn recursion a formula refers to itself.
This is indicated in Figure 3.8 by the nestingof the boxes, which each describe the (same) function, In mathematical analysis anexpansion in continued fractions is an example of recursion. Among programminglanguages both C and Pascal allow recursive definition of functions, but Fortranusually does not allow recursion. In Fortran if recursive use of a function or subroutine is hidden from the compiler, which can be done because subroutines may becompiled independently, such an error (which is not usually diagnosed by the computer) will usually produce incorrect program execution.3.5 TESTING THE CONVERGENCE OF SERIES85In recursive program functions, a copy of the function has to be saved for eachrecursion (each box in Figure 3.8).
The number of recursions must therefore befinite, and there must be a termination path out of each function copy, as indicatedby the arrows at the bottom of each box in Figure 3.8. Recursive functions are notused in this book. Extensive discussions of the pleasures and perils of recursion inC programming are provided in the book by Kernighan and Ritchie, the authors ofthe C language. The self-referencing nature of recursive functions gives rise to interesting logical and philosophical problems, as discussed by Hofstadter in hisbook.3.5PROJECT 3: TESTING THE CONVERGENCE OF SERIESIn this project we program the functions for the Maclaurin expansions of several ofthe series that are derived in Section 3.2, then we use these functions to explore thenumerical convergence of series, particularly to compare and contrast analytical andnumerical convergence properties, We made a beginning on the latter topic in Section 3.2, where we programmed and used the Maclaurin series for the exponentialfunction to examine its numerical convergence.One of our main goals in this project is to practice developing computer programs by first coding and testing separate modules, then to combine these into acomplete and usable program.
From this example you will readily see the advantages of such a “divide and conquer” strategy for program construction and verification.Project 3 begins with coding and checking of five Maclaurin series expansions,for the exponential (initiated in Section 3.2), the cosine and sine, the arcsine, andthe logarithm. Then there are suggestions for including the series expansions of thehyperbolic functions, as well as for file output and graphics options. After you areconvinced of the correctness of each function (by experience rather than by faith),you may combine them into a composite program Power Series Convergence.This program includes one more function, the inverse cosine, which is efficientlycomputed from the inverse-sine Maclaurin series. The programming of the arcosinefunction also illustrates how to pass functions as parameters of other functions in C.Suggestions for using the program to explore the convergence of power series arethen given.Coding and checking each series expansionIn this subsection we summarize the coding and checking of the formulas for sixfunctions of interest.
For each of them a main program, similar to that made for theexponential function in Section 3.2, may be used for checking against values obtained from tables of functions or from calculations with an electronic calculator.1. Exponential function first function whose Maclaurin expansion we computed numerically was the exponential, discussed in Section 3.2. It is called PSexpin the complete program below.
The methods of programming and testing the series86POWER SERIESexpansions for the three functions cosine, sine, arcsine, and the natural logarithm aresimilar. Numerical comparisons for the power series of the exponential function areprovided in Section 3.2 and Figure 3.3.2. Cosine function.
The cosine Maclaurin expansion may be written, by (3.20), as(3.62)where the terms, tk, are obtained by the recurrence relation(3.63)(3.64)In (3.63) the presence of the brackets around x2 indicates that this quantity may becomputed and saved before the series is begun. In the coding shown in the nextsubsection for the function PScos this argument is denoted by variable xs.Exercise 3.30Show that recurrence formula (3.63) for the expansion coefficients is consistentwith the Maclaurin expansion of the cosine function (3.20).
nChecking the cosine series may be done by using input values of x less thanunity. Values of kmax about 20 should give accuracy better than 1%. Also checkthat your programmed cosine series is an even function of x, for any value of kmax.If you use values of x more than unity, then convergence will be slow. For a bettercomputation you could use x modulo /2 and appropriate sign changes, so thatthe x value for which the series was evaluated was always less than this number.Figure 3.9 compares the Maclaurin series expansions of the cosine function fork max = 1 and 2. Notice the very rapid convergence of the series that arises fromthe approximate 1/k2 dependence of the successive terms in the series.3. Sine function. The Maclaurin expansion is obtained from (3.21), written as(3.65)in which the successive terms are obtained by using the recurrence relation(3.66)(3.67)Just as for the cosine series, the x2 in the brackets is to be precomputed and saved.3.5 TESTING THE CONVERGENCE OF SERIES87FIGURE 3.9 Power series (dashed curves) for the Maclaurin expansion of the cosine function(solid curve).Exercise 3.31Verify that formula (3.66) for the expansion coefficients of the sine functions isconsistent with the Maclaurin expansion of the sine function, (3.21).