Initialization of Defined Functions, Etc.


This is an evaluated Mathematica notebook. Since the notebook is intended to be interactive, it may be helpful to also view the unevaluated version


Copyright 1996
Gary S. Stoudt
Mathematics Department
Indiana University of PA
Indiana, PA 15705
GSSTOUDT@grove.iup.edu

This notebook collects all of the Mathematica packages and user defined functions that are necessary to use the notebooks "vectors.ma", "veccalc.ma", "streamlines.ma", "veccalc2.ma", "surfaces.ma", "surfaceint.ma", and "potentialflow.ma". Examples can be found in those notebooks.

To do vector cross products:

In[1]:=

  <<LinearAlgebra`CrossProduct`

To plot vectors with tail at "from" and head at "to":

In[2]:=

  vector2DPlot[from_,to_]:=
  Show[Graphics[{AbsolutePointSize[4],RGBColor[1,0,0],
  Map[Point,to],Map[Line[{from[[#]],to[[#]]}]&,
  Table[i,{i,Length[from]}]]}],
  Axes->True,AspectRatio->Automatic]

In[3]:=

  vector3DPlot[from_,to_]:=
  Show[Graphics3D[{AbsolutePointSize[4],RGBColor[1,0,0],
  Map[Point,to],Map[Line[{from[[#]],to[[#]]}]&,
  Table[i,{i,Length[from]}]]}],
  Axes->True,AspectRatio->Automatic]

In[4]:=

  vectorPlot[from_,to_]:=	
  If[Length[N[from[[1]]]]==2,Return[vector2DPlot[from,to]],
  		Return[vector3DPlot[from,to]]];

These functions will plot a vector with tail at the point "from" and head at the point "to". The dot is at the head of the vector. These functions are designed to plot more than one vector. This means that the "from" and "to" arguments must be a LIST of vectors (even if you only have one vector in each). Here are some examples.

To plot a surface implicitly:

In[5]:=

  <<Graphics`ContourPlot3D`

Length of a vector:

In[6]:=

  norm[x_]:=Sqrt[x.x]

Gradient of a scalar field. grad3[f][x,y,z] gives gradient using variables x, y, and z.

In[7]:=

  grad2[f_]:={D[f[#1,#2],#1],D[f[#1,#2],#2]}&
  

In[8]:=

  grad3[f_]:={D[f[#1,#2,#3],x],D[f[#1,#2,#3],#2],D[f[#1,#2,#3],#3]}&

Arc length of r[t] from a to b:

In[9]:=

  arclength[r_,a_,b_]:=Integrate[norm[D[r[#],#]],{#,a,b}]

Curvature of F[t]:

In[10]:=

  curvature[F_,t_]:=Module[{T,c},
      T[t]=D[F[t],t]/Sqrt[D[F[t],t].D[F[t],t]];
      c=Sqrt[D[T[t],t].D[T[t],t]]/Sqrt[D[F[t],t].D[F[t],t]];
      Return[c]]

Necessary to plot vector fields:

In[11]:=

  <<Graphics`PlotField`

In[12]:=

  <<Graphics`PlotField3D`

To graph implicitly defined functions, that is, f[x,y]=0.

In[13]:=

  <<Graphics`ImplicitPlot`

To calculate divergence and curl of a vector field. Works like the grad2 and grad3 functions.

In[14]:=

  div2[f_]:=D[f[#1,#2][[1]],#1]+D[f[#1,#2][[2]],#2]&

In[15]:=

  div3[f_]:=
  D[f[#1,#2,#3][[1]],#1]+D[f[#1,#2,#3][[2]],#2]+D[f[#1,#2,#3][[3]],#3]&

In[16]:=

  curl2[f_]:=- D[f[#1,#2][[1]],#2] + D[f[#1,#2][[2]],#1]&

In[17]:=

  curl3[f_]:={- D[f[#1,#2,#3][[2]],#3] + D[f[#1,#2,#3][[3]],#2],
     D[f[#1,#2,#3][[1]],#3] - D[f[#1,#2,#3][[3]],#1],
     - D[f[#1,#2,#3][[1]],#2] + D[f[#1,#2,#3][[2]],#1]}&

Mathematica has built in functions for divergence, gradient, and curl, but I like these better. They allow me to use different letters for variable names. It would be easy to change the definitions to not require the dimension "2" or "3" afterwards (curl instead of curl2 and curl3), but I do not think it is harmful to students to keep reminding them what dimension they are in. You can modify them using the same technique as in the definiton of the "norm" function.

Go up to Calculus and Differential Equations Project Description