3D Plotting in Mathematica

Christopher Lum
lum@uw.edu

This document accompanies the YouTube video at <TBD>

Creating a Surface Using ‘Plot3D’

In order to generate a 3D plot in Mathematica, we first define a function with two inputs and a one output.

f[x1_,x2_]=Cos[x1] x2;

We can consider the domain of
	
	(Subscript[x, 1])∈[-π,π]
	(Subscript[x, 2])∈[-10,16]
	
We can now use Mathematica’s ‘Plot3D’ function to visualize this.

Plot3D[f[x1,x2],{x1,-π,π},{x2,-10,16}]

Just like other Mathematica plots, we can add options to add features to the plot

plotSurface=Plot3D[f[x1,x2],{x1,-π,π},{x2,-10,16},
(*Plot options*)
AxesLabel->{"(Subscript[x, 1])","(Subscript[x, 2])","f((Subscript[x, 1]),(Subscript[x, 2]))"},
PlotLabel->"f=cos((Subscript[x, 1])) (Subscript[x, 2])",
ColorFunction->"BlueGreenYellow",
PlotStyle->Opacity[0.25],
FaceGrids->All]

You can also change the view point of the graphic

Plot3D[f[x1,x2],{x1,-π,π},{x2,-10,16},
AxesLabel->{"x","y","z"},
ViewPoint->Front]

Notice that the viewpoint is not at infinity.  You can specify the viewpoint to be at infinity if you like.

Plot3D[f[x1,x2],{x1,-π,π},{x2,-10,16},
AxesLabel->{"x","y","z"},
ViewPoint->{0,-∞,0}]

Creating a Line Using ‘ParametricPlot3D’

ParametricPlot3D is a good way to make a line in 3D.  To use this, we need to define functions that specify how the the x, y, and z values vary with respect to a parameter.  For example, we could consider points along the line specified by (Subscript[x, 2])=4(Subscript[x, 1])-2

	(Subscript[f, x])=t
	(Subscript[f, y])=4 t-2

fx=t;
fy=6 t+2;

For the z values, let us use values that ensure the line lies on the surface we defined in the previous section.

	(Subscript[f, z])=f((Subscript[x, 1]),(Subscript[x, 2]))
	
	   =f(t,6t+2)

fz=f[fx,fy]

We can now plot this using ‘ParametricPlot3D’.

plotLine=ParametricPlot3D[{fx,fy,fz},{t,-2,2},
(*Plot options*)
PlotStyle->{Red,Thickness[0.02]}]

We can show the surface and line on top of each other.

Show[plotSurface,plotLine]

Creating a Point Using ‘Graphics3D’

We may want to add single points to this plot.  We can plot single points in 3D in Mathematica using ‘Graphics3D’

(*Define the point of interest*)
x1o=-2;
x2o=3;

(*P is a 1x3 matrix*)
P=(({{x1o, x2o, f[x1o,x2o]}}));

(*Create a Point object*)
pt=Point[P];

(*Draw the point using Graphics3D*)
Graphics3D[pt]

Note that this is very small and will be difficult to see

(*We can change how the Graphics3D is shown*)
plotPoint=Graphics3D[
(*Note the order, the parameters such as point size and color are listed BEFORE the pt object*)
{AbsolutePointSize[15],Green,pt}
]

We can simultaneously display multiple plots using ‘Show’

Show[
(*Plot the surface*)
plotSurface,

(*Plot the line*)
plotLine,

(*Plot the point*)
plotPoint,

(*Plot options*)
PlotLabel->"A surface, line, and point all drawn together",
AxesLabel->{"(Subscript[x, 1])","(Subscript[x, 2])","f((Subscript[x, 1]),(Subscript[x, 2]))"}
]