Introduction to computer graphics with Maple
Making graphs and plots is fun and is also useful to understand
mathematical structures. Maple can produce several forms of graphs.
This tutorial will give you a simple introduction. Maple's graphics
can do much more than what is described here, including animation
of graphs. If you want to know more, use the on-line help facility
as explained in Section 8, which also contains many useful examples.
Login a computer in the Oracle cluster, click the Oracle Software
program group and click the maple leaf to fire up Maple. Go through
the following tutorial and try the exercises at the end. To exit
Maple click the top left button. Do not forget to logout when you
have finished.
Maple's prompt is the symbol > so in all the examples below
the command which you have to type is the text which follows >.
To execute a command, press the return key. Be very careful about
the notation; note for example that all Maple's commands given here
terminate with a semi colon. If you want to print, click the print button
on Maple's menu bar.
Remember that an efficient way to use Maple, when you have had
some initial practice, is not to re-type commands, but rather
edit previous commands, change them and execute them again. It is
also very useful to copy and modify the examples given by the
on-line help.
1. Simple graphs of functions in two dimensions
It is very simple to plot a function given in explicit form.
Try for example
>plot(sin(x),x);
and Maple will plot the function y(x)=sin(x) on the screen.
Note that the range of both the x and the y coordinates are
chosen automatically by Maple. If you want, you can set the
range yourself: for example type
>plot(sin(x),x=0..12);
and Maple will graph the function y(x)=sin(x) in 0plot(sin(x)/x,x=-2*Pi..2*Pi);
to plot, for example, the function y(x)=sin(x)/x between
-2*Pi and 2*Pi.
You can set the y range too: for example
>plot(x*sin(x),x=0..12,y=-12..12);
The ability of Maple to find automatically the y range of a
curve is evident if you type
>plot(exp(x),x=0..4);
to plot the exponential function y(x)=exp(x).
Note that, in order to show the picture more clearly, Maple has
used different scales for the horizontal x axis and the vertical
y axis. We say that the plot was 'unconstrained'. If you want Maple
to use the same scale on both axes type
>plot(exp(x),x=0..4,scaling=constrained);
As said above, you can zoom the graph in the region which you want,
for example type
>plot(exp(x),x=1..4,y=10..60);
A title can be added in a simple way, for example type
>plot(sin(x),x,title=`SIN(X)`);
2. Parametric plots
Sometimes we want to plot functions given in 'parametric' form.
This means that both the x and the y coordinate are functions
of a single parameter t. An example is the parabola y(x)=x^2;
following Section 1, we can be plot it in 0plot(x^2,x=0..10);
In parametric form we set x=t, so y=x^2 becomes y=t^2,
and we type
>plot( [t,t^2,t=0..10]);
In this way, as t varies from 0 to 10, Maple plots the curve of
coordinates x(t)=t and y(t)=t^2.
To apply this idea we can plot a circle. For most of values of x
there are two values of y, so the parametric form is convenient.
Type
>plot( [cos(t),sin(t),t=0..2*Pi ] );
and Maple will graph a circle. A more complicated curve is for
example
>plot( [sqrt(t)+sin(t),t^2,t=0..20] );
3. Multiple plots
It is possible to show more than one curve on the same graph.
For example suppose that we want to see how the slopes of the
four curves y=x, y=x^2, y=x^3, y=x^4 differ from each others.
Type
>plot( [x,x^2,x^3,x^4],x=-10..10);
to plot them together.
Note that Maple likes to work with colours, which is useful
to distinguish one curve from the other. In the previous
graph Maple used default values, but you can set the colour
which you want, for example type
>plot( [x,x^2,x^3,x^4],x=-10..10,color=blue);
Now all curves are in blue. You can also set the sequence of
colours which you want, for example type
>plot( [x,x^2,x^3,x^4],x=-10..10,color=[yellow,orange,red,blue]);
In this way the curve y(x)=x will be yellow, y(x)=x^2 will be
orange, y(x)=x^3 will be red and y(x)=x^4 will be blue.
4. Data points
If you want to plot numeric data, you need to know what is a 'list'.
A list consists of two (or more) numbers enclosed in a square
bracket. For example [2,8] is a list. Given two (or more) lists,
a 'list of lists' is obtained by enclosing these lists within square
brackets. For example [ [1,1],[2,8],[3,27],[4,64] ] is a list of lists.
To plot it type
>plot( [ [1,1],[2,8],[3,27],[4,64] ]);
and Maple will automatically draw a line between the data points.
Sometimes it is convenient first to define a quantity as a list,
call it q, and then plot it. To do this type
>q:= [ [1,1],[2,8],[3,27],[4,64] ];
>plot(q);
It is easy to change the style; for example, rather than drawing
lines between the points, you can mark the points by typing
>plot(a,style=point);
The default used by Maple before was style=line.
5. Three dimensional plots
Maple can be used to plot functions of two variables x and y as
a surface in three dimensional space. For example type
>plot3d(1-x^2-y^2,x=-1..1,y=-1..1);
and Maple will plot the surface z=1-x^2-y^2 in the region -1plot3d(1-x^2-y^2,x=-1..1,y=-1..1,axes=framed);
You can also make Maple to draw a full box around the picture: type
>plot3d(1-x^2-y^2,x=-1..1,y=-1..1,axes=boxed);
It is very convenient to have the graph boxed in this way,
because you can rotate the picture to look at it from the direction
which you want. To do this, use the mouse to grab the frame of the box,
holding the left click down, and turn the box around the way which
you like. When the box is in the right position, release the click,
and move the prompt to the top menu bar, clicking the button marked
'R' for 'redraw'.
By default Maple draws pictures as an opaque wire frame, but you
can change the style by typing for example
>plot3d(1-x^2-y^2,x=-1..1,y=-1..1,axes=boxed,style=patch);
which puts colours in the wire frame, or
>plot3d(1-x^2-y^2,x=-1..1,y=-1..1,axes=boxed,style=patchnogrid);
which leaves the colours only.
6. Contour plots
Another way to visualize three dimensional surfaces is to draw
a 'contour plot'. A contour plot draw the lines of equal height,
like it is done on a map of Ordnance Survey to indicate
the height of hills. For example consider the function
z(x,y)=sin(x)*sin(y) for -Piplot3d(sin(x)*sin(y),x=-Pi..Pi,y=-Pi..Pi,axes=boxed,
scaling=constrained);
Maple will show the shape of the function. Rotate it around to
make sure you understand the shape, which consists of
two 'hills' and two 'valleys'.
Now we are ready to draw the same surface as a countour plot.
First we load Maple's high level graphics routines by typing
>with(plots);
then we type
>contourplot(sin(x)*sin(y),x=-Pi..Pi,y=-Pi..Pi,axes=boxed,
contours=10);
Maple will draw 10 contour lines to indicate the height of the
'hills' or 'valleys'.
7. Parametric plots in 3 dimensions
The surface of a sphere of radius r has equation x=r*sin(theta)*cos(phi),
y=r*sin(theta)*sin(phi), z=r*cos(theta) where the angle theta (called
the colatitude) varies from 0 to Pi, and the angle phi (called the
longitude) varies from 0 to 2*Pi. We can plot the sphere of radius r=1
in parametric form by typing
>plot3d([sin(t)*cos(p),sin(t)*sin(p),cos(t)],t=0..Pi,p=0..2*Pi,
axes=boxed, scaling=constrained);
8. Using Maple quicky
Remember that an efficient way to use Maple, when you have had
some initial practice, is not to re-type all commands, but rather
edit previous commands, make the necessary changes and execute
them again: this saves you from retyping everything.
To do so, you have to go up the window (using the scroll buttons
if necessary), find the old command, use the mouse to bring the
prompt onto the old command, insert/delete new text, and press the
return key again.
Another way is to proceed is to cut and paste. Suppose that you
want to make minor changes to a previous command. Go back to it,
moving the prompt to the beginning of the command at the left;
hold down the left button and move to the right to highlight the
text which you want, then press Control C. Now go back to where
you want, and press Control V to copy the old command. Indert/delete
text and execute by pressing return.
9. Using the on-line help
It was mentioned at the beginning that the help button can be used
to learn more commands. It is good practice to copy the examples
which are on-line and then modify them. Click 'help' below the main
window bar of Maple, select 'topic search', enter for example
'plot' in the box for 'topic', select 'apply' and press return.
You will find a description of the 'plot' command, including various
options. At the end of the description (use the scroll button to
reach it quickly) there are some examples. These examples can be
copied onto your Maple's window. as explained in Section 8.