Math 421  •  September 2006

Mathematica Introduction—Part 3

Your name(s):

What else can Mathematica do?

Already you have seen some examples of numerical, symbolic, and graphical work with Mathematica.  Here are a few more.  Try to predict the output before evaluating each input cell

Simplifying expressions

Mathematica has only a limited number of rules automatically applied to expressions you evaluate.  For example:

Log[2 Exp[5]]

You can undoubtedly see another, possibly simpler form for this.  (Of course simplicity is in the eye of the beholder.)  Often you can something more closely resembling the "simple" form you expected by using the Simplify function:

Simplify[Log[2 Exp[5]]]

Here's another one:

Sqrt[(x^2  -  9)/(x - 3)]

To simplify that result, you may Simplify to the preceding-result variable %, which holds the output of the most recently evaluated input cell:apply output:

Simplify[%]

The preceding-result variable (%) can be used in any expression and not just for Simplify.  For example:

((25 +  I)/(3  -  4  I)) ((9  -  71  I)/(100  I))

%  +  (1/(4  +  6  I))

Working with lists

Several objects may be combined into a list, which is denoted with braces.  For example:

\({15\/30, \ π, \ "Hello, world!", \(3\  + \ 4  I\)\/\(5\  + \ 6  I\), \ \((a + \ b\ I)\)\^3, \ Expand[\((a + \ b\ I)\)\^3]}\)

z  =  3  +  4  I;
{Re[z],  Im[z]}

A quick way to form a list of the positive integers 1, 2, …n for some positive integer n is to use the Range function:

Range[10]

Then to form the list {(1^2),  (2^2),  (3^2),  …,  (10^2)} of squares of those numbers, you may use the Sum function with a "counter" j.

Sum[(j^2),  {j,  1,  10}]

Here's another, "functional", way that involves no counter:

Apply[Plus,  Range[10]^2]

Individual entries in a list can be extracted by indexing, as follows:

w  =  5  +  6  I;  theParts  =  {Re[w],  Im[w]}
theParts[[2]]

For indexing, you may prefer to type Esc[[Esc and  Esc]]Esc  to get more distinctive double brackets:

theParts[[2]]

Try it now:  (a) Type and evaluate an expression that creates the list {1 + i, 2 + 2i, 3 + 3i, …, 6 + 6i} and assigns it to the name nums.  [Hint: Recall that the shortcut for i is EsciiEsc (with a double i).]
(b) Then use indexing to extract the third entry in nums.  
(c) Finally, type another expression that uses indexing to extract both the first and third entry in nums together in the form of a list with two entries.

Numerical values

As you have already seen, Mathematica can work with exact rational and irrational quantities.  When you want to convert such an exact quantity, say one involving (Sqrt[2]), into its numerical equivalent, use the function N.

{(Sqrt[2])  +  (Sqrt[2]) I,  N[(Sqrt[2])  +  (Sqrt[2]) I]}

By default, Mathematica displays only 6 significant digits, but you can ask for more if you start with exact numbers:

N[(Sqrt[2])  +  (Sqrt[2]) I,  40]

If, though, your input includes even one floating-point (approximate) number including a decimal point, then Mathematica will only give meaningful results to machine precision:

N[(Sqrt[2.0])  +  (Sqrt[2]) I]

Predict the result of evaluating the following cell:

N[(Sqrt[2.])   +  (Sqrt[2]) I,  12]

Are your surprised at the result?  Did you expect to see 20 digits?  Actually, Mathematica did approximate (Sqrt[2.])   +  (Sqrt[2.])  i to 20 digits, but it still printed the result with the default 6 digits precision.  To see all 20 digits, wrap the expression inside the NumberForm function:

NumberForm[N[(Sqrt[2.])   +  (Sqrt[2]) I,  20],  20]

High precision arithmetic is possible with Mathematica.  Try it yourself now:  Create a cell below with an expression that will give 100 digits of π, then evaluate that cell to see the result.

Solving equations

⚠  Equations are formed with two equal signs!

2  +  2  ==  4

2  +  3  ==  4

Clear[z]

z (z  +  z)  ==  2 (z^2)

To solve an equation, use the Solve function:

Solve[3  z  -  7  ==  (8/5)  +  2  I,  z]

Surprised at the output?  It does not directly give you the solution, but rather a replacement rule for the solve variable.  (Moreover, the rule is in the form of a brace-enclosed list; more about that later.)

To convert a replacement rule into an actual value, use the form expr /. rule.

z  /. %

Better yet, anticipate having to do this and assign the replacement rule result of the Solve to a name:

mysoln  =  Solve[3  z  -  7  ==  (8/5)  +  2  I,  z]

z  /.  mysoln

There is still one nasty thing about the answer—it's a list, albeit with a single entry.  And that's because the result of Solve was a one-entry list, and that entry is itself a list.  To get rid of the outer list braces, you can index with [[1]] or use in this case the equivalent function First:

mysoln  =  
      First[Solve[3  z  -  7  ==  (8/5)  +  2  I,  z]]
z /.  mysoln

Of course equations may have more than one solution—especially when the solutions may be non-real complex numbers:

Solve[(z^2)  +  1  ==  0,  z]

Something like the following is perfectly legal in Mathematica:

z  =  z  /.  Solve[(z^2)  +  1  ==  0,  z]

But then z now has a fixed value—a list of the two complex square-roots of -1—and so if you want to use z again as a variable, you'll have to clear it first.  Watch what happens if you don't:

Solve[(z/(1  +  2  I))   ==  3,  z]

So now clear the value assigned to z and try again:

Clear[z]
Solve[(z/(1  +  2  I))   ==  3,  z]

To solve several equations simultaneously, put them into a list:

Solve[{2 x  -  3 y  ==  7,  5 x  +  6 y  ==  4},  {x,  
        y}]

Creating notebooks

To create a new notebook, from the File menu you would select New.  The new, blank notebook that opens will be untitled; you will give it a name when you first save the notebook.

Try it now:  Create a new notebook.  In it type some input cell and evaluate that cell.  At the top of the notebook, create a text cell and type your name(s) there.

If you create a new notebook, it will doubtless look plainer than this one.  This one has had a distinctive style sheet applied to format the entire notebook in order to make it more attractive (I hope) and to more readily distinguish input and output cells from others.

You may have several notebooks open at once in a Mathematica session.  To select the one in which you want to work, just click anywhere in its window.

What's next?

Congratulations! You have completed this brief introduction to Mathematica. 
Now you're ready to start using Mathematica to learn topics in this course.
Go to the Math 421 web site Files page again and open the notebook nthRoots.nb.  Work through that notebook.
If you're out of time, just "clean up" as indicated  below.

Cleaning up