Math 455  •  8 February 2009

Introduction to Mathematica

Part 1: Interacting with Mathematica

Your name(s):

Notebooks

This document is a Mathematica notebook.  It's just an ordinary text file that Mathematica's user interface—its front end—interprets and displays in special ways.

Using the File menu, you can create a new notebook, open an existing one, or save an open notebook.

To save an open notebook, use menu item File ⊳ Save.  In the pop-up dialog box, browse to where you want to save.

You can save the notebook to your UMass UDrive or to removable media—e.g., a USB flash drive.  For faster saves—but for temporary storage only—save to your computer's desktop.
Do it now:  Save this notebook.  Get in the habit of saving your open notebook regularly so that you don't risk losing all your work in case of a catastrophic error.  (If you're working with a partner, be sure that eventually you have saved copies for each of you on removable media or UDrives.)

You'll also want to make printed copies of the notebooks you work in today.  Towards the end of this notebook you'll be told how.

Cells and evaluation

A notebook is divided into distinct parts called cells.  Typically, a cell is enclosed by a cell bracket along its right edge. ⟶

Types of cells

Cells come in various styles:

• Text style cells—they just contain text meant for reading..  The text can include mathematical expressions in-line, such as (∫_0^π) ((e )^t)cos 2tdt  and  (({{cos θ, -sin θ}, {sin θ, cos θ}})) .  And the text can be adorned with various kinds of formatting if you really wish.  You use text cells to explain things.

• Specially formatted text-like cells such as those for headings.  For example, the cell above with the words "Cells and evaluation" is a Section style cell.

• Input cells, containing Mathematica expressions to be evaluated.  The following cell is an unevaluated input cell.  To evaluate an input cell, click anywhere inside the cell and then press together ⇧ ⏎ (Shift and Return).

Do it now—evaluate the following cell.  And please be patient for this evaluation—wait until you see the answer.

1 + 1

⌚  Sorry if that took a bit longer than you expected!  What happened is that Mathematica's kernel—its evaluation engine—had to be loaded into memory before it could evaluate the input cell.  (Subsequent evaluations should be much faster.)

• Output cells, containing the results of evaluated Mathematica expressions.  Below that input cell you should now see the corresponding output cell containing the result 2.  (Ask for help if you don't see it!)

The next input cell is going to add up the first one million digits in the decimal expansion of π.  (Don't worry now about how the code is doing it.)  Notice that while a cell is evaluating, the legend Running... appears in the title bar at the top of the notebook window and the cell bracket assumes a doubled outline shape.  Do it now—evaluate the following cell.

Plus @@ First@ RealDigits[ N[Pi,(10^6)] ]

Evaluated input cells and their corresponding output cells receive numbered In and Out labels.  The numbers indicate the order in which the input cells have been evaluated and not their order in the notebook.  For example, do this:  Evaluate the next three input cells in this order:
	• the second to start;
	• the third; next; and
	• the first, finally,
Notice the numbers in the cell labels.

Plot[Sin[x], {x, 0, 2 π}]

(4/20)+(135/136)

Factor[(a^5) + 14 (a^4) - 8 (a^3) - 368 (a^2) + 1040 a - 800]

A single input cell may contain several separate expressions to be evaluated, as in:

2^5
Log[2, 32]

If you look carefully, you will see that different types of cells have distinctive iconic markings on their cell brackets.

A single cell can even produce output that combines numeric, symbolic, and graphical output.  For example, evaluate the following cell.

threeTypes={(2^10), ListPlot[Table[RandomInteger[100],{20}]], Expand[(((a+b))^4)]}

The input and output cells just above used lists, enclosed in curly brackets { and } .

Errors

Sooner or later you will try to type something in an Input cell that Mathematica cannot understand.  If what you type is not a legitimate Mathematica form—that is, it is not syntactically correct—then Mathematica will often alert you even before you finish typing or evaluate the cell.

For example, see what happens if your forget a closing bracket ] to match an opening bracket [.  Try this—in the blank input cell below, type exactly the following, but do not evaluate the cell yet:

	Plot[x^2

Next, see what happens if you forget to supply two "arguments" (that is, inputs) to a function such as Plot that requres two.  Try this—in the blank input cell below, type exactly the following:

	Plot[x^2]

Mathematica gives many other clues to actual or potential errors through such syntax-coloring.

If you ask Mathematica to evaluate an expression that it does not know how to evaluate, or that it suspects is not what you really intended, one of several things can happen.

If the expression makes sense (that is, has a syntactically correct form) but Mathematica does not know any rule for evaluating it, then the output will be exactly the same as the input.  For example, try these:  evaluate the next two input cells.

Cos[something]

myfunction[2]

Iif there is an actual error in your input expression, or if evaluating that expression causes an error or unusual condition, then Mathematica will issue an appropriate error message or warning message.  For example, try this:  evaluate the following cell

2 - 3 +

Did you notice that the cell bracket turned red, and there's a + icon next to it?  Click that + icon to see the error message, which will display in a separate pop-up Messages window.

The following input cell already has a color clue that something's wrong.  Nonetheless, just go ahead and evaluate it to see what happens.

Plot[Sin[x], {x, 0, 2 π}

Did you see the error message?

Plot[Sin[x], {x, 0, 2 π}

The next cell has no coloring that would indicate a problem with its form.  Go ahead and evaluate it.  Do it now.

(∫_0^2)(1/x)\[DifferentialD]x

For the preceding input, the error was not syntax, but rather a problem with the actual attempt to evaluate the integral.  So Mathematica displayed an error message automatically (and returned the input unevaluated).

(By the way, how come Mathematica couldn't evaluate that integral?)

Comments

Whereas text cells are wonderful for annotating work—by students as well as teachers!—sometimes all that's needed is a brief comment.  A comment is indicated by included its text between the symbol pairs (* *).  The comment is ignored when the input cell is evaluated.  Try it now:  Evaluate the following cell and be sure you understand the result.

2   10        (* an unrelated problem: 3134 + 227 *)

Cell grouping and cell selection

Do this:  Evaluate the following input cell.  Then look at the resulting input-output cell pair.

Range[-2,8]

In addition to the individual cell brackets of the input and output cell above, there's a larger bracket  ↗ grouping the two cells together.  Such grouping helps indicate the notebook's logical structure and provides a handy mechanism for editing the notebook.

Many operations in Mathematica require you to act upon an entire cell, which you must first select.  To select a cell, click the mouse cursor on its bracket.  To select several contiguous cells, click on one of their brackets and drag along the other brackets.  Try it now:  Select this cell.

You can also evaluate a cell by selecting its bracket—instead of clicking inside the cell—and, as usual, pressing ⇧ ⏎.  And you can evaluate several contiguous cells by selecting all their brackets first.  Try it now:  Evaluate all three of the following input cells together:

(Sqrt[2])+1

((((Sqrt[2])+1))^10)

Expand[((((Sqrt[2])+1))^100)]

Some cells below have been grouped together into groups, some of which may be grouped into larger groups, etc.  These groups have been closed so that only the first cell of the group shows.  You can tell that a group is closed from the triangular icon at the bottom of the group's cell bracket.

Try it now:  The first cell in the group below has the subsection title "Creating and editing cells".  Open that group and then continue working.  (From now on do the same with all closed groups as you get to them.)

(Did you follow the directions about opening this closed group?  If not, do it now!)
Creating and editing cells

To create a new cell, move the cursor to a position above or below the cell where the new cell should go.  Adjust the cursor position until the cursor changes to a horizontal I-beam shape and then click.  You'll see a horizontal line across the window marking the top of the new cell.  Then just type.  By default, the new cell will be an input cell.  Try it now: create an input cell immediately below this one and type into it:  N[Pi, 100]

You couldn't resist evaluating the cell above that you just created, could you?  By all means, from now on evaluate input cells whenever you come to them.  Note, though, that expressions used in some input cells later on will depend upon earlier cells having been evaluated.

Try it now:  Change the following input cell into a text cell.

guess the result of this one

Try it now:  In the following cell, change the function to be plotted to be the cosine instead of the sine and, if you wish, change its amplitude and period.

Plot[Sin[x], {x, 0, 2 π}]

To copy or cut text that's inside a cell, you first select that text.  To select any text, of course, you just click and drag over it.

💡  Shortcut: To select a subexpression of a larger expression in an input cell, first click inside that expression.  Then repeatedly click the (left) mouse button—or, equivalently, use the key combination Ctrl . (Control together with period)—as successively larger expressions are highlighted until you reach the desired one.  Try it now:  Use the shortcut to select the argument (input) -(θ/10)to Exp in the following cell.

0.75Cos[2θ] Exp[-(θ/10)]

The usual Edit menu Copy, Paste, and Cut commands work not just upon selections inside a cell but also selections of entire cells (or even groups of cells).  So do their equivalent keyboard combinations  Ctrl c, Ctrl v, and Ctrl x.  Often the quickest way to build an expression in an input cell is to copy parts of expressions from other cells.

Try it now: The following cell contains an expression in the variable θ.  Without changing that cell and without typing everything from scratch, create a new cell below it that will plot the corresponding function of θ on the interval from -3π to 3π.

0.75Cos[2θ] Exp[-(θ/10)]

(Did you remember to open and work through this closed group?  If not, do it now!)
Printing notebooks

To print an open notebook, from the File menu select Print.  If you're working with somebody, be sure to opt for 2 copies and check the Collate check box.  Then click on the OK button.

(If you reached this section by clicking on a hyperlink in the "Notebooks" section near the start of this notebook, you may click here to return there.)

What's next?

If you have time at this lab session—you should!—continue this introduction by opening the notebook Intro2.nb the same way you opened this notebook.  You may download it by visiting the Math 455.1 web site Files page.
Otherwise, "clean up" as indicated  below.

Cleaning up

Copyright © 2009 by Murray Eisenberg. All rights reserved.