A Three Dimensional Plotting Tutorial
Part I: The Basics
Plotting Space Curves
One Space Curve
The typical way to plot a curve in three dimensional space is to parameterize it.
A parameterized curve in space is a 4-tuple, that is a comma-separated sequence of four objects with the sequence surrounded by square brackets.
The first three components are expressions that depend on a parameter (say
t
).
The fourth entry is then a range of the form
t = a .. b
.
Then the function
spacecurve
is called with the parameterized curve as its argument.
Since this function is in the
plots
package that package must be loaded first. The following example will illustrate.
Example:
Plot the helix
x = t*cos(t)
y = t*sin(t)
z = 4*t
for
t
ranging from
0
to
.
Solution:
First we load the plots package. Because we are using it for the first time, we will terminate the loading command with the
semicolon
, which is Maple's
noisy terminator
. All statements require a terminator because a long statement could occupy more than one line. There are two types of terminators: the
noisy terminator
and the
silent terminator
, the
colon
. Whichever terminator is used, Maple executes the statement. With the noisy terminator, Maple prints the result of the execution. Here, because of the noisy terminator, you will see all the plotting functions that are part of the plots package.
We will only use a couple of these in this worksheet.
> with(plots);
> spacecurve( [ t*cos(t), t*sin(t), 4*t, t = 0 .. 8*Pi ] );
The command
spacecurve
accepts Maple's standard plot options. By increasing the number of points used to plot the curve, we can obtain a smoother curve. We do this by adding the optional argument
numpoints = a_number_of_your_choice
.
We will see some other plot options later.
> spacecurve( [ t*cos(t), t*sin(t), 4*t, t = 0..8*Pi ], numpoints=160);
It is usually most efficient to name things. Any object can be named in Maple.
In practice, the helix might have been plotted as follows:
> helix := [ t*cos(t), t*sin(t), 4*t ]:
Here we have a 3-tuple consisting of the parametric equations of the helix. It is assigned as the value of the
name
helix
. Notice that assignment is achieved through the operator
:=
(as in the Pascal programming language).
Of course, there is nothing significant about the choice of the name
helix
.
Felix
is a nice name also and it would have served our purpose just as well.
By the way, there was no need to see Maple echo the assignment so I used a silent terminator.
> spacecurve( helix, t = 0..8*Pi, numpoints=160);
One advantage of naming objects is that syntax is easier to follow. Another advantage is that you can easily refer to the object later in the worksheet if necessary.
Changing the View
By clicking a three dimensional plot, we can rotate a space curve in real time . I can't illustrate this in a static html document-without incorporating an animated gif-but I can give the idea of what is involved.
Here is a trefoil knot:
> trefoil := [(2+cos(3*t/2))*cos(t),(2+cos(3*t/2))*sin(t),sin(3*t/2)]:
> spacecurve( trefoil, t = 0 .. 4*Pi );
What view does this correspond to? To find out, I click the plot. This brings up some plotting tools on the context bar (the lowest bar that is above the worksheet area). Here is a screen capture:
You can see that icons are available to control the framing of the curve. Also, on the context bar at the left, you can see that
and
. These values automatically change as you rotate a curve by dragging the mouse. Or, you can manually specify them to get a particuular view.
By setting
and
we get the view from the positive x-axis looking at the yz-plane.
By setting
and
we get the view from the positive y-axis looking at the xz-plane.
By setting
and
we get the view from the positive z-axis looking at the xy-plane. Here is that view:
In this case it is best to rotate until the trefoil nature becomes clear.
As is often the case with three dimensional plots, no one static view is convincing.
More than One Space Curve
We plot a
set
of space curves by specifying a set of space curves as the first argument of
spacecurve
.
(Maple's syntax is usually so logical that it seems silly to describe it.)
All the standard Mathematical objects (variables, expressions, functions, ranges, equations, sets, lists, strings, vectors, matrices, ... ) are Maple objects. A
set
in Maple is a comma separated sequence enclosed by brace brackets.
Example:
Plot the linked circles
x = sin(t)
y = 0
z = cos(t), t = 0 ..
and
x = 1 + cos(t)
y = sin(t)
z = 0, t = -
..
What is the projection of these two curves in the xy-plane?
Solution:
> circle1 := [sin(t),0,cos(t),t=0..2*Pi]:
> circle2 := [cos(s)+1,sin(s),0,s=-Pi..Pi]:
> pair_of_circles := {circle1,circle2};
This time I will add a box around the plot via the
axes = you_choose_the_style
plot option. The same effect could have been done by plotting without this option and then using the appropriate icon.
The latter approach keeps the syntax brief.
> spacecurve( pair_of_circles, axes=box );
To see the projection, I set
and
to get the view from the positive z-axis looking at the xy-plane. Here is that view:
A Typical Vector Calculus Application
One application in calculus which calls for the simultaneous visualization of two curves is the tangent line approximation to a curve.
Example:
Consider the curve
x = 2*cos(t)
y = 2*sin(t)
z = 1-4*cos(t)+2*sin(t), t = -
..
Plot the tangent line at
t =
/4
.
Solution:
Here it will be convenient to do some vector calculations. As vectors are in the linear algebra package, we load that first.
> with(linalg):
Warning, the protected names norm and trace have been redefined and unprotected
The tangent line is obtained by adding two vectors. One vector is the radius vector to the point of contact.
To that we add a scalar multiple of the velocity vector.
Here is the radius "vector" to the point of contact.
> ellipse := [2*cos(t), 2*sin(t), 1-4*cos(t)+2*sin(t)]:
> contact_point := subs(t=Pi/4, ellipse);
Actually,
contact_point
is a 3-tuple, i.e. a list with three entries.
> whattype( contact_point );
> type( contact_point, vector);
Maple does not automatically identify a 3-tuple with a vector. After all, the entries of a 3-tuple might be equations, or sets, or other objects for which an addition is not defined. So we tell Maple to convert this 3-tuple into a vector:
> contact_point := convert(contact_point, vector);
> type( contact_point, vector);
Now we find the velocity vector:
> velocity := diff(ellipse, t);
> velocity_vector := subs(t = Pi/4, velocity);
> velocity_vector := convert( % , vector);
The percent sign refers to Maple's last return. It is very handy.
Now that we have two
vectors
, we can add them. The rule for adding vectors is, of course, a special case of the rule for adding matrices. The command for this is
matadd
.
> tangent_line := matadd(contact_point , t*velocity_vector);
Now we are ready to plot:
> spacecurve( {ellipse, tangent_line} , t = -Pi .. Pi);
A High Level Routine
When Maple is taught to beginners it is used interactively, one command line at a time. However, Maple is a very powerful programming language. Instructors can write their own Maple functions and add them as a package (similar to the plotting and linear algebra packages that we have already used). For example, C. K. Cheung at Boston College has written a multivariable calculus package, mvcal that is widely used.
If an instructor does not want students to have to deal with the distinction between a list and a vector, then a custom function can be written to do the conversions automatically. For example, here is one that I wrote. It accepts a list
F
as the first argument, an equation of the form
t = c
as the second argument, and a variable
s
as the third argument.
It calculates the radius vector
r
, the value of
F
when
t = c
, it calculates the derivative of
F
with respect to
t
, it evaluates this derivative at
t = c
to obtain
v
, and it returns the formula for the tangent line,
r + s*v
.
The Code for tangent_line is in this subsection
Here is the code for
tangent_line
:
>
tangent_line := proc()
local tt, ss, Contact_pt, Velocity_vct;
if nargs <> 3 then ERROR("tangent_line requires three arguments.");
elif not type(args[1],list) then
ERROR("tangent_line expects its first argument to be a list");
elif not type(args[2],equation) then
ERROR("tangent_line expects its second argument to be an equation.");
elif not type(args[3],algebraic)
then
ERROR("tangent_line expects its third argument to be an algebraic expression.");
elif not type(lhs(args[2]), name) or not type(rhs(args[2]), algebraic) then
ERROR("The second argument of tangent_line is expected to be of the form name = expr.");
else
tt := lhs(args[2]);
ss := args[3]:
Contact_pt := convert(subs(args[2],args[1]),vector):
Velocity_vct := diff(args[1], tt):
Velocity_vct := convert( subs(args[2],Velocity_vct), vector):
return convert(linalg[matadd](Contact_pt,ss*Velocity_vct),list);
fi;
end;
A significant portion of the routine is devoted to error-checking, in the Maple style. For those who are knowledgable about programming, Maple is a very strongly typed language.
If you care to look at the code, then you will see that it looks complicated. Remember, in practice
the code is completely in the background
. There is no more reason to see the code for
tangent_line
than there is to see the code for
spaceecurve
.
The function
tangent_line
, like any other home-brewed function, can be compiled and added to the installation's kernel. The student would then use it as he would any other Maple function.
Documentation for tangent_line
tangent_line - calculate the tangent line to a parameterized curve
tangent_line(F(t), t = c, s)
Parameters:
F(t) - a list of expressions in t
t - the parameter of the parameterized curve
c -
the value of t at the desired point of tangency
s -
the parameter of the tangent line
In this example we will use the Folium of Descartes.
> F := [ 3*t/(1+t^3), t^2/(1+t^3)];
> tangent_line( F , t = 1/2, t-1/2);
> plot( [ [ 3*t/(1+t^3), t^2/(1+t^3) , t = 0..1 ] , [4/9+16/9*t, -4/27+20/27*t, t = 0 .. 1]] , color = [navy,plum]);
showtangent
Example:
Using the function
tangent_line
, redo the problem of the section titled "A Typical Vector Calculus Application". That is, plot the tangent line to the curve
x = 2*cos(t)
y = 2*sin(t)
z = 1-4*cos(t)+2*sin(t), t = -
..
at
t =
/4
.
Solution:
Now the solution to this problem (and every subsequent one like it) is a one-liner!
> spacecurve({ellipse,tangent_line(ellipse,t=Pi/4,t)}, t= -Pi..Pi);
Plotting Surfaces
Plotting One Surface
The typical call is of the form
> plot3d( f(u,v), u = a..b, v = c..d);
Example:
Plot the cone
.
Solution:
> plot3d(4*sqrt(x^2+y^2), x=-8*Pi..8*Pi, y=-8*Pi..8*Pi);
Adding a Space Curve to a Surface
Two different plot structures can be superimposed by using the display command.
Example:
Show that the helix
x = t*cos(t)
y = t*sin(t)
z = 4*t
,
t > 0
lies on the cone
.
Plot the two figures in such a way that the containment is clear.
Solution:
The algebraic verification is routine:
> assume(t > 0);
> subs( {x = t*cos(t),y = t*sin(t), z = 4*t}, z = 4*sqrt(x^2+y^2));
> simplify(%);
> testeq(%);
Now we create two appropriate plot structures and combine them:
> helix_plot := spacecurve([t*cos(t),t*sin(t),4*t], t=0..8*Pi,numpoints=100,color=MAGENTA,thickness=3):
We have used plot options to thicken the curve, and to color it so that it will stand out.
If we had used the noisy terminator here, then we would have received as output the very long list of points that make up the plot structure.
> cone_plot := plot3d(4*sqrt(x^2+y^2),x=-8*Pi..8*Pi,y=-8*Pi..8*Pi):
> display({cone_plot,helix_plot});
(By right clicking on the plot, I changed the coloring scheme of the cone to grayscale, to better view the winding helix.)
Plotting More than One Surface
To plot a set of surfaces, use a set of surfaces for the first argument of plot3d .
Example:
Plot
and its tangent plane at the point
.
Solution:
There is no necessity to use functions here but it is convenient to do so. Heretofore in this worksheet, we have used the
subs
command for substitution. With functional notation substitution is very simple.
A function in Maple has the form:
(comma_separated_sequence_of_independent_variables) -> expression
To take advantage of this construction, a function should be assigned as the value of a name. This example will illustrate.
> c := 0.1,0.3;
> F := (x,y) -> sin(sqrt(x^2+y^2))/sqrt(x^2+y^2);
> tp := (x,y) -> F(c)+D[1](F)(c)*(x-0.1)+D[2](F)(c)*(y-0.3);
> plot3d( {F(x,y),tp(x,y)}, x=-2*Pi .. 2*Pi, y=-2*Pi .. 2*Pi );
As usual with a 3d plot, it is necessary to rotate the plot to understand the objects in it. Here is another view:
Plotting Level Curves
To plot the level curves of a surface, plot the surface as usual. Then click on the surface and use two tools on the context bar. First, click on the
style = contour
icon. Then choose the
and
viewing orientation as we have done before. Alternatively, include the options
style = contour, orientation = [0,0]
in the original plot command.
Example:
Plot the level curves of
.
Solution:
> plot3d(sin(x*y),x=-Pi..Pi,y=-Pi..Pi);
Click on the plot and then click on the icon illustrated in the following screen capture:
The result is:
To view these contours projected into the xy-plane, as is usually done, click on the plot again and change both orientation variables to 0. The result is:
This plot could have been obtained in one step as follows:
> plot3d(sin(x*y),x=-Pi..Pi,y=-Pi..Pi, style = contour, orientation=[0,0]);
And to save typing, there is even a more direct way to do this using a special plot command, contourplot , for the job:
> contourplot(sin(x*y),x=-Pi..Pi,y=-Pi..Pi);
Plotting Parametric Surfaces
Plotting One Parametric Surface
Plotting a parametric surface is like plotting a parametric curve: only plot3d is used instead of spacecurve .
Example:
Plot the torus whose parametric equations are given by
x = (4+2*sin(v))*cos(u)
y = (4+2*sin(v))*sin(u)
z = 2*cos(v), u = 0 .. 2*Pi, v = 0.. 2*Pi
Solution:
> plot3d([(4+2*sin(v))*cos(u),(4+2*sin(v))*sin(u),2*cos(v)], u = 0 .. 2*Pi, v = 0.. 2*Pi);
Plotting More than One Parametric Surface (Same Parameter Range)
If the ranges of the parameters are the same for each surface, then we simply specify a set of 3-tuples for the first argument of plot3d.
Example:
The parametric surface
x = (6+2*sin(v))*cos(u)
y = (6+2*sin(v))*sin(u)
z = cos(v), u = 0 .. 2*Pi, v = -Pi..Pi
is a torus through which passes the twisted pipe
x = (2+sin(v))*cos(u)
y = (2+sin(v))*sin(u)
z = u-2*cos(v)-4, u = 0 .. 2*Pi, v = -Pi..Pi
Illustrate with an appropriate plot.
Solution:
>
plot3d({[(6+2*sin(v))*cos(u),(6+2*sin(v))*sin(u),cos(v)],
[(2+sin(v))*cos(u),(2+sin(v))*sin(u),u-2*cos(2*v)-4] },
u = 0 .. 2*Pi, v = -Pi..Pi);
That is the set
and
to view. Here is the
and
view:
Plotting More than One Parametric Surface (Different Parameter Range)
If two or more parametric surfaces are to be plotted and different parameter ranges are needed, then each plot must be done individually. The multiple plots are then simultaneously displayed with the display command.
Example:
Plot the graph of
and its tangent plane at the point
.
Solution:
After a little experimentation, we find that the tangent plane is most visible if it pokes out from the surface.
This calls for different parameter ranges.
Here is the surface:
> F := (x,y) -> exp(y/(2+y^2 + sin(x))):
> plot3d(F(x,y),x=0..12,y=0..12);
We use the percent operator (i.e., the result of Maple's last return) to name this plot structure so that we can easily recreate it later.
> bumpy_thing := %:
We now turn to the tangent plane.
> c := 8,6:
> tp := (x,y) -> F(c)+D[1](F)(c)*(x-8)+D[2](F)(c)*(y-6);
> plane_plot := plot3d( tp(x,y),x=-2..14,y=-2..12,shading = ZGRAYSCALE):
There is no point in plotting a plane by itself (since all planes look alike after rotation). We therefore have simply created and assigned the plot structure.
We have used the grayscale shading to better distinguish the plane from the surface it is tangent to. Now we combine our two plot structures. Voila:
> display( {bumpy_thing,plane_plot} );
Copyright and Author Information
Copyright and Author Information:
MultivariablePlottingI.mws A Maple Release 6 worksheet.
Author: Brian E. Blank (13 March 2000)
This html document may not be distributed by any medium,
including print, disk, and electronic transfer, without
prior written permission of the author.
The worksheet MultivariablePlottingI.mws on which this html'ed format
is based may be downloaded for
personal, noncommercial
use.
Please check the author's Maple page (http://ascc.artsci.wustl.edu/~bblank/Maple/)
for a link.
For more information, please contact the author:
Department of Mathematics,
Washington University in St. Louis
St. Louis, MO 63130
Telephone: (314) 935-6763
e-mail: brian@math.wustl.edu