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

Problem

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

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

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

Comment 2

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

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

Solution - "Curve" Options

Comment 1

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

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

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

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

Comment 2

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

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

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

Save Your Notebook