12.07.2015 Views

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

28 chapter 1Your problem is to use this series to calculate sin x for x2π, with anabsolute error in each case of less than 1 part in 10 8 . While an infinite series is exactin a mathematical sense, it is not a good algorithm because we must stop summingat some point. An algorithm would be the finite sumsin x ≃N∑n=1(−1) n−1 x 2n−1(2n − 1)!(algorithm). (1.15)But how do we decide when to stop summing? (Do not even think of saying, “Whenthe answer agrees with a table or with the built-in library function.”)1.6.1 Numerical Summation (Method)Never mind that the algorithm (1.15) indicates that we should calculate(−1) n−1 x 2n−1 and then divide it by (2n − 1)! This is not a good way to compute. Onthe one hand, both (2n − 1)! and x 2n−1 can get very large and cause overflows, eventhough their quotient may not. On the other hand, powers and factorials are veryexpensive (time-consuming) to evaluate on the computer. Consequently, a betterapproach is to use a single multiplication to relate the next term in the series to theprevious one:(−1) n−1 x 2n−1(2n − 1)!=⇒ nth term =−x 2 (−1) n−2 x 2n−3(2n − 1)(2n − 2) (2n − 3)!−x 2× (n − 1)th term. (1.16)(2n − 1)(2n − 2)While we want to ensure definite accuracy for sin x, that is not so easy to do. Whatis easy to do is to assume that the error in the summation is approximately the lastterm summed (this assumes no round-off error, a subject we talk about in Chapter 2,“Errors & Uncertainties in Computations”). To obtain an absolute error of 1 part in10 8 , we then stop the calculation whennth term∣ sum ∣ < 10−8 , (1.17)where “term” is the last term kept in the series (1.15) and “sum” is the accumulatedsum of all the terms. In general, you are free to pick any tolerance level you desire,although if it is too close to, or smaller than, machine precision, your calculationmay not be able to attain it. A pseudocode for performing the summation is✞☎term = x , sum = x , eps = 10^(−8) // Initialize dodo term = −term∗x∗x/((2n−1) + (2n−2) ) ;/ / New wrt oldsum = sum + term / / Add termwhile abs ( term/sum) > eps / / Break iterationend do✝−101<strong>COPYRIGHT</strong> <strong>2008</strong>, PRINCET O N UNIVE R S I T Y P R E S SEVALUATION COPY ONLY. NOT FOR USE IN COURSES.ALLpup_06.04 — <strong>2008</strong>/2/15 — Page 28

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!