Divergence and Curl

Dr. Therese Shelton		shelton@southwestern.edu
Southwestern University   Georgetown, TX

updated for Mathematica 10

Function from R2 to R2

Clear[fcn,f1,f2,x,y];
f1[x_,y_] = x^2 y^3;
f2[x_,y_] =  Cos[x y];
f[x_,y_] =  {f1[x,y],f2[x,y]};

Clear[xlo,xhi,ylo,yhi]
xlo = -2; xhi = 3; ylo = -π; yhi = π;

{VectorPlot[f[x,y],{x,xlo,xhi},{y,ylo,yhi},Axes->True],
VectorPlot[f[x,y],{x,xlo,xhi},{y,ylo,yhi},VectorScale->{Tiny,Tiny,None}]}

Clear[x,y]

manually....

Clear[divergenceR2]
divergenceR2[x_,y_] = D[f1[x,y],x] + D[f2[x,y],y]

Notice that the divergence is the sum of diagonal elements on the derivative matrix:

MatrixForm[{{ D[f1[x,y],x] , D[f2[x,y],x]},
{ D[f1[x,y],y] , D[f2[x,y],y]}}]

From Colley's Vector Calculus p2129 of 4th Edition:  divergence of a vector field at a particular point gives a measure of the "net mass flow" or "flux density" of the vector field in or out of that point.  
If the vector field represents velocity of a fluid, then positive divergence signifies that more fluid is flowing out than in at that point.
From Smith and Minton Calculus Concepts and Connections, p 1023, a point is a source if the divergence is positive there and a sink if the divergence is negative there. 
A mix from both texts: The vector field is “source-free”, “incompressible”, or “solenoidal” in a region provided the divergence is everywhere zero in the region.

{Plot3D[divergenceR2[x,y],{x,xlo,xhi},{y,ylo,yhi},AxesLabel->{" x "," y "," z "}],Show[{Plot3D[divergenceR2[x,y],{x,xlo,xhi},{y,ylo,yhi},AxesLabel->{" x "," y "," z "}],Plot3D[0,{x,xlo,xhi},{y,ylo,yhi},PlotStyle->Red]}],ContourPlot[2 x (y^3)-x Sin[x y],{x,xlo,xhi},{y,ylo,yhi}]}

From the graph of the divergence (left) and from the algebraic function, we see that the divergence is zero when x=0 and when y=0, at least.  Examining the vector field above shows that indeed, along the x-axis there seems to be as much flow into each point as there is out of it; same for the y-axis. 

The plot in the middle shows the divergence function and the x-y plane, z=0,  in red.  This more clearly shows where the divergence is zero.  In addition to the divergence being zero along x=0 and along y=0, there are curves that are like a parabola where the divergence function is zero.

The graph on the right above is a contour plot of the divergence function.  The divergence has the largest positive values where (x,y) are either both positive or both negative. The divergence has the most negative values where (x,y) have opposite sign.

We can show more exactly where the divergence is zero below with the following implicitly defined function, although there is really no gap in the middle. 
You should be able to indicate where the divergence is positive and where it is negative on a plot like the one below on the right that combines a contour plot with the vector field.

{ContourPlot[2 x (y^3)-x Sin[x y]==0,{x,xlo,xhi},{y,ylo,yhi}],Show[{
VectorPlot[f[x,y],{x,xlo,xhi},{y,ylo,yhi}],VectorPlot[f[x,y],{x,xlo,xhi},{y,ylo,yhi},VectorScale->{Tiny,Tiny,None}],ContourPlot[2 x (y^3)-x Sin[x y]==0,{x,xlo,xhi},{y,ylo,yhi}]
}]}

Function from R3 to R3

We can show the vector field without the arrowheads, so that we really only show a slope field, and with the arrowheads so we can tell the direction of flow.  It looks as though we always flow away from any point, so the divergence should be positive.

Clear[g,g1,g2,x,y,z];
g1[x_,y_,z_] =x + y z;
g2[x_,y_,z_] =  y + x z;
g3[x_,y_,z_] = z + x y;
g[x_,y_,z_] =  {g1[x,y,z],g2[x,y,z],g3[x,y,z]};

Clear[xlo,xhi,ylo,yhi,zhi,zlo]
xlo = -2; xhi = 3; ylo = -π; yhi = π;zlo=-2;zhi=2;
VectorPlot3D[g[x,y,z], {x,xlo,xhi},{y,ylo,yhi}, {z,zlo,zhi},AxesLabel->{"x","y","z"}]

We can use Mathematica's functions...

The divergence is positive, as we suspected.

Div[g[x,y,z],{x,y,z}]

Curl[g[x,y,z],{x,y,z}]

or we can define our own

Clear[divergenceR3]
divergenceR3[x_,y_] = D[g1[x,y,z],x] + D[g2[x,y,z],y]+ D[g3[x,y,z],z]

Clear[curlR3]
curlR3 ={D[g3[x,y,z],y] - D[g2[x,y,z],z],
D[g1[x,y,z],z] - D[g3[x,y,z],x],
D[g2[x,y,z],x] - D[g1[x,y,z],y]}

See Colley's Vector Calculus  p231 (paraphrased here):  The curl does measure, in a sense,  the twisting or circulation of a vector field -- how quickly and in what orientation an object that is dropped into the vector field rotates as it moves.  

A vector field is said to be irrotational if the curl is 0 everywhere.

For our example:
We do not twist; we mostly move away from the origin.

Notice that the divergence is the sum of diagonal elements on the derivative matrix:

MatrixForm[{{D[g1[x,y,z],x], D[g1[x,y,z],y], D[g1[x,y,z],z]},
{D[g2[x,y,z],x], D[g2[x,y,z],y], D[g2[x,y,z],z]},
{D[g3[x,y,z],x], D[g3[x,y,z],y], D[g3[x,y,z],z]}}]

The Jacobian is the determinant of the derivative matrix.

Det[{{D[g1[x,y,z],x], D[g1[x,y,z],y], D[g1[x,y,z],z]},
{D[g2[x,y,z],x], D[g2[x,y,z],y], D[g2[x,y,z],z]},
{D[g3[x,y,z],x], D[g3[x,y,z],y], D[g3[x,y,z],z]}}]