A few notes on graphics in Mathematica 4.0

Introduction

Mathematica is an "all-purpose" program for mathematics.  It is capable of symbolic manipulations, numeric calculations, and graphics.  We have full versions of the program available on the machines in the Thompson 225 computer lab.  These notes highlight the graphics capabilities that are relevant to our course.

A few notes to get started

Mathematica comes in two pieces, a "front-end" and a "kernal."  You interact with the front-end while the kernal does all of the hard work.  The front-end is responsible for formatting output on the screen or printer.  When you first launch the front-end, the kernal is not launched (unless the preferences have been changed from the default).  The kernal is launched after you enter the first input and ask Mathematica to process that input.  The front-end will bring up a dialog box asking you to find the kernal.  Normally the dialog box opens the correct file containing the kernal and you need only double-click on the application called MathKernal.  

To have Mathematica process input you must first enter the correct commands (either in text or using the palettes).  You must then use the Enter key on the number pad or the combination Shift-Return.  Pressing the Return key alone gives a new line for input but does not send the command to the kernal for processing.

Another feature to notice is the hierarchy of vertical bars on the right edge of the Mathematica window.  These bars indicate "cells."  Different cells have different types of content.  The two types of cell you will see most often are "input cells" and "output cells".

Basic plots of functions  f : ℝ → ℝ

We start with a simple plot.  Have Mathematica  execute the following command.

Plot[x^2, {x, -3, 3}]

If all has gone well, a number of things should have happened.  Most dramatically, a graph of the squaring function should have appeared.  Other things have also happened: Note that the program has labeled the input line with "In[1]:=".  There is a corresponding "Out[1]=" in the cell that contains the word "-Graphics-".  You can use these labels to refer back to this output (which in this case is the information used to draw the picture).  The next input and output will be labeled with a 2.  Try the command in the following input cell and note what the result is.

Show[Out[1],PlotRange->{0,6}]

Study the syntax of these two commands and make sure you understand what each piece does.  In general, Mathematica  commands start with an upper case letter.  Each command has an "argument" inside square brackets.  The argument may have any number of pieces each separated by a comma.  For example, in the Plot command, their are two required pieces in the argument.  The first is an expression for the function to be plotted, x^2 in the example above.  The second gives the name of the input variable and the interval to be plotted.  In the example above, {x,-2,2} means that x is the input variable and the plot is to be made for x in the interval from -2 to 2.  Most Mathematica  commands allow certain "options" in the argument.  For example, try the following input.

Plot[x^2,{x,-2,2},Axes->False]

Plots of functions f : (ℝ^2) → ℝ

The next command will produce a surface plot of the graph of a function f : (ℝ^2) → ℝ.

Plot3D[x^2-y^2,{x,-2,2},{y,-2,2}]

You might be interested in seeing this surface from a different viewpoint. For this you can use the Viewpoint option as in the following input.

Show[%,ViewPoint->{2,1,1}]

The percent symbol in the previous input is shorthand for "the previous output".  You could also have used Out[ ]  with the appropriate output number.  The percent symbol shorthand can also be used to refer to other labled outputs.  That is, the output Out[n] can be refered to with %n.

You can also plot level curves for functions f : (ℝ^2) → ℝ. Here's the command:

ContourPlot[x^2-y^2,{x,-2,2},{y,-2,2}]

Note that  the default option includes shading.  You could turn this off if you wish.  Try this:

Show[%,ContourShading->False]

Movies

Mathematica can animate a sequence of frames.  As an example, we first use the Table command to generate 11 frames of a movie.

Table[Plot[Sin[x-t],{x,-6 Pi,6Pi}],{t,0,2Pi,Pi/5}]

You can hide all but the first frame using the cell brackets.  Note that each graph is in its own cell (indicated by the inner most cell brackets).  These individual cells are collected in a higher level cell (indicated by the second bracket counting from the inside).  Double-clicking on the second bracket in will collapse the inner cells.  

To animate the frames, select the cell bracket containing all of the frames and then choose "Animate Selected Graphics" from the Cell menu.  This will start the animation and pop up some animation controls in the lower left corner of the main window.

Plots of vector fields

The Mathematica  command for plotting vector fields is contained in a "package" which you must load.  The command for this is in the next input cell which you should execute.  Be sure to note that the marks around the package name PlotField are left single quote marks (on the key above the Tab key.)

<<Graphics`PlotField`

An example of the proper syntax for the PlotField command is

PlotVectorField[{y,-x},{x,-2,2},{y,-2,2},Axes->True]

Plots of output curves for (Overscript[r,  ->]) : ℝ → (ℝ^2) and  (Overscript[r,  ->]) : ℝ → (ℝ^3)

An example of the command to plot a curve in the plane is given below.

ParametricPlot[{Cos[t],Sin[t]},{t,0,2*Pi}]

Hopefully you recognized the parametric function as that of the unit circle centered at the origin.  The default options for the ParametricPlot command produce something that doesn't look round.  The reason is that the default aspect ratio is not 1 so one unit in the horizontal direction is not equal in size to one unit in the vertical direction.  (The default aspect ratio is the reciprocal of the Golden  Ratio.)  Here is one way you could fix this up:

Show[%,AspectRatio->1]

To make a plot of a curve in space, use the command ParametricPlot3D:

ParametricPlot3D[{Cos[t],Sin[t],t},{t,0,4Pi}]