Visualizing Gradients

Here is a function f, of two variables:

Needs ["VectorAnalysis`"]
f=Exp[-x^2]*Exp[-y^2]

Plot the function.

Plot3D[f,{x,-1,1},{y,-1,1},BoxRatios-> Automatic,ViewPoint->{0,0,Infinity},ColorFunction->Hue,PlotPoints->50]

Make a contour plot of the function.

ContourPlot[f,{x,-1,1},{y,-1,1},BoxRatios-> Automatic]

Calculate the gradient of the function.  There is an extra step in the calculation, because Mathematica does not like the third component of the gradient when it's plotting in two dimensions.

Gradf=Grad[f,Cartesian[x,y,z]]

gradf=Gradf[[{1,2}]]

Plot the gradient of the function.

VectorPlot[gradf,{x,-1,1},{y,-1,1},VectorPoints->10,VectorScale->Small,Axes-> True,AxesLabel-> {"x","y"}]

We will now display the contour plot and the gradient on the same  axis.  What do you notice?

Show[ContourPlot[f,{x,-1,1},{y,-1,1}],VectorPlot[gradf,{x,-1,1},{y,-1,1},VectorPoints->10,VectorScale->Small,Axes-> True,AxesLabel-> {"x","y"}]]

Now try other functions by plugging in something else for f(x,y) and then going back through the early part of the worksheet.  A particularly nice choice is: (but try your own too!)

f=Exp[-x^2]*Exp[y^2]

What about functions of three variable?  Look at:

h=1/Sqrt[x^2+y^2+z^2]

Why is it hard to plot this function?
Find the gradient of h(x,y,z):

gradh=Grad[h, Cartesian[x,y,z]]

Now plot the gradient.  Is this easier to plot than h itself?  Why or why not?

VectorPlot3D[gradh,{x,-1,1},{y,-1,1},{z,-1,1},VectorPoints->5,VectorScale->Small,AxesLabel->{"x","y","z"}]

You may also want to try: (Log[x] gives the natural log in Mathematica.  Log[b,x] gives the log base b of x)

h=Log[Sqrt[x^2+y^2+z^2]]

by Corinne Manogue