MAT223 Surfaces in Space

32 days ago by jonathan.senning

This tutorial will guide you through the basic operations necessary to use Sage to handle basic surface graphing in 3D.  Sage is a powerful tool; we'll only just begin to see its capabilities here.  If you are interested you can learn much more about its capabilities at

If you are reading this as a "published worksheet" and have not yet done so, look for the "Edit a copy" text in the upper-left corner of the page and click on it.  That will make this worksheet available for you to use and change.  For the instructions below to make sense, after you've made your own copy of the worksheet, use the "Action" pull-down menu to select "Delete All Output".  This will create the graphs one at a time as you work through them.

We'll begin by learning how to interact with the worksheet interface.  It's quite straightforward with the exception that to evaluate a command you must either press "shift-enter" or click the "evaluate" link below the command entry cell.  Pressing "enter" alone will merely create a new line in the command entry cell.

Click on the cell below to select it.  Hold the shift key down and press the enter key or click the "evaluate" button to evaluate the cell's contents.

You've just told Sage that you'll be using the variables x, y, and z.  If we hadn't done this before using the commands below we would seen an error saying that the variables were not defined.
Before we plot in 3D, lets try one plot in 2D.   The command below will graph two periods of the sine function:  Notice that we can use "pi" and that we explicitly indicated multiplication with the * symbol.

If you want, you can change the function in the above command to plot different functions.  Remember that we've defined the variables x, y, and z so those are the only ones you can use (unless you define others).  Just retype the function, change the range for the independent variable (if necessary) and press shift-enter.  This can be repeated as many times as you like.

Alright, let's start some 3D graphing.

Example 1: Plot the right circular cylinder given by x2 + y2 = 1.

First we begin by declaring the variables we will use.  This only needs to be done once, but if you reload this worksheet you'll need to evaluate this cell again (or use the "Action" pull-down menu and select "Evaluate All").

Next we plot the surface.  If the function we want to plot can be written as z=f(x,y) then we can use the plot3d() function.  If, however, z is implicit (or missing, meaning the expression should be true for all z), we need to use implicit_plot3d().  The first argument is the equation we want to plot (be sure and use == for equality) and the next three arguments specify the range for each axis)

implicit_plot3d(x^2+y^2==1, (x,-2,2), (y,-2,2), (z,-2,2)) 

If your browser supports Java applets you can rotate and scale the graph.  Click on the "Make Interactive" button.  If requested, allow the JmolApplet to run.  Now press and hold the left mouse button while the pointer is on the figure to change its orientation.  Doing the same with the middle mouse button allows you to zoom in or out.  It takes some practice to figure out how to move it the way you want.  If you want to "start over," merely re-evaluate the cell that draws the figure.

Example 2: Next we want to plot the elliptic paraboloid given by z = x2/2 + y2.  Since this is written in the form z = f(x,y) we can use the regular plot3d() function.  The first argument is the function, the next two specify the range for the axes, and the last argument (called a named argument) causes the color to vary with z value.


We could, of course, plot the same function using the implicit_plot3d() function...

implicit_plot3d(z==x^2/2+y^2,(x,-3,3), (y,-3,3), (z,0,15)) 

We can place restrictions on one or more variables to help us see the plot.  For example, often we draw paraboloids with the opening bounded by an ellipse.  To do this for this example we want 0 <= z <= 4.  The "region" parameter takes a Python "lambda"  function with three variables and specifies the portion of the range we want to see.

implicit_plot3d(z==x^2/2+y^2,(x,-3,3), (y,-3,3), (z,0,15), region=lambda x,y,z: z>=0 and z<=4) 

Example 3: We can also plot planes.  The plane 3yz = 2 is plotted using the color blue:

plot3d(3*y-2, (x,-3,3), (y,-3,3), color='blue') 

Example 4: The surface given by z = y2x2 is a hyperbolic paraboloid.

plot3d(y^2-x^2, (x,-3,3), (y,-3,3), adaptive=True) 

Example 5: Sometimes we want to see two surfaces at once, perhaps to study the curve formed by their intersection.  This can be done with two plot3d() commands displayed together.   Here we show the plane and hyperbolic paraboloid we've just examined together on one plot  Notice how the opacity named parameter can be used for a translucent effect.  An opacity of 0.0 means completely transparent whlie an opacity of 1.0 means completely opaque.

A=plot3d(3*y-2, (x,-3,3), (y,-3,3), color='blue', opacity=0.9) B=plot3d(y^2-x^2, (x,-3,3), (y,-3,3), color='red', opacity=0.9) A+B 

Example 6: Finally, we draw the ellipsoid x2 + y2/9 + z2/4 = 1.  This function is implicit for z so we use implicit_plot3d() as in the first example.

implicit_plot3d(x^2+y^2/9+z^2/4==1, (x,-3,3), (y,-4,4), (z,-3,3)) 

You can find out all about the functions we've used here, including different options that can be used to control how they work, by reading the help information about them.  To read help about a command in Sage you only need to type the command name followed immediately by a question mark and then press shift enter (or click on "evaluate").

That wraps up this tutorial.  Below are some additional examples.  Sage does many other things that you might find helpful in this class and other math classes.  We may use it again during the quad, but if your interested in finding out more you should feel free to talk with the professor.

plot3d(x*exp(-(x^2)/2-(y^3)/3+y), (x,-3,3), (y,-2,3), adaptive=True) 
implicit_plot3d(z==x^2/2+y^2,(x,-3,3), (y,-3,3), (z,0,5), region=lambda x,y,z: z>=0 and z<=4) 
parametric_plot3d( (sin, cos, lambda u: u/10), (0, 20)) 
s, t = var('s,t') parametric_plot3d((cos(s), sin(s) + cos(t), sin(t)), (s, 0, 2*pi), (t, -pi, pi)) 
parametric_plot3d((2*cos(s)*cosh(t),sin(s)*cosh(t), sqrt(2)*sinh(t)), (s,0,2*pi), (t,-2,2)) 
x0=-6; x1=6; y0=-6; y1=6; z0=-6; z1=6 A=implicit_plot3d(x-2*y+4*z==15,(x,x0,x1),(y,y0,y1),(z,z0,z1),color='blue') B=implicit_plot3d(-x+3*y-z==2,(x,x0,x1),(y,y0,y1),(z,z0,z1),color='red') C=implicit_plot3d(2*x-3*z==-17,(x,x0,x1),(y,y0,y1),(z,z0,z1),color='green') (A+B+C).show()