Math 245 • 9 September 1999

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.)  Ofen you can something more closely resemblying 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 the output, we could use one of the following methods:
•  Edit the original input cell so as to apply Simplify.
 •  Copy the input cell to a new one and apply Simplify in the new one.
 •  In the original or a copied input cell, assign the value to a name, for example:  quantity =  Sqrt[(x^2 - 9)/(x-3)].  Then evaluate Simplify[quantity].
 •  Apply Simplify to the preceding-result variable %, which holds the output of the most recently evaluated input cell:

Simplify[%]

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

(25/3) (9/100)

%  +  (1/4)

Lists

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

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

Vectors

A list consisting of several number represents a vector. The operations of adding two vectors and multiplying a vector be a scalar (that is, a number) are done so:

{1,  2}  +  {5,  -8}

4 {1,  2}

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

vec  =  {4,  8};
vec[[2]]

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

vec[[2]]

Equations and solving (optional for now)

⚠  Equations are formed with two equal signs!

2  +  2  ==  4

x (x  +  x)  ==  2 (x^2)

To solve an equation, use the Solve function:

Solve[3  x  -  7  ==  (89/5),  x]

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 such a replacement rule into an actual value, use the form expr /. rule.

x  /. %

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

mysoln  =  Solve[3  x  -  7  ==  (89/5),  x]

x  /.  mysoln

There 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 or apply in this case the equivalent function First:

mysoln  =  First[Solve[3  x  -  7  ==  (89/5),  x]]
x  /.  mysoln

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

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

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]),  N[(Sqrt[2])]}

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

N[(Sqrt[2]),  40]

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

N[(Sqrt[2.0])]

High precision arithmetic is possible with Mathematica:

N[π,  100]

Unfortunately, Mathematica precision is a complicated subject, and that's all we'll say about it here.

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.  Tthis 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.  If you have time today, go next to the notebook Flatland.nb.  Otherwise," clean up" as indicated  below and work on that notebook next time.

Cleaning up