This notebook contains the Mathematica commands from Chapter 5 of Introduction to Scientific Programming by Joseph L. Zachary.

(5.3)   We compute the distance to the horizon using the straightforwad approach.

Sqrt[(2.09*^7 + 66)^2 - (2.09*^7)^2]

(5.4)  We simulate what would happen if Mathematica provided floating-point numbers with only four-digit mantissas.  First we add the radius and hill height.

2.09*^7 + 66.

(5.5)  Then we round the result to four digits and square it.

(2.090*^7)^2

(5.6)  Next we square the radius.

(2.09*^7)^2

(5.7) We subtract the four-digit version of (5.6) from (5.5).

4.386*^14 - 4.386*^14

(5.8)  Finally, we take the square root of the result from (5.7), which is what Mathematica would have produced as a result if its floating-point numbers had four-digit mantissas.

Sqrt[0]

(5.11)  Now we simulate what would happen with four-digit mantissas using the improved method for computing the distance to the horizon.  First we double the radius.

2 * 2.09*^7

(5.12)  Next we multiply this result by the hill's height.

4.18*^7 * 66.

(5.13)  We square the hill's height.

66.^2

(5.14)  We add the four-digit versions of the results from (5.12) and (5.13).

2.759*^9 + 4356.

(5.15)  And finally we take the square root.  52530 is what Mathematica would have produced as a result if its floating-point numbers had four-digit mantissas.

Sqrt[2.759*^9]

(5.16)  This is the distance to the horizon from a 100 foot hill.

Sqrt[2 * 2.09*^7 * 100 + 100^2]

(5.17)  We define a function called "horizon" that encapsulates distance to the horizon calculations.

horizon[R_, h_] := Sqrt[2*R*h + h^2]

(5.18)  We use "horizon" to compute the distance to the horizon from the top of a 100 foot hill.  The answer is the same as in (5.16).

horizon[2.09*^7, 100]

(5.20)  We use "horizon" to compute the distance to the horizon from Kill Devil Hill using interval arithmetic.  This is the maximum possible distance ...

horizon[2.095*^7, 66.5]

(5.21)  ... and this is the minimum possible distance.

horizon[2.085*^7, 65.5]

(5.23)  We create a programmer-defined function "horizonFast" to calculate the distance to the horizon using an algebraically suspect but computationally acceptable approximation.

horizonFast[R_, h_] := Sqrt[2*R*h]

(5.24)  We use "horizon" to compute the distance to the horizon at high tide ...

horizon[2.09*^7 + 5, 66 - 5]

(5.25)  ... and at low tide.

horizon[2.09*^7 - 5, 66 + 5]

(5.26)  We compute the distance to the horizon from Kill Devil Hill when our eyepoint is three feet above the summit.

horizon[2.09*^7, 69]