Graphing Curves and Surfaces in Mathematica
Math 211  Prof. Leise

Introduction to Mathematica

Three rules of thumb to remember about Mathematica:

1) Commands and functions like Sin[x] and Sqrt[16] begin with capital letters.  Also, constants like Pi are capitalized.

2) Use square brackets with commands and functions (not parentheses).  For example, Cos[x] or Exp[x^2].

3) Press shift-enter to evaluate a group of commands (grouped by brackets at far right).

Work through the examples below to get the hang of Mathematica.

Example 1.  Evaluate the command line below by putting the cursor anywhere on that line and pressing shift-enter.  Note that saying 2 Pi means 2 times Pi.  This command plots the graph of y=cos(x) for x from 0 to 2π.

Plot[Cos[x],{x,0,2 Pi}]

Example 2.  Put your cursor just below this cell so that a long horizontal line appears, then click to create a new command line.  Have Mathematica multiply 345 by 29 on this new line, pressing shift-enter to evaluate.  To multiply negative numbers or do other arithmetical grouping, use parentheses: (-345)(29+12).

Example 3.  Find the square root of 4489 by using the command Sqrt[4489].  Also find the square root of 4500.  Note that Mathematica gives the answer in exact form.  If you want an decimal approximation, use N[number] (N stands for numerical approximation), e.g., N[Sqrt[4500]].

Example 4.  Mathematica can do some algebraic and trigonometric simplification.  For example, see what happens if you ask for the cosine of the arcsine of x:  Cos[ArcSin[x]].

Example 5.  To evaluate (e^x), write Exp[x]; to find the natural logarithm, use Log[x].  Try finding decimal approximations for (e^3) and for ln4 (natural log of 4).

Example 6.  One of the major purposes for using Mathematica in Math 211 is for plotting curves and surfaces.  Imitate the plotting command in Example 1 to plot the following curves:

(a) sin(x) for 0≤x≤4π

(b) ln(4x) for 1≤x≤5 (remember to use Log)

(c)  (e^x) for -2≤x≤2 (you can use Exp[x] or  (e^x))

Example 7.  To plot multiple graphs on the same set of axes, put the functions inside curly braces, as in the following example:

Plot[{Sin[x],Cos[x],1-x/3},{x,0,2 Pi}]

Plot the square root of x and x-squared on the same plot.

Example 8.  Mathematica can take derivatives; use the command D[f[x],x], for example, D[Sin[x],x].  Try taking the derivatives of Exp[x^2] and of Sin[x]Cos[x].

Example 9.  Mathematica can find some integrals; use the command Integrate[f[x],x] for indefinite integrals and Integrate[f[x],{x,a,b}] for definite integrals. For example, Integrate[Sin[x],x] or  Integrate[Sin[x],{x,0,Pi}].  Try finding the indefinite integral of Log[x^2] and the definite integral of Sin[x]Cos[x] for x from 0 to Pi/2.

Plotting Lines and Surfaces

Explicit equations give one variable in terms of another: y==3x+1 (line)   or   z==2x-3y-4 (plane).  In contrast, implicit equations involves mixes of the variable, for example, 3x-y-1==0 (line)  or  x^2+y^2+z^2==1 (sphere).  For today, we will focus on graphing explicit equations.

The double equal sign indicates an equation in Mathematica, in contrast to a single equal sign that assigns values to variables. That is, y=3 sets y to have the value 3, while y==3 describes a vertical line.

To plot explicit equations, use Plot and Plot3D, which each take the expression on the right hand side of the equation (not the full equation).

For example, to plot y==3x+1, use the following command:

Plot[3x+1,{x,-2,2}]

To graph multiple curves, make a list in curly braces inside the Plot command:

Plot[{3x+1,-x+4},{x,-2,2},PlotStyle->{Red,Blue}]

Plot3D takes the expression you want to set equal to z. For example, to plot the plane z==2x-3y-4:

Plot3D[2x-3y-4,{x,-2,2},{y,-1,1}]

You can grab the 3D plot and move your mouse to see the surface from different perspectives.

To show multiple surfaces in the same plot, remember to make a list in curly braces:

Plot3D[{ x+3 y,3-x-y},{x,-1,1},{y,-1,1}]

Cylinders

The textbook’s definition of a cylinder is a surface that consists of all lines that are parallel to a given line and pass through a given plane curve. Another way to think about it: a cylinder is a surface that is generated by moving a straight line along a curve while holding the line parallel to a fixed line. This process creates a “stack” of copies of the curve.

In the plot below for the parabolic cylinder y==x^2, observe that Mathematica forms the surface by combining parallel vertical lines perpendicular to a stack of parabolas:

ContourPlot3D[y==(x^2),{x,-3,3},{y,0,9},{z,0,5},AxesLabel->{x,y,z},BoxRatios->Automatic]

An example of a circular cylinder is given by x^2+y^2==1 (other examples are x^2+z^2==4 and y^2+z^2==9). These surfaces are composed of parallel lines aligned to a stack of circles.

ContourPlot3D[(x^2)+(y^2)==1,{x,-1,1},{y,-1,1},{z,0,2},AxesLabel->{x,y,z},BoxRatios->Automatic]

Any curve can be used to generate a cylinder. Note the general pattern in how we’re creating these cylinders is to define an equation in a coordinate plane (xy-plane, xz-plane, or yz-plane), so that one variable is missing from these equations.

ContourPlot3D[z==Sin[x],{x,0,2π},{y,-2,2},{z,-1,1},AxesLabel->{x,y,z},BoxRatios->Automatic]

Example 10.  Plot a cylinder different from any of those given above. You may use any curve you like that is distinct from the previous examples.

Quadric surfaces

Quadric surfaces are the counterparts in 3 dimensions of conic sections.

Recall that conic sections result from the intersection of a cone and a plane and include ellipses ((x^2)/(a^2))+ ((y^2)/(b^2))=1, parabolas y=((x^2)/a)+c, and hyperbolas ((x^2)/(a^2))- ((y^2)/(b^2))=1.  (More generally we can replace x and y in these equations with any pairs among x, y, and z.)

The most general equation describing quadric surfaces is

		A (x^2) + B (y^2) + C (z^2) + D xy + E yz + F xz + G x + H y + J z + K = 0

where  A, B, C, D, E, F, G, H, J, and K are constants.  By translation and rotation it can be brought into one of these standard forms:

((x^2)/(a^2)) + ((y^2)/(b^2))+((z^2)/(c^2)) = 1  (ellipsoid)
((x^2)/(a^2)) + ((y^2)/(b^2))-((z^2)/(c^2)) = 1 (hyperboloid of one sheet) or ((x^2)/(a^2)) - ((y^2)/(b^2))-((z^2)/(c^2)) = 1 (hyperboloid of two sheets)
 ((x^2)/(a^2))+  ((y^2)/(b^2))=((z^2)/(c^2))     (cone) 
 ((x^2)/(a^2)) + ((y^2)/(b^2))=(z/c)  (elliptic paraboloid)
 ((x^2)/(a^2)) - ((y^2)/(b^2))=(z/c)  (hyperbolic paraboloid)

We can swap around the roles of x, y, and z to get other forms of the same surfaces.

Example 11.  Graph the ellipsoid  ((x^2)/16)+ ((y^2)/9)+ ((z^2)/4) = 1 with  -4 < x < 4,  -3 < y < 3,  and -2 < z < 2. Include BoxRatios->Automatic in your Plot3D command as shown in the examples above, so the axes are scaled equally (otherwise it will look like a sphere instead of an ellipsoid).

Example 12.  Observe that the contour curves for the ellipsoid above all look like ellipses. We can use ContourPlot to verify that plugging in a constant value for z (or for x or y) yields an ellipse. For instance, you can plug in z=0 or z=1 (but what happens if you plug in z=2 or z=3?).

ContourPlot[(x^2)/16+(y^2)/9+1^2/4==1,{x,-4,4},{y,-3,3},AxesLabel->{x,y},AspectRatio->Automatic,Axes->True]

Example 13.  Graph the hyperboloid of one sheet given by  ((x^2)/16)+(( (y^2))/(9 ))- ((z^2)/4) = 1. Choose axis ranges that yield a good view of the surface.

Example 14.  Contour curves for the hyperboloid look like hyperbolas and ellipses. Use ContourPlot to verify that plugging in a constant value for z yields an ellipse, while plugging in a constant value for x or y yields a hyperbola.

Example 15.  Graph the hyperboloid of two sheets given by  ((x^2)/16)-(( (y^2))/9)- ((z^2)/4) = 1. Choose axis ranges that yield a good view of the surface. Again contour curves look like hyperbolas and ellipses for this type of surface.

Example 16.  Graph the cone given by  ((x^2)/16)+ (((y^2) )/9)= ((z^2)/4).

Example 17.  What curves are the contours for this cone? Use ContourPlot to create graphs of the different types of contour curves occurring for the cone.

Example 18.  Graph the elliptic paraboloid given by  (x^2)+(( (y^2))/4) = z. Choose axis ranges that yield a good view of the surface. What are the contour curves for this type of surface?

Example 19.  Graph the hyperbolic paraboloid given by  (x^2)-(y^2) = z. Choose axis ranges that yield a good view of the surface. What are the contour curves for this type of surface?

Example 20.  2.  Use ContourPlot3D to graph the following surfaces, make sure you label the axes and use BoxRatios->Automatic. Adjust the ranges for x, y, and z so that you can recognize the surfaces.  Classify each surface as an ellipsoid, hyperboloid of one sheet, hyperboloid of two sheets, cone, elliptic paraboloid, or hyperbolic paraboloid:

a)  4 (x^2) + 9 (y^2)= (z^2)

b)  144 z = 16(x^2) -  9(y^2)

c)  9(x^2) -  4(y^2) + 4(z^2) = 36

d)  4z =4 (x^2) +  (y^2)

e)   9(x^2) + 2 (y^2) + 4 (z^2) = 36

f)   (x^2) - 9 (y^2) - 9 (z^2) =9