Math 421  •  September 2006

Mathematica Introduction—Part 1

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, from the File menu select Save.  In the dialog box that pops up, navigate to the place where you want to save the notebook.

You can save the notebook to removable media—a floppy disk or Zip disk.  For faster saves—but for temporary storage only—navigate to a suitable directory on the computer's hard disk.
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 system crash or other catastrophic error.  (If you are working with a partner, be sure that eventually you have saved copies for each of you on removable media.)

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.  (Some specially formatted cells, such as the two at the top of this notebook that precede the Section cell "Notebooks", do not normally display their cell brackets.)

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\(TraditionalForm\`\(\(\ \)\(∫\_0\%π\ \(\(\(e\)\(\ \)\)\^t\) cos\ 2  
          t \(\(d\)\(t\)\(\ \)\)\)\)\) and  (({{cos θ, -sin θ}, {sin θ, cos θ}})) .  And the text can be adorned with various kinds of formatting if you really wish.  You will use text cells to type some answers and explanations.

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

• Input cells, containing Mathematica expressions to be evaluated.  The indented cell below that's highlighted with an orange background is an unevaluated input cell.

To evaluate an input cell, click the cursor anywhere inside the cell;
then press at the same time the two keys ⇧ ⏎ (Shift and Return).

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 filled with color.

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 the input cell above you should now see the corresponding output cell containing the result 2.  (Ask for help if you don't see it!)

Notice above the larger group bracket that encloses the individual cell brackets of the input and output cells.

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 second of the three input cells below; next the third and fourth; and finally the first.  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]

(Sqrt[-2]) (Sqrt[-3])

Like text cells, graphics cells are inactive—they cannot be evaluated.

Evaluating the input cell containing Plot[Sin[x], {x, 0, 2 π}]  produced not only a correspondingly numbered output cell (containing just the mysterious legend ⁃Graphics⁃) but also, as a side-effect, a graphics cell displaying the plot.  To suppress printing of that ⁃Graphics⁃ output cell, terminate the Plot input cell with a semicolon.  
Try it now:  Evaluate the following cell after typing a semicolon ( ; ) at the end of the expression in it.

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

Mathematica can produce parametric plots as well as ordinary plots.  Evaluate this:

ParametricPlot[{Cos[θ],   Sin[θ]},  {θ,  
          0,  (π/2)}];

I hope you're disappointed with what was displayed: you did expect a circular arc, didn't you?  To fix this, use a suitable option to the function ParametricPlot.  Namely, the option AspectRatio -> Automatic.  Evaluate this version:

ParametricPlot[{Cos[θ],   Sin[θ]},  {θ,  
          0,  (π/2)},  AspectRatio  ->  Automatic];

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.

Error messages

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:

Cos[something]

myfunction[2]

If you use a name similar to a built-in name, then you will see a spelling-error warning message (often, just the first time).  Try these:

sin[π]

Sin[π]

Finally, if 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 these:

2  -  3  +

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

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

Cell grouping and cell selection

Look at one of the input-output cell pairs above.  In addition to the individual cell brackets, there is also a larger bracket that groups the two cells together as a single entity.  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 ⇧ ⏎.

To evaluate several cells together, one after another, select all their brackets first.
Then press  ⇧ ⏎.

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.  And 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.

To open a closed group, double-click its cell bracket.
(To close the group again, double-click its cell bracket again.)

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

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.

To change the type of a cell, select the cell's bracket.  From the Format menu select Style and then select the style you want.  Try it now:  Change the following input cell into a text cell.

guess the result of this one

To edit the contents of a cell, just move the cursor there and make the changes you want.  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, you can 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 use the key combination Ctrl . (Control together with period) as successively larger expressions are highlighted until you reach the desired one.  Try it now:  Select the argument -(θ/10)to Exp in the following cell.

0.75  Cos[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.75  Cos[2  θ] Exp[-(θ/10)]

Printing notebooks

To print an open notebook, from the File menu select Print.  In the dialog box that pops up, the Number of Copies field is highlighted with a default number of 1, that is, a single copy.  To print 2 copies, just type 2 into that field and click 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.)

Did you remember to open the closed group immediately above this cell?  If not, do it now!

What's next?

If you have time at this lab session—you should!—continue this introduction with notebook Intro2.nb.  Get that from the Math 421 web site Files page  (Click on the underlined, blue hyperlink in the preceding sentence!)  Open that notebook and work through it.
Otherwise, if you're out of time, then " clean up" as indicated  below.

Cleaning up