This notebook contains the Mathematica 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 six-digit floating-point approximations to the rational sum from (4.2).

N[rat10]

(4.4)  We use floating-point arithmetic to add up the reciprocals 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.

(4.5)  We load the "Blocks" package from the custom Mathematica library "ISP" that accompanies the book.

Needs["ISP`Blocks`"]

(4.6)  We display the names of the functions contained in the "Blocks" package.

Names["ISP`Blocks`*"]

(4.7)  We calculate the sum from (4.2) using "BlockRat".

BlockRat[10]

(4.8)  We calculate the sum from (4.3) using "BlockFloat".

BlockFloat[10]

(4.9)  We calculate the same sum as in 4.8, but using simulated two-digit floating-point numbers.  Notice that quite a bit of roundoff error accumulates.

BlockPrecision[10, 2]

(4.10)  This calculates the amount of computer time it takes to calculate "BlockFloat[5000]" on your computer.

Timing[BlockFloat[5000]]

(4.11)  Here we see that, using two-digit floating-point arithmetic, the ten-term sum ...

BlockPrecision[10, 2]

(4.12) ... and the twenty-term sum produce the same answer.

BlockPrecision[20, 2]

(4.13)  If we sum the terms beginning with the smallest, however, the ten-term sum ...

BlockPrecision[-10, 2]

(4.14) ... and the twenty-term sum produce different answers.

BlockPrecision[-20, 2]

(4.18)  We can exploit a much better method for computing the sum of the first billion even terms of the harmonic series.

0.5 * (PolyGamma[1.*^9 + 1] + EulerGamma)

(4.19)  The ! operator computes the factorial function.

5!