Mathematica Numerics

by
K. D.  Stroyan
University of Iowa

copyright 2007 by Keith Stroyan - All rights reserved.

Review: Closed and Open Mathematica Cells

The NoteBook MmaNtBkCells introduces you to use of cells.

To open a closed cell, use your mouse to position the cursor over the dark arrowhead and click the button on the mouse twice rapidly.  When you are done looking at a section, you can position the cursor at the far right bracket again and double click to close that section.

Open the cell containing "Notebook Overview" now.  Then close this cell.

Notebook Overview

This NoteBook introduces you to numerical computations in Mathematica.  This includes both exact and decimal arithmetic and simple variable expressions that are assigned numerical values.  Work the Exercises as they arise in the NoteBook.  The other calculations show you how to solve the exercises.

References to the Text

This NoteBook goes with Chapter 1 of the text.

Numerics in Mathematica

Mathematica can do arithmetic just like an ordinary calculator.  Unlike a calculator, however, Mathematica can also give exact results.

Exact Rational Arithmetic

Computations can only be executed if they are entered in "input" cells.  These are marked with the plain square brackets (with no flags or arrows.)  Inactive text and "output" cells have the little tick mark (or flag) at the top.  Mathematica cannot do a computation if you type it in an inactive cell.

Once a computation is typed in an input cell, you must press the Enter key at the far right of the keyboard (you can also use <Shift> + <Return>).  Your commands must be in an input (or active computation) cell and the answer will be in an output (inactive) cell.  The pair looks like the following:

15+20

A space (or * ) between numbers is Mathematica's notation for multiplication.  For example,  2*3 evaluates to 6.  So does 2 3, but it is sometimes hard to read the space...

2*3

Now YOU try an exact arithmetic computation.  Move the cursor to the empty input cell just below this cell (see the bars on the right side of the window), click once (so you see a little vertical line) and type

15/14 +5/7

Then press the <Enter> key (or hold <Shift> and press the <Return> key.)

You need to be a little careful with typewriter arithmetic.  For example, how much is  2/14*7 ?  It could mean  2/(14*7) = 2/98 = 1/49 or (2/14)*7 = (1/7)*7 = 1.  Enter the computation in the next cell to learn the order of Mathematica operations.

2/14*7

Mathematica has operators that look almost the same as traditional mathematical notation.  For example, here is the previous computation in traditional notation:

((2*7)/14)

You can enter these operators from a "Palette".  To get theBasid Input Palette, click and hold on the File menu at the top of the screen.  Drag your mouse down to Palettes and selece BasicInput.  If you put the cursor in the next input cell and click on the palette, you get an operatof that you can fil out.  For example, use the following to fill in the computation  ((2*3)/12)(Sqrt[49]).  (You can also drag over this and paste it into the Input Cell.)

(\[Placeholder]/\[Placeholder])(Sqrt[\[Placeholder]])

Most calculators cannot raise 3 to the 100th power and give an exact answer.  Mathematica can.  Move the cursor to the next cell, click, and then press <Enter> to see this number.

(3^100)

Compute 3 raised to the 1000th power in the next cell.

Mathematica can also perform exact calculations with fractions and built in operations.  For example, Enter the cell below to see the exact sum of

1/2 + 1/3 + 1/4 + ... + 1/25

We use the Sum command to do the arithmetic.

(∑_(i=2)^25)(1/i)

Floating Point (Real Number) Arithmetic

Often exact answers like the ones above above are not as useful as decimal approximations, especially for estimation.  We can use the N command to get "numerical" (or decimal) approximations as follows:

N[(15/14)+(5/7)]

Enter the computation in the next cell to compare the exact sum  1/2 + 1/3 + ...+ 1/25 that you computed above with it's decimal approximation.

N[(∑_(i=2)^25)(1/i)]

Mathematica has many important constants built in.  The natural base of logarithms is known to Mathematica as (upper case)
			E
or the special (open face lower case)

			e

Mathematica does not approximate this conastant unless you ask it to do so.  For example, here are some exact computations:

E

Log[(E^2)]

The N command can be used to get numerical approximations of built-in exact constants.  Enter the cell below to see an approximate value of  E.

N[ E ]

You can get higher precision as follows:

N[E,555]

The number usually denoted by Greek "pi" is known exactly to Mathematica as
			Pi  or   π
π is the area of the unit circle, for example. You should know the exact values of the Sine and Cosine functions at angles such as "30 degrees," which is π/6 radians.  Enter the computation in the next cell to see the value of  Sin[π/6].

Sin[(π/6)]

Pi

Practice Arithmetic Exercises

1)  Use Mathematica to compute  5/13 + 10/29  both exactly as a ratio of integers and approximately as a decimal number.

2)  Use Mathematica to give the decimal approximation to the number  π  to  666  places.

3)  Use Mathematica to give the exact value of Cosine of "30 degrees",  Cos[π/6].

3)  Compute the decimal approximation of Cosine of "30 degrees",  Cos[π/6].

Numerical Variables

Mathematica can assign values to variables.  When you Enter the cell below, Mathematica will remember that "a" refers to the value 0.0001.

a=0.0001

Now if you Enter the computation in the blank cell below, Mathematica will give you the value associated with the constant a.  (Be sure you pressed Enter with the insert cursor in the  "a = 0.0001"  cell above before you press enter in the  "a" cell below here.

a

A space between two numbers or constants means multiplication in Mathematica.  Enter the cell below and Mathematica will multiply a by 12.

12 a

Mathematica will do similar algebra using this value of  a.  Enter the computation in the next cell now.

3 a+5

Mathematica will remember the values of constants until you change them.  If you enter the cell below, Mathematica will reset the value of a to 50.  Do NOT Enter the next computation now, but move down two cells and Enter that computation.

Then move back up and Enter the  "a = 1/50"  computation and the following computation again.  What you will see is that Mathematica remembers the computations that you have Entered, not their position on the screen.  Moreover, it performs exact arithmetic when the value assigned to a variable is exact and decimal arithmetic when the assigned value is approximate.

a=(1/50)

a+a

You may have noticed Mathematica reprints the value of the constant  "a"  after you Enter that cell.  Sometimes this may be undesirable.  If a semi-colon is typed at the end of a line Mathematica will not print anything.  For example, entering the cell below will set the value of b to be 3/4.

b=(3/4);

Notice that no output is generated.  If you type b in the blank cell below, however, and Enter you will see that Mathematica has still set the value of b to be 3/4

c=.25;
a-c

Variables can be assigned values by expressions,

f=a+b+c
g=(∑_(i=2)^25)(1/i)
f g

Practice with Numerical Variables.

1)  Assign values  c = 15  and  b = 14  in a Mathematica cell and Enter the assignments.

2)  Compute the value of the variable  a = b c  (or b*c)  given by the product of b and c.