Vector Fields and Line Integrals

In this  Mathematica  notebook we will learn how to graph two-dimensional vector fields and evaluate line integrals of these vector fields over curves.  To graph a vector field, we will use the PlotVectorField[ ]  and PlotVectorField3D[ ] commands.  Before using these commands, be sure to first enter the following lines.

Needs["Graphics`PlotField`"]
Needs["Graphics`PlotField3D`"]

These lines load two special packages of programs containing the PlotVector Field[ ]   and PlotVectorField3D[ ] functions.  You need to enter these lines only once when you begin a session on graphing vector fields.  Note that the symbol around the word PlotField is not the single quote; it is found on the key to the left of the number one.

Examples

Vector Fields

EXAMPLE 1:  In this example we graph a two-dimensional vector field  F(x, y) = -y i + x j  for - 1 ≤ x ≤ 1 and - 1 ≤ y ≤ 1.  Before continuing, be sure you have  entered the line about "Needs["Graphics.......], then enter the next line.

F[x_,y_]={-y,x};
PlotVectorField[F[x,y],{x,-1,1},{y,-1,1}];

If we add the Axes option, we put in the x and y axes.

PlotVectorField[F[x,y],{x,-1,1},{y,-1,1},Axes->True];

The default for the PlotPoints option is 15 points (vectors) in each direction.  Sometimes it is useful to set PlotPoints so that fewer points are used in each direction.  The following has  5  vectors across the horizontal directions and  10  vectors in the vertical direction.

PlotVectorField[F[x,y],{x,-1,1},{y,-1,1},
	Axes->True,PlotPoints->{5,10}];

EXAMPLE 2:  PlotVectorField3D[f, {x,a,b},{y,c,d},{z,e,f}] describes a three-dimensional vector field, if we want to see a three-dimensional vector field for the function F(x,y,z) = y i -x j + (z/2)k we enter the following.

Clear[F]
F[x_, y_, z_] = {y, -x, (z/2)}; 
PlotVectorField3D[
      F[x, y, z], {x, -1, 1}, {y, -1, 1}, {z, 0, 2}];

The default is 7  PlotPoints (vectors) in each direction.  We can add vector heads with VectorHeads->True.

PlotVectorField3D[F[x, y, z],{x, -1, 1},{y, -1, 1},{z, 0, 2},VectorHeads -> True];

EXAMPLE 3:  If  f  is a scalar function of two variable, its gradient ∇f is defined by 
			∇f  =  (Subscript[f, x])(x,  y) i  + (Subscript[f, y])(x,  y) j
If  f(x, y) = (1/(Sqrt[1  +  (x^2)  +  (y^2)])) we  can graph its gradient vector field,  ∇f,  with the next lines.

Clear[f]
f[x_, y_] = (1/(Sqrt[1 + (x^2) + (y^2)]));
PlotGradientField[
        f[x, y], {x, -1, 1}, {y, -1, 1}];

If we want to see the level curve  f(x, y) = 0.8  for  -1 ≤ x ≤ 1  and  - 1 ≤ y ≤ 1.  We can enter the following

\(ContourPlot[
      f[x, y], \ {x, \(-1\), 1}, \ {y, \(-1\), 
        1}, 
	\ \ \ \ \ \ ContourShading -> False, 
      Contours -> {0.8}]\)

EXAMPLE 4:  We can also look at gradient vector fields in three dimensions.  If  f  is a scalar function of three variables its gradient ∇f is defined by 
			∇f  =  (Subscript[f, x])(x,  y,  z) i  + (Subscript[f, y])(x,  y, z) j + (Subscript[f, z])(x,  y,  z) k
If  f(x, y, z)  =  1/(Sqrt[1  +  (x^2)  +  (y^2)  +  (z^2)])we can graph its gradient vector field,  ∇f,  with the next lines.

Clear[f]
f[x_, y_, z_] = (1/(Sqrt[1 + (x^2) + (y^2) + (z^2)]))
PlotGradientField3D[
      f[x, y, z], {x, -1, 1}, {y, -1, 1}, {z, -1, 1}]

If we want to see a three-dimensional contour plot of  f(x, y,z) = 0.8  for  -1 ≤ x ≤ 1, - 1 ≤ y ≤ 1 ,  and  - 1 ≤ z ≤ 1.  We first need to enter the following line

Needs["Graphics`ContourPlot3D`"]

Now we can execute a contour plot of f(x, y, z); notice that the restrictions we put on x, y, and z require  the function to be between 0.5 and  1 so we will try a contour level of 0.6.

ContourPlot3D[
      f[x, y, z],  {x, -1, 1},  {y, -1, 1}, {z, -1, 1}, 
      Contours -> {0.6}]

Line Integrals

EXAMPLE 5:  In this example we want to evaluate the line integral of a vector field  F(x, y) = x i - y j   over the unit circle lying in the first quadrant in the counterclockwise direction.  We first define the vector field and a parametrization for the curve.

Clear[F]
F[{x_,y_}]={x,-y};
r[t_]={Cos[t],Sin[t]};

To plot the vector field together with the curve we can enter the following line.  The command "DisplayFunction->Identity" suppresses the original graph and the command "DisplayFunction->$DisplayFunction" tells Mathematica to finally show the suppressed graphs.

vectors = 
        PlotVectorField[F[{x, y}], {x, 0, 1.5}, {y, 0, 1.5}, 
          Axes -> True, PlotPoints -> 10, 
          DisplayFunction -> Identity];
curve = 
        ParametricPlot[{Cos[t], Sin[t]}, {t, 0, (π/2)}, 
          DisplayFunction -> Identity];
Show[vectors, curve, 
        DisplayFunction -> $DisplayFunction];

Now, to find the line integral of  F  over the unit circle lying in the first quadrant in the counterclockwise direction, we can enter the following where a period is  the Mathematica symbol for the dot product of vectors.

(∫_0^((π/2)))F[r[t]].r'[t]\[DifferentialD]t

EXAMPLE 6:  This time we will let F(x, y, z) = -y i + (x - z) j + y k  and we will plot both the vector field and the curve given by the helix  r(t) = cos(((π t)/2)) i + sin(((π t)/2)) j + t k with  0 ≤ t ≤ 1  as well as evaluate the integral of  F  along this part of the helix.  We will start by clearing the functions  F  and  r  as well as the terms  "vectors" and "curve" and then redefining them.

Clear[F, r, vectors, curve]
F[{x_, y_, z_}] = {-y, x - z, y};
r[t_] = {Cos[((π t)/2)], Sin[((π t)/2)], 
          t};

Now we can tell Mathematica to graph the functions along with the helix.  The term Thickness[ ], tells Mathematica to make the helix thicker than the vectors so that it is easier to see.

vectors = 
        PlotVectorField3D[F[{x, y, z}], {x, 0, 1}, {y, 0, 1}, {z, 0, 1}, 
          PlotPoints -> 5, VectorHeads -> True];
curve = 
        ParametricPlot3D[Append[r[t], Thickness[ .02]], {t, 0, 1}, 
          DisplayFunction -> Identity];
Show[vectors, curve, 
        DisplayFunction -> $DisplayFunction];

Finally, the line integral of  F  taken along the helix is given by the next line.

(∫_0^1)F[r[t]].r'[t]\[DifferentialD]t

Exercises

1.  Graph each of the following vector fields, showing the coordinate axes, and obtain a printout.
     a)  F(x, y) = (x + 2y) i  +  (3 - x) j,    - 2 ≤ x ≤ 2, - 1 ≤ y ≤ 1
     
     b)  F(x, y) = (((x )^2)+ 2(y^2)) i  +  (2 sin(x)) j,   - 1 ≤ x ≤ 1, - 2 ≤ y ≤ 2

2.  Graph each of the following vector fields, showing the vector heads, and obtain a printout.
     a)  F(x, y, z) = (x + y - z) i  +  (x - y) j  + (z - x) k,    - 1 ≤ x ≤ 1, - 1 ≤ y ≤ 1,  0 ≤ z ≤ 2
     
     b)  F(x, y, z) =  x i  +  2y j +  (x + 2z) k,   - 1 ≤ x ≤ 1, - 2 ≤ y ≤ 2,  0 ≤ z ≤ 2

3.  Graph the gradient vector field,  ∇f,   together with the specified level curve for each of the following functions and get a printout.  What seems to be the relationship between the vector field  at any point on the level curve and the level curve in each case?
     a)  f(x, y) = cos (π x) cos(π y),  f(x, y) = 0.5
     
     b)  f(x, y) = (x^2)+ ((y^2)/2),  f(x, y) = 0.2

4.  Graph the gradient vector field,  ∇f,  for each of the following functions and get a printout.  
     a)  f(x, y, z)  =  x + y + z
     
     b)  f(x, y, z) =  (Sqrt[(x^2)  +  (y^2) +  (z^2)])

5.  Evaluate the line integral of the vector field,  F,  along the curve,  C,  for each of the following vector fields together with the curve in each case.  Include a  printout  showing the vector field together with the curve for each vector field.  On your printout, also show the Mathematica commands for each line integral.
     a)  F(x, y) = 2x i + y j  and  C  is the segment of  y = (x^2)  -  1 from  (- 1, 0)  to  (0, - 1).
     
     b)  F(x, y) = (x + y) i + y j  and  C  is the quarter unit circle  from  ( 1, 0) to (0, 1).

6.  Evaluate the line integral of the vector field,  F,  along the curve,  C,  for each of the following vector fields together with the curve in each case.  Include a  printout  showing the vector field together with the curve for each vector field.  On your printout, also show the Mathematica commands for each line integral.
     a)  F(x, y, z ) = (x^2) i + xy j  + (z^2) k  and  C  is the vector function r(t) = t i + (t^2) j  + (t^3) k.
     
     b)  F(x, y, z) = ((x^4) (e^y)) i + (ln z) j  + (Sqrt[(y^2)  +  (z^2)])k  and  C  is the line segment from (1, 2, 1) to (6, 4, 5).

7.  Find the work done by the force field  F(x, y, z) = xz i + yx j + zy k  on a particle that moves along the curve  r(t) = (t^2) i - (t^3)j  + (t^4)k,  0 ≤ t ≤ 1.