Example 05
Two Dimensional Graphics
Using  Plot[]

Copyright „ 1993 by Bill Titus, Carleton College, 
Department of Physics and Astronomy, Northfield, MN  55057-4025
September 6, 1993

Topics and Skills

1.   The bare  Plot[]  command.
2.   The general function  Options[].
3.   Eliminating  -Graphics- .
4.   Resizing a plot area.
5.   Reading out graph coordinates.
6.   Displaying multiple graphs on a single plot using one  Plot[]  command.
7.   The general structure of  Plot[]  options.
8.   Curve options:  Compiled,  PlotPoints,  PlotDivision,
      MaxBend.
9.  "Appearance" Options
       a.  Axes:  Axes,  AxesLabel,  AxesOrigin,  AxesStyle.
       b.  Frame:  Frame,  FrameStyle,  FrameLabel, Ticks,
            FrameTicks.
       c.  General:  AspectRatio,  PlotLabel,  DefaultFont, 
            Background,   DefaultColor,  GridLines,  PlotRange,
            PlotStyle.
       d.  Graphics Directives:  RGBColor[],  Thickness[],  GrayLevel[].
10.  Assigning names to plots.
11.  Plotting multiple graphs on a single plot using  Show[].
12.  Miscellaneous option:  DisplayFunction.
13.  GraphicsArray[].
14.  Animation.

Problem

An object moves vertically in one dimension near the surface of the earth under the force due to gravity.  Assuming that the object is launched from the origin and that the x-axis points vertically upward, the relevant equations are

v[t] = v[0] - gt
	x[t] = v[0]t - (1/2)gt2,

where  g = 9.8 m/s2.  Plot  v[t]  and  x[t]  as a function of  t  when the initial velocity  v[0]  equals  20 m/s  and  40 m/s.

Solution:  Creating the Appropriate Functions

Step 1 - Complete the code below which creates the functions  x[vo,t]  and  v[vo,t]  that allows you to calculate the position and velocity of the object as a function of the initial velocity  vo  and the time  t.  When you're done, activate the cell.

Clear[g, vo, t, v, x]
g = 9.8;
v[vo_, t_] = 
x[vo_, t_] =

Comment 1

1.  You could have included  g  as an argument in the functions.  What would be the advantages and disadvantages of doing so?
2.  Did you use semicolons to suppress any output?  If so, why?  If not, why not?

Solution:  The Bare Plot

Step 1 -  Use help and extended help to find and describe the MMA command for plotting curves in two dimension.

Comment 1

1.  How do the results for the commands  ?  and  ??  applied to  Plot[]  differ?
2.  Options[]  looks like a function.  It is.  When applied to the head of a function, it gives the options of a function, but not the function's usage statement.  You'll use this command later on.
3.  For now, don't worry about the options to  Plot[].  To start with, just concentrate on the bare  Plot[]  command.  What do you know about this command so far?

Step 2 - For  vo = 20, plot   x[vo, t]   from  t = 0.0  to  5.0.   Describe what you see displayed.

Comment 2

1.  Select the cell containing the plot by clicking on its cell boundary.  Then use the  Format \ Style Inspector  on the NeXT or  Style \ Cell Style  on the Mac to find out the cell style for the cell containing the plot.  
2.  What's the cell style of the cell containing the word  -Graphics- ? 
3.  The cell with   -Graphics-  doesn't seem to serve a purpose other than bookkeeping.  You can prevent this cell from appearing on the screen by putting a semicolon after  Plot[].  Try it.
4.  In your function  x[vo,t], should you replace  vo  by  20  or  20.0?

Step 3 - Click inside the graph.  You should see an outline of a box appear around the plot that has eight handles.  Click and drag one of the handles.  Describe what happens.  Note that you can't move the sides of the outlined box independently.  Why not?

Comment 3

1.  The aspect ratio is preserved unless you uncheck "Preserve aspect ratio"  in NeXT's Style Inspector's  Graphics Attributes  menu or in the menu item Graph  on the Mac.

Step 4 - Disable the Preserve aspect ratio, and then click and drag on one of the handles in the plot area.  What happens now?

Step 5 - Read out coordinates in the graph region by clicking on the graph to highlight it, holding down the <Command>  key, and moving the mouse without clicking.  Coordinates are displayed in the bottom right corner of the MMA window on the NeXT and in the bottom left corner on the Mac.

Step 6 - Plot graphs of  x[t]  versus  t   for  vo  equal to  20.0  and  40.0  on the same plot.  To do so, just include the functions to be plotted in a list as the first argument of  Plot[].

Solution -  Comments on  Plot[]'s Options

Step 1 - Find out the possible options for  Plot[]  by using the MMA function  Options[].

Comment 1

1.  The options are all in the form of rules with the right hand side of the rules set to various values or expressions.  Where else have you seen rules used?
2.  The options listed above have the default settings; other possible setting are discussed in Wolfram, A.8,  "Listing of Built-in Mathematica Objects".
3.  Automatic setting use various internal algorithms to choose appropriate values for parameters. 
4.  Options are added to the  Plot[]  command after the range specification in  Plot[]'s  argument.  Each option is separated from one another by commas.
5.  There are two classes of options for  Plot[]:  (1)  those that effect the calculation of the curve, and  (2)  those that enhance the overall appearance of the graph.

Solution - "Curve" Options

Comment 1

1.  There are four "curve" options specific to  Plot[].
2.  Compiled -> True  causes expressions to be automatically compiled.  Why might you want to do this?  If you need high precision, you should set this option to  False.
3.  PlotPoints  gives the minimum number of points sampled.  This is usually the option you should try if you want to increase resolution.
4.  PlotDivision  gives the maximum number of times an interval is subdivided.
5.  MaxBend  is the maximum angle (in degrees) between sampled points, consistent with the value of  PlotDivision. 
6.  See Blachman, Sec. 4.2.2, for a nice example of plotting problems that can occur because of MMA's adaptive sampling procedure.

Solution - "Appearance" Options

Step 1 - You're going to look at a small, but useful set of plot options in some detail and the remaining options more superficially.  Activate the cell below and ascertain what the various options are doing.

Plot[x[20, t], {t, 0, 5.0},
     PlotStyle -> RGBColor[1, 0, 0],
     AxesLabel -> {"t", "x"},
     PlotLabel -> FontForm["Position versus Time",
                    {"Helvetica-Bold", 12}],
     PlotRange -> {{0, 4.5}, {0, 25}}
    ];

Comment 1

1.  Get help on each of the four options used in the plot above.
2.  Get help on the graphics directives  RGBColor[]  and   FontForm[].
3.  Note how strings, the stuff between double quotes, are used for plotting purposes.

Step 2  - Activate the following cells to get a feeling for some of the other options that are available for  Plot[].

Plot[x[20, t], {t, 0, 5.0},
     PlotStyle   -> RGBColor[1, 0, 0],
     AxesLabel   -> {"t", "x"},
     PlotLabel   -> FontForm["Position versus Time",
                      {"Helvetica-Bold", 12}],
     AspectRatio -> 0.25
    ];

Plot[x[20, t], {t, 0, 5.0},
     PlotStyle  -> RGBColor[1, 0, 0],
     AxesLabel  -> {"t", "x"},
     PlotLabel  -> FontForm["Position versus Time",
                     {"Helvetica-Bold", 12}],
     AxesOrigin -> {2.1, 0.0}
    ];

Plot[x[20, t], {t, 0, 5.0},
     PlotStyle -> RGBColor[1, 0, 0],
     AxesLabel -> {"t", "x"},
     PlotLabel -> FontForm["Position versus Time",
                    {"Helvetica-Bold", 12}],
     Axes -> {True, False}
    ];

Plot[x[20, t], {t, 0, 5.0},
     PlotStyle   -> RGBColor[1, 0, 0],
     AxesLabel   -> {"t", "x"},
     PlotLabel   -> "Position versus Time",
     DefaultFont -> {"Helvetic-Bold", 16}
    ];

Plot[x[20, t], {t, 0, 5.0},
     PlotStyle -> RGBColor[1, 0, 0],
     AxesLabel -> {"t", "x"},
     PlotLabel -> FontForm["Position versus Time",
                    {"Helvetica-Bold", 12}],
     AxesStyle -> {RGBColor[0, 0, 1], Thickness[0.02]}
    ];

Plot[x[20, t], {t, 0, 5.0},
     PlotStyle  -> RGBColor[1, 0, 0],
     AxesLabel  -> {"t", "x"},
     PlotLabel  -> FontForm["Position versus Time",
                     {"Helvetica-Bold", 12}],
     Background -> GrayLevel[0.5]
    ];

Plot[x[20, t], {t, 0, 5.0},
     PlotStyle -> RGBColor[1, 0, 0],
     AxesLabel -> {"t", "x"},
     PlotLabel -> FontForm["Position versus Time",
                    {"Helvetica-Bold", 12}],
     DefaultColor -> RGBColor[0.2, 0, 1]
    ];

Plot[x[20, t], {t, 0, 5.0},
     PlotStyle -> RGBColor[1, 0, 0],
     AxesLabel -> {"t", "x"},
     PlotLabel -> FontForm["Position versus Time",
                    {"Helvetica-Bold", 12}],
     Ticks -> {{2, 4}, Automatic}
    ];

Plot[x[20, t], {t, 0, 5.0},
     PlotStyle  -> RGBColor[1, 0, 0],
     PlotLabel  -> FontForm["Position versus Time",
                     {"Helvetica-Bold", 12}],
     Frame      -> True,
     FrameLabel -> {"t", "x"},
     FrameStyle -> RGBColor[0, 0, 1]
    ];

Plot[x[20, t], {t, 0, 5.0},
     PlotStyle  -> RGBColor[1, 0, 0],
     PlotLabel  -> FontForm["Position versus Time",
                     {"Helvetica-Bold", 12}],
     Frame      -> True,
     FrameLabel -> {"t", "x", "x vs t", ""},
     FrameTicks -> {{0, 2, 4},
                    {-15, 0, 15}},
     GridLines  -> { {0, 2, 4}, {-15, 0, 15}},
     PlotRange  -> {-30, 30}
    ];

Comment 2

1.  Frame diagrams are quite useful.  One can get a lot of milage out of the options  Frame  and  FrameLabel.  The latter option is a cheap way to title your plot as well as label the axes.

Multiple Plots

Step 1 - Produce four separate framed plots:  x[20,t],  v[20,t],  x[40,t],  and  v[40,t].  Each plot should have its axes appropriately labeled.  Draw the curves in a different color for each value of  v[0].  Assign the plots the names  plotx1,  plotv1,  plotx2,  and  plotv2, respectively.  In writing your code, make liberal use of copy and paste.

Comment 1

1.  Why might it be useful to assign names to plots?
2.  An error message appears.  What's it about?

Step 2 - Display  plotx1  and  plotx2  together, and  plotv1  and  plotv2  together by using the MMA command  Show[].

Comment 2

1.  Did you get help on  Show[]?  If not, do so.
2.  How else have you displayed multiple graphs on the same plot?  How does that technique compare to the technique using  Show[]?

Step 3 - Suppose you didn't want the individual plots to be shown, but just the composite ones.  To do so, you can insert the option

DisplayFunction -> Identity

in each individual plots and then the option

DisplayFunction -> $DisplayFunction

in  Show[].  Try this out by activating the two cells below.

Clear[plotx1, plotv1, plotx2, plotv2]

plotx1 = Plot[x[20, t], {t, 0, 5.0},
             PlotStyle  -> RGBColor[1, 0, 0],
             Frame      -> True,
             FrameLabel -> {"t", "x"},
             DisplayFunction -> Identity];
             
plotv1 = Plot[v[20, t], {t, 0, 5.0},
             PlotStyle  -> RGBColor[1, 0, 0],
             Frame      -> True,
             FrameLabel -> {"t", "v"},
             DisplayFunction -> Identity];
             
plotx2 = Plot[x[40, t], {t, 0, 5.0},
             PlotStyle  -> RGBColor[0, 0, 1],
             Frame      -> True,
             FrameLabel -> {"t", "x"},
             DisplayFunction -> Identity];
             
plotv2 = Plot[v[40, t], {t, 0, 5.0},
             PlotStyle  -> RGBColor[0, 0, 1],
             Frame      -> True,
             FrameLabel -> {"t", "v"},
             DisplayFunction -> Identity];

Show[plotx1, plotx2,
     DisplayFunction -> $DisplayFunction];
Show[plotv1, plotv2,
     DisplayFunction -> $DisplayFunction];

Comment 3

1.  Options placed in  Show[] override those in the individual graphics.
2.  The symbol  $  signifies pre-defined global variables.  Get help on  $DisplayFunction  and then locate all the symbol that have a  $  in front.

Step 4 - You can produce a two dimension graphics array of plots using a combination of the MMA functions  GraphicArray[]  and  Show[].  Get help on   GraphicsArray[]  and then activate the cell below.  You should find that the first row has  plotx1  and  plotv1, while the second row has  plotx2  and  plotv2.

Show[
 GraphicsArray[{{plotx1, plotv1}, {plotx2, plotv2}}]];

Comment 4

1.  You may need to enlarge the graphics to see the individual plots better.  How do you do this?

Step 5 - Active the following cell which produces a table of plots of  x[t]  for  vo  going from  0  to  40  in steps of  5.  The option

    PlotRange -> {-100, 100}

is included so that all the plots have the same range.

Table[Plot[x[vo, t], {t, 0, 5.0},
      PlotStyle  -> RGBColor[1, 0, 0],
      Frame      -> True,
      FrameLabel -> {"t", "x"},
      PlotRange  -> {-100, 100}],
      {vo, 0, 40, 5}];

Step 6 - Double-click on the cell grouping for the plots and collapse the cells to a single cell.  Then press  <Command>-y.  What happens?

Comment 6

1.  Play with the animation controls.
2.  You could also activate animation with  Graph \ Animate Selected Graphics.
3.  Do you see why it was nice to have the same range on all the plots?
4.  Never save a file containing lots of graphics.  Why?
5.  Where else might grouping cells be useful?

Save Your Notebook

Remove any output cells from this notebook and then use  Save As  to store your notebook under the name  myEx05.