Learning Mathematica Basics—Graphics I

Solve each of the following problems using Mathematica exclusively.

1.  Let's begin with probably the most common graphing command of all, Plot, whose purpose is to display a the graph of a function of one variable, i.e. a two dimensional graph.  (You may recall that we've actually used the Plot command in previous notebooks, but this is our "official" introduction to it.)  Ask for its syntax.

2.   OK, so it looks like it has two basic forms.  The first form shown is intended for graphing a single function, whereas the second form graphs a set of functions on the same coordinate system.  Issue the command Plot[Sin[x], {x,0,3Pi}] to see how a basic graph is implemented.

3.   Well, that was easy enough.  Now do one by yourself.  Graph x(x - 2)(2x + 5) on the interval -3≤x≤3.

4.    You should have produced a cubic graph with three x-intercepts.  Anyway, enough of this easy stuff!  Let's look at how you spice up a graph like the one above.  The output of a graphics command can be modified by including a list of options at the end of the command, but before the last bracket.  To see what options are available for Plot,  issue the command Options[Plot].

5.   As you can see, there are a lot of choices.  To find out what an individual one of these options actually does you can issue a ? command about it.  Go ahead and issue the command ?AxesLabel.

6.   Well hopefully you understood some of that.  Let's go ahead and modify the graph that we produced in problem 3 so that it puts labels of x and yon the respective axes.  The modified command would be:

         Plot[x(x-2)(2x+5), {x,-3,3}, AxesLabel->{"x","y"}]
         
(The arrow (->) is just a minus sign followed by a greater than sign.)  Go ahead and issue the command.

7.   Hopefully you got the appropriate labels!  Now issue the following set of commands, in order:

                   x=3
       y=x^(1/2)
       Plot[Log[x],{x,0.1,10},AxesLabel->{x,y}]

8.   Look carefully at your resulting axis labels.  Mathematica didn't use the letters x and y as the labels.  Instead it used the values that had been given to x and y earlier.  How can this be avoided?  Well look at how I did it in problem 6.  Enclosing the labels in quotes forces Mathematica to treat the labels as ordinary text, as opposed to actual variables.

Let's look at some of the other options for Plot. Consider the graph of the function e^x cos x.  Let's first Plot the function with no bells and whistles.  Use the interval 0≤x≤6.

9.   Now look at the right hand end of the graph you just made!  Even though we specified an interval that went as far as 6, the actual plot disappears at about 5.  Why does this happen?  Well Mathematica does this on purpose because it thinks that if it extends the vertical scale any further then the details of the curve might be obscured from view.  (Even with the current scale the local maximum of the function at about 0.7 is almost invisible.)  But what if we really do want to see what the curve does at around x=6?  We can force the issue by using another option command.  The one we want this time is called PlotRange.  Go ahead and redo your command from problem 8, but insert the option    ,PlotRange->All   before the final bracket.

10.   Look what happened to the vertical scale!  It now extends up to about 400, as opposed to about 50.  All the functions values are now visible, even those near to x = 6.  But we paid a price!  The local maximum at about 0.7 is now completely invisible!  You can see from these last two examples that Mathematica is usually doing you a favor when it automatically adjusts the vertical scale (the range.)  It's nice to know, however, that you can override this feature if you need to.

By the way, you may already be wondering how you can include more than one option in a Plot command.  It's easy!  You just keep on inserting them before the final bracket, and separate them all by commas.  In fact practice this!  Go ahead and edit the previous problem's command again so that it also includes labels of x and y on the two axes (in addition to showing the complete y-range.)

11.   OK, let's move on to the second syntax of Plot—multiple function plotting.  All that we need to change here is to replace the function plotted by a list of functions to be plotted.  (Remembering to enclose the list in braces.)  For example, issue the command:

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

12.  All three functions appear on the graph as expected.  The only trouble is that all three of them have identical colors, namely black.  It would be nice if we could color them all differently, say red, green, and blue, respectively.  It's time for another option to come to the rescue!  Ask Mathematica for the syntax of PlotStyle.

13.  The description doesn't make this completely clear, but a style as far as we're concerned is going to be a list of the three colors we want, i.e. red green and blue.  (Style information can also specify things like dashing, and line thickness, but we'll not worry about that right now.)  The way that we specify colors for graphics in Mathematica varies depending on what system we want to use.  Two of the most useful color specifications are RGBColor and Hue.  In fact ask for both of their syntaxes.

14.   The "red, green, blue", the "h" and the "h, s, b" are all numerical values between 0 and 1 inclusive.  They may be thought of as percentages, e.g. red=0.3 would signify about 30% red.  In the RGBColor  command you can mix the colors in any proportion you wish, and no the three do not have to add up to 1.

To conclude with this line of discussion, the way to color our three graphs as desired would be to include the option 

  PlotStyle->{RGBColor[1,0,0],RGBColor[0,1,0],RGBColor[0,0,1]}.
  
The first item in this list specifies 100% red, the second 100% green, etc.  Go ahead and redo problem 11 with this option included.

15.   Hopefully you produced three nice colorful graphs that time!

Your turn.

a)  Graph e^(sin t) on the interval -3≤t≤3.  Label the axes t and u.

b)  Graph (ln x) +  (sin 3x) on the interval 0.1≤x≤10.  Color the graph red.  (Hint:  the command for natural logarithm in Mathematica is not Ln, it's Log.)

c)   Plot x^(cos x) and 5e^(-x) on the same graph using the interval 0≤x≤10.  Color them blue and red, respectively.

d)   Use the Table command you learned in an earlier notebook to generate a list of Hues of the form {Hue[0], Hue[0.1], ..., Hue[1]}.  Read the result into a variable called huetable.

e)   Now use another Table command to create a list of the powers of t of the form {t, t^2, t^3,..., t^10}.  Read the result into a variable called powerlist.

f)   Plot the list of functions contained in powerlist using PlotStyle->huetable on the interval -2≤t≤2.  (Hint:  The Plot command will complain unless you wrap an Evaluate command around powerlist.  i.e. your command should start Plot[Evaluate[powerlist],...)