Chapter 1 Mathematics and Mathematica

. Computers as Tools

Computation is a characteristic of human endeavor. Tools to aid computation are among the oldest human inventions. Paleolithic cultures made scratches on bones 30,000 years ago to record and possibly compute property and astronomical data. Thousands of years ago Chinese, Mayan, Arabic and Jewish cultures recorded numerical and historical information on knotted cords and notched sticks. Babylonians impressed clay, Egyptians painted papyrus, Greeks scratched wax and today we transfer graphite, ink and toner to paper. [1]

Over the ages numerous devices have been employed to aid computations, ranging from pebbles (L. calculi) and beads (Heb. abaq, L. abacus) to perform arithmetic calculations, to astrolabes (Gr. astron star + lab take), quadrants (L. quadrans fourth part), sextants (L. sextans sixth part) and clocks to recon navigation. With the advent of gunpowder in the 16th Century, these instruments were adapted to determine projectile trajectories ("ballistics"). Military applications have remaind a principle computing enterprise driving computer technological development.

Mechanical computers date from Kepler's acquaintance, Wilhelm Shickard, who built a device in 1623 with eleven sprocketed wheels to automate multiplication. Computing machines designed by Pascal [1623-1662], Leibniz [1646-1716] and others based on similar principles evolved into the mechanical "adding" machines of the twentieth Century, able to do arithmetic (addition, subtraction, multiplication, division, roots) on numbers and store results in "registers".
Realistic ballistic calculations involve the solutions of differential equations yielding fractional numerical values ("floating point, real" numbers as distinct from "natural" integers). During the first half of the 19th Century James Thompson (Lord Kelvin's brother) designed and built an analogue (continuous) Differential Analyzer and Charles Babbage designed (but did not complete) digital (discrete) Difference and Analytical Engines to perform numerical integrations. The first electromechanical computers (using relays) were constructed during World War II, principally in Germany, Great Britain and the United States. These precursors to electronic digital computers shared the three fundamental components with modern computers; control, storage and arithmetic calculation.

Computer hardware performs certain classes of calculations and stores and reports the results. Since the orchestration of the calculations and the flow of results can vary from task to task, the control function needs to be flexible and general. Computer software, or programs provides the instructions to the hardware. Much effort has gone into the design of efficient procedures (algorithms) and languages to communicate with and control electronic hardware.

One task assigned to computers during the WW II era was the encryption and decryption of text information. This led naturally to the notion that computers could process general symbolic information beyond numbers, with comparable efficiency and accuracy. A variety of programming languages have been designed to implement algorithmic, logic and symbolic mathematical processes. Some currently popular symbolic computation languages include Maple, Mathcad and Mathematica. They share a great deal of common functionality and preference is a matter of choice. We have chosen Mathematica as our computational tool for this textbook.

.1 Introduction to Mathematica

Computers process information ("data") according to prescribed operations. Compilers and interpreters are programs that translate high-level operations understandable to humans into low-level operations understood by the computer. The language at each level is clearly and precisely defined in terms of a finite set of symbols (alphabet) representing data and operations, and rules of structure (grammar) and interpretation (semantics). In the case of electronic digital computers, the low-level alphabet is binary digits (0 and 1), whereas in the case of symbolic computation languages the alphabet may consist of Arabic numbers, Roman and Greek letters and mathematical symbols such as ∀ or ∫. Symbolic computation programs are designed to process (evaluate) mathematical expressions and statements, such as [Graphics:1gr1.gif] and ∫ f(x).

The Mathematica (abbreviated MMa) symbolic computation progam has two principle components, a processing program called the "compute engine" or kernel (a German word for the inside part of a nut) and an input/output user interface, "front end" or shell (refering to the ouside part of a nut). There are two versions of Mathematica front ends, one designed for text-based, or "character" terminals (such as the DEC VT100) and one based on graphics terminals (such as the MIT xterm). In an Unix operating system the former can be invoked with the command math and the latter with the command mathematica. Both versions can perform the same operations, but the text version has input and output limitations. In the Unix math program, input is entered through the keyboard, evaluated when the Enter key is pressed and output is displayed on the video screen. Output, including graphs, is limited to keyboard characters displayed on the screen. After invoking Mathematica on a graphics terminal (say, by entering mathematica from the keyboard in Unix), input may be entered through the keyboard or by mouse selection and is evaluated when the combination SHIFT/ENTER (or the numeric pad ENTER) key is pressed, and graphical output is presented at the resolution of the graphics display.

The graphics version of Mathematica organizes input and output into segments called cells, which, in turn, are collected into notebooks that can be saved and transported. Cells may be nested inside other cells, and are denoted with vertical right-hand brackets. (If you are viewing a Mathematica notebook version of this document, note the brackets dividing the headings and paragraphs up to this point.) Cell divisions of text material provides spacing but is otherwise unimportant. Cell divisions are important for cells containing mathematical objects, since groups of expressions in cells may be evaluated together.

Mathematica notebooks are stored internally with (ASCII) character encoding which renders them portable for transfer between systems. In addition to text symbols (Latin letters and Arabic numbers), Mathematica has a rich graphical alphabet of mathematical symbols, each having an equivalent text version for generality and portability. Without exception, all Mathematica character objects are Capitalized to distinguish them from user-defined objects. This includes mathematical objects such as Pi, E (base of natural logarithms) and I (imaginary part of a complex number) and operations such as Sqrt and Sin. With some common exceptions, Mathematica objects are unabbreviated.

In ordinary mathematics, parentheses are ambiguous ("overloaded") symbols, commonly used to denote both grouping such as 2*(3+2)and function arguments as with f(x). Mathematica distinguishes these uses by restricting parentheses to grouping and uses square brackets ([]) to denote function arguments as with f[x]. This distinction is important to allow computers to determine the intended semantics (meaning) of expression.

There is always a tradeoff between generality and complexity. Mathematica, being a very sophisticated computational system, has a rather steep learning curve. Because there is so much new notation and jargon to learn, we will sometimes unexcusedly repeat terminology and concepts in our development in order to enhance their familiarity. After introducing various aspects and capabilities of Mathematica through numerous small examples, we will close this chapter with some larger examples from a selection of disciplines. These examples (and those in the remainder of the book) should convince one of the usefulness and power of tools like Mathematica in modern science.

Before delving into some Mathematica examples, we should note that there is an on-line manual for Mathematica. In both the text version and the graphics version, brief summaries of the syntax (format), semantics (interpretation) and perhaps examples of each object that Mathematica knows may be displayed by entering a question mark (?) followed immediately by the name of the object. Whole categories of objects can be referred to by using the Unix "wildcard" asterisk (*) before and/or following a string of characters. The graphics version has, in addition, a more extensive Help "Browser," invoked by selecting Help/Find in Help Browser, containing articles, definitions, demonstrations and extensive examples.

õ( Read the on-line information about some familiar objects such as the Log[] function. Identify all the known logarithm functions using wild cards.

?Log
Log[z] gives the natural logarithm of z (logarithm to base e). Log[b, z]
gives the logarithm to base b.

.1.1 Arithmetic

Mathematica understands the operations of arithmetic, including addition (+), subtraction (-), multiplication (*), division (/), exponentiation (^) and factorial (!). The objects operated on include numbers, letters, strings of letters (words), sets, arrays and more general objects. When presented with a mathematical expression, Mathematica attempts to evaluate and simplify it. If one types 2+2 and presses the ENTER key in math, or the SHIFT and ENTER keys (or the ENTER key on the keypad) in Mathematica, a numbered output cell is generated.

õ( Evaluate the following arithmetic expression by pressing ENTER in math, or by clicking the left mouse button on the bracket at the right side of the cell in Mathematica and pressing the SHIFT/ENTER keys:

2+2
     4

You may notice a delay in the first evaluation while the "compute engine" kernel is loading into memory. After that, any delays will be due to evaluations (and perhaps network transfers). (There is a Mathematica function that will report execution time; can you identify it?)

õ( Here's a slightly more sophisticated example. Are you surprised at the result?

4!/(3*2^3)
     1

As an interactive Mathematica notebook, you can change any of the input cells. Use the mouse to position the cursor to a place in the last expression you would like to modify, then use the BACKSPACE key to erase characters and type in new values. Then reevaluate the cell with the SHIFT/ENTER keys to get a new result.

õ( Modify the above example several ways to produce the same output value.

Multiplication can be inferred without the explicit use of the * symbol, when the context is obvious. Thus 2x is interpreted to mean 2*x but x2 is interpreted to mean a variable whose name is x2, and not to mean x*2. Spaces are often (but not always) ignored, so 2 + 2 is the same as 2+2 (but sometimes more readable) and 2 x is the same as 2x, or 2*x.

õ( Can you predict the output from the following example before you execute it? (* Comments inside these symbols are not evaluated *)

2x + 2 x + x2 + x 2   (* the meaning of spaces *)
     6 x + x2

Each input cell has a corresponding output cell, and both are numbered for future reference. Cell numbers are incremented automatically by default and can be referenced using the Out[] operator, abbreviated as %. This can cause some confusion if cells are evaluated out of order, for the numbering increments chronologically, which may not correspond to the lexical order shown on the screen. In Mathematica, % represents the output of the previous evaluation and %% refers to the second previous output. %n refers to output cell number n.

õ( Try first guessing the results of the following example, and then execute it (by clicking with the left mouse button on the cell bracket on the right side of the cell, and pressing the SHIFT/ENTER keys):

2 + 2
2 + %
2 + %
%%
2 + %
     4
     6
     8
     6
     8

Mathematica apparently remembers things (values, definitions, etc.). This can be a curse as well as a blessing. It provides the opportunity to recall a previous result, but if some cells are reevaluated the numbering continues where it left off. For this reason the Out[] function needs to be used with care, most safely to refer to the previous output in a series of evaluations.

After a notebook has been created to the satisfaction of the user, it is always a good idea to reevaluate the entire notebook to ensure it behaves the way one wants when it is reopened. This can be done by selecting the Kernel/Evaluation/Quit Kernel/Local/OK menu options, followed by Kernel/Evaluation/Evaluate Notebook options.

Reevaluate the cell of the previous example (by clicking with the left mouse button on the cell bracket on the right side of the cell and pressing the SHIFT/ENTER keys in Mathematica).

A better way to remember values for future reference is to assign them to user-defined variable names. This is accomplished using the equals symbol (=) between the name (on the left) and its value (on the right). Here is a simple example you may try:

x = 2
x
     2
     2

õ( Now try to guess what the following statements will produce, then execute them:

x = x + x
x
     4
     4

Notice the way the variable named x can be reassigned. By the way, since Mathematica objects are capitalized, it is a good idea to avoid capitalized names for user-defined objects.

The value of x will be remembered until it is reassigned, or cleared using the Mathematica Clear[] function (ClearAll["`*"] (with a backquote `) clears the values of all user-defined names). Remove[] is stronger and removes the names themselves. Remove["`x"] removes all user-defined names. Mathematica protects its own objects and they are not removed (unless they are first unprotected with the Unprotect[] function).

õ( Clear x and see what its value has become:

x = 2
x
Clear[x]
Names["x"]
     2
     2
     {x}

õ( Now remove x from the system:

Remove[x]
Names["x"]
     {}

A semicolon (;) at the end of an expression tells Mathematica to suppress displaying the output. It should be used with caution because Mathematica remembers values (why?).

õ( What output is going to be displayed from the following input cell?

Clear[x]
x = 2;
2x;
2x
     4

Here is a spiffy formula discovered by Leonhard Euler [1707-1783] (pronounced "Oiler"), perhaps the most prolific mathematician of all time (his papers are being republished and run to 700 volumes so far), not surprisingly called Euler's formula .

õ( Evaluate Euler's formula and note how many fundamental numerical quantities it ties together! (Note the use of a space to indicate multiplication.)

E^(I Pi)
     -1

Let's have a look at an expression which employs a couple of arithmetic functions sequentially. Note the capitalization (why?), the abbreviation (not the usual case in Mathematica) and the "nesting" of functions (using brackets inside brackets).

õ( Evaluate the following:

Log[Sqrt[123456789]] 
     Log[3 Sqrt[13717421]]

When the above expression is evaluated, it might not produce the result you expect. Why is this? This is because Mathematica works only with integers, and after it has reduced the expression to the simplest form it can using integers, the number 3 has been factored out. Floating decimal fractions are approximated as integer fractions inside Mathematica, and Mathematica is reluctant to express the results of the Sqrt[] and Log[] functions as decimal fraction values. If you want Mathematica to convert the result to a decimal fraction, you may use the N[] function:

õ( Evaluate

N[Log[Sqrt[123456789]]]
     9.3157

Now you have something you can compare with the result you would obtain with a pocket calculator. But wait a minute! Mathematica's result has only 7 significant digits. Is this due to a limitation in Mathematica? No, it is just the default number of digits displayed (default means what you get if you don't ask for something else). If you want to see more digits, add a second argument (what the functions operate on) to the N[] (numerical) function, like this (to show 20 significant figures):

?N
N[expr] gives the numerical value of expr. N[expr, n] attempts to give a
result with n-digit precision.

N[Log[Sqrt[123456789]], 20]
     9.315700883084009017

Now, try to get your pocket calculator to produce this [Graphics:1gr2.gif] óa

The question naturally arises, "What are the limits of precision using Mathematica?" The limits are determined by the amount of space (bits) used to represent numbers inside the computer. Using integer fractions to approximate decimal fractions, Mathematica can arrange to store numbers to arbitrary precision. Certainly, Mathematica has no trouble dealing with numbers more accurate than any that can be measured experimentally (limited to the definition of time measured by atomic and molecular processes to about 15 significant figures).

Before we move on, it should be noted that Mathematica may be thought of as a (large) collection of functions . Abbreviated function names have equivalent unabbreviated forms. Thus you may compare the evaluation of 5! with Factorial[5], and 2*3 with Times[2,3]. Note Factorial[] takes one argument (the variable operates on) whereas the Times[] function takes two arguments. Mathematica stores its information internally in unabbreviated form as ASCII encoded characters (the characters that are found on a keyboard), thereby rendering Mathematica notebooks portable between various types of computers ("platforms") (since ASCII encoding is an universal representation of [Graphics:1gr3.gif]

So far, we have considered some arithmetic expressions, or collections of objects (numbers) and functions (operands) that combine them into new objects, like 2+2. Mathematica treats all its objects as general expressions, including formulas, functions, graphs, etc. When presented with an input expression, Mathematica always tries to evaluate it to produce some result as output. If it cannot do so, it echos the input as output, usually with some diagnostic message and perhaps hints to improve the evaluation.

õ( Try to valuate

2+
Syntax::sntxi: Incomplete expression; more input is needed.

1.1.2 Algebra

Can Mathematica do algebra? Piece of cake. Algebra itself is tautological in the sense that evaluating (simplifying) expressions and solving (rearranging) equations employs equivalence relations which produce results that contain no more truth or information than the original expression or equation, merely a more convenient or more compact form. Since Mathematica automatically employs its Simplify[] function (among others), evaluation of algebraic expressions is almost automatic! However, there are specific Mathematica functions to perform algebraic manipulations. Here are some examples, (which you should evaluate):

Clear[x]
Expand[(1 + x)^3]
Factor[%]
                  2    3
1 + 3 x + 3 x + x
            3
(1 + x)

Note the use of the Clear[] function, always a safe way to begin a new calculation. Not using it in a large notebook can be a source of error and confusion because Mathematica remembers uncleared values of variables.

õ( See what happens when we don't clear variables:

x = 2
Expand[(1 + x)^3]
Factor[%]
     2
     27
     27

The evaluation isn't wrong, but imagine the consequence if x had been assigned a value in a previous cell out of sight (as it is in this notebook. Where?).

õ( Continuing with our examples, let's solve a simple algebraic equation:

Clear[x]
Solve[x-2 == 0, x]
     {{x -> 2}}

Note the double equals (==) "sign" or symbol. The mathematical equals symbol demonstrates another example of overloading mathematical symbols (like the parenthesis discussed above). A single equal symbol is interpreted by Mathematica to assign an expression to a user-defined symbol, or string of one or more characters (name) which can be used to refer to the expression later on. The double equals is interpreted by Mathematica as an identity or equivalence between the expressions on the right and left sides of the double equals sign. Caution: using = when one means to use == is one of the most common mistakes made using Mathematica.

Solutions to equations are expressed using arrows, which stand for something called rules in Mathematica. (An arrow can be indicated in ASCII with a dash followed by a greater than sign, ->, or as the Rule[] function). Mathematica rules are reminiscent of assignments, but are more appropriately thought of as replacements where a variable is replaced by a value. Rules are applied using the so-called replacement operator, invoked with a slash immediately followed by a period (/.) separating an expression (on its left) from a replacement rule, or list of rules (on its right). (Lists will be discussed shortly.)

?/.
expr /. rules applies a rule or list of rules in an attempt to transform
each subpart of an expression expr.

õ( This example shows one way to check the solution(s) to an algebraic equation. Note the distinction between assignment (i.e. assigning a name to an object, an equation in this case) and equivalence in the first line. (The spaces around the minus sign are for readability and are a matter of style). In the second line, the Mathematica function Solve[] is called to "solve" the equation (i.e. isolate the unknown). In the third line, the solution is applied as a rule to the equation to test the validity of the solution.

equation = x - 2 == 0
solution = Solve[equation, x]
equation/.solution
[Graphics:1gr5.gif][Graphics:1gr4.gif]
[Graphics:1gr5.gif][Graphics:1gr6.gif]
[Graphics:1gr5.gif][Graphics:1gr7.gif]

The result of calling Solve[] produces a list of possible solutions, as indicated by the curly braces ({}). In this case, there is only one solution, so there is only one element in the list. However, in the general case, there may be multiple solutions, which would be listed as separate elements in the list. Members of a list can be referenced using the Mathematica Part[] function, or its abbreviated form of double square brackets ([[]]).

?Part
expr[[i]] or Part[expr, i] gives the ith part of expr. expr[[-i]] counts
from the end. expr[[0]] gives the head of expr. expr[[i, j, ... ]] or
Part[expr, i, j, ... ] is equivalent to expr[[i]] [[j]] ... . expr[[
{i1, i2, ... } ]] gives a list of the parts i1, i2, ... of expr.

Multiple applications of the Part[] function may be needed to pick out a desired solution or part of a solution.

õ( Study the sequence of parts selected in these statements:

Part[solution,1]
Part[Part[solution,1],1]
Part[Part[Part[solution,1],1],1]
Part[solution,1,1,2]
solution[[1]]
solution[[1]][[1]]
solution[[1]][[1]][[2]]
[Graphics:1gr5.gif][Graphics:1gr8.gif]
[Graphics:1gr5.gif][Graphics:1gr9.gif]
[Graphics:1gr5.gif][Graphics:1gr10.gif]
[Graphics:1gr5.gif][Graphics:1gr11.gif]
[Graphics:1gr5.gif][Graphics:1gr12.gif]
[Graphics:1gr5.gif][Graphics:1gr13.gif]
[Graphics:1gr5.gif][Graphics:1gr14.gif]

Lists are widely used fundamental objects in Mathematica. Many Mathematica functions have a listable attribute, or capability, which allows them to operate on all members of a list simultaneously. Lists may be constructed using the List[] function or using curly braces, with the list members, or elements separated by spaces. Yet another way to construct a list is with the Table[] function. Note the use of a list to specify the index variable and its range of values.

myList = List[1,2,3]
yourList = {4,5,6}
theirList = Table[i^2, {i,1,3}]
     {1, 2, 3}
     {4, 5, 6}
     {1, 4, 9}

õ( See how arithmetic can be performed on lists:

myList + yourList + theirList
myList * yourList * theirList
     {6, 11, 18}
     {4, 40, 162}

The use of lists as ordered sets of objects gives Mathematica power to perform operations on sets of objects simultaneously, in the fashion of parallel processing.

õ( The following example shows how to solve a list of simultaneous equations for a list of variables using the Mathematica Solve[] function. Work out the answer by hand and then use Mathematica to check your result. Would you care to race Mathematica to the solution?

Clear[x,y,z]
Solve[{x+y+z == 6, 2x-y+z == 3, x+2y-3z == -4}, {x, y, z}]
     {{x -> 1, y -> 2, z -> 3}}

õ( Consider next a slightly more complicated equation:

Clear[x]
Solve[x^2 - 2 == 0, x]
     {{x -> -Sqrt[2]}, {x -> Sqrt[2]}}

Since the highest power in the variable of this equation is 2, this is a second degree, or quadratic equation, and it has two solutions, or values of x for which it is true. (In general nth degree equations have n solutions). Note that the result is given as a list of solutions. Can you pick out the positive root?

Since a list may contain only a single element, it would be possible (but redundant) to write the example involving a single equation with a single variable as lists, thus:

Clear[x]
Solve[{x^2 - 2 == 0}, {x}]
     {{x -> -Sqrt[2]}, {x -> Sqrt[2]}}

Traditional algebra courses limit treatment to general first (linear) and second (quadratic)degree equations and special cases of third (cubic) degree equations in one variable, and to systems of first-degree equations in several variables. But with the availability of powerful computational tools like Mathematica such limits need not be imposed. There is a theoretical limit however; general algebraic equations of degree higher than 5 have no analytic solutions, that is, solutions that can be expressed in terms of simple algebraic functions. Numerical solutions involving decimal fractions are available in all cases, and Mathematica provides a numerical equation solver called NSolve[] to obtain them.

õ( Compare the two ways to attempt to solve a simple 6th degree equation:

Clear[x]
Solve[x^6 == 2, x]
NSolve[x^6 == 2, x]
              1/6          1/6               1/3  1/6              1/3  1/6
{{x -> -2 }, {x -> 2 }, {x -> -((-1) 2 )}, {x -> (-1) 2 },

2/3 1/6 2/3 1/6
{x -> -((-1) 2 )}, {x -> (-1) 2 }}
     {{x -> -1.12246}, {x -> -0.561231 - 0.972081 I}, 

{x -> -0.561231 + 0.972081 I}, {x -> 0.561231 - 0.972081 I},

{x -> 0.561231 + 0.972081 I}, {x -> 1.12246}}

Note the conjugate (+,-) pairs of complex solutions.

õ( Now attempt to solve a more general 6th degree equation:

Clear[x]
Solve[x^6+x^5+x^4+x^3+x^2+x+1 == 0, x]
NSolve[x^6+x^5+x^4+x^3+x^2+x+1 == 0, x]
                 1/7             2/7              3/7             4/7
{{x -> -(-1) }, {x -> (-1) }, {x -> -(-1) }, {x -> (-1) },

5/7 6/7
{x -> -(-1) }, {x -> (-1) }}
     {{x -> -0.900969 - 0.433884 I}, {x -> -0.900969 + 0.433884 I}, 

{x -> -0.222521 - 0.974928 I}, {x -> -0.222521 + 0.974928 I},

{x -> 0.62349 - 0.781831 I}, {x -> 0.62349 + 0.781831 I}}

õ( Now note how Solve[] dies on a general 6th degree equation but NSolve[] is unaffected:

Clear[x]
Solve[6x^6+5x^5+4x^4+3x^3+2x^2+x+1 == 0, x]
NSolve[6x^6+5x^5+4x^4+3x^3+2x^2+x+1 == 0, x]
                              2       3       4       5       6
{{x -> Root[1 + #1 + 2 #1 + 3 #1 + 4 #1 + 5 #1 + 6 #1 & , 1]},

2 3 4 5 6
{x -> Root[1 + #1 + 2 #1 + 3 #1 + 4 #1 + 5 #1 + 6 #1 & , 2]},

2 3 4 5 6
{x -> Root[1 + #1 + 2 #1 + 3 #1 + 4 #1 + 5 #1 + 6 #1 & , 3]},

2 3 4 5 6
{x -> Root[1 + #1 + 2 #1 + 3 #1 + 4 #1 + 5 #1 + 6 #1 & , 4]},

2 3 4 5 6
{x -> Root[1 + #1 + 2 #1 + 3 #1 + 4 #1 + 5 #1 + 6 #1 & , 5]},

2 3 4 5 6
{x -> Root[1 + #1 + 2 #1 + 3 #1 + 4 #1 + 5 #1 + 6 #1 & , 6]}}
     {{x -> -0.703387 - 0.365055 I}, {x -> -0.703387 + 0.365055 I}, 

{x -> -0.116036 - 0.731154 I}, {x -> -0.116036 + 0.731154 I},

{x -> 0.402756 - 0.567471 I}, {x -> 0.402756 + 0.567471 I}}

Even in the case of lower degree equations Mathematica has something to teach us.
õ( Read the on-line manual about Solve[] and Reduce[] and decide which is the correct quadratic formula. The boolean logical operators ==, !=, || and && stand for equality, inequality, (either) or and (both) and, respectively.

Clear[a,b,c,x]
Solve[a*x^2 + b*x + c == 0, x]
Reduce[a*x^2 + b*x + c == 0, x]
                       2                             2
-b - Sqrt[b - 4 a c] -b + Sqrt[b - 4 a c]
{{x -> ---------------------}, {x -> ---------------------}}
2 a 2 a
                               2
-b - Sqrt[b - 4 a c]
a != 0 && x == --------------------- ||
2 a

2
-b + Sqrt[b - 4 a c]
a != 0 && x == --------------------- || c == 0 && b == 0 && a == 0 ||
2 a

c
b != 0 && x == -(-) && a == 0
b

.1.3 Geometry

Geometry (Earth measurements) grew out of the need to measure property. Euclid's [-300 C.E.] Elements, which summarized earlier developments, was one of the few books to survive the destruction of the great library at Alexandria around 650 C.E. During the Dark Ages, algebra ascended to dominate mathematical practice over geometric methods. In the first half of the 17th century, René Descartes revived Euclidean geometry and showed the equivalence between the two approaches to solving mathematical problems. Specifically, Descartes showed how to represent mathematical functions as graphs, using "Cartesian" coordinate axes. This led to the notion that there are three equivalent ways to represent (unambiguous) relationships, as tables, as graphs and as functions.

We have seen how Mathematica represents tables as lists contained between curly braces. The geometric analogue of a list (n-tuple) is an "array," or "vector.,"

õ( Here is a 3-dimensional vector and some vector operations. In this example, a list of function values (squares) is generated. Note how the syntax of the Table[] function uses a list to specify the name of the index to increment. Also note the use of nested functions to condense intermediate output in the last line. The TableForm[] function displays lists in a familiar two-dimensional format. Compare v1.v2 with v1*v2.

v1 = {1,2,3}
v2 = Table[n^2, {n,1,3}]
Sqrt[v1]
N[TableForm[Sqrt[v1]]]
v1+v2
v1.v2
[Graphics:1gr5.gif][Graphics:1gr15.gif]
[Graphics:1gr5.gif][Graphics:1gr16.gif]
[Graphics:1gr5.gif][Graphics:1gr17.gif]
[Graphics:1gr5.gif][Graphics:1gr18.gif]
[Graphics:1gr5.gif][Graphics:1gr19.gif]
[Graphics:1gr5.gif][Graphics:1gr20.gif]

Matrices can be constructed as lists of lists. The next example shows two ways to construct nested lists or matrices and illustrates the operation of matrix multiplication. It also shows an alternative way to call Mathematica functions using the double slash (//). In Mathematica, expr//f means the same thing as f[expr], i.e., apply the function f to the argument expr, and is a convenient for for tacking some function on at the end of an expression as a final transformation for display, simplification, etc.

m1 = {{1,2},{3,4},{5,6}}
MatrixForm[%]
m2 = Table[i^2+j, {i,1,2}, {j,1,3}]
MatrixForm[%]
m1.m2//MatrixForm
m2.m1//MatrixForm
m2.v2//MatrixForm
v2.m2//MatrixForm
[Graphics:1gr5.gif][Graphics:1gr21.gif]
[Graphics:1gr5.gif][Graphics:1gr22.gif]
[Graphics:1gr5.gif][Graphics:1gr23.gif]
[Graphics:1gr5.gif][Graphics:1gr24.gif]
[Graphics:1gr5.gif][Graphics:1gr25.gif]
[Graphics:1gr5.gif][Graphics:1gr26.gif]
[Graphics:1gr5.gif][Graphics:1gr27.gif]
[Graphics:1gr5.gif][Graphics:1gr29.gif]

Note that vectors are special cases of matrices and that matrix multiplication requires the matrices to be conformable, that is, the number of columns of the first matrix must equal the number of rows of the second matrix.

Since vectors and matrices are lists, particular members are selected with the Part[] function:

Part[m1, 2]
m1[[3]][[2]]
     {3, 4}
     6

Two topics related to geometry are trigonometry and graphing. Mathematica can do both within certain limitations. In the case of trigonometry, Mathematica may need to be coached into using certain trigonometry rules, or identities.

Remove[x]
Sin[x]^2 + Cos[x]^2
Simplify[Sin[x]^2 + Cos[x]^2]

Sqrt[1 - Sin[x]^2]
Simplify[Sqrt[1 - Sin[x]^2]]
           2         2
Cos[x] + Sin[x]
     1
                    2
Sqrt[1 - Sin[x] ]
                2
Sqrt[Cos[x] ]

Plots and graphs are naturally limited to two-dimensional presentations (2D projections of higher dimensional figures) with current display technology. Mathematica knows how to make many types of graphics presentations, including various kinds of plots, bar charts and pie charts. Since all Mathematica objects are expressions and Mathematica evlautes all expressions to produce an output object, a judicious semicolon is often added to the end of a graphics input line to suppress displaying the output object, but not the graph. Try leaving the semicolin out in the following example.

õ( Plot the following U.S. census continental population data using the Mathematica ListPlot[] function:

population = {{1800,5.3},{1850,23.2},{1900,76.2},{1950,151.2},{2000,281}}
ListPlot[population];
[Graphics:1gr5.gif][Graphics:1gr30.gif]
[Graphics:1gr5.gif][Graphics:1gr31.gif]

Various optional arguments (options) can be used to aid visualization. Values are assigned to options using the Rule function, usually in the form Option->value. There are many options to plotting functions to control the way they appear.

õ( Replot the following U.S. census data using the PlotJoined->True option:

ListPlot[population, PlotJoined->True];
[Graphics:1gr5.gif][Graphics:1gr32.gif]

In plotting functions, only numerical values can be displayed, so any non-numerical quantities, such as constant parameters, need to be assigned values. Also, because functions may have unlimited ranges of independent variable, the range of plotting variables needs to be specified to produce a finite plot.

õ( Here is an application of the simple Plot[] function. Determine the effect of deleting the assignment of values to the parameters A and B. Change the values of the x range to see how Mathematica automatically adjusts the function axis scale.

Clear[A,B,x]
Plot[A E^(B*x)/.{A->2, B->0.5}, {x,1,5}];
[Graphics:1gr5.gif][Graphics:1gr33.gif]

Two dimensional plots use the Plot3D[] function, which projects the three-dimensional object f(x,y) onto two dimensions (with perspective).

õ( Study the ViewPoint option to the Plot3D[] function and view the following graph from different vantage points.

Plot3D[Sin[x]*Cos[2*y], {x, 0, Pi}, {y, 0, 2 Pi}];
[Graphics:1gr5.gif][Graphics:1gr34.gif]

The next example shows the algebraic and the geometric solution to two simultaneous equations (of different degree). Here we use a trick to suppress plotting the individual functions and then subsequently plot them simultaneously. DisplayFunction is an option to the Plot[] function. The graphics objects are evaluated and assigned names (p1 and p2), and their display is suppressed with the DisplayFunction -> Identity option value. The default value of True for DisplayFunction is stored in the environment variable $DisplayFunction. Default means what is used if not changed, and in the case of plotting, the default value causes the graph to be displayed. Show[] is used to combine the graphics objects and display them when the DisplayFunction option is reset to the default value.

?Show
Show[graphics, options] displays two- and three-dimensional graphics, using
the options specified. Show[g1, g2, ... ] shows several plots combined.

õ( Show graphically where a diagonal line intersects a unit parabola.

Clear[x,y]
p1 = Plot[y = x, {x, -2, 2}, DisplayFunction -> Identity];
p2 = Plot[y = x^2, {x, -2, 2}, DisplayFunction -> Identity];
Show[p1, p2, DisplayFunction -> $DisplayFunction];
[Graphics:1gr5.gif][Graphics:1gr35.gif]

õ( Solve algebraically for the points where a diagonal line intersects a unit parabola.

Clear[x,y]
Solve[{y == x, y == x^2}, {x, y}]
     {{y -> 0, x -> 0}, {y -> 1, x -> 1}}

.1.4 Calculus

Calculus is the study of slopes of curves and areas bounded by curves (volumes bounded by surfaces in higher dimensions). Slopes and areas are called, respectively, derivatives and integrals in calculus. Curves and surfaces can be represented geometrically or analytically; analytic representations are called functions. Functions modify or operate on objects called arguments to produce new objects according to specified recipes, procedures, rules or algorithms. We are most familiar with numerical functions which operate on numerical variables (representing values of numbers that can vary) to produce new numbers, but this is only a special case. One may think of a computer program as a function that takes in input and generates output. A factory receives raw material and produces products, a nerve responds to a stimulus, and so forth.

Mathematica knows many types of functions, and for generality has a variety of mechanisms for users to define their own functions. Perhaps the simplest user-defined Mathematica functions are the pure or anonymous functions which specify transformation rules but lack function and argument names. More useful for our purposes are functions and arguments that have names so they can be referred to repeatedly. The most common type of user-defined function is the delayed function, so called because it is evaluated only when it is referenced (i.e. Mathematica remembers the function definition, but evaluates a function value only when it is referred to, or called). It has the form (syntax)

f[x_] := some expression

Note the underscore symbol (_), called a placeholder (used only on the left side) to indicate the independent variable and the colon equals combination (:=) to cause evaluation delay (another case of overloading the = symbol in mathematics).

õ( Here is a simple user-defined function:

Clear[x,f]
f[x_] := x^2

f[3]
f[Sin[y]]
f[f[x]]
     9
           2
Sin[y]
      4
x

Note that when f is called, the underscore is not used on its argument, only when f is defined is it used. This makes x a dummy variable, which stands for any expression (such as the number 3 or the function Sin[y]). Thus the argument to the function can be any kind of object, not just numbers. The last line shows an unusual self-referential or nested or iterated function, relevant to fractals. It is given to illustrate the generic nature of a function argument.

Once we have a function defined, we can apply it to various tasks.

Plot[f[x], {x,0,10}];
Solve[f[x] == 2, x]
[Graphics:1gr5.gif][Graphics:1gr36.gif]
     {{x -> -Sqrt[2]}, {x -> Sqrt[2]}}

Calculus operations can be performed on functions. The derivative function is D[], which is actually a partial derivative, meaning the derivative with respect to a specified variable when the function has more than one independent variable. In the case of a function of a single independent variable, the Derivative[] function can be used, or a single forward quote (') in shorthand form. Of course, if a function has only one independent variable, its partial derivative is its total derivative.

The integral function is Integrate[]. Both D[] and Integrate[] need to specify the variable to be operated on since functions can have multiple independent variables. Integration and differentiation are inverse processes in the sense that differentiation of an integrated function and integration of a differentiated function both return the original function. This is easily recognized in the case of a power function.

õ( Read the online descriptions of the total derivative,or Derivative[] function and the partial derivative, or D[] function,and the integration or Integrate[] function to see how the function and the variable of interest are specified.

?D
?Integrate
D[f, x] gives the partial derivative of f with respect to x. D[f, {x, n}]
gives the nth partial derivative of f with respect to x. D[f, x1, x2,
... ] gives a mixed derivative.
Integrate[f, x] gives the indefinite integral of f with respect to x.
Integrate[f, {x, xmin, xmax}] gives the definite integral of f with
respect to x from xmin to xmax. Integrate[f, {x, xmin, xmax}, {y, ymin,
ymax}] gives a multiple definite integral of f with respect to x and y.

õ( Demonstrate the inverse relationship between differentiation and integration for a simple function.

Clear[x,f,n]
f[x_] := x^n
f[x]

D[f[x], x]
Integrate [%, x]

Integrate[f[x], x]
D[%, x]
      n
x
        -1 + n
n x
      n
x
      1 + n
x
------
1 + n
      n
x

Differential equations contain derivatives and their solutions are the corresponding integrated forms. They may be easy to write down but difficult to solve in the general case. Solutions to algebraic equations involving functions produce numbers, but solutions to differential equations involving derivatives produce functions. So differential equations may be thought of as one level higher in abstraction than algebraic equations. Differential equations are classified in terms of the highest derivative they contain, called the order of the differential equaiton, and the number of independent variables they involve, total in the case of one variable and partial in the case of more than one variable. The simplest differential equation, then, is the first order total differential equation (containing first derivatives of one independent variable). Mathematica has powerful methods for integrating (solving) differential equations, and numerical techniques for cases in which analytical solutions don't exist.

õ( Read the online descriptions of the DSolve[] and NDSolve[] functions to see how the function is specified, how the independent variable is defined, how the solution function is specified, and in the case of NDSolve[], how the range of independent variable is specified.

?DSolve
?NDSolve
DSolve[eqn, y, x] solves a differential equation for the function y, with
independent variable x. DSolve[{eqn1, eqn2, ... }, {y1, y2, ... }, x]
solves a list of differential equations. DSolve[eqn, y, {x1, x2, ... }]
solves a partial differential equation.
NDSolve[eqns, y, {x, xmin, xmax}] finds a numerical solution to the
ordinary differential equations eqns for the function y with the
independent variable x in the range xmin to xmax. NDSolve[eqns, y, {x,
xmin, xmax}, {t, tmin, tmax}] finds a numerical solution to the partial
differential equations eqns. NDSolve[eqns, {y1, y2, ... }, {x, xmin,
xmax}] finds numerical solutions for the functions yi.

õ( Solve the differential equation describing Malthusian growth, which states that the rate of change of the population is proportional to the current population. Rate of change means time derivative. So, if the population at time t is p(t), Malthus' law becomes

[Graphics:1gr37.gif] = kp(t),

where the proportionality constant k is a parameter characteristic of the system, sometimes called the specific rate because it is equal to the rate for the case p(t) = 1 (unity).

Remove[t,p]
DSolve[D[p[t],t] == k*p[t], p[t], t]
                k t
{{p[t] -> E C[1]}}

The solution produces a function [Graphics:1gr38.gif]and a constant (C[1]), used to select a particular solution from the family of functions that satisfy the differential equation. The value of the constant can be determined from a particular situation, such as the population at some beginning of time, or initial condition. This particular solution can be incorporated in the differential equation as an additional algebraic equality Hence DSolve[] can solve systems of differential and algebraic equations together! We will use the shorthand notation for derivatives in this extension of the previous example that includes the initial condition that p(t) at time zero equals p0.

Remove[t,p,soln]
soln = DSolve[{p'[t] == k*p[t], p[0] == p0}, p[t], t]
                k t
{{p[t] -> E p0}}

We can generate a visual representation of the resulting population explosion using the graphing functions of Mathematica, but this requires picking out the proper part of the solution, expressed as a list of lists (note the double braces above), and specifying numerical values for the parameters (as a list of rules) so tha Plot[] is dealing only with numbers.

õ( Study the following frequently used construction to convert expressions into plots:

solutionFunction = soln[[1]][[1]][[2]]
      k t
E p0

Plot[solutionFunction/.{k->1,p0->1}, {t,0,5}];
[Graphics:1gr5.gif][Graphics:1gr39.gif]

.3 Libraries

Mathematica has a rich storehouse of tools, but the computers to employ them have practical limitations. For this reason, certain groups of functions are stored in Mathematica libraries, called packages that may be called upon when needed using the Needs[] function, or its abbreviation <<.

Suppose we are interested in unit conversions. These are kept in a library package in some versions of Mathematica.

õ( Convert speed:

<<Miscellaneous`Units`    (* Load library package *)
75 Mile/Hour
Convert[%, Feet/Second]//N
[Graphics:1gr5.gif][Graphics:1gr40.gif]
[Graphics:1gr5.gif][Graphics:1gr41.gif]

Some things to observe about this example are the backquotes (`)needed in the syntax of the package subcategory, and the natural space between the value of a quantity and its units, representing multiplication. A * could have been used instead of the space because, from a mathematical point of view, units and values (numbers) are manipulated the same way in arithmetic. The second thing to note is that the units are completely spelled out (and capitalized as usual). Also note the plural form of Feet used here (try it using Foot), and note how combined units are expressed as arithmetic expressions (using the division symbol in this case). Finally, a decimal numerical value is obtained by applying the N operator at the end.

Most unit conversions involve multiplications and divisions. More complicated formulas are involved with temperature conversions, however.

õ( Convert temperature:

<<Miscellaneous`Units`    (* Load library package *)
Convert[20 Celsius, Fahrenheit]
ConvertTemperature[20, Celsius, Fahrenheit]//N
Convert::temp: Warning: Convert[old,new] converts units of temperature.    
ConvertTemperature[temp,old,new] converts absolute temperature.
     36 Fahrenheit
     68.

The Mathematica environment variable $Packages shows which packages are loaded into the kernel.

$Packages
[Graphics:1gr5.gif][Graphics:1gr42.gif]

.4 Larger Examples

.4.1 Fitting Data

Observations of relationships may be expressed as tables of independent and dependent variables. In the simplest case of one independent and one dependent variable, these may be plotted as pairs of points in Cartesian coordinates. It may be possible to summarize and possibly analyze the behavior relationship with a mathematical functional relation that fits or reproduces the observations (at least to some degree of accuracy). The function contains variables describing the observations and parameters, which are special variables that remain constant for a given system but vary between systems of a given class described by the form of the function.

The fitting of data is ordinarily done by choosing a fitting fuction, then determining values for the parameters of the fitting function according to some criterion of "good" or "best" fit. The least squares fitting criterion specifies that the fitting equation curve pass optimally as close to the data points as possible in the sense that the sum of the squares of the distances between the points and the curve be a minimum possible value. (Squaring the distances avoids an artificial minimization from adding positive and negative distances together.) Mathematica provides a variety of fitting procedures. Linear fitting is called regression and is provided on many handheld calculators. Sometimes relationships are linear, or proportional, or may be made so by transforming the data. Often, however, relationships are not so simple. Nonlinear fitting treats more general cases (including linear cases) but requires more sophisticated computations.

õ( Find the various Fit functions and read the online description of the Fit[] functions to see how the data is incorporated, how the fitting function is defined.

?*Fit*
?Fit
BestFit                Fit                    FitResiduals
BestFitParameters FitCurvatureTable NonlinearFit
BestFitParametersDelta
[Graphics:1gr5.gif][Graphics:1gr43.gif]

One of the dangers of using an automated procedure to fit data is illustrated by the fact that any n data points can be fit exactly by an n-1 degree polynomial. But the higher the degree of the polynomial the more "wiggles" (extrema) the polynomial has. Thus it may be unrealistic to to interpolate or extrapolate new values from a high degree polynomial. It can be quite useful to monitor the fit by visualizing the data and fit together.

õ( Suppose we are given the following data and want to fit it. We first try a linear least squares fit (x to the zeroth power and x to the first power functions). Try different polynomial fits and see how well they extrapolate to the value at x = 5.

data = {{-4,.1},{-3,2},{-2,16},{-1,35},{0,51},{1,37},{2,14},{3,3},{4,0}};

Clear[x]
Print["Fit = ", fitF = Fit[data, {1,x}, x]]

Show[ListPlot[data, Prolog->PointSize[.03], DisplayFunction->Identity],
Plot[fitF,{x,-4,4}, DisplayFunction->Identity,
PlotRange->{{-4,4},{0,52}}],
DisplayFunction->$DisplayFunction];

fitF/.x->5
[Graphics:1gr5.gif][Graphics:1gr44.gif]
[Graphics:1gr5.gif][Graphics:1gr45.gif]
[Graphics:1gr5.gif][Graphics:1gr46.gif]

õ( Read the online descriptions of the LinearFit[] and NonlinearFit[] functions to see how the data is incorporated, how the fitting function is defined and how the parameters are specified. In the case of NonlinearFit[], if starting guess values for the parameters are not given, Mathematica assigns them default values of unity.

<< "Statistics`NonlinearFit`"  (* Load package *)
?NonlinearFit
[Graphics:1gr5.gif][Graphics:1gr47.gif]

õ( Study the following (artificial) data described by a nonlinear bell curve or gaussian curve, defined as the exponential of a square. Try different starting guesses for the values of the fitting parameters A and b. Vary the data slightly to see the effect on the fitting coefficients, A and b (this is called sensitivity analysis). Add a ShowProgress->True option to the NonlinearFit[] function to follow the course and goodness of the fit.

data = {{-4,.1},{-3,2},{-2,16},{-1,35},{0,51},{1,37},{2,14},{3,3},{4,0}};

<< "Statistics`NonlinearFit`" (* Load package *)

Print["Fit = ", fitF = NonlinearFit[data, A*Exp[-b*x^2], x, {{A,1},{b,.5}}]]

Show[ListPlot[data, Prolog->PointSize[.03], DisplayFunction->Identity],
Plot[fitF,{x,-4,4}, DisplayFunction->Identity],
DisplayFunction->$DisplayFunction];
        50.2908
Fit = ------------
2
0.314495 x
E
[Graphics:1gr5.gif][Graphics:1gr48.gif]

Note the assignment of a name to the fit for future reference (data), the use of the Print[] function to report the fit, and how the point and curve plots are combined by first suppressing the output and then releasing it with the Show[] function.

.4.2 Balancing Chemical Reactions

John Dalton's great insight was the recognition that chemical substances consist of stable collections, or molecules of fundamental indestructible particles called atoms, and that chemical processes, or reactions consist of rearrangements of the atoms of reactant molecules to produce new product molecules. This explained the conservation of mass that was noted in careful observations of chemical reactions. It also laid the foundation for predicting the amounts of reactants and products involved in reactions (stoichiometry). Central to the chemical process is the balanced chemical reaction, which shows at the molecular level the conservation of atoms during their rearrangement. The process of balancing a chemical reaction involves determining the numbers of each molecule needed to maintain the conservation of atoms as they are rearranged from reactants to products. This process is essentially a mathematical one based on solving simultaneous equations. We would like to use Mathematica to automate balancing chemical reactions.

Chemical reactions involve molecules, which are collections of atoms of fixed composition or number of atoms. Since the molecules are of fixed composition, their numbers of atoms (molecular formula) cannot vary. What can vary is the number of molecules involved in the reaction. This number of molecules is called the balancing coefficient of the molecule when the reaction is written as an algebraic identity, or chemical equation, since it is written as a prefix to the molecular formula. The balancing procedure treats the balancing coefficients as unknowns to be determined by solving a set of linear equations expressing the conservation of matter (atoms) on both sides of the reaction equation. This means conservation of nuclei (mass) as well as electrons (charge).

There is one additional mathematical consideration, however. There will invariably be one more unknown coefficient than there are elements, leading to n equations in n+1 unknowns. This is due to the fact that a balanced chemical reaction does not have an unique set of balancing coefficients. If the number of each species is doubled, or halved, or multiplied by any amount, the reaction remains balanced. This ambiguity is expressed mathematically by the condition that only ratios of pairs of coefficients relative to one arbitrary value of one of the coefficients can be determined. This is reflected in the n equations in n+1 unknowns. As a practical matter, an arbitrary value can be assigned to one of the coefficients to provide an additional equation. This eliminates ratios and produces absolute values for the balancing coefficients. The result of solving the equations will produce fractions in the general case, however, and the conventional set of coefficients is the smallest set of integers satisfying the balancing conservation conditions. These considerations lead to the following procedure.

Method:
1. Assign unknown stoichiometric coefficients to each molecule.
2. Set the number of atoms of each element on each side of the reaction equal to each other. Do the same for the sum of the molecular charges of all molecules.
3. Set one of the stoichiometric coefficients arbitrarily equal to 1.
4. Solve the set of resulting equations for all unknowns .
5. Clear any fractions.

õ( Example: a [Graphics:1gr49.gif] + b Co[Graphics:1gr50.gif] = c [Graphics:1gr51.gif] + d CoO + e NO + f [Graphics:1gr52.gif]

Remove["`*"]
n = 6; (* number of unknown stoichiometric coefficients *)
coeffs = {a,b,c,d,e,f}; (* list of unknown coefficients *)

solns = Solve[{ (* Pb *) a == 3f,
(* N *) 2*3a == e,
(* Co *) b == d,
(* Mn *) 3b == c,
(* O *) 3*4b == 2c + d + e + 4f,
a == 1 },
coeffs][[1]]

frxns = coeffs/.solns;
Table[ReplacePart[solns[[i]],
solns[[i]][[2]]/Apply[GCD,frxns],2],{i,1,n}]
                   22       22       22               1
{a -> 1, b -> --, c -> --, d -> --, e -> 6, f -> -}
15 5 15 3
     {a -> 15, b -> 22, c -> 66, d -> 22, e -> 90, f -> 5}

Note the use of the part operator ([[]]) to pick out physically realistic algebraic solution from the mathematically correct but physically unreal solutions (reexecute the cell to generate all 6 solutions), and the use of the replacement operator (/.) to assign values to the coefficients. The last line is fairly complicated and should be studied carefully to understand how it works. Read up on the GCD[] (Greatest Common Denominator), Apply[] and ReplacePart[] functions. Some interaction with Mathematica may be needed to obtain the lowest set of integer balancing coefficients.

.4.3 A Cooling Law

The first model describing the rate of cooling of a hot object not surprisingly comes from the inventor of the theory of rate processes, Isaac Newton (1642-1727), that theory being the calculus. Newton's Cooling Law describes the rate of cooling of an object hotter than its surroundings. Rate means change in something with time, defined as the ratio of the change in something to the change in time. Change means a difference between a final state and an initial state. For example, the speed of a moving object is measured by the change in distance D with time t, symbolized as the ratio [Graphics:1gr53.gif]. The limiting rate for an infinitesimal time change in calculus is the derivative, so the speed is [Graphics:1gr54.gif]. (Difference, derivative and distance all begin with the letter d; to distinguish them with one-letter abbreviations, we have used &Dgr;, d and D, respectively.) Thus rate of change means time derivative.

Newton's Cooling Law is expressed in terms of the difference between the temperature of the object and the temperature of its surroundings, call it x. (We have used x for this difference instead of &Dgr;t or &Dgr;T to avoid the confusion of d, D and &Dgr; for all the quantities whose names begin with the letter d, as well as t and T for the quantities whose names begin with the letter t.) Given this simplification of notation, the law states that the rate of change of this temperature difference is proportional to the temperature difference x, or

[Graphics:1gr55.gif] = k x

where k is a proportionality constant, which will have a negative value if the object cools (why?).

What we have is not an explicit statement of the cooling law as temperature difference x as a function of time, but rather an implicit statement of the law in terms of the time derivative of the temperature difference. The equation involved is a differential equation because it involves derivatives, and the first function to use to try to solve it is DSolve[]. We will add a minus sign to k so the value of k is a positive number, (for convenience), and an initial temperature difference x0 to select a particular solution (for practicality).

Remove["`*"]
DSolve[{x'[t] == -k*x[t], x[0] == x0}, x[t], t]
Plot[%[[1]][[1]][[2]]/.{k->1,x0->1}, {t,0,5}];
                x0
{{x[t] -> ----}}
k t
E
[Graphics:1gr5.gif][Graphics:1gr56.gif]

.4.3 Constrained Growth

Populations grow as the number of reproducing members increases. Biological population models describe the growth rate as a derivative of change in population with time, [Graphics:1gr57.gif]. In the simplest case the rate is constant and describes a stable population with no growth (or decline). A more interesting situation is the next simplest case, growth rate is proportional to population. This is reasonable for the organisms that continuously reproduce; the size of each new generation being proportional to the size of the previous reproducing members. Bacterial infections, epidemics and dominating species grow according to such laws, at least initially. The population model of Thomas Robert Malthus (1766-1834) in 1797 results in unlimited (exponential) growth according to such a description. Eventually, natural systems reach diminishing returns as the result of diminishing resources or other limiting processes (war, disease, famine). Other environmental factors such as the rise of predators should be incorporated into realistic models. Here we will restrict ourselves to the simplest limited growth model due to P. Verhulst in 1847, which he called logistic growth:

[Graphics:1gr58.gif] = kP(t)*(L - P(t)),

where k is a growth parameter and L is a limiting resource parameter. It is seen that initially, when P is small, the growth rate is proportional to P (the Malthus model), but eventually as P approaches L the growth rate falls to zero (why?). The question arises, what is the value of the population in the intermediate case, or rather, what does the population look like as a function of time? This question is answered by solving the differential equation for the growth rate. We will simplify the equation by considering the case where L = 1 (equivalent to considering a reduced population p = P/L).

Remove["`*"]
soln = DSolve[{p'[t] == k*p[t]*(1-p[t]), p[0] == p0}, p[t], t]
                    k t
E p0
{{p[t] -> ----------------}}
k t
1 - p0 + E p0

We can now evaluate p for any given time, or plot p as a function of time to display the general trend. Since plots need numbers, we will assign a value to the initial population, p0.


Plot[soln[[1]][[1]][[2]]/.{k->1,p0->.02}, {t,0,10}];
[Graphics:1gr5.gif][Graphics:1gr59.gif]

This so called sigmoid (or s-shaped) curve is characteristic of many growth situations.

õ( Vary the parameters of the Verhulst equation and explain the results.

.5 Creating Notebooks

Since Mathematica is primarily a mathematics tool, the default input to a notebook is of input type, or style. This means it will be evaluated as mathematical input. Text, on the other hand, needs no evaluation. When text is mixed with math, the text can be identified as such by highlighting it (or cells containing text) with the mouse and selecting the Format/Style/Text options. Similarly, headings can be identified, causing them to stand out. The Format feature of Mathematica has a variety of options to specify headings, formatting, fonts and general presentation style that can be applied to cells and selected portions of cells (using a mouse).

õ( Experiment with various Format features on your notebook of examples.

.6 Discussion

Mathematica can be thought of as a highly sophisticated pocket calculator. Like many calculators, it can do arithmetic, evaluate formulas and plot graphs. It has been said that Mathematica has the capability to perform all the manipulations commonly encountered in grade school through high school, and most of undergraduate college mathematics. It is a powerful tool for research in mathematics and science as well. At present, Mathematica executes on computers, but, in the sense that hand calculators are mini computers, Mathematica could run on a pocket-sized calculator if it were sufficiently powerful.

Not only does Mathematica know literally thousands of functions, it it capable of evaluating new objects designed by the user. In this sense it is a programming language similar to those used to instruct computers. Mathematica also has the capability to read programs written in other languages such as C and Fortran, and has interfaces to text-processing languages such as Latex, PostScript and HTML. When one considers the generality and flexibility of Mathematica not only to store information but to process it and present it, one can see the advantages of writing interactive "textbooks," such as this one, in Mathematica.

One advantage of using mathematics to model physical systems is the ability to bring the tools of mathematical manipulation and logic to bear in analyzing and applying the model. Another feature is the abstract nature of mathematics, which is independent of the existence of any observable system. Note that Malthus' population law and Newton's cooling law have exactly the same mathematical form (except possibly the sign of the proportionality parameter, although both laws can accommodate positive and negative constants with realizable interpretation). Thus, once one model is understood, it may be applied to an apparently unrelated system simply by reinterpreting the meaning of the symbols in the formulas. This generates the notion of classes of systems obeying the same laws and having the same behavior.

We have used Mathematica to illustrate how the hard sciences employ mathematics as the language to describe physical systems. We have not discussed how the mathematical statements are created, only how they are manipulated (solved). As powerful a tool as Mathematica is, it is of limited use in the creative process of scientific description, called modeling. However, it very valuable for exploring different models to verify their usefulness and suggest improvements. We will explore that subject shortly, but mention here that a tool like Mathematica is quite useful in assisting (forcing) us to express our concepts and thinking in clear, precise language. Often it is the interaction between the Mathematica programmer and the program which keeps us honest, helps us formulate problems in correct ways, and actully assist us in creating solutions.

Footnotes

1. Some expensive calculators can calculate 20 significant digits, but all of them will soon fall behind Mathematica's accuracy. Actually, many calculators have higher intrinsic precision than most computers, which are limited to 7 significant digits using 16 bit words and 14 using 32 bit words.

2. What about alphabets and numbers besides Latin and Arabic? Mathematica can display Greek, Hebrew, etc. In the graphics version of Mathematica, tables of symbols, called palettes can be displayed and entries selected and pasted into notebooks. Standard palettes can be invoked by selection from the FILE/PALETTES option.

Further Reading

[Graphics:1gr60.gif] A History of Computing Technology, Second Ed., Michael R. Williams, IEEE Computer Society Press, 1997.

[Graphics:1gr61.gif] The Mathematica Book, third Edition. Stephan Wolfram, Wolfram Media, Inc., 1997.

[Graphics:1gr62.gif] What is Mathematics, Really. Reuben Hersch, .

Web sites

. System`Convert`HTMLDump`HyperlinkAnchor[{URL[http://www.wri.com], None}, System`Convert`CommonDump`InlineCell -> True]Wolfram Research Institute (the MMa home page)

. System`Convert`HTMLDump`HyperlinkAnchor[{URL[http://library.wolfram.com/tutorials/], None}, System`Convert`CommonDump`InlineCell -> True]WRI tutorials

. System`Convert`HTMLDump`HyperlinkAnchor[{URL[http://saaz.lanl.gov/math/math_pagelast.html#web], None}, System`Convert`CommonDump`InlineCell -> True]MMa web sites maintained at LANL

. System`Convert`HTMLDump`HyperlinkAnchor[{URL[http://saaz.lanl.gov/Math/Math_Home.html], None}, System`Convert`CommonDump`InlineCell -> True]LANL tutorials

. System`Convert`HTMLDump`HyperlinkAnchor[{URL[http://www.nscp.umd.edu/~kelly/EssentialMathematica], None}, System`Convert`CommonDump`InlineCell -> True]Downloadable tutorial notebooks

Exercises

1. What is the default base for the Mathematica Log[] function? Find the value of the logarithm of 2 to the base 10.

2. Solve an algebraic equation to produce the square root of 2 to 20 significant digits.

3. Show two different ways to obtain decimal numbers for the answers given in the previous exercise.

4. Explain the operations and the result of the following Mathematica statement:
{Sqrt[x*x], Sqrt[x]*Sqrt[x]} /. x->-1

5. Compare plots of a unit circle (circle with radius equal to unity) with Mathematica's default value of the AspectRatio option to a unit circle having an AspectRatio set to unity (1).

6. Rework the example that shows the intersection of a diagonal line and a unit parabola using user defined functions for the two curves.

7. Present the graphical and algebraic solution to find the intersection of a diagonal line with a unit circle.

8. Demonstrate the inverse relationship between the operations of differentiation and integration (in both directions) on some common functions.

9. Balance the following chemical equation to obtain integer molecular coefficients. (Hint: electrons, or charge must be conserved as well as mass, or nuclei.)
CuP + [Graphics:1gr63.gif] + [Graphics:1gr64.gif] = [Graphics:1gr65.gif] + [Graphics:1gr66.gif] + [Graphics:1gr67.gif] + [Graphics:1gr68.gif]O

10. Fit the United States population to some reasonable analytic function(s).

11. Who's data is this? {{12,117.5},{16,87.2},{20,70.7},{24,58.8},{32,44.2},{40,35.3}}

12. Collect the solutions to the previous exercises into a single notebook and include some headings and text to make it presentable.

Projects

1. Create a function that uses the Mathematica ChemicalElements package it to determine the molecular mass of a molecule. Apply it to the water molecule.

2. Extend the chemical equation balancing example to perform stoichiometry calculations. That is, given the amount of one substance (in number, say mols, mass, say grams or perhaps volume, say liters), determine the amount of another substance needed or produced by the reaction.

3. Develop a biological growth model that incorporates predation as a linear negative contribution to growth proportional to the population.