Math 236  •  5 February 2001

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)

Forming 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)]}

Doing algebraic operations on 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,  5};
vec[[2]]

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

vec[[2]]

Try it now:  Type an expression that uses indexing to extract the third entry in vec.  Type another that uses indexing to extract both the first and third entry in vec together in the form of a list with two entries.

Solving equations

⚠  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 with [[1]] or use 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}]

Forming matrices

A matrix to Mathematica is just a special kind of list of lists: a list whose entries are in turn lists of equal lengths that  constituting the successive rows of the matrix.  For example,  the 3-by-4 matrix
	M = (({{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}))
may be created in Mathematica by evaluating the following cell:

M  =  { {1,  2,  3,  4},  {5,  6,  7,  8},  {9,  
          10,   11,  12} }

Using spaces as shown is helpful for revealing the structure of such a list of lists.

To make a matrix appear in the usual matrix format, use the MatrixForm function:

MatrixForm[M]

Extract a row of a matrix by indexing into the overall list:

M[[2]]

Then an individual entry in a matrix can be extracted by indexing into the entry's row:

M[[2]][[3]]

The two steps of indexing may be combined into a double index, like this:

M[[2,  3]]

By contrast, what does the following do?

M[[{2,  3}]]

Since a matrix is stored as a list of its rows, extracting a column of a matrix is more complicated than extracting a row:

MatrixForm[M]
M[[All,  3]]

Solving linear systems in matrix form

A system of linear equations such as
	2 x  -  3 y  =  
        7 , 
5 x  +  6 y  =  4
can, as you know, be written in matrix form as
	(({{2, -3}, {5, 6}})) (({{x}, {y}})) = (({{7}, {4}})).

In Mathematica, the product A (Overscript[v,  ->])  of a matrix A and a vector (Overscript[v,  ->])  is denoted by putting a dot between the matrix and vector.  For example:

A  =  { {2,  -3},  {5,  6} };
MatrixForm[A]
v  =  {-5,  4};
A  .  v

Notice that the above used "row vectors"—just ordered lists—instead of "column vectors".  You may use column vectors instead:

MatrixForm[A]
v  =  { {-5},  {4} };
A  .  v
MatrixForm[A  .  v]

Then the linear system
	(({{2, -3}, {5, 6}})) (({{x}, {y}})) = (({{7}, {4}}))
has the Mathematica representation:

b  =  {7,  4};
xy  =  {x,  y};
A  .  xy  ==  b

Now use Solve on the equation in matrix form:

Solve[A  . xy  ==  b,  xy]

Againt here you could have used column vectors instead of row vectors.  Try it!:

Row reduction

Mathematica's Solve function uses internally a sophisticated variant of Gaussian elimination to solve systems of linear equations.  But you can explicitly do row reduction on a linear system's augmented matrix if you so choose.  For example:

Ab  =  { {2,  -3,  7},  {5,  6,  
            4} };
MatrixForm[Ab]
RowReduce[Ab]
MatrixForm[%]

From the last result here, tell what the solutions of the linear system \(TraditionalForm\`A\ x\&\(\(->\)\(\ \)\)\  = \ \(b\& -> \)\) are (A and (Overscript[b,  ->])  are as before).

Use RowReduce to determine the solutions of the linear system:

2 (Subscript[x, 1])  +  3 (Subscript[x, 2])  =  
        7, 
4 (Subscript[x, 1])  +  6 (Subscript[x, 2])  =  4.

Use RowReduce again to determine the solutions of the linear system:
	2 (Subscript[x, 1])  +  3 (Subscript[x, 2])  =  
        7, 
4 (Subscript[x, 1])  +  6 (Subscript[x, 2])  =  14.

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])]

Predict the result of evaluating the following cell:

N[(Sqrt[2.]) ,  20]

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

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

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.  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.  If you have time today, go next to either the notebook Vectors.nb.for some technicalities on manipulating vectors, or to the notebook Leontieff.nb for solving in Mathematica the Leontieff open input-output model of the three-partner consulting firm.  Otherwise, "clean up" as indicated  below and work on those notebooks another time.

Cleaning up