Vector Fields, Divergence, and Curl Demonstration

This notebook describes how to work with vector fields in Mathematica.  In particular, you'll see how to:
- Define and plot 2D vector fields.
- Define and plot 3D vector fields.
- Compute the general divergence of a vector field and evaluate at a specific point.
- Compute the general curl vector of a vector field and evaluate at a specific point.
- Plot a vector field and overlay it onto a contour plot of  its general divergence function.

The functions in this notebook are set up to be easily cut-and-pasted so you can easily experiment with these functions on other vector fields.

TO SEE WHAT THIS NOTEBOOK PROVIDES:
- Select Kernel|Evaluation|Evaluate_Notebook to execute all the cell and produce the graphics.
- Then scroll through reading the commands and associated comments while you look at the commands' results.
- All input cells are set up such that they can be cut-and-pasted in their entirety for easy "re-use".

The material in this notebook was originally developed by Capt Paul Simonich and was re-formatted into a tutorial by Lt Col Steve Hadfield, both of the USAF Academy, Dept of Mathematical Sciences.	
Filename:  vf_div_curl.nb

Define and Plot 2D Vector Fields

First we'll need to load two packages.  We use the "Needs" command to avoid multiple loadings and the associated multiply-defined symbols that can result.

Needs["Calculus`VectorAnalysis`"]
Needs["Graphics`PlotField`"]

Now let's define a simple 2D vector field and plot it.  The Mathematica list notation ({,}) is used to define vectors.  NOTE:  For more info, lookup "PlotVectorField" under Built-In Functions from within Mathematica help.

F[x_, y_] := {x*y,  x + y};
PlotVectorField[
      F[x, y], {x,  -2, 2},  {y, -2, 2}]

Define and Plot 3D Vector Fields

Again we'll need to load two packages.  We use the "Needs" command to avoid multiple loadings and the associated multiply-defined symbols that can result.

Needs["Calculus`VectorAnalysis`"]
Needs["Graphics`PlotField3D`"]

Now let's define a simple 3D vector field and plot it.  The Mathematica list notation ({,}) is used to define vectors.   NOTE:  For more info, lookup "PlotVectorField3D" under Built-In Functions from within Mathematica help.

G[x_, y_, z_] := {x*z,  x + y, y*Cos[z]};
\(PlotVectorField3D[
      G[x, y, z], 
	{x, \(-2\), 2}, \ {y, \ \(-2\), 2}, {z, \(-2\), 
        2}]\)

Divergence of a Vector Field

The divergence function in Mathematica expects a 3D vector field.  If you are dealing with a 2D vector field you can extend it to 3D by appending a 3rd term which is zero.

In the commands below, we first ensure that the Calculus`VectorAnalysis` package is loaded (since the divergence command is in this package).  Then we clear any prior field definition for "F".  In the third line, we tell Mathematica what coordinate system we are working in.  In line 4, we define the vector field (2D extended to 3D).  Line 5 computes the general divergence and line 6 uses this result and substitutes in x->2 and y->3 to get the divergence at a specific point.   Note:  We need to set the coordinate system first.  We could also have done this in cylindrical or spherical coordinates.  See the SetCoordinates function in the Mathematica Help system for more details.

Needs["Calculus`VectorAnalysis`"]
Remove[F]
SetCoordinates[Cartesian[x, y, z]];
F[x_, y_] := {x*y , x + y, 0}
Div[F[x, y]]
% /. {x -> 2, y -> 3}

Now let's try it for a 3D vector field.  NOTE:  Here we tell Mathematica the coordinate system directly in the Div command as a second parameter.

Needs["Calculus`VectorAnalysis`"]
Remove[G]
G[x_, y_, z_] := {x*y*z , x + y + z, x*Sin[y]*Cos[z]}
Div[G[x, y, z], Cartesian[x, y, z]]
% /. {x -> 1, y -> π, z -> 0}

Curl of a Vector Field

The curl function in Mathematica also expects a 3D vector field.  If you are dealing with a 2D vector field you can extend it to 3D by appending a 3rd term which is zero.

The commands below do the following:  
1 - "Needs" ensures that the Calculus`VectorAnalysis` package is loaded as this is what defines the "Curl" function.
2 - "Remove[F]" clears any prior definitions for "F".
3 - "SetCoordinates" tells MMA what coordinate system we are working in.
4 - Defines the vector field F(x,y)=<x*y, x+y> (extending it to 3D)
5 - Computes the general expression for curl.
6 - Evaluates the curl of the field at the point (2,3).
  Note:  Again, we need to set the coordinate system first.  We could also have done this in cylindrical or spherical coordinates.  See the SetCoordinates function in the Mathematica Help system for more details.

Needs["Calculus`VectorAnalysis`"]
Remove[F]
SetCoordinates[Cartesian[x, y, z]];
F[x_, y_] := {x*y , x + y, 0}
Curl[F[x, y]]
% /. {x -> 2, y -> 3}

Now let's try it for a 3D vector field.  NOTE:  Here we tell Mathematica the coordinate system directly in the Curl command as a second parameter.

Needs["Calculus`VectorAnalysis`"]
Remove[G]
G[x_, y_, z_] := {x*y*z , x + y + z, x*Sin[y]*Cos[z]}
Curl[G[x, y, z], Cartesian[x, y, z]]
% /. {x -> 1, y -> π, z -> 0}

Contour Map of Divergence and the Vector Field

Here's an interesting combination.  We define a 2D vector, compute it's general divergence function and then plot the contour map of this divergence expression with the vector field itself.  The results graph is something that is interesting to think about.

Needs["Calculus`VectorAnalysis`"]
Needs["Graphics`PlotField`"]
Remove[F, DF, divPlot, vfPlot]
SetCoordinates[Cartesian[x, y, z]];
F[x_, y_] := {Sin[x*y], Cos[x*y]}
DF = Div[Append[F[x, y], 0]];
\(\(divPlot = 
        ContourPlot[
          DF, {x, 0, π}, {y, 0, π}, 
		PlotPoints -> 
            30];\)\)
vfPlot = 
        PlotVectorField[F[x, y], {x, 0, π}, {y, 0, π}];
Show[divPlot, vfPlot]

GrayLevel[1]