Math 2374 Notebook Template - 2/18/2008 Version

You should not do anything with this section.  These cells make sure that certain commands are loaded and defined.  If you don't evaluate these initialization cells, certain parts of your labs will not work.

We're going to try to load every package and define every new command which will be needed this semester.  Experience has shown that asking students to load packages causes more problems than it's worth.  This new approach has the disadvantage that students will never learn how to load a package.  But anybody who is interested enough in Mathematica to want to load new packages can probably figure out how.  (Perhaps with the help of a TA, if needed.)

Significant updates (mostly deletions) made in Spring 2008 for Mathematica 6.0.  We finally don't have to use add-on Java packages for interactivity -- but it's slow.  Win some, lose some...

Jonathan Rogness
rogness@math.umn.edu
Spring 2004 through 2008

(* Minor issues; set axes labels by default so students feel oriented. *)
SetOptions[Plot3D,Axes->True, AxesLabel->{"x","y","z"}];
SetOptions[ContourPlot3D,Axes->True, AxesLabel->{"x","y","z"}];
SetOptions[ParametricPlot3D,Axes->True, AxesLabel->{"x","y","z"}];
SetOptions[Graphics3D,Axes->True, AxesLabel->{"x","y","z"}];


Off[General::spell];
Off[General::spell1];

(* Now some other packages which get used now and then. *)
Needs["VectorFieldPlots`"]

(* Vector Analysis Stuff *)
Needs["VectorAnalysis`"]
SetCoordinates[Cartesian[x,y,z]];
(* Here's a really cheap way of defining a 2D grad function *)
Grad2D[f_]:=D[f,{{x,y},1}]
(* Argh -- Mathematica now comes with a Norm function, but it
  assumes we're dealing with complex numbers, giving
  monstrosities like Sqrt[Abs[x]^2+Abs[y]^2].  So we not only
  have to do it the old fashioned way, but first we have
  to use the Attributes command to allow Norm to be 
  redefined! *)
Attributes[Norm]={};
Norm[v_]=Sqrt[v.v];

Options[Arrow3D]={HeadLength->0.3,HeadNormal->{0,0,1},HeadWidth->0.5};
Arrow3D[a_,b_,opts___]:=Module[{abLength=N[Sqrt[(b-a).(b-a)]],abUnit,headPerp,headPerpLength,headLength,headNormal,headWidth},{headLength,headNormal,headWidth}={HeadLength,HeadNormal,HeadWidth}/.{opts}/.Options[Arrow3D];
abUnit=(b-a)/abLength;
headPerp=Cross[abUnit,N[headNormal]];
headPerp=headPerp/Sqrt[N[headPerp.headPerp]];
{Line[{a,b-abUnit*headLength}],Polygon[{b,b-abUnit*headLength+headPerp*headWidth/2*headLength,b-abUnit*headLength,b-abUnit*headLength-headPerp*headWidth/2*headLength}]}];

(* Needed in Lab 6A - Graphics`Shapes` now gone!??! *)

ShowTorus[a_,b_]:=ParametricPlot3D[{(b Cos[t]+a) Cos[s],(b Cos[t]+a) Sin[s],b Sin[t]},{s,0,2Pi},{t,0,2Pi}]

(* Next up - my Arctools.m package.  *)

(* This is a conglomeration of two packages. 

"segments" and "estimate" are taken from a package
created at Lafayette College named "Arclength.m." 
The original message from that package follows:

	This package may be distributed freely, but is 
	copyright by Lafayette College, 1994.

(Note that the command "legs" has been removed from 
the package, and the remaining function names have been
capitalized to correspond to standard usage.)

PathAnimate and PathTangentAnimate were written for use 
at the University of Minnesota in 2002.  They may be freely 
used, but please keep this message intact.

This hodge-podge was created because these three commands
are used in one of our labs for MATH 2374, Multivariable
Calculus.

Questions to: rogness@math.umn.edu
*)


Segments::usage="Segments[{f[t]?,g[t]},{t,tmin,tmax},n] plots the
	parametric curve and n line segments which can be used to 
	approximate the length of the curve.";

Estimate::usage="Estimate[{f[t],g[t]},{t,tmin,tmax},n] estimates the 
	length of the curve given parametrically by x = f(t), y = g(t), 
	over the interval tmin < t < tmax, using n subdivisions of the 
	arc.";

PathAnimate::usage="PathAnimate[{f[t],g[t]},{t,tmin,tmax}] animates 
	a particle moving along the curve given parametrically by x = f(t),
	y = g(t), over the interval tmin < t < tmax.";

PathTangentAnimate::usage="PathTangentAnimate[{f[t],g[t]},{t,tmin,tmax},n] 
	animates a particle and its velocity vector moving along the 
	curve given parametrically by x = f(t), y = g(t), over the 
	interval tmin < t < tmax, using n equally spaced values of t 
	throughout the time interval.  The PlotRange is chosen
	to be as small as possible while still showing the vectors.";

PathAnimate3D::usage="A 3D version of PathAnimate";

PathTangentAnimate3D::usage="A 3D version of PathTangentAnimate";

DoAnimation::usage="DoAnimation is called by PathAnimate and
	PathTangentAnimate to actually create their pictures.";
	
Segments[funclist_,paramlist_,n_,options___] :=
  Block[{tmin, tmax, plotcurve, plotlines, points,
    stepsize, param},
    param = paramlist[[1]];
    tmin = paramlist[[2]]; tmax = paramlist[[3]];
    stepsize = (tmax - tmin)/n;
    plotcurve = ParametricPlot[Evaluate[funclist/.param->t],
      {t,tmin,tmax},PlotStyle->RGBColor[1,0,0],
      DisplayFunction->Identity];
    plotlines = Graphics[{RGBColor[0,0,1], Line[Table[
      funclist/.param->t,
      {t, tmin, tmax, stepsize} ]]}];
    points = Table[Graphics[{RGBColor[0,0,1], Point[
      funclist/.param->t ]}],
      {t, tmin, tmax, stepsize}];
    Show[plotcurve,plotlines, points,options,
      DisplayFunction->$DisplayFunction]
    ]

Estimate[funclist_,paramlist_,n_] :=
  Block[{tmin, tmax, h, param, k},
    param = paramlist[[1]];
    tmin = paramlist[[2]];
    tmax = paramlist[[3]];
    h = (tmax - tmin)/n;
    Sum[N[Sqrt[
      ((funclist[[1]] /. param -> k h) -
       (funclist[[1]] /. param -> (k-1)h))^2
    + ((funclist[[2]] /. param -> k h) -
       (funclist[[2]] /. param -> (k-1)h))^2]],
      {k,1,n}]
    ]

PathAnimate[fun_,{u_,u0_,u1_},options___]:=DynamicModule[{tmin,tmax,param,graph,t,f},

Animate[Show[ParametricPlot[fun,{u,u0,u1},options],Graphics[{PointSize[0.03],Point[fun]}]],{u,u0,u1},DisplayAllSteps->True]
];

PathAnimate3D[fun_,{u_,u0_,u1_},options___]:=DynamicModule[{tmin,tmax,param,graph,t,f},

Animate[Show[ParametricPlot3D[fun,{u,u0,u1},options],Graphics3D[{PointSize[0.03],Point[fun]}]],{u,u0,u1},DisplayAllSteps->True]
];

PathTangentAnimate[fun_,{u_,u0_,u1_},options___]:=DynamicModule[{tmin,tmax,param,graph,t,f,Df},

(* Kludgy way to do this, but it works *)
f[t_]=Evaluate[fun/.u->t];
Df[t_]=D[f[t],t];

Animate[Show[ParametricPlot[fun,{u,u0,u1},options],Graphics[{PointSize[0.03],Point[f[t]],Red,Thickness[Medium],Line[{f[t],f[t]+Df[t]}]}]],{t,u0,u1},DisplayAllSteps->True] 
];

PathTangentAnimate3D[fun_,{u_,u0_,u1_},options___]:=DynamicModule[{tmin,tmax,param,graph,t,f,Df},

(* Kludgy way to do this, but it works *)
f[t_]=Evaluate[fun/.u->t];
Df[t_]=D[f[t],t];

Animate[Show[ParametricPlot3D[fun,{u,u0,u1},options],Graphics3D[{PointSize[0.03],Point[f[t]],Red,Thickness[Medium],Line[{f[t],f[t]+Df[t]}]}]],{t,u0,u1},DisplayAllSteps->True] 
];

(* For the record, the button which loads the commands was created with the following command; the button was then cut and pasted into its current location:

Cell[BoxData[ButtonBox[StyleBox["Click Here to Load Math 2374 Commands",FontWeight->Bold,FontVariations->{"Underline"->False},FontColor->RGBColor[0,0,0]],
ButtonFunction:>Module[{nb=ButtonNotebook[]},
NotebookLocate["initcell"];
SelectionEvaluateCreateCell[nb];
SelectionMove[nb,After,ButtonCell];
CellPrint[Cell["Done! You can now continue working.","Text",FontWeight->"Bold",CellFrame->True,Background->GrayLevel[0.699992],TextAlignment->Center]];
FrontEndExecute[{FrontEnd`NotebookFind[FrontEnd`InputNotebook[],
"initgroup",All,CellTags],FrontEndToken[FrontEnd`InputNotebook[],"SelectionCloseAllGroups"]}];SelectionMove[nb,After,Notebook];],ButtonEvaluator->Automatic,Active->True,ButtonFrame->"DialogBox",Background->GrayLevel[0.7],ButtonMargins->5]],TextAlignment->Center]//CellPrint

*)

You should click on the button below before you begin reading any labs or doing your own work.  If you use Mathematica to write your solutions to lab exercises, please don't include this message or the template header above; delete them before handing anything in.

Done! You can now continue working.