Math 21a: Mathematica Lab

A Far Too Brief Introduction to Mathematica

This introduction will get you started if you've never used Mathematica before, but there are other better introductions available.  See those linked from the  Labs page and also explore the introductions and examples offered by the Mathematica program itself when it starts.

Mathematica Basics

Mathematica organizes itself into cells.  This is a text cell, but we can do some mathematics in another cell and have Mathematica evaluate it.  Here's some simple computations any graphing calculator can do.  To have Mathematica evaluate it, simply click on the cell (or the cell's bracket on the right-hand side) and hit Shift-Enter (that's Enter while holding down the shift key).

Exp[ Sin[Pi/2]]-ArcTan[1]+5^7

Notice that Mathematica hasn't given you a numerical approximation but has instead given you an exact answer.  To get, say, 20 digits of the numerical value of the last cell's output, we can do this:

N[%,20]

We can assign values to variables and use these variables to compute.  Here are some cells to evaluate:

a=23

NewVariable=17

a^2-NewVariable^2 + a NewVariable

A couple things you should have noticed by now:
 1. Mathematica can use single letters or long words as variable names.
 2. We've multiplied by simply putting our two variable names next to each other (just as we would write xy to mean x times y).  But we have to put a space between them, otherwise Mathematica will think we're talking about a new variable named aNewVariable.  Just be careful.
 3. The functions we've used above have capital letters and square brackets rather than parentheses.  It's just the way Mathematica does things.
 4. Here's a quick tip if you want to start experimenting.  Click between cells (you're between cells when the cursor is a horizontal I-beam rather than a vertical one) to create a new input cell and start typing!

Functions

Mathematica can define functions as well.  Here we have a function named f  with variable x.  Note the underscore in the definition; this lets Mathematica know that x is the variable.

f[x_]=x-Sqrt[x]+x^3

Now we can use this function in various ways:

f[5]

Plot[f[x],{x,0,16}]

Mathematica can define functions of more than one variable, too.  Here's a function of three variables:

g[x_,y_,z_]=x^2+z y^2-2x y z+3

We can work with this in sophisticated ways:

g[0,5,t]

g[Cos[t],Sin[t],t^2]

Calculus

Here's Mathematica differentiating the functions f[x] and g[x,y,z] we just defined.   Note the different  ways to differentiate!

f'[x]

D[f[x],x]

D[g[x,y,z],y]

D[g[t,t^2,t^3],t]

Integration is just as straightforward.  First an indefinite example (note the lack of an arbitrary constant, or +C).

Integrate[f[x],x]

Integrate[g[x,y,z],z]

Integrate[f[x],{x,0,1}]

Integrate[g[x,y,z],{x,0,10}]

Part One: Parameterized Curves and Surfaces

A parametric curve in Mathematica can be plotted simply using either ParametricPlot or ParametricPlot3D (for curves in the plane and in space, respectively).  Here are a couple of examples we've seen through the semester, the helix in the plane and the twisted cubic in space:

ParametricPlot[{t*Cos[t],t*Sin[t]},{t,0,6*Pi}]

ParametricPlot3D[{t,t^2,t^3},{t,-1,1}]

Of course, we can get much fancier.  Here's a Slinky, similar to one that appeared on the Fall, 2008 final exam:

ParametricPlot3D[{Sin[60 t], (2+Cos[60 t]) Cos[t], (2+Cos[60 t]) Sin[t]},{t,0,Pi},PlotStyle->Thickness[0.01],Axes->False,ViewPoint->{2,0,1/2},ColorFunction->Function[{x,y,z,t},Hue[5 (t/Pi)/6]],ColorFunctionScaling->False]

Parameterized surfaces are done in much the same way, simply with two parameters instead of one.  Here are some of our old friends the quadric surfaces.  First a simple ellipsoid:

ParametricPlot3D[{3Sin[u]Cos[v],5Sin[u]Sin[v],9Cos[u]},{u,0,Pi},{v,0,2Pi}]

Of course, things can get much fancier, as we see with this hyperboloid of two sheets:

ParametricPlot3D[{5Sqrt[4y^2 - 1 ] Cos[t],y,Sqrt[4y^2 - 1 ] Sin[t]},{y,-5/2,5/2},{t,0,2Pi},Boxed->False,Axes->False,ViewPoint->{3,1/4,0},Mesh->{{-5/2,-2,-3/2,-1,-1/2,0,1/2,1,3/2,2,5/2},{0,Pi/4,Pi/2,3Pi/4,Pi,5Pi/4,3Pi/2,7Pi/4,2Pi}},ColorFunction->Function[{x,y,z,t},Hue[y/3]],ColorFunctionScaling->False]

Or this lovely shape (see Problem 35 in Section 12.8):

ParametricPlot3D[(5+Cos[5 theta] Cos[ 6 phi]){Sin[phi]Cos[theta],Sin[phi]Sin[theta],Cos[phi]},
{theta,0,2Pi}, {phi,0,Pi},Boxed->False,Axes->False,
ColorFunction->Function[{x,y,z,phi,theta},Hue[5 (phi)/ (6Pi)]],ColorFunctionScaling->False]

Problem One (15 Points)

Here is a surface given by the equation z=2x^2-3y^2 over the unit square:

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

Plot as a parameterized curve the curve found by the intersection of this surface with the cylinder x^2+y^2=1.

Problem Two (15 Points)

Here's a quadric surface graphed as a level surface:

Show[
ContourPlot3D[2x^2-y^2+3z^2==1,{x,-1,1},{y,-2,2},{z,-1,1},ViewPoint->{2.7,1.7,1}],
Graphics3D[{Text["x",{0,-2.4,1.2}],Text["y",{0.9,0,-1}],Text["z",{0.9,-1.9,0}]}]
]

Produce another (nicer?) picture of this surface, this time using ParametricPlot3D.  This means that you'll have to parameterize this surface.  (Here's a tip: use the same viewpoint as we do above to confirm that our surfaces look similar.)

Part Two: Graphs and Level Curves

While parameterized curves and surfaces are most useful to us (particularly for computing line and flux integrals), it can be useful to have other ways to visualize surfaces.  One way is the most basic approach: graphing functions.   Another way is to look at level curves.  Naturally Mathematica does both very well.

Here are several examples of graphs of functions of two variables:

Plot3D[Cos[x^2+y],{x,-3,3},{y,-3,3}]

Plot3D[Cos[x^2+y^2]*Exp[-(x^2+y^2)/6],{x,-3,3},{y,-3,3},Boxed->False,Axes->False,ColorFunction->Function[{x,y,z},Hue[z]]]

Here are two examples of contour plots (level curves and surfaces), one of curves in the plane and the other of surfaces in space.   Notice that in the first example we've specified the values of z=f(x,y) to be used as contours.

ContourPlot[x^4+y^4+4*x*y+3,{x,-2,2},{y,-2,2},ContourShading->None,Contours->{1,2,3,4,5,6,7,8}]

In this contour plot, we're looking at three level surfaces of the function g(x,y,z)=x^2-y^2-z^2 at the levels -1 (a one-sheeted hyperboloid), 0 (a cone), and +1 (a two-sheeted hyperboloid).   We've made the surfaces see-through (this is the opacity expression) so we can see all three surfaces.

ContourPlot3D[x^2-y^2-z^2,{x,-2,2},{y,-2,2},{z,-2,2},Mesh->None,ContourStyle->Opacity[0.6],Contours->{-1,0,1}]

Problem Three (10 Points)

Produce a contour plot of level curves of a function z=f(x,y) for your choice of function (other than the example we have given).

Problem Four (10 Points)

Produce a contour plot of level surfaces of a function g(x,y,z) for your choice of function (other than the example we have given).

Part Three: Integration and the Integral Theorems

Mathematica can integrate extremely well, but it needs to be given limits of integration rather than an abstractly defined region.  For example, either of these iterated triple integrals will give me the volume of a sphere of radius a:

Integrate[Integrate[Integrate[1,{z,-Sqrt[a^2-x^2-y^2],Sqrt[a^2-x^2-y^2]}], {y, -Sqrt[a^2-x^2], Sqrt[a^2-x^2]}], {x, -a, a}]

Wait a second -- what's happened?  Shouldn't this involve a?  If you've evaluated all the cells in the introduction, you may remember that we set a=23 early on.  Well, Mathematica still believes that.  If you're getting an answer that doesn't involve the letter a, this is probably the trouble.  We need to Clear the variable, then re-evaluate the above triple integral:

Clear[a]

Integrate[Integrate[Integrate[r^2 Sin[u], {v,0,2 Pi}],{u,0,Pi}],{r,0,a}]

One of these was much easier to type (and Mathematica seemed to calculate quicker as well).

Stokes' Theorem:

We can verify our integral theorems by writing line integrals, flux integrals, and triple integrals all as iterated integrals.  For example, here is a line integral and a flux integral that Stokes' theorem says should agree (the surface S is the top half of a sphere of radius a, oriented with the upward-pointing normal; so the curve C is the circle of radius a in the xy-plane centered at the origin):

F[{x_,y_,z_}]={x^2 y,y^2 z,x z^2};
r[t_]={a*Cos[t],a*Sin[t],0};
Integrate[F[r[t]].r'[t],{t,0,2Pi}]

(Notice the dot product, computed using a simple dot between two vectors.)  To use Curl and Div, we first define them as functions:

Curl[{P_,Q_,R_}] := {D[R,y]-D[Q,z], D[P,z]-D[R,x], D[Q,x]-D[P,y]}

Div[{P_,Q_,R_}] := D[P,x] + D[Q,y] + D[R,z]

G[{x_,y_,z_}]=Curl[{x^2  y, y^2  z, x  z^2}]

r[phi_,theta_]={a*Sin[phi]*Cos[theta],a*Sin[phi]*Sin[theta],a*Cos[phi]};
normal[phi_,theta_]=Cross[D[r[phi,theta],phi], D[r[phi,theta],theta]];
Integrate[Integrate[G[r[phi,theta]].normal[phi,theta],{phi,0,Pi/2}],{theta,0,2Pi}]

Notice two things.  First, we got the same result for the flux integral that we obtained for the line integral, just as Stokes' theorem says we should.  Second, we didn't explicitly check the orientation of the normal, and we should have.  Here the normal is at the point (x,y,z)=(1/Sqrt[2],0,1/Sqrt[2]) (where (phi,theta)=(Pi/4,0)).  Notice that it is upward-pointing:

r[phi_,theta_]={a*Sin[phi]*Cos[theta],a*Sin[phi]*Sin[theta],a*Cos[phi]};
normal[phi_,theta_]=Cross[D[r[phi,theta],phi], D[r[phi,theta],theta]];
normal[Pi/4,0]

Problem Five (20 Points)

Verify Stokes' theorem on the surface of Problem Two (2x^2-y^2+3z^2=1 with y between -2 and +2), oriented so that the normal points upward at the point (0,0,1/Sqrt[3]).  Use the vector field F=< z, x, y >.  This asks you to compute a flux integral across the surface and a line integral around both pieces of the boundary curve.

The Divergence Theorem:

Now we give an example of the divergence theorem in action.  We'll use a region / surface that isn't too difficult to parameterize, namely the ellipsoid 2(x^2)+3(y^2)+(z^2)=6.  The vector field will also be unpleasant, so in the end it will be an integral we don't want to compute by hand:

r[phi_,theta_]={Sqrt[3]*Sin[phi]*Cos[theta],Sqrt[2] Sin[phi] Sin[theta], Sqrt[6] Cos[phi]};
OutwardNormal[phi_,theta_]=Cross[D[r[phi,theta],phi],D[r[phi,theta],theta]]

G[{x_,y_,z_}]={-y ,2 y  , z +y-x}

Integrate[Integrate[G[r[phi,theta]].OutwardNormal[phi,theta],{phi,0,Pi}],{theta,0,2Pi}]

We don't know how to adopt spherical coordinates to this triple integral, sp we instead integrate Div[G] = 3 over this solid in rectangular coordinates:

Integrate[Integrate[Integrate[3,{z,-Sqrt[6-2x^2-3y^2],Sqrt[6-2x^2-3y^2]}],{y,-Sqrt[(6-2x^2)/3],Sqrt[(6-2x^2)/3]}],{x,-Sqrt[3],Sqrt[3]}]

Problem Six (20 Points)

Verify the divergence theorem on the ellipsoid 2x^2 + 3y^2 + 4z^2 = 1 with the vector field F = < x z, y z, x^2 - y^2 >.  Thus you must compute two integrals: one flux integral (oriented with the outward-pointing normal) and one triple integral.

Part Four: Further Explorations

Mathematica can do many other things and is widely used across disciplines.  Here are a couple of quick examples:

Here's a transition between an elliptic paraboloid an a hyperbolic paraboloid that you can manipulate:

Manipulate[Plot3D[x^2-c*y^2,{x,-2,2},{y,-2,2},Boxed->False,Axes->False],{c,-1,1}]

Here's a molecular model of a common substance.  (This may take a moment to load the first time you try it.)

ChemicalData["Caffeine","MoleculePlot"]

You can manipulate other things as well.  Here's a point on a helix:

Manipulate[
Show[
ParametricPlot3D[{3Cos[Pi t],3Sin[Pi t],t},{t,0,6},Boxed->False,Axes->False],
Graphics3D[{PointSize[0.05],RGBColor[0.25,0.25,0.5],Point[{3Cos[Pi s],3Sin[Pi s],s}]}]
],{s,0,6}]

Problem Seven (10 Points)

Show us something cool that Mathematica can do. It can be anything, from a contour plot to a surface graph to a manipulated digital photo.  Explore and have fun!  We'll put our favorites in an online gallery.