Gradients and Directional Derivatives

This notebook describes how to compute gradients and directional derivatives in Mathematica.  In particular, you'll see how to:
- Define a function of several variables and plot functions of two variables.
- Compute general expression for a gradient and evaluate it at a specific point.
- Compute the general expression for the magnitude of a gradient and evaluate it at a specific point.
- Compute the general expression for the directional derivative in a given direction and evaluate that directional derivative as a specific point.
- Plot a gradient field for a function of two variables.

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: grad_dir_deriv.nb

Define and Plot Functions of Multiple Variables

In the code below, we first define a functions of two variables and then use the Plot3D command to plot the function.  We could also evaluate the function at specific points with commands like "f[2,3]" which would evaluate this function for x->2 and y->3.  NOTE:  For more info on plotting such functions, lookup "Plot3D" under Built-In Functions from within Mathematica Help.

\(f[x_, y_] := x^2\  + x*Cos[y] - Exp[y]; 

    Plot3D[f[x, y], {x, \ \(-2\), 2}, \ {y, \(-2\), 2}]\)

Computing Gradients

We'll need the package: Calculus`VectorAnalysis` to get the gradient command in Mathematica.  We use the "Needs" command to avoid multiple loadings and the associated multiply-defined symbols that can result.

Needs["Calculus`VectorAnalysis`"]

Now we'll define a function of several variables and compute it's general expression for the gradient and evaluate the gradient at a specific point.  Below Line 1 repeats the Needs command (to make it easy to cut-and-paste this code), Line 2 defines the function, Line 3 specifies the coordinate system in use, Line 4 computes the gradient (in general), and Line 5 evaluates the gradient at the point (2,1, π),

Needs["Calculus`VectorAnalysis`"]\(g[x_, y_, z_] := x*E^\((3*y)\)*Cos[z]; 

    SetCoordinates[Cartesian[x, y, z]]; 
Grad[g[x, y, z]]\)% /. {x -> 2, y -> 1, z -> π}

Computing Magnitude of the Gradient

Again, we need the package: Calculus`VectorAnalysis` to get the gradient command in Mathematica.  We use the "Needs" command to avoid multiple loadings and the associated multiply-defined symbols that can result.

Needs["Calculus`VectorAnalysis`"]

Now we'll define a function of several variables and compute it's general expression for the magnitude of the gradient and evaluate the magnitude of the gradient at a specific point.  Below Line 1 repeats the Needs command (to make it easy to cut-and-paste this code), Line 2 defines the function,  Line 3 computes the gradient (in general), Line 4 computes the norm of that gradient, and Line 5 evaluates the gradient's magnitude at the point (2,1, π),  NOTE:  In this code, we specify the coordinate system directly in the Grad command with a second parameter instead of using the SetCoordiates command as we did above.

Needs["Calculus`VectorAnalysis`"]\(g[x_, y_, z_] := x*E^\((3*y)\)*Cos[z]; 

    gradvector[x_, y_, z_] := Grad[g[x, y, z], Cartesian[x, y, z]]; 

    Sqrt[gradvector[x, y, z] . gradvector[x, y, z]]\)% /. {x -> 2, y -> 1, z -> π}

Computing a Directional Derivative

Again, we need the package: Calculus`VectorAnalysis` to get the gradient command in Mathematica.  We use the "Needs" command to avoid multiple loadings and the associated multiply-defined symbols that can result.

Needs["Calculus`VectorAnalysis`"]

Now we'll define a function of several variables and compute it's directional derivative at a specific point in a given direction.  NOTE:  The direction does not need to be given as a unit vector.  The code to accomplish this is given in the input cell below.  Here's what it does:
1)  Line 1 repeats the Needs command (to make it easy to cut-and-paste this code).
2)  Line 2 is where the direction vector is specified (does not need to be a unit vector).
3)  Line 3 defines the function.
4)  Line 4 computes the gradient (in general).
5)  Lines 5 and 6 are where the general directional derivative is computed by taking a dot product of the gradient with the direction vector and dividing by the norm of the direction vector.
6)  Line 7 is where the general expression for the directional derivative (just computed) is evaluated at a specific point (in this case (2, 1, π) ).
NOTE:  In this code, we specify the coordinate system directly in the Grad command with a second parameter instead of using the SetCoordiates command as we did above.

Needs["Calculus`VectorAnalysis`"]\(directionvector = {1, 3, \(-2\)}; 

    g[x_, y_, z_] := x*E^\((3*y)\)*Cos[z]; 

    gradvector[x_, y_, z_] := Grad[g[x, y, z], Cartesian[x, y, z]]; 

    \((gradvector[x, y, z] . directionvector)\)/
      Sqrt[directionvector . directionvector]\)% /. {x -> 2, y -> 1, z -> π}

Plotting a Gradient Field

Here's how to plot the gradient field for a function of 2 variables.  We use the "PlotGradientField" command which is included in the Graphics`PlotField` package, hence the Needs command on the first line.  On Line 2, we define the function.  On Line 3 we plot the gradient field.  For more info on the PlotGradientField command, look it up in the Mathematica Help Master Index or type in and execute "??PlotGradientField".

Needs["Graphics`PlotField`"]\(f[x_, y_] := x^2\  + x*Cos[y] - Exp[y]; 

    PlotGradientField[f[x, y], {x, \(-2\), 2}, {y, \(-2\), 2}]\)