Getting Started in Plotting in 3D

Introduction

OBJECTIVE: To learn to use Mathematica to plot lines, cylinders, and quadric surfaces.

In this module, you will learn how Mathematica can help you plot the lines, planes, cylinders, and quadric surfaces you have been reading about. In the process of plotting lines and planes, you will gain insight into their vector definitions.

Technology Guidelines

NOTE:  If you have just finished a module, restart Mathematica or Quit Kernel under the Evaluation pull-down menu before executing a new module.
TO OPEN CELLS, click on the arrow to the left of the cell or put your cursor on the right cell bracket and double click. To open all cells, highlight the bracket on the far right and select Grouping under the Cell pull-down menu and select Open All Subgroups.
TO STOP AN EXECUTION
Select the Evaluation pull-down menu and click on Abort Evaluation.
ORDER OF EXECUTION
Execute cells in the order given. Do not skip any Input cells within a given notebook.
SAVING NOTEBOOKS
You can save anytime to any directory you choose, and it is wise to save often. However, before you do your final save to reduce the size of your file, it is a good idea to delete all your output by selecting the Delete All Output selection under the Cell pull-down menu.
EXPERIENCING MAJOR PROBLEMS
Save if appropriate, then shut down Mathematica and start it up again.

Part I: Lines and Planes

Interpreting Lines Using Their Vector Definition

You can construct straight lines in two and three dimensions using the vector definition. Given two points on the line, we will first determine the direction of the line and then write any other point on the line as the position vector to that point plus a multiple (t) of the vector in the direction of the line.  We will start with two points, finding the direction determined by those two points, and then writing and plotting the parametric equations for the line in three dimensions.

Clear[x,y,z,t]
origin={0,0,0};
p1={1,5,-6};
p2={4,2,5};
Print["The direction of the line is ",dir=p1-p2]
Print["{x,y,z} = ",eqs=(p1-origin)+t dir]
ParametricPlot3D[Evaluate[eqs],{t,-10,10},Boxed->False,Axes->True,AspectRatio->1,AxesLabel->{x,y,z}]

Constructing Planes Using Vectors

We begin by constructing a plane from three points on the plane. We use those three points to determine two directions in the plane and then compute other points on the plane by adding their position vector to a linear combination (using parameters s and t) of the known vectors in the plane.

Clear[x,y,z,s,t,p1,p2,p3,pts]
origin={0,0,0};
p1={2,3,-1};
p2={8,-2,5};
p3={-4,0,9};
Print["{x,y,z} = ",pts=Simplify[(p1-origin)+s (p2-p1)+t (p3-p2)]]

We can also find the equation of the plane using the cross product to find the normal vector to the plane. Then we can find the points (x, y, z) that lie in the plane by dotting any vector in the plane into that normal vector and setting the dot product equal to 0.

vec1=p2-p1;
vec2=p3-p2;
normal=vec1\[Cross]vec2;
eq=normal.({x,y,z}-p1)==0;
sol=Solve[eq,z]
Plot3D[sol[[1,1,2]],{x,-10,10},{y,-10,10},AxesLabel->{"x","y","z"}]

Note that the relationships among x, y, and z are exactly the same in both parametric and nonparametric form.

Clear[x,y,z]
sol
{x,y,z}=pts
z==sol[[1,1,2]]//Simplify

You Try It: Part I

Finding equations of lines

To find the equations of lines and planes, just change the numbers in the points given, then re-execute the commands to see other lines. You can replace the red items in the following input commands to create a new line.

Clear[x,y,z,t]
origin={0,0,0};p1={2,-3,0};p2={-1,8,4};Print["The direction of the line is ",dir=p1-p2]
Print["{x,y,z} = ",eqs=(p1-origin)+t dir]
ParametricPlot3D[Evaluate[eqs],{t,-10,10},Boxed->False,Axes->True,AspectRatio->1,AxesLabel->{x,y,z}]

Finding equations of planes

Now, pick any 3 points, replace the red numbers with your points, and execute the cell to see the plane determined by the three points.

Clear[x,y,z,s,t,p1,p2,p3,pts]
origin={0,0,0};p1={-2,5,1};p2={0,3,-4};p3={1,0,7};Print["{x,y,z} = ",pts=Simplify[(p1-origin)+s (p2-p1)+t (p3-p2)]]
vec1=p2-p1;
vec2=p3-p2;
normal=vec1\[Cross]vec2
eq=normal.({x,y,z}-p1)==0;
sol=Solve[eq,z]
Plot3D[sol[[1,1,2]],{x,-10,10},{y,-10,10},AxesLabel->{"x","y","z"}]

Part II: Cylinders and Quadric Surfaces

When you look at the equations for cylinders or quadric surfaces, notice that they are not in the standard form for graphing on most graphing calculators or software packages, because you cannot usually solve explicity for the z variable. To get around this, we use a special command called ContourPlot3D. In a later chapter, you will learn about contour plots, but, for now, just think of this as a command to get the job done.

Suppose that you want to plot the cylinder y = (x^2). We think of this as f(x,y,z)=y-(x^2)=0, and we ask for a ContourPlot3D of this function. The default is to plot only the contour when the function specified is 0. Note that we must specify the values of x, y, and z overwhich to extend our plot, even though z does not appear in the function itself.

Clear[x,y,z,function]
function=y-(x^2);
ContourPlot3D[function==0,{x,-3,3},{y,-2,2},{z,-4,4},Axes->True,AxesLabel->{x,y,z},Boxed->False]

Let's try another cylinder.

function=(y^2)-4 (z^2)-1;
ContourPlot3D[function==0,{x,-2,2},{y,-2,2},{z,-2,2},Axes->True,AxesLabel->{x,y,z},Boxed->False]

Next we will plot the quadric surface, (z^2)-2(x^2)-3(y^2)= 0.

function=(z^2)-2 (x^2)-3 (y^2);
ContourPlot3D[function==0,{x,-3,3},{y,-2,2},{z,-4,4},Axes->True,AxesLabel->{x,y,z},Boxed->False]

Here's an ellipsoid.

function=(x^2)+4 (y^2)+(z^2)-4;
ContourPlot3D[function==0,{x,-2,2},{y,-1,1},{z,-2,2},Axes->True,AxesLabel->{x,y,z},Boxed->False]

You Try It: Part II

To plot the cylinders and quadric surfaces, go back to any of the plots and alter the functions as you wish. Be sure to use correct terminology. Note that some 3-D plots take quite a bit of time to plot. If you find you are waiting too long, you can always pull down the Kernel menu and select Abort Evaluation. To experiment, replace the function in red with another function of x, y, and z. Be certain to use correct terminology. You might want to see what this one looks like first, before putting in a different function. Why isn't this a quadric surface?

function=(E^(-x)) (y^2)-Cos[z];
ContourPlot3D[Evaluate[function==0],{x,-4,4},{y,-4,4},{z,-4,4},Axes->True,AxesLabel->{x,y,z},Boxed->False]