In this lab you will demonstrate the relationship between the gradients and level curves of functions.
The gradient of a function, is a vector whose components are the partials of the original function;
Define the function by
f[x_,y_] := (x^2 + 4 y^2) Exp[1 - x^2 -y^2]To visualize the function, you may wish to do a Plot3D or a ContourPlot,
fcontours = ContourPlot[f[x,y], {x,-3,3}, {y,-3,3}, ContourShading->False, PlotPoints->40];It is easy to find the gradient in Mathematica,
gradf = {D[f[x,y],x], D[f[x,y],y]}What make the above result a vector, as far as Mathematica is concerned, is the presence of the `{ , }.'
The gradient of a function is a vector field over the domain of the function. We can see what the above vector field looks like. First we need to load an external package for plotting vector fields. Type the following command exactly (notice that the single quotes are single left quotes),
Needs["Graphics`PlotField`"]and shift-return. Then try this:
fgradplot = PlotVectorField[gradf, {x,-3,3}, {y,-3,3}];What you should see is a plot of many vectors. The tail of each vector resides where Mathematica evaluated the gradient. Try overlaying the vector field plot with the plot of level curves of f generated above using the Show command:
Show[fcontours, fgradplot];You can `zoom' into some particular portion of the graph by specifying the x,y range for the plot using the PlotRange option, as follows:
Show[fcontours, fgradplot, PlotRange->{{0, 2},{0, 2}}];
Examine the functions
and
Superpose level curves and plots of the gradient field to demonstrate that the gradient is perpendicular to the level curves of the function.