Tutorial 5:  Plotting

by R. Dover, IMSA

Basic graphs

One of the many strengths of Mathematica is its graphing capabilities.  You created a couple of simple 2-D plots in the first tutorial, but let's review that here.  And there's much more to see.
	The coding below is for the graph of f(x) = x sin(2x) for -1 ≤ x ≤ 5.  (Again, note the space between x and Sin[2x] to denote multiplication.  Otherwise, this would be seen as one variable "xSin".)

Plot[x Sin[2  x],  {x,  -1,  5}]

As a default, the range is chosen automatically.  This and other options may be controlled by the user.  As examples, check the following to see the results of each command.  Look at each one carefully.  There are lots of arrows, braces, capitals, and commas to watch.  Understanding the system will make it much easier to use a variety of options.

Plot[x Sin[2  x],  {x,  -1,  5},  
      Frame  ->  True]

Plot[x Sin[2  x],  {x,  -1,  5},  
      PlotRange  ->  {-5,  5}]

Plot[x Sin[2  x],  {x,  -1,  5},  
      PlotStyle  ->  {{Dashing[{ .03}],  
            Thickness[ .01]}}]

Plot[x Sin[2  x],  {x,  -1,  5},  
      AspectRatio  ->  Automatic,  
      Ticks  -> {{π/2,  π,  3  π/2},  
          Automatic}]

Plot[x Sin[2  x],  {x,  -1,  5},  
      PlotLabel  ->  "f[x] = x Sin[2x]"]

Create one of your own graphs.  Then make several changes including the range, the styles, and the tickmarks.  Vary the values used by dashing and thickness to see the effects.  Experiment for a while.

Help!

Clearly, there are lots of commands and options in Mathematica. And clearly, it's impossible to remember all of them. Fortunately, Mathematica also provides a lot of help.  First suppose you have forgotten the basic coding for Plot.  We saw earlier that this command is available through the BasicCalculations palette.  To see an explanation, execute the cell below.

?Plot

As you can see, this can be quite helpful.  This technique works for all of Mathematica's built-in functions.  Try it for a few of the other commands we have used.

But what about options?  What options may be used with the command?  Ask it.

Options[Plot]

Whoa!  Read through the list carefully.  You will note the the default value for each option is shown.

Colors

There are two basic methods of showing colors.  The first uses Red, Green, and Blue.  ("Log" refers to the natural logarithm.)

Plot[x Log[x + 2],  {x,  -1.99,  3},  
      PlotStyle  ->  RGBColor[1,  0,  0]]

Make the graph blue instead of red.  Then try combining the three colors by using numbers between 0 and 1, inclusive, in the 3 locations.

A second way to show a graph in color is by using Hue[k].  One example is shown.  Try other values for k between 0 and 1, remembering your rainbow.

Plot[x Log[x + 2],  {x,  -1.99,  3},  
      PlotStyle  ->  Hue[ .4]]

More graphs

To graph two or more functions on one graph, we create a list of functions.

Plot[{x Sin[2  x],  (x^3)  -  3  x  +  1},  {x,  -4,  
        4}]

Though this will be fine in some cases, options may be particularly helpful to clarify which graph is which .  To correspond to the list of functions, we create a list of styles.  Pay close attention here.

Plot[{x Sin[2  x],  (x^3)  -  3  x  +  1},  {x,  -4,  4},  
      PlotStyle  ->   {Thickness[ .03],  
          Dashing[{ .02}]}]

Note the curly braces.  Check back to see how we added two styles to one graph in the first section of this tutorial.  This is different, since we have given one style to each of two graphs.

Perhaps you prefer the more colorful look:

Plot[{x Sin[2  x],  (x^3)  -  3  x  +  1},  {x,  -4,  4},  
      PlotStyle  ->  {Hue[0],  Hue[ .6]}]

Or both looks:

Plot[{x Sin[2  x],  (x^3)  -  3  x  +  1},  {x,  -4,  4},  
      PlotStyle  ->  {{Hue[0],  Thickness[ .03]},  { Hue[ .6],  
            Dashing[{ .02}]}}]

Your turn...

Let f(x) = cosx and g(x) = 1 - (x^2).  Define both of these functions.

Plot them together, making f  blue and thick while g is green with dashes.  Put a frame around the plot and label it "Approximations."

Be sure to clear f and g when you are done.