Math 2374 Notebook Template - 8/31/04 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.)
    
Jonathan Rogness
rogness@math.umn.edu
Spring 2004
    
PS.  If you're teaching this class and I'm not around, make sure that you use the correct commands below to define the ShowLive command.  Things sometimes need to be changed according to the location of the Java tree.

UPDATE -- now that the math department is using Fedora, none of this matters.  (I think.)  This file should be updated, with all of this complicated nonsense removed....

Here is the section which defines the ShowLive command.  This is based on LiveGraphics3D by Martin Kraus.  The math department's Java tree is organized differently than most, so you have to comment out the appropriate command below.

I don't do anything to add live.jar to the ClassPath here.  This works fine as long as the file live.jar is in either of the following directories:

(system wide base Mathematica 5.0 directory)/AddOns/Applications/LiveGraphics3D/Java/
~/.Mathematica/Applications/LiveGraphics3D/Java/

Currently Brad has set this up via the system-wide method on the math department computers; the base directory is /usr/local/math5.0/.  We haven't had to do this in the IT Labs yet.

\(\(\(Needs["JLink`"]\)\(
\)\(
\)\( (*\ Simple\ version\ of\ the\ command\ now\ works\ for\ Math\ Dept\ Systems\ *) \)\(
\)\(
\)
    \) (*\ On\ IT\ Systems, \ 
      use\ the\ first\ version . \ \ On\ Math\ Department\ Systems, \ 
      use\ the\ second\ version . \ \ In\ either\ case, \ 
      you\ should\ comment\ out\ the\ one\ you\ _don' 
          t_\ want\ students\ to\ use ... \ *) \)
InstallJava[];

    
\(\(\(ShowLive[graphics__, 
        opts___Rule] := 
Module[{g = 
            Show[Switch[{graphics}, __ContourGraphics, 
                Graphics3D[SurfaceGraphics[{graphics}]], __DensityGraphics, 
                Graphics3D[SurfaceGraphics[{graphics}]], __SurfaceGraphics, 
                Graphics3D[{graphics}], __, {graphics}], 
              DisplayFunction -> Identity, opts], liveApplet, 
          liveFrame}, 
        BeginJavaBlock[]; 
liveApplet = 
          JavaNew["Live"]; 

        liveFrame = 
          JavaNew["com.wolfram.jlink.MathAppletFrame", 
            liveApplet, {"INPUT=" <> 
                ToString[
                  InputForm[N[g]]], "WIDTH=500", "HEIGHT=500"}]; 
        EndJavaBlock[]; 
Return[
          ToExpression[
            StringReplace[
              ToString[
                InputForm[
                  g]], "DisplayFunction -> Identity"\  -> \ "DisplayFunction -> $DisplayFunction"]]];]\)\(
\)
    \)\)
Plot3DLive[x__] := 
      ShowLive[Plot3D[x, 
          DisplayFunction -> Identity]]
ParametricPlot3DLive[x__] := 
      ShowLive[ParametricPlot3D[x, 
          DisplayFunction -> 
            Identity]]

  

    
SetOptions[
        Plot3D, 
LightSources -> {{{1, 0, 1}, 
              RGBColor[1, 0, 0]}, {{1, 1, 1}, RGBColor[0, 1, 0]}, {{0, 1, 1}, 
              RGBColor[0, 0, 1]}, {{0, 0, -1}, 
              RGBColor[1, 1, 1]}}];
SetOptions[
        ParametricPlot3D, 
LightSources -> {{{1, 0, 
                1}, RGBColor[1, 0, 0]}, {{1, 1, 1}, 
              RGBColor[0, 1, 0]}, {{0, 1, 1}, 
              RGBColor[0, 0, 1]}, {{0, 0, -1}, 
              RGBColor[1, 1, 
                1]}}];

  

Off[General::spell];
Off[General::spell1];
Needs["Graphics`ImplicitPlot`"]
\(\(\(Needs["Graphics`ContourPlot3D`"]\)\(
\)\(
\)
    \) (*\ 
      Now\ some\ other\ packages\ which\ get\ used\ now\ and\ \(\(then\)\(.\)\)\ *) \)
Needs["Graphics`PlotField`"]
Needs["Graphics`PlotField3D`"]
Needs["Graphics`FilledPlot`"]
\(\(\(Needs["Graphics`Shapes`"]\)\(
\)\(
\)
    \) (*\ Vector\ Analysis\ Stuff\ *) \)
Needs["Calculus`VectorAnalysis`"]
SetCoordinates[Cartesian[x, y, z]];
  
Grad2D[f_, 
        opts___] := {Grad[f, opts][[1]], Grad[f, 
            opts][[2]]}

     (* 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}]}];\)


 (*\ 
      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},n] animates 
	a particle 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.";\)\(
\)
    \)\)
\(\(\(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.";\)
	\)
Needs["Graphics`Arrow`"];
\(\(\(Needs["Utilities`FilterOptions`"];\)\(
\)
    \)\)
\(\(\(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[funclist_, paramlist_, n_, options___] := 
      DoAnimation[funclist, paramlist, n, 0, 2, options]\)\(
\)
    \)\)
PathTangentAnimate[funclist_, paramlist_, n_, options___] := 
      DoAnimation[funclist, paramlist, n, 1, 2, options]
hi = 4;
PathAnimate3D[funclist_, paramlist_, n_, options___] := 
      DoAnimation[funclist, paramlist, n, 0, 3, 
        options]

    
\(\(\(PathTangentAnimate3D[funclist_, paramlist_, n_, options___] := 
      DoAnimation[funclist, paramlist, n, 1, 3, options]\)\(
\)
    \)\)
Off[General::spell];
DoAnimation[funclist_, paramlist_, n_, flag_, dim_, options___] := 
        Block[{tmin, tmax, stepsize, param, graph, s, t, f, Df, x, Dx, y, Dy, 
            z, Dz, xmin, xmax, ymin, ymax, zmin, zmax, radius}, 
          param = paramlist[[1]]; 
tmin = 
            paramlist[[2]]; 
          tmax = paramlist[[3]]; 
stepsize = (tmax - 
                  tmin)/n; 
 (*Note : 
              flag should be True (1) or False (0) . If it' 
                s false, 
            then only the position of the particle is animated . 
                If it' s true, 
            then the tangent vectors are also shown . 
                dim (ension) should be either 2 or 3, 
            and should correspond to the number of components in the parametrization.*)  (*These could be done within the later commands, 
            but I' m doing them here just to make those commands easier to read . Also, 
            I' m going to handle the third dimension inefficiently here . It won' t slow the command down too much and it' 
              s easier to be lazy . If there' s a third dimension, 
            we' ll use it . If not we' 
              ll double up the work in the first dimension.*) f[t_] = 
            Evaluate[funclist /. param -> t]; 
Df[
              t_] = D[f[t], t]; 
x[t_] = f[t][[1]]; 
          Dx[t_] = D[x[t], t]; 
y[t_] = f[t][[2]]; 
          Dy[t_] = D[y[t], t]; 
Which[dim == 2, 
            z[t_] = f[t][[1]]; Dz[t_] = D[z[t], t], dim == 3, 
            z[t_] = f[t][[3]]; 
            Dz[t_] = D[z[t], t]]; 
Off[
            ParametricPlot::ppcom]; 
Off[
            ParametricPlot3D::ppcom]; 
Off[$MaxExtraPrecision::meprec]; 
 (*Try to determine a good PlotRange . This isn' t a perfect method, 
            but Mathematica can' t find global extrema, 
            even for fairly simple functions on a compact interval . [I don' 
                      t want to rely on the Experimental package ... ] I need two separate procedures depending on whether I need to include tangent vectors or not.*) xmin = xmax = x[tmin]; 
          ymin = ymax = y[tmin]; 
          zmin = zmax = z[tmin]; 
Which[
            flag == 
              0, Do[xmin = Min[x[s], xmin]; 
xmax = 
                  Max[x[s], xmax]; 
ymin = 
                  Min[y[s], ymin]; 
ymax = 
                  Max[y[s], ymax]; 
zmin = 
                  Min[z[s], zmin]; 
zmax = 
                  Max[z[s], zmax];, {s, tmin, tmax, stepsize}];, 
            flag == 
              1, Do[xmin = 
                  Min[x[s] + Dx[s], xmin]; 
xmax = 
                  Max[x[s] + Dx[s], xmax]; 
ymin = 
                  Min[y[s] + Dy[s], ymin]; 
ymax = 
                  Max[y[s] + Dy[s], ymax]; 
zmin = 
                  Min[z[s] + Dz[s], zmin]; 
zmax = 
                  Max[z[s] + Dz[s], zmax];, {s, tmin, tmax, 
                  stepsize}];]; 
 (*This is kind of a superfluous step, 
            but we need to increase the ranges just a tad or points on the edge of the box won' 
              t be displayed . These lines also prevent problems if x[
                t], y[t], or z[t] are constant.*) xmin = 
            xmin - 0.1; 
ymin = 
            ymin - 0.1; 
zmin = 
            zmin - 0.1; 
xmax = 
            xmax + 0.1; 
ymax = 
            ymax + 0.1; 
zmax = 
            zmax + 0.1; 
Which[dim == 2, 
            graph = ParametricPlot[f[t], {t, tmin, tmax}, 
                DisplayFunction -> Identity, 
                AspectRatio -> Automatic, 
                PlotRange -> {{xmin, xmax}, {ymin, ymax}}, options], 
            dim == 3, 
            graph = ParametricPlot3D[f[t], {t, tmin, tmax}, 
                DisplayFunction -> Identity, 
                AspectRatio -> Automatic, 
                PlotRange -> {{xmin, xmax}, {ymin, ymax}, {zmin, zmax}}, 
                options]]; 
radius = 
            Min[Abs[xmax - xmin], Abs[ymax - ymin], Abs[zmax - zmin]]/
              60; 
radius = 
            Min[radius, 0.03]; 
radius = 
            Max[radius, 0.03]; 
Do[
            Show[graph, 
              If[dim == 
                  2,  (*Are we showing Tangent Vectors?*) If[
                  flag == 1, 
                  Graphics[{Hue[0], Arrow[f[t], f[t] + Df[t]]}], 
                  Graphics[{PointSize[radius], Point[f[t]]}]], 
                If[flag == 1, 
                  Graphics3D[{Hue[0], Line[{f[t], f[t] + Df[t]}]}], 
                  Graphics3D[{PointSize[radius], Point[f[t]]}]]], 
              If[dim == 2, Graphics[{PointSize[radius], Point[f[t]]}], 
                Graphics3D[{PointSize[radius], Point[f[t]]}]], 
              DisplayFunction -> $DisplayFunction, 
              AspectRatio -> Automatic, 
              If[dim == 2, 
                PlotRange -> {{xmin, xmax}, {ymin, ymax}}, 
                PlotRange -> {{xmin, xmax}, {ymin, ymax}, {zmin, 
                      zmax}}], options], {t, tmin, 
              tmax, (tmax - tmin)/
                n}];];

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.