In this worksheet we explore creating 3D plots.
Maple worksheet: 3dplots.mw
> | restart;
with(plots): |
Basic Plots
For basic plots use "plot(expression, domain);" As always, for more options and examples try looking up help pages on plots and plot options.
Example: Let's plot where .
> | plot(x^2,x=-1..2); |
> | ? plot |
> | ? plot,options |
Defining Functions
Often it is convenient to define a function (which will be reused later). Recall that "A := B;" assigns "B" to the variable "A". Also, in Maple "A -> B" means "A" maps to "B". So "x -> sqrt(x)" denotes the map which sends x to its square root. The minus sign followed by a greater than sign is supposed to resemble an arrow (read "maps to"). If you are working in Math mode, Maple will automatically turn "->" into "". Now putting an assignment together with a map we get commands like: "f := x -> x^2;" assigns the name "f" to the map which sends x to x^2.
To print this out all nice a pretty, we can use the command " 'f(x)' = f(x) ". The f(x) in single quotes is just prints out verbatim. The f(x) out of quotes is evaluated according to how the function f is defined. I usually place a colon ":" after the assignment to suppress the akward looking "". I prefer just to show "" which is the output resulting from ".
> | f := x -> x^2; # I usually hide this command's output.
'f(x)'=f(x); plot(f(x),x=-1..2); |
Plugging values into the map is easy. Let's plug into .
> | f(3); |
(1) |
Parametric plots are accompished with the same "plot" command, but the syntax is a little different. Use "plot( [ x(t), y(t), t=a..b ] );" to plot the parametric curve defined by x(t) and y(t) which ranges .
Example: where . Colored in green and made extra thick.
> | plot([exp(t)*cos(t),exp(t)*sin(t),t=0..4*Pi],color=green,thickness=5); |
Unlike parametric curves in 2-space (which are graphed with the same "plot" command which plots regular graphs), in 3-space you need the special command "spacecurve" to plot a parametric curve. The syntax is easy: "spacecurve( [ x(t), y(t), z(t) ], t = a..b );"
Example: A Helix. [I made the curve thicker so it's easier to see and added some coordinate axes.]
> | spacecurve([cos(t),sin(t),t],t=0..4*Pi,thickness=5,axes=boxed,viewpoint=circleleft); |
plot3d and Non-Rectangular Domains
Functions of more than one variable and their plots work about the same as those with one variable.
Example: We define the function which maps to calling it and then printing it out "nicely".
> | f := (x,y) -> sqrt(x^2+y^2):
'f(x,y)' = f(x,y); |
(2) |
Let's plug in and . I'll make the value 2.0 instead of 2 to give Maple the clue that I want a decimal answer.
> | f(1,2.0); |
(3) |
Let's plot the piece of the surface defined by if we restrict the domain to be all points where and .
Note: I've added plot options: "axes=boxed" (to show axes) and "viewpoint=circleleft" (to play an animation of the surface spinning around -- for the html version of this worksheet).
> | plot3d(f(x,y),x=-2..2,y=-1..1,axes=boxed,viewpoint=circleleft); |
The surface is a cone. But it looks weird because we chose a rectangle as our domain. It would look better if we used a circular domain. Let's graph it over the domain (this is the disk centered at the origin of radius 3). To restrict our domain to this region, we need to give Maple
the curves which define its boundaries. Let's take the defining equation and write the boundaries in terms of "y = something". So we can enter the equation, right-click, and choose solve --> solve for variable --> y. OR we could just use the "solve" command directly.
> | x^2+y^2=3^2;
solve(x^2+y^2=3^2,y); |
(4) |
> | solve({x^2+y^2=3^2,y=0}); |
(5) |
The first solve command tells us that our region is bounded by the curves . The second solve command determines where these curves intersect the -axis (i.e. ). Thus our region is defined by and
Before getting back to our 3D plot. Let's plot the circle. To do this with regular "plot" we need to graph the top and the bottom simultaneously. To do this we "list" the expressions we want graphed. "plot([expression1, expression2, etc. ], x=a..b, options);"
Note: I set the color to "[red,red]" so both parts of the circle are graphed using the same color.
> | plot([sqrt(9-x^2),-sqrt(9-x^2)],x=-3..3,color=[red,red]); |
So our domain is everything on and inside this circle. Notice our choice of domain for : which matches what we found out from the solver. Of course, this is a round about way to graph a circle. It's much easier to use "implicitplot" which is a plotting tool for graphing implicitly defined functions. The function "implicitplot" is not automatically loaded when Maple starts, so to use it we need to load the "plots" package. Which I did at the beginning of this worksheet.
> | implicitplot(x^2+y^2=3^2,x=-3..3,y=-3..3); |
Just for fun, here's an ellipse (I'll use the plot option "scaling=constrained" so Maple doesn't rescale the ellipse back into a circle).
> | implicitplot(x^2/3^2+y^2/2^2=1,x=-3..3,y=-3..3,scaling=constrained); |
Now to plot our cone over the disk region. The plot option "orientation=[0,70]" determines how the 3D plot is rotated. I tried to pick angles which display the cone "nicely". Then I made the viewpoint to circle around the surface so we produce a nice animation for the webpage version.
> | plot3d(f(x,y),x=-3..3,y=-sqrt(9-x^2)..sqrt(9-x^2),orientation=[0,70],viewpoint=circleleft); |
Example: Let's plot a disk of radius 3 centered at with -coordinate along with the surface above it.
First, let's gather some information about the domain of our surfaces. I'll chuck in a plot of the boundary of this domain.
> | EQN := (x-1)^2+(y-2)^2=3^2;
solve(EQN,y); solve({EQN,y=2}); solve({EQN,x=1}); implicitplot(EQN,x=-2..4,y=-1..5); |
Now we can plot the disk and the surface together. Also, let's color the paraboloid blue and the disk red.
> | f := (x,y) -> 10-(x-1)^2-(y-2)^2:
'f(x,y)' = f(x,y); plot3d({1,f(x,y)},x=-2..4,y=2-sqrt(8-x^2+2*x)..2+sqrt(8-x^2+2*x),axes=boxed,viewpoint=circleleft,color=[red,blue]); |
Example: Plot the function inside the disk of radius 3 centered at the origin.
> | f := (x,y) -> ln(4*x^2+y^2):
'f(x,y)' = f(x,y); plot3d(f(x,y),x=-3..3,y=-sqrt(9-x^2)..sqrt(9-x^2),viewpoint=circleleft); |
Notice that except at the origin where . Since is undefined, our plot has an asymptote there.
Example: Let's define "sEqn" to be the equation of the sphere whose center is and radius is 2. Also, we will define "xyPlane" to be the equation of the -plane. Then let's plot the sphere and -plane together in the same plot. We'll plot the sphere using implicitplot3d and the plane using plot3d. The "display" command allows us to combine several plots together.
Note: I used "numpoints=5000" to make the sphere less boxy. The ranges for the and coordinates are chosen so that the whole sphere is plotted (although we really only need: , , and ). Using the option "scaling=constrained" guarantees that the sphere isn't squished and rescaled. Of course, "viewpont=circleleft" is to generate a nice animation for the web version of this sheet.
> | sEqn := (x-1)^2+(y-2)^2+(z-3)^2=2^2;
xyPlane := z=0; sPlot := implicitplot3d(sEqn,x=-5..5,y=-5..5,z=-2..7,numpoints=5000): pPlot := plot3d(rhs(xyPlane),x=-5..5,y=-5..5): display({sPlot,pPlot},axes=normal,scaling=constrained,viewpoint=circleleft); |
Now let's use "solve" to solve sEqn for and then plot both of these surfaces. Even though we don't need to, I'll select and save each solution separately just to show off a few more Maple commands.
> | solns := solve(sEqn,z);
eqn1 := solns[1]; eqn2 := solns[2]; upSphere := plot3d(eqn1,x=-5..5,y=-5..5,numpoints=5000): downSphere := plot3d(eqn2,x=-5..5,y=-5..5,numpoints=5000): display({pPlot,upSphere,downSphere},axes=normal,scaling=constrained,viewpoint=circleleft); |
Notice the nasty jagged edges. Even though implicitplot3d doesn't produce the most beautiful surfaces, solving and graphing each half is far worse!! So is Maple incapable of plotting nice spheres? No. We just need to use the right coordinate system, namely spherical coordinates. I'll plot the sphere using a parametrization derived from spherical coordinates.
> | niceSphere := plot3d([2*cos(theta)*sin(phi)+1,2*sin(theta)*sin(phi),2*cos(phi)+3],theta=0..2*Pi,phi=0..Pi):
display({pPlot,niceSphere},axes=normal,scaling=constrained,viewpoint=circleleft); |
Example: Let's assemble an ice cream cone from a sphere, torus, and cone. We'll need to resize each piece, change their locations, and adjust their colors so the plot resembles an ice creme cone.
> | sphere := plot3d([5*cos(theta)*sin(phi),5*sin(theta)*sin(phi),5*cos(phi)+10],theta=0..2*Pi,phi=0..Pi):
torus := plot3d([(2*cos(phi)+6)*cos(theta),(2*cos(phi)+6)*sin(theta),2*sin(phi)],theta=0..2*Pi,phi=0..2*Pi): cone := plot3d([r*cos(theta),r*sin(theta),3*r-15],r=0..3,theta=0..2*Pi): display(sphere,torus,cone,scaling=constrained,viewpoint=circleleft); |
Notice that all three surfaces are defined parametrically. The sphere's parametrization comes from spherical coordinates. The torus is constructed by sweeping a circle around a circle. The cone's parametrization comes from cylindrical coordinates. I kept the cone the same size but shrunk down the sphere to have a radius of 3 instead of 5. Then I moved the sphere and cone veritcally until they intersected in a reasonable way. I had to shrink both radii defining the torus (2 & 6 to 0.75 & 3) to make it fit around the sphere to look like a scoop of ice cream. Finally, the cone should be brown (it's a sugar cone) and I decided to go with green mint ice cream.
> | sphere := plot3d([3*cos(theta)*sin(phi),3*sin(theta)*sin(phi),3*cos(phi)+0.5],theta=0..2*Pi,phi=0..Pi,color=green):
torus := plot3d([(0.75*cos(phi)+3)*cos(theta),(0.75*cos(phi)+3)*sin(theta),0.75*sin(phi)],theta=0..2*Pi,phi=0..2*Pi,color=green): cone := plot3d([r*cos(theta),r*sin(theta),3*r-9],r=0..3,theta=0..2*Pi,color=brown): display(sphere,torus,cone,scaling=constrained,orientation=[65,50,-25],viewpoint=circleleft); |
Plots of Quadric Surfaces
Given a first-degree equation in three (or fewer) variables, we get a plane: .
> | plot3d(solve(x+y+z=1,z), x=-1..1, y=-1..1, title="A Plane", axes=boxed, orientation=[45,70], viewpoint=circleleft);
contourplot(solve(x+y+z=1,z), x=-1..1, y=-1..1, title="Contours of planes are lines."); |
Given a second-degree equation in two variables (one of the variables is "missing"), we get a cylinder.
Example: (parabolic cylinder) and (circular cylinder). We can also have hyperbolic and elliptic cylinders. The contours of our parabolic cylinder are lines. Then in the example below that all of the contours of the circular cylinder are the exact same circle!
> | plot3d(solve(z=x^2,z), x=-1..1, y=-1..1, title="A Parabolic Cylinder", axes=boxed, orientation=[45,70], viewpoint=circleleft);
contourplot(solve(z=x^2,z), x=-1..1, y=-1..1, title="The contours of this parabolic cylinder are lines"); |
> | implicitplot3d(x^2+y^2=1, x=-1..1, y=-1..1, z=-1..1, title="A Circular Cylinder", axes=boxed, orientation=[45,70], viewpoint=circleleft); |
A quadric surface is the graph of a second-degree equation in (exactly) three variables. Thus its equation fits into the general form: where and cannot all be zero (simultaneously). Using a translation and a rotation, this equation can be put into a standard form which looks like one of the following: or . Quadric surfaces only come in six different flavors. Here is an example of each one... [The equations given are in a "standard form". Finding such a form involves some non-trivial algebra. We will not worry about this.]
Ellipsoids:
Notice that when we have a very special type of ellipsoid, namely, a sphere: .
> | implicitplot3d(x^2/4+y^2/9+z^2=1, x=-3..3, y=-3..3, z=-1..1, numpoints=2000, scaling=constrained, title="An Ellipsoid", viewpoint=circleleft, axes=boxed);
contourplot({solve(x^2/4+y^2/9+z^2=1,z)}, x=-3..3, y=-3..3, scaling=constrained, title="The contours of an ellipsoid are ellipses."); |
Elliptic Paraboloids:
> | implicitplot3d(z=x^2/4+y^2/9, x=-3..3, y=-3..3, z=0..1, numpoints=2000, title="An Elliptic Paraboloid", viewpoint=circleleft, axes=boxed);
contourplot(solve(z=x^2/4+y^2/9,z), x=-3..3, y=-3..3, scaling=constrained, title="These contours are ellipses."); |
Cones:
Actually we have a "double" cone. Solving for , gets us where the "+" gives us the upper cone and the "-" gives us the upside-down cone.
> | implicitplot3d(z^2=x^2+y^2, x=-1..1, y=-1..1, z=-1..1, numpoints=2000, title="A (Circular) Cone", viewpoint=circleleft, axes=boxed);
contourplot({solve(z^2=x^2+y^2,z)}, x=-1..1, y=-1..1, scaling=constrained, title="These contours are circles."); |
Hyperboloids of One Sheet:
> | implicitplot3d(x^2/4+y^2/9-z^2=1, x=-4..4, y=-4..4, z=-2..2, numpoints=2000, title="A Hyperboloid of One Sheet", viewpoint=circleleft, axes=boxed);
contourplot({solve(x^2/4+y^2/9-z^2=1,z)}, x=-4..4, y=-4..4, scaling=constrained, title="These contours are ellipses."); |
Hyperboloids of Two Sheets:
> | implicitplot3d(-x^2/4-y^2/9+z^2=1, x=-4..4, y=-4..4, z=-4..4, numpoints=2000, title="A Hyperboloid of Two Sheets", viewpoint=circleleft, axes=boxed);
contourplot({solve(-x^2/4-y^2/9+z^2=1,z)}, x=-4..4, y=-4..4, scaling=constrained, title="These contours are ellipses."); |
Hyperbolic Paraboloids:
> | implicitplot3d(z=x^2/4-y^2/9, x=-3..3, y=-3..3, z=-1..1, numpoints=2000, title="A Hyperbolic Paraboloid", viewpoint=circleleft, axes=boxed);
contourplot(solve(z=x^2/4-y^2/9,z), x=-3..3, y=-3..3, scaling=constrained, title="These contours are hyperbolas."); |