An introduction to plotting with Maple
Worksheet by Mike May, S.J. - maymk@slu.edu
> restart;
In previous courses you have been using a graphing calculator to graph functions of one variable. Having a convenient grapher makes the concepts of calculus easier to understand.
However, most of Calculus III deals with functions of several variables and our calculators are not very good at graphing surfaces. The point of this worksheet is to give an introduction to graphing with Maple. This should make it easy for you to produce the relevant pictures for this course.
An introduction with functions of one variable:
A simple plot
Since functions of one variable are more familiar, we start with them.
The syntax for plotting a function of one variable is
plot("function of x", x="low value of x".."high value of x");
where the expressions in quotation marks are to be replaced by appropriate values.
Hit enter to execute the command below.
> plot(sin(x), x=-Pi..Pi);
Things to notice:
1) The "function of x" is not an equation. It is what we would set y equal to.
2) We specified the x-range we wanted, and Maple decided on an appropriate y-range. We will learn how to specify the y-range a little later.
3) Click once on the graph. The graph should now be enclosed in a box. Dragging the corners of the box will let you resize the graph. The second row of icons above has also been replaced be a new row. The new row has buttons that control the style of the graph. From left to right you should see a box with the coordinates of the point you clicked on, a set of 4 buttons concerned with the style of the graph, a set of 4 buttons concerned with the style of the coordinate axes, and a "1:1" button that looks at whether the x and y axes need to have the same scale.
Exercises:
1) Plot the graph of y = cos(2*x) with
and
Stretch the graph to cover the screen.
>
2) Replot the graph above, but modify the style so that it is done with dots rather than with connected lines, and so the axes are on the sides of the graph rather than in the middle of them.
>
3) Replot the graph above, but modify the style so that it is a connected curve, with no axes, and the same scale for the x and y axes.
>
Specifying the y-range
We would like to be able to specify the y-range of a plot.
If a plot has a vertical asymptote, Maple will give such a large y-range that we miss details we are interested in. The solution is to specify the y-range. Specifying the y-range is done using the same conventions used to specify the x-range. The command
plot("function of x", x="low value of x".."high value of x");
becomes
plot("function of x", x="low value of x".."high value of x", y="low value of y".."high value of y");
For an example, execute the plotting commands given below.
>
plot(1/x * cos(x), x=-10..12);
plot(1/x * cos(x), x=-10..12, y=-2..2);
The second graph is much more informative than the first about the behavior of this function.
It is worthwhile at this point to note where we have been using inprecise language. By "x-range" we mean "the range of the independent variable", while "y-range" is "the range of the dependent variable". Equivalently, x-range and y-range respectively stand for the ranges of the input and output variables. The examples below show what happens when we change the names of the variables.
>
plot(1/x * cos(x), x=-10..12, z=-2..2);
plot(1/y * cos(y), y=-10..12, x=-2..2);
plot(1/x * cos(x), y=-10..12, x=-2..2);
Plotting error, empty plot
Notice that we can use any letter for input and output, but the input range is specified before the output range or we get an error.
>
Plotting several functions on the same graph
It is also useful to be able to see the graphs of several functions on the same graph. In Calculus I and II we were interested in seeing the graph of a function and its derivative at the same time. We also wanted to compare a function with the polynomial approximations we obtained for it.
To plot several functions at the same time, the command
plot("function of x", x="low value of x".."high value of x");
becomes
plot({"function1 of x", "function2 of x", "etc."}, x="low value of x".."high value of x");
Note that the set of functions is enclosed in braces.
For an example, consider the following commands which plot cos(x) along with some of the Taylor polynomial approximations of cos(x).
>
plot({cos(x), 1, 1-x^2/2, 1-x^2/2+x^4/24}, x=-3..5);
plot({cos(x), 1, 1-x^2/2, 1-x^2/2+x^4/24}, x=-3..5, y=-1.5..1.5);
>
More Exercises:
4) Produce a nice plot of the graph of y = tan(x) with x between 0 and 10. You will need to specify the y-range.
>
5) Plot
with
and
, along with the lines tangent to that graph when x = -1.5, -.5, .5, and 1.5. Crop the graph to show the region where y is between -2 and 4.
>
As was noted earlier, you can graph a function of one variable on a graphing calculator. Since you bring it to class every day, that will generally be the easier way to deal with such graphs. There are two reasons to look at them with Maple when your calculator is already at hand:
1) It gives an introduction to graphing with Maple using familiar material. (This is the reason we include it here.)
2) Maple graphs can easily be printed out if you want to write on them or turn them in.
>
Plotting with functions of two variables:
Basic plotting
The syntax for plotting the graph of a function in 2 variables is similar to the one variable case. We use the command:
plot3d("function of x and y", x="low x".."high x", y="lowy".."high y");
as we see with the following command.
> plot3d(sin(x)+sin(y), x=-3..5, y= -Pi..2*Pi);
Once again, if you click once on the graph, it will be enclosed in a box, and a set of buttons will appear up on the menu bar. There are 2 boxes labeled
and
with scroll arrows for specifying the viewing angle, a set of 7 buttons for style used on the graph itself, a set of 4 buttons for style used on the axes, a "1:1" button to use the same scale on all 3 axes, and an "R" button to redraw the graph when all the options are chosen.
The default has a wire frame model, that is opaque (the front bump hides the back bump), the axes are not printed, from a viewing angle of 45 degrees on both angles.
Exercise:
6) Redraw the graph given above. Then use the buttons to produce an image that
shades the image, draws a grid of lines on the image, and boxes the axes around the image;
> plot3d(sin(x)+sin(y), x=-3..5, y= -Pi..2*Pi);
Shades the image and draws contour lines on it, while putting the axes in standard position through the origin;
> plot3d(sin(x)+sin(y), x=-3..5, y= -Pi..2*Pi);
Uses a wire frame model with contour lines, and suppresses the axes.
> plot3d(sin(x)+sin(y), x=-3..5, y= -Pi..2*Pi);
Viewing angles and contour plots
The list of controls for changing the appearance of a graph includes controls for theta and phi, the coordinates of the position in spherical coordinates that the graph is viewed from. The angle phi is the angle to the positive z-axes. Thus an angle of
looks down on the x-y plane from above,
gives a viewpoint looking down the x-y plane, while
looks up from below. The angle theta measures the rotation of the x-y plane. If
so we are looking down the z-axis, then
has the y- axis horizontal increasing from left to right and the x-axis vertical decreasing as you go up. The default setting has
and
both set to 45.
>
7) Redraw the graph above. Adjust the viewing angle so that theta = 60 and phi = 30. Use a wireframe model that is opaque, and have the axes on the outside of the graph.
> plot3d(sin(x)+sin(y), x=-3..5, y= -Pi..2*Pi);
8) Find the angle settings that look down the z axis and put the x-y plane in standard position for the plane. (Standard position has the x-axis horizontal and increasing to the right. The y- axis is vertical and increasing as you rise.)
>
The correct settings are.....
9) View the graph above as a contour map with the x-y plane in standard position.
>
This contour map view can be obtained more directly with the plots[contourmap] command, using
plots[contourplot]("func(x,y)", x="low x".."hi x", y="low y".."hi y");
The plots[contourplot] command has the option of letting you specify which contours will be drawn with a
contours=["list of values" option. Thus to get the map above with contours corresponding to the values of -2/3, 0, 1/4, and 5/4 we use the command:
plots[contourplot](sin(x) + sin(y), x=-3..5, y= -Pi..2*Pi, contours= [-2/3, 0, 1/4, 5/4]);
10) Redraw the contour plot you found in the last exercise, using the plot options rather than adjusting the controls and redrawing.
>
>
Multiple graphs and controling the z-range
The syntax for doing more than one graph on the same plot is similar to the one variable case. We simply replace the function with a set of functions.
> plot3d({sin(x)+sin(y), x^2 + y^2 - 5}, x=-3..5, y= -Pi..Pi);
The control of the z-range is done with a
view = "low value of z".."high value of z"
clause inserted into the plot3d command.
> f1 := plot3d(x^2 + y^2 - 5, x=-3..5, y= -Pi..Pi, view = -3..3, color=red):
> f2 := plot3d(sin(x)+sin(y), x=-3..5, y= -Pi..Pi, view = -3..3, color=green):
> display(f1, f2);
Exercises:
11) Plot the functions
and
on the same axes. Plot over the rectangle with x and y between -1 and 3.
>
12) Regraph the same pair of functions, this time constraining the z-range between -1 and 7.
>
These 2 surfaces have a simple relationship. Describe the relationship and replot the graph viewed at an angle to make the relationship clear visually.
>
The relationship is.......
The viewing angle that makes this obvious is ......