6760, Math 21a, Fall 2009
Mathematica project Math 21a, Multivariable Calculus
Lab
Course head: Oliver Knill
Office: SciCtr 434


Exhibit of some Mathematica submissions.


To install Mathematica 7 on your own computer, get the program here. start up the application and follow the instructions. You need the Harvard Licence number L2482-2405. The from your computer generated Machine ID will need to be entered into the Mathematica Registration page. In return, you will be sent a password by email. This is what you see during installation in Contact Oliver if you plan to use Mathematica on a Linux system.

The Mathematica assignment is available here. (You have to save this file on your desktop and then open it with Mathematica). Here are examples of demonstration ideas for the third "creative" problem.

Mathematica is launched like any other application on your Macintoshs or PC's. On Linux, just type "mathematica" to start the notebook version, or "math" to start the terminal version. Example code: Here is Mathematica code to classify critical points:
 
 f[x_,y_]:=4 x y - x^3 y - x y^3;
 a[x_,y_]:=D[f[u,v],u] /. {u->x,v->y}; b[x_,y_]:=D[f[u,v],v] /. {u->x,v->y};
 A=Solve[{a[x,y]==0,b[x,y]==0},{x,y}];
 CriticalPoints=Table[{A[[i,1,2]],A[[i,2,2]]},{i,Length[A]}];
 H[{x_,y_}]:={{D[f[u,v],{u,2}],D[D[f[u,v],v],u]},{D[D[f[u,v],u],v],D[f[u,v],{v,2}]}} /. {u->x,v->y};
 F[A_]:=A[[1,1]]; Discriminant=Map[Det,Map[H,CriticalPoints]]
 FirstEntry=Map[F,Map[H,CriticalPoints]]
 Decide[B_]:=If[Det[B]<0,"saddle",If[B[[1,1]]<0,"max","min"]];
 Analysis=Map[Decide, Map[H,CriticalPoints]];
 Table[{CriticalPoints[[i]],Analysis[[i]]},{i,Length[CriticalPoints]}]
Here is the same, but more elegantly (credit: Matt Leingang, 2006):
ClassifyCriticalPoints[f_,{x_,y_}] := Module[{X,P,H,g,d,S},
X={x,y}; P=Solve[Thread[D[f,#] & /@ X==0],X];H=Outer[D[f,#1,#2]&,X,X];g=H[[1,1]];d=Det[H];
S[d_,g_]:=If[d<0,"saddle",If[g>0,"minimum","maximum"]]; 
TableForm[{x,y,d,g,S[d,g],f} /. Sort[P],TableHeadings->{None,{x,y,"D","f_xx","Type","f"}}]]
ClassifyCriticalPoints[4 x y - x^3 y - x y^3,{x,y}]
Here is an example on how to solve a Lagrange problem for functions of 2 variables:
F[x_,y_]:=2x^2+4 x y
G[x_,y_]:=x^2 y
Solve[{D[F[x,y],x]== L*D[G[x,y],x],D[F[x,y],y]==L*D[G[x,y],y],G[x,y]==1},{x,y,L}]
and here an example with functions of 3 variables:
F[x_,y_,z_]:=x^2+y^2+z^2;
G[x_,y_,z_]:=x-y^2+z;
Solve[{D[F[x,y,z],x]== L*D[G[x,y,z],x],
       D[F[x,y,z],y]== L*D[G[x,y,z],y],
       D[F[x,y,z],z]== L*D[G[x,y,z],z],
       G[x,y,z]==1},{x,y,z,L}]
or for functions of 3 variables with two constraints:
F[x_, y_, z_] := x^2 + y^2 + z^2;
G[x_, y_, z_] := x - y^2 + z;
H[x_, y_, z_] := x + y - 2;
Solve[{
  D[F[x,y,z],x] == L*D[G[x,y,z],x]+M*D[H[x,y,z],x],
  D[F[x,y,z],y] == L*D[G[x,y,z],y]+M*D[H[x,y,z],y],
  D[F[x,y,z],z] == L*D[G[x,y,z],z]+M*D[H[x,y,z],z],
  G[x, y, z] == 1, H[x, y, z] == 1}, {x,y,z,L,M}]
Here is an example how to check that a function solves a partial differential equation:
f[t_,x_]:=(x/t)*Sqrt[1/t]*Exp[-x^2/(4 t)]/(1+ Sqrt[1/t] Exp[-x^2/(4 t)]);
D[f[t,x],t]+f[t,x]*D[f[t,x],x]-D[f[t,x],{x,2}]
Simplify[%] Chop[%]
And here are some commonly used examples. It should be pretty self explanatory:
Plot[x Sin[x],{x,-10,10}]
Plot3D[Sin[x y],{x,-2,2},{y,-2,2}]
ParametricPlot[{Cos[3 t],Sin[5 t]} ,{t,0,2Pi}]
ParametricPlot3D[{Cos[t],Sin[t],t} ,{t,0,4Pi}]
ParametricPlot3D[{Cos[t] Sin[s],Sin[t] Sin[s],Cos[s]},{t,0,2Pi},{s,0,Pi}]
ContourPlot[ Sin[x y],{x,-2,2},{y,-2,2} ]
Integrate[ x Sin[x],x]
Integrate[ x y, {x,0,1},{y,0,x}]
Integrate[ x y z, {x,0,1},{y,x,x^2},{z,x,x y}]
NIntegrate[Exp[-x^2],{x,0,10}]
D[Cos[x]^5,x ]
D[Cos[x]^5,{x,2}]
Series[Exp[x],{x,0,3} ]
DSolve[ x''[t]==-x[t],x,t ]
ContourPlot3D[x^2+2y^2-z^2==1,{x,-2,2},{y,-2,2},{z,-2,2}]
VectorPlot[{-y,x},{x,-2,2},{y,-2,2}]
Questions and comments to knill@math.harvard.edu
Math21b | Math 21a | Fall 2009 | Department of Mathematics | Faculty of Art and Sciences | Harvard University