Plotting 3-Dimesional Stuff 

 

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 y = `*`(`^`(x, 2)) where `and`(`<=`(-1, x), `<=`(x, 2)). 

 

> plot(x^2,x=-1..2);
 

Plot_2d
 

> ? 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 "f := proc (x) options operator, arrow; `*`(`^`(x, 2)) end proc". I prefer just to show "f(x) = `*`(`^`(x, 2))" 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);
 

 

 

proc (x) options operator, arrow; `*`(`^`(x, 2)) end proc
f(x) = `*`(`^`(x, 2))
Plot_2d
 

 

Plugging values into the map is easy. Let's plug x = 3 into f(x). 

 

> f(3);
 

9 (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 `and`(`<=`(a, t), `<=`(t, b)).  


Example:  r(t) = `<,>`(`*`(exp(t), `*`(cos(t))), `*`(exp(t), `*`(sin(t))))where `and`(`<=`(0, t), `<=`(t, `+`(`*`(4, `*`(Pi))))). Colored in green and made extra thick.
 

> plot([exp(t)*cos(t),exp(t)*sin(t),t=0..4*Pi],color=green,thickness=5);
 

Plot_2d
 

 

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);
 

Plot_2d
 

 

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 x, y to sqrt(`+`(`*`(`^`(x, 2)), `*`(`^`(y, 2)))) calling it f and then printing it out "nicely". 

 

> f := (x,y) -> sqrt(x^2+y^2):
'f(x,y)' = f(x,y);
 

f(x, y) = `*`(`^`(`+`(`*`(`^`(x, 2)), `*`(`^`(y, 2))), `/`(1, 2))) (2)
 

 

Let's plug in x = 1 and y = 2. I'll make the y value 2.0 instead of 2 to give Maple the clue that I want a decimal answer. 

 

> f(1,2.0);
 

2.236067977 (3)
 

 

Let's plot the piece of the surface defined by z = f(x, y) if we restrict the domain to be all points x, y where `and`(`<=`(-2, x), `<=`(x, 2)) and `and`(`<=`(-1, y), `<=`(y, 1)).  

 

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);
 

Plot_2d
 

 

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 `<=`(`+`(`*`(`^`(x, 2)), `*`(`^`(y, 2))), `^`(3, 2)) (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);
 

 

`+`(`*`(`^`(x, 2)), `*`(`^`(y, 2))) = 9
`*`(`^`(`+`(`-`(`*`(`^`(x, 2))), 9), `/`(1, 2))), `+`(`-`(`*`(`^`(`+`(`-`(`*`(`^`(x, 2))), 9), `/`(1, 2))))) (4)
 

> solve({x^2+y^2=3^2,y=0});
 

{x = 3, y = 0}, {x = -3, y = 0} (5)
 

 

The first solve command tells us that our region is bounded by the curves y = `&+-`(sqrt(`+`(`-`(`*`(`^`(x, 2))), 9))). The second solve command determines where these curves intersect the x-axis (i.e. y = 0).  Thus our region is defined by `and`(`<=`(`+`(`-`(sqrt(`+`(`-`(`*`(`^`(x, 2))), 9)))), y), `<=`(y, sqrt(`+`(`-`(`*`(`^`(x, 2))), 9)))) and `and`(`<=`(-3, x), `<=`(x, 3.)) 

 

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]);
 

Plot_2d
 

 

So our domain is everything on and inside this circle. Notice our choice of domain for x: `and`(`<=`(-3, x), `<=`(x, 3)) 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);
 

Plot_2d
 

 

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);
 

Plot_2d
 

 

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);
 

Plot_2d
 

 

Example: Let's plot a disk of radius 3 centered at 1, 2 with z-coordinate z = 1along with the surface z = `+`(10, `-`(`*`(`^`(`+`(x, `-`(1)), 2))), `-`(`*`(`^`(`+`(y, `-`(2)), 2)))) 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);
 

 

 

 

 

`+`(`*`(`^`(`+`(x, `-`(1)), 2)), `*`(`^`(`+`(y, `-`(2)), 2))) = 9
`+`(2, `*`(`^`(`+`(8, `-`(`*`(`^`(x, 2))), `*`(2, `*`(x))), `/`(1, 2)))), `+`(2, `-`(`*`(`^`(`+`(8, `-`(`*`(`^`(x, 2))), `*`(2, `*`(x))), `/`(1, 2)))))
{x = -2, y = 2}, {x = 4, y = 2}
{x = 1, y = -1}, {x = 1, y = 5}
Plot_2d
 

 

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]);
 

 

f(x, y) = `+`(10, `-`(`*`(`^`(`+`(x, `-`(1)), 2))), `-`(`*`(`^`(`+`(y, `-`(2)), 2))))
Plot_2d
 

 

Example: Plot the function f(x, y) = ln(`+`(`*`(4, `*`(`^`(x, 2))), `*`(`^`(y, 2)))) 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);
 

 

f(x, y) = ln(`+`(`*`(4, `*`(`^`(x, 2))), `*`(`^`(y, 2))))
Plot_2d
 

 

Notice that `>`(`+`(`*`(4, `*`(`^`(x, 2))), `*`(`^`(y, 2))), 0) except at the origin where `+`(`*`(4, `*`(`^`(x, 2))), `*`(`^`(y, 2))) = 0. Since ln(0) is undefined, our plot has an asymptote there. 

 

 

Example: Let's define "sEqn" to be the equation of the sphere whose center is 1, 2, 3 and radius is 2. Also, we will define "xyPlane" to be the equation of the xy-plane. Then let's plot the sphere and xy-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 z coordinates are chosen so that the whole sphere is plotted (although we really only need: `and`(`<=`(-1, x), `<=`(x, 3)), `and`(`<=`(0, y), `<=`(y, 4)), and `and`(`<=`(1, z), `<=`(z, 5))). 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);
 

 

 

`+`(`*`(`^`(`+`(x, `-`(1)), 2)), `*`(`^`(`+`(y, `-`(2)), 2)), `*`(`^`(`+`(z, `-`(3)), 2))) = 4
z = 0
Plot_2d
 

 

Now let's use "solve" to solve sEqn for z 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);
 

 

 

 

`+`(3, `*`(`^`(`+`(`-`(1), `-`(`*`(`^`(x, 2))), `*`(2, `*`(x)), `-`(`*`(`^`(y, 2))), `*`(4, `*`(y))), `/`(1, 2)))), `+`(3, `-`(`*`(`^`(`+`(`-`(1), `-`(`*`(`^`(x, 2))), `*`(2, `*`(x)), `-`(`*`(`^`(y, 2...
`+`(3, `*`(`^`(`+`(`-`(1), `-`(`*`(`^`(x, 2))), `*`(2, `*`(x)), `-`(`*`(`^`(y, 2))), `*`(4, `*`(y))), `/`(1, 2))))
`+`(3, `-`(`*`(`^`(`+`(`-`(1), `-`(`*`(`^`(x, 2))), `*`(2, `*`(x)), `-`(`*`(`^`(y, 2))), `*`(4, `*`(y))), `/`(1, 2)))))
Plot_2d
 

 

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);
 

Plot_2d
 

 

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);
 

Plot_2d
 

 

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);
 

Plot_2d
 

 

Plots of Quadric Surfaces 

 

Given a first-degree equation in three (or fewer) variables, we get a plane: `+`(`*`(A, `*`(x)), `*`(B, `*`(y)), `*`(C, `*`(z)), D) = 0. 

 

> 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.");
 

 

Plot_2d
Plot_2d
 

 

Given a second-degree equation in two variables (one of the variables is "missing"), we get a cylinder. 

 

Example: z = `*`(`^`(x, 2)) (parabolic cylinder) and `+`(`*`(`^`(x, 2)), `*`(`^`(y, 2))) = 1 (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");
 

 

Plot_2d
Plot_2d
 

> 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);
 

Plot_2d
 

 

A quadric surface is the graph of a second-degree equation in (exactly) three variables. Thus its equation fits into the general form:`+`(`*`(A, `*`(`^`(x, 2))), `*`(B, `*`(`^`(y, 2))), `*`(C, `*`(`^`(z, 2))), `*`(D, `*`(xy)), `*`(E, `*`(yz)), `*`(F, `*`(xz)), `*`(G, `*`(x)), `*`(H, `*`(y)), `*`(I, `*`(z)), J) = 0 where and F 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:`+`(`*`(A, `*`(`^`(x, 2))), `*`(B, `*`(`^`(y, 2))), `*`(C, `*`(`^`(z, 2))), J) = 0  or  `+`(`*`(A, `*`(`^`(x, 2))), `*`(B, `*`(`^`(y, 2))), `*`(I, `*`(z))) = 0. 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: `+`(`/`(`*`(`^`(x, 2)), `*`(`^`(a, 2))), `/`(`*`(`^`(y, 2)), `*`(`^`(b, 2))), `/`(`*`(`^`(z, 2)), `*`(`^`(c, 2)))) = 1 

 

Notice that when `and`(a = b, b = c) we have a very special type of ellipsoid, namely, a sphere: `+`(`*`(`^`(x, 2)), `*`(`^`(y, 2)), `*`(`^`(z, 2))) = `*`(`^`(c, 2)). 

 

> 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.");
 

 

Plot_2d
Plot_2d
 

 

Elliptic Paraboloids: `/`(`*`(z), `*`(c)) = `+`(`/`(`*`(`^`(x, 2)), `*`(`^`(a, 2))), `/`(`*`(`^`(y, 2)), `*`(`^`(b, 2)))) 

 

> 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.");
 

 

Plot_2d
Plot_2d
 

 

Cones: `/`(`*`(`^`(z, 2)), `*`(`^`(c, 2))) = `+`(`/`(`*`(`^`(x, 2)), `*`(`^`(a, 2))), `/`(`*`(`^`(y, 2)), `*`(`^`(b, 2)))) 

 

Actually we have a "double" cone. Solving for z, gets us z = `*`(`&+-`(c), `*`(sqrt(`+`(`/`(`*`(`^`(x, 2)), `*`(`^`(a, 2))), `/`(`*`(`^`(y, 2)), `*`(`^`(b, 2))))))) 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.");
 

 

Plot_2d
Plot_2d
 

Hyperboloids of One Sheet: `+`(`/`(`*`(`^`(x, 2)), `*`(`^`(a, 2))), `/`(`*`(`^`(y, 2)), `*`(`^`(b, 2))), `-`(`/`(`*`(`^`(z, 2)), `*`(`^`(c, 2))))) = 1 

 

> 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.");
 

 

Plot_2d
Plot_2d
 

Hyperboloids of Two Sheets: `+`(`-`(`/`(`*`(`^`(x, 2)), `*`(`^`(a, 2)))), `-`(`/`(`*`(`^`(y, 2)), `*`(`^`(b, 2)))), `/`(`*`(`^`(z, 2)), `*`(`^`(c, 2)))) = 1 

 

> 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.");
 

 

Plot_2d
Plot_2d
 

Hyperbolic Paraboloids: `/`(`*`(z), `*`(c)) = `+`(`/`(`*`(`^`(x, 2)), `*`(`^`(a, 2))), `-`(`/`(`*`(`^`(y, 2)), `*`(`^`(b, 2))))) 

 

> 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.");
 

 

Plot_2d
Plot_2d