Math 455  •  1 February 2009

Introduction to Mathematica

Part 3: Doing more things with Mathematica

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 the output, you could use any 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)

Simplify is one of a number of functions that you may think of using "after the fact"—after you've already typed a whole expression and then realize you want to make that function act upon the entire function.  For example, after you've typed 
 
 	Sqrt[(x^2 - 9)/(x - 3)]
 
you may realize you want to use Simplify upon it.  Rather than going back to put "Simplify[" at the extreme leftt end, then returning to the right end and appending the closing bracket ], there's a quicker way: use the postfix notation  //Simplify.

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

Any function of one argument can be used with postfix notation.  For example:

Exp[2]Exp[5]//Log

Working with lists

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

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

The entries in a list do not need to be all of the same "type".  Thus in the preceding example the list consisted of an exact rational number, a symbol representing the exact real number π, a text string, and a symbolic algebraic expression.  (Notice that the double quotes surrounding a string don't appear in the output.)

A list is used to indicate the variable, starting value, and ending value for a .

Plot[Exp[-2t]Cos[(t^2)],{t,1,π}]

For a plot in 3-dimensional space of a function of two variables, you'd need two such lists.  For example:

Plot3D[Sin[x Cos[y]],{x,-1,1},{y,-π,π}]

Try this:  Put your cursor over the surface just plotted; the cursor will change shape.  Now "fly around" the surface: while holding the left mouse button down, move the cursor around.

Even graphics may be included in a list.  For example:

{"A saddle", Plot3D[(x^2)-(y^2),{x,-1,1},{y,-1,1}]}

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 calculate the sum of entries in 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 way that involves no counter:

Apply[Plus, ((Range[10])^2)]

Another way to create a table is to use the Table function.  For example, the next cell creates a list of the first 10 prime numbers.

Table[Prime[i],{i,1,10}]

Suppose you want to label each of these ten primes with its position in the list, that is, to indicate that 2 is number 1, that 3 is number 2, etc.  You may do so like this:

Table[{i,Prime[i]},{i,1,10}]

That does it, but it would be nicer to format this the way a table is ordinarily displayed, with rows and columns.

Table[{i,Prime[i]},{i,1,10}] // TableForm

Did you notice the postfix notation // TableForm there?  If you want to put a header row on the table, you may do so like this:

TableForm[Table[{i,Prime[i]},{i,1,10}],TableHeadings->{None,{"i","(Subscript[p, i])"}}]

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

nums = {4, 8, 5};
nums[[2]]

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

nums[[2]]

Try it now:  Type an expression that uses indexing to extract the third entry in nums.  Type another 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]), 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.  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.

Like Simplify, N is one of the functions you may often want to apply "after the fact"—by using postfix notation.

Log[2]//N

Solving equations (may be skipped 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[3x - 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[3x - 7 == (89/5), x]

x /. 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[3x - 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}]

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, download and begin working through the notebook SetsAndFunctions.nb from the Math 455.1 web site Files page.

Otherwise, "clean up" as indicated  below and work on those notebooks another time.

Cleaning up