# This worksheet contains the Maple commands from Chapter 4 of # Introduction to Scientific Programming by Joseph L. Zachary. # # (4.2) We use rational arithmetic to add up the reciprocals of the # first ten even numbers. The answer is exact. # > rat10 := 1/2 + 1/4 + 1/6 + 1/8 + 1/10 + 1/12 + 1/14 + 1/16 + 1/18 + > 1/20; # # (4.3) This is a ten-digit floating-point approximation to the # rational sum computed in (4.2). # > evalf(rat10); # # (4.4) We use floating-point arithmetic to add up the recriprocals of # the first ten even numbers. We get the same answer as in (4.3). # > float10 := 1/2. + 1/4. + 1/6. + 1/8. + 1/10. + 1/12. + 1/14. + 1/16. + > 1/18. + 1/20.; # # We change over to two-digit mantissas. # > Digits := 2; # # (4.5) We repeat (4.4) using two-digit floating-point numbers. The # answer is significantly different. # > float10 := 1/2. + 1/4. + 1/6. + 1/8. + 1/10. + 1/12. + 1/14. + 1/16. + > 1/18. + 1/20.; # # (4.6) Adding an 11th term onto the sum from (4.5) has no effect. # > float11 := float10 + 1/22.; # # (4.7) We load the blocks package from the custom Maple library that # accompanies this book. # > with(blocks); # # We switch back to ten-digit mantissas. # > Digits := 10; # # (4.8) We use two functions from the blocks package to repeat the sums # from (4.4) and (4.2). # > blockFloat(10); blockRat(10); # # (4.9) We display the number of seconds of computer time that Maple # has consumed since it was started. # > time(); # # (4.10) We display the elapsed time before and after the evaluation of # a call to blockFloat. The difference is the number of seconds of # computer time used by blockFloat to do its calculations. # > time(); blockFloat(100); time(); # # (4.11) We time blockFloat in a different way so that the third value # displayed in the actual elapsed time. # > start := time(); blockFloat(100); time() - start; # # (4.12) We follow the first two commands from (4.11) with colons # instead of semicolons to suppress the display of their results. Only # the time required to run blockFloat is shown now. # > start := time(): blockFloat(100): time() - start; # # (4.16) We exploit a much better method for computing the sum of the # first trillion even terms of the harmonic series. # > evalf(1/2 * (Psi(1e12 + 1) + gamma)); # # (4.17) The ! operator computes the factorial function. # > 5!; >