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

(A.1)  An example of how Mathematica can automatically perform dimensional analysis.

72 in * 2.54 cm/in

(A.2)  The expression from (A.1) is equivalent to this one.  This illustrates how, in Mathematica, the multiplication operator can be omitted.

72 * in * 2.54 * cm / in

(A.3)  Here we see that Mathematica does dimensional analysis by combining and cancelling symbolic constants.  It doesn't know that one foot is 12 inches.

6 ft * 2.54 cm/in

(A.4)   If, however, we first tell Mathematica what a foot is ...

ft  =  12 in

(A.5)  ... we obtain the simplification that we expected.

\(\(
6\ ft\ *\ 2.54\ cm/in\)\)

Let's clear the definition of "ft".

Clear[ft]

(A..6)  When doing dimensional analysis, you must be careful to keep Mathematica's precedence rules in mind.   This expression doesn't simplify properly ...

360 deg / 7.2 deg * 5000. stade * 0.1575 km/stade

(A.7)   ... until we group "7.2" and "deg" together with parentheses.

360 deg / (7.2 deg) * 5000. stade * 0.1575 km/stade

(A.8)  Here we see that Mathematica doesn't always simplify units as much as might seem reasonable.  The problem here is that there are two possible ways to simplify the square root.

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

(A.9)  This is the definition of "horizontal" from Chapter 7,

horizontal[v_, theta_, t_] := v * t * Cos[theta]

(A.10)  this is the definition of "vertical",

vertical[v_, theta_, t_] := v * t * Sin[theta] - 1/2 * g * t^2

(A.11)  and this is the gravitational constant, which we have defined with appropriate units.

g = 9.8 m/sec^2

(A.12)  The call to "horizontal" yields a properly dimensioned result,

horizontal[100 m/sec, Pi/4., 10 sec]

(A.13)  as does this call to "vertical".

vertical[100 m/sec, Pi/4., 10 sec]

(A.14)  Adding a unit to Pi/4 doesn't work, however, because the built-in "Cos" function isn't expecting it.

horizontal[100 m/sec, Pi/4. rad, 10 sec]

(A.15)  Units don't mix well with plotting commands either, as both this example ...

Plot[vertical[100 m/sec, Pi/4, t],
            {t, 0 sec, 20 sec}]

(A.16)  ... and this example illustrate.

Plot[vertical[100, Pi/4, t], {t, 0, 20}]

(A.17)  Mathematica's "Units" package provides help with unit conversions.

Needs["Miscellaneous`Units`"]

(A.18)  The "Convert" function requires that you spell the names of the units exactly as it expects.

Convert[60 Mile/Hour,  Kilo Meter/Second]

(A.19)  Here is an example of how calculations can be typeset in Mathematica.

rat10 = 
      (1/2) + (1/4) + (1/6) + (1/8) + (1/10) + (1/12) + (1/14) + (1/16) + (1/18) + 
        (1/20)

(A.20)  Here is an example involving a radical and an exponent.

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

(A.21)  Typesetting can be used when defining functions, as illustrated here ...

horizon[R_, h_]  :=  (Sqrt[2  Rh + (h^2)])

(A.22)  ... and here.

compound[p_, r_, n_, m_]  :=  p (((1 + (r/m)))^(m n))

(A.23)  Here is one example of typesetting a Greek letter,

horizontal[100,  (π/4),  10]

(A.24)  and here is another.

horizontal[v_,  θ_,  t_]  :=  v t Cos[θ]

(A.25)  Here is one example of a typeset Calculus expression ...

makePower[pos_,  mass_]  :=  
      mass (∂_t) (∂_t) pos (∂_t) pos

(A.26)  ... and here is another.

(∫_0.0^8.0)(Sin[x] + 2) \[DifferentialD]x

(A.27)  The "ComputerArithmetic" package will allow you to experiment with simulated floating-point number systems.

Needs["NumericalMath`ComputerArithmetic`"]

(A.28)  We specify trhat we wish to experiment with a base-ten system with two-digit mantissas and exponents that range between -1 and 1, inclusive.

SetArithmetic[2, 10, ExponentRange->{-1, 2}]

(A.29)  In this system, 11.2 is represented by 11,

ComputerNumber[11.2]

(A.30)  0.555 is represented by 0.56,

ComputerNumber[.555]

(A.31)  112. causes an overflow, and

ComputerNumber[112.]

(A.32)  0.0555 causes an underflow.

ComputerNumber[.0555]

(A.33)  We can do arithmetic in the simulated system.  Here we find the two-digit result of dividing 1 by 8 ...

ComputerNumber[1] / ComputerNumber[8]

(A.34)  ... and here we find the two-digit result of multiplying 12 and 1.2.

ComputerNumber[12] * ComputerNumber[1.2]

(A.35)  We can change to a new simulated system at any time.  Here, we specify that the base is ten, that there are two digits of mantissa, and that the exponents range from -2 to 2.

SetArithmetic[2, 10, ExponentRange->{-2, 3}]

(A.36)  The next two expressions demonstrate that multiplication is not associative ...

quot1 = (ComputerNumber[14] * ComputerNumber[11]) * ComputerNumber[2]

(A.37)  ... in a floating-point system.

quot2 = ComputerNumber[14] *
         (ComputerNumber[11] * ComputerNumber[2])

(A.38)  We can use "InputForm" to learn more about a simulated floating-point number.

InputForm[quot1]

(A.39)  Compare this number with the one from (A.38).

InputForm[quot2]

(A.40)  "Arithmetic" shows us the details of the floating-point system currently being simulated.

Arithmetic[]

(A.41)  This is a 30-digit arbitrary-precision number.

radius = N[1/7, 30]

(A.42)  This is a 25-digit approximation of Pi.

pi = N[Pi, 25]

(A.43)  This is a 25-digit approximation of Pi/49.

area = N[Pi*(1/7)^2, 25]

(A.44)  Arbitrary-precision numbers keep track of how many digits are meaningful, as illustrated here,

Precision[radius]

(A.45) and here,

Precision[pi]

(A.46)  and here.

Precision[area]

(A.47)  We calculate a number using a 30-digit radius and a 25-digit pi.

area = pi * radius^2

(A.48)  The result has 25 digits.

Precision[area]

(A.49)  Squaring a 25-digit number produces a 25-digit number.

Precision[pi^2]

(A.50)  Digits are gradually lost as more operations are performed.

Precision[pi^4]

(A.51)  Here, we have lost five digits from the original 25-digit precision of pi.

Precision[pi^100000]

(A.52)  When converting a number into arbitrary-precision form, always specify a rational number.  Otherwise, the result will have only 16 digits of precision.

Precision[N[1.234, 50]]

(A.53)  Writing an extra-long "floating-point" number ...

x = .12345678901234567890*^20

(A.54)  ...  is another way to create an arbitrary-precision number.

Precision[x]

(A.55)  The largest

$MaxNumber

(A.56)  and smallest arbitrary-precision numbers,

$MinNumber

(A.57)  are much more extreme than the largest

$MaxMachineNumber

(A.58)  and smallest floating-point numbers.

$MinMachineNumber

(A.59)  Instead of overflowing, large floating-point results are converted into arbitrary-precision numbers ...

$MaxMachineNumber^2

(A.60)  ... unless the result is too big.

$MaxMachineNumber ^ $MaxMachineNumber

(A.62)  Once you have translated heat12.tm into a C program, and compiled the program into an executable called "heat12", you can make the "simulate" function that it contains accessible to Mathematica.

\(\(
Install["heat12"]\)\)

(A.61)  Simulate can then be invoked just as if it were built in to Mathematica.

simulate[1.752, 10., 100., 50., 0., 5., 5]

(A.63)  Similarly, we can install "heat13" ...

Install["heat12"]

(A.64)  ... and invoke its version of "simulate".

simulate[1.752, 10., 100., 50., 0., 5., 5]