Gradients

We must load the vector field plotting package

<<  Graphics`PlotField`

Here is a simple function:

f[x_, y_] := x^2 + y^2

f[x, y]

Here are it's partial derivatives:

D[f[x, y], x]
D[f[x, y], y]

fx[x_, y_] := 2  x
fy[x_, y_] := 2  y

Here we define a function that gives us the gradient vector

gradf[x_, y_] := {fx[x, y], fy[x, y]}

An example

gradf[0, 1]

Here we plot the vector field determined by the function

g1=PlotVectorField[gradf[x, y], {x, -3, 3}, {y, -3, 3}]

g2=ContourPlot[f[x, y],{x, -3, 3},{y, -3, 3},ContourShading -> False]

Show[{g1, g2}]

Here is a graph of our function

Plot3D[f[x, y], {x, -3, 3}, {y, -3, 3}]

This is the norm of the gradient vector for this function. It is a cone.

Plot3D[Norm[gradf[x, y]], {x, -3, 3}, {y, -3, 3}]

Another view

Plot3D[Norm[gradf[x, y]], {x, -3, 3}, {y, -3, 3}, 
      ViewPoint -> { .7, 1,  .1}, 
      AspectRatio -> Automatic]

Another example

f[x_, y_] := Sin[x]*Sin[y]

f[x, y]

Plot3D[f[x, y], {x, -5, 5}, {y, -5, 5}]

The partial derivatives

D[f[x, y], x]
D[f[x, y], y]

A function defining the gradient

gradf[x_, y_] := {Cos[x] Sin[y], Cos[y] Sin[x]}

Here is the gradient vector field

g1=PlotVectorField[gradf[x, y], {x, -5, 5}, {y, -5, 5}]

Here is the gradient vector field shown with more vectors plotted

g1=PlotVectorField[gradf[x, y], {x, -5, 5}, {y, -5, 5}, 
          PlotPoints -> 20]

Here is the countour plot of our surface

g2=ContourPlot[f[x, y],{x, -5, 5},{y, -5, 5},ContourShading -> True,PlotPoints -> 50,ColorFunction -> Hue]

The gradient vector field and contour plot are shown together.

Show[{g2, g1}]

The norm of the gradient is graphed below

g3 = 
      Plot3D[Norm[gradf[x, y]], {x, -5, 5}, {y, -5, 5}, 
        PlotPoints -> 50]

The graph of our function

g4 = Plot3D[f[x, y], {x, -5, 5}, {y, -5, 5}]

Here they are shown together

Show[{g4, g3}]