Graphing Surfaces and Curves in Mathematica

Graphing functions of the form z =f(x,y) or g(x,y,z) = 0

To graph a surface in 3-space we have a choice of Plot3D and ContourPlot3D.  ContourPlot3D is necessary when we graph implicitly defined functions of the form G(x,y,z) = 0.   The next two examples  employ the Plot3D command:

Plot3D[5 (x^2)+5 (y^2)+1, {x,-9,9},{y,-9,9}]

Plot3D[Sin[(x^2)+(y^2)],{x,-3,3},{y,-3,3}]

Notice that in the two examples above, we plotted surfaces of the form z = f(x,y).    However, each of these surfaces may also be obtained by graphing a  particular level set of the form w(x,y,z)=0.  
Study the examples below:

ContourPlot3D[5 (x^2)+5 (y^2)+1-z==0,{x,-9,9},{y,-9,9},{z,0,800}]

Notice that the function g(x,y,z) is set equal to 0 using a double equal sign.

Note also that, when using  ContourPlot3D,  we need to specify a range for z.

ContourPlot3D[Sin[(x^2)+(y^2)]-z==0,{x,-3,3},{y,-3,3},{z,-1,1}]

Notice that the appearance of this surface is somewhat different from the earlier one, but this can be accounted for by the scale (which we will learn to adjust later).

Here is yet another example:

ContourPlot3D[Exp[-(x^2+y^2)]-z==0,{x,-2,2},{y,-2,2}, {z,0,1}]

Plotting several surfaces on the same set of axes.

Plot3D[{Sqrt[7-x^2-y^2],-Sqrt[7-x^2-y^2],7 Exp[-2 x^2-y^2], -7 Exp[-2 x^2-y^2]}, {x,-3,3},{y,-3,3}, BoxRatios->{1,1,2}]

Plot3D[{x^2+y^2,-x^2-y^2},{x,-2,2},{y,-2,2},BoxRatios->{1,2,4}]

Plot3D[{x^2+y^2,-x^2-y^2},{x,-2,2},{y,-2,2},BoxRatios->{1,3,1}]

What is the purpose of BoxRatios?

restricting the domain of the surface

Plot3D[{x^2+y^2,-x^2-y^2},{x,-1,1},{y,-1,1},BoxRatios->Automatic, RegionFunction->Function[{x,y,z},x^2+y^2≤1]]

Intersecting Sufaces and Opacity

Study the following two examples of intersecting surfaces.

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

ContourPlot3D[{(x-1)^2 +(y-1)^2+ z^2 ==2, x^2+y^2+z^2 - 1==0}, {x,-2,3},{y,-2,3},{z,-2,2},BoxRatios->Automatic , ContourStyle->Directive[Opacity[0.5]]]

What is the purpose of ContourStyle->Directive[Opacity[0.5]] in the example above?

ContourPlot3D[{x^2+y^2+z^2-4==0, x^2+y^2==1, x^2+z^2==1}, {x,-4,4},{y,-4,4},{z,-4,4},ContourStyle->Directive[Opacity[0.6]], BoxRatios->Automatic]

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

Note that in the graph above the sphere inside the box is completely hidden.

ContourPlot3D[{x^2+y^2+z^2 - 1==0, x+1==0,x-1==0,y+1==0,y-1==0,z+1==0,z-1==0}, {x,-1,1},{y,-1,1},{z,-1,1}, ContourStyle->Directive[Opacity[0.5]]]

Why is the sphere inside the cube now visible?

ContourPlot3D[{(x^2)+(y^2)+(z^2)}, {x,-1,0},{y,-1,1},{z,-1,1},BoxRatios->Automatic , ContourStyle->Directive[Opacity[0.2]], RegionFunction->Function[{x,y,z},(x^2)+(y^2)+(z^2)≤1]]

ContourPlot3D[{(x^2)+(y^2)+(z^2)-4==0, (x^2)+(y^2)+(z^2)-1==0, (x^2)+(y^2)==1}, {x,-2,2},{y,-2,2},{z,-4,4},ContourStyle->Directive[Opacity[0.4]], BoxRatios->Automatic]

Level Curves

Study the following examples:

ContourPlot[x+3y, {x,-3,3}, {y,-3,3}, ContourLabels->True]

ContourPlot[(x^2)+(y^2), {x,-3,3}, {y,-3,3}]

Notice that as you move the cursor over a level curve (in Mathematica, but perhaps not on this webpage), you are given the c-value of the level curve.

Here is a more unusual example:

ContourPlot[Max[Abs[x],Abs[y]], {x,-3,3}, {y,-3,3}, ContourLabels->True]

Next,to plot a particular level curve, we need to use the == symbol.

ContourPlot[Max[Abs[x],Abs[y]]==1, {x,-3,3}, {y,-3,3}]

To have the contour values printed on the contour diagram, we may use the ContourLabels command:

ContourPlot[(x^2)+(y^2), {x,-3,3}, {y,-3,3}, ContourLabels->True]

To plot the curves without the default shading,use the ContourShading parameter:

ContourPlot[Sin[x]Sin[y],{x,0,3Pi},{y,0,3Pi},ContourShading->None]

To specify an explicit set of contours:

ContourPlot[(x^2)-(y^2),{x,-2,2},{y,-2,2},Contours->{-1,0,0.1,0.7,1.5},ContourShading->None, ContourLabels->True]

ContourPlot[Sin[(x^2)+(y^2)], {x,-5,5}, {y,-5,5},ContourLabels->True]

ContourPlot[(x^2)-(y^2), {x,-5,5}, {y,-5,5}]

ContourPlot[Exp[-(x^2)-(y^2)]  (Sin[(x^2)] +Cos[(y^2)]), {x,-2,2}, {y,-2,2}, Contours->16]

Avoiding Truncation

Consider the following attempt at graphing:

Plot3D[ Exp[-(x^2)-(y^2)], {x, -3,3}, {y,-3,3}]

Notice that, although the surface peaks at the origin with a value of 1, the surface is truncated.  There are two ways to address this problem.  The first is to change the x and y domains.

Plot3D[ Exp[-(x^2)-(y^2)], {x, -2,2}, {y,-2,2}]

The second is to use PlotRange:

Plot3D[ Exp[-(x^2)-(y^2)], {x, -3,3}, {y,-3,3}, PlotRange->{0,1}]

Graphing Parameterized Curves in two or three dimensions

Study the following three examples of parameterized curves:

ParametricPlot3D[{Sin[5 u],Cos[5 u],u},{u,0,5},ColorFunction->"Rainbow", PlotStyle->Thickness[0.02]]

ParametricPlot[{Cos[t]-(((Sin[t])^2)/(Sqrt[2])),Cos[t] Sin[t]},{t,0,2 π},PlotStyle->{Red, Thickness[0.01]}]

ParametricPlot[{Sin[3 t+Pi/4],  Sin[t]},{t,0,2 Pi}, PlotStyle->{Green, Thickness[0.011]}]

Polar Coordinate Graphs

PolarPlot[θ,{θ,0,10}]

PolarPlot[(1/θ),{θ,0,15}, PlotStyle->{Green, Thickness[0.008]}]

PolarPlot[1-Cos[θ], {θ, 0, 2π},PlotStyle->{Blue, Thickness[0.003]}]

PolarPlot[1+2 Sin[θ], {θ, 0,2 π},PlotStyle->{Red, Thickness[0.008]}]

PolarPlot[3+2 Sin[θ], {θ, 0,2 π}]

PolarPlot[6 Sin[θ], {θ, 0,2 π}]

PolarPlot[ Cos[2 θ], {θ, 0, 2π}]

PolarPlot[ Cos[3 θ], {θ, 0,2 π}]

PolarPlot[ Cos[4 θ], {θ, 0,2 π}]

PolarPlot[ {Sqrt[θ],-Sqrt[ θ]}, {θ, 0,2 π},PlotStyle->{Blue, Thickness[0.008]}]

PolarPlot[ {Sqrt[Cos[2  θ]],-Sqrt[Cos[2θ]]}, {θ, -(π/4), (π/4)},PlotStyle->{ Thickness[0.008]}]