Gradients

We must load the vector field plotting package

In[13]:=

<< Graphics`PlotField`

Here is a simple function:

In[2]:=

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

In[3]:=

f[x, y]

Out[3]=

x^2 + y^2

Here are it's partial derivatives:

In[5]:=

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

Out[5]=

2 x

Out[6]=

2 y

In[7]:=

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

Here we define a function that gives us the gradient vector

In[9]:=

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

An example

In[12]:=

gradf[0, 1]

Out[12]=

{0, 2}

Here we plot the vector field determined by the function

In[21]:=

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

[Graphics:HTMLFiles/index_13.gif]

Out[21]=

⁃Graphics⁃

In[22]:=

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

[Graphics:HTMLFiles/index_16.gif]

Out[22]=

⁃ContourGraphics⁃

In[23]:=

Show[{g1, g2}]

[Graphics:HTMLFiles/index_19.gif]

Out[23]=

⁃Graphics⁃

Here is a graph of our function

In[24]:=

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

[Graphics:HTMLFiles/index_22.gif]

Out[24]=

⁃SurfaceGraphics⁃

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

In[25]:=

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

[Graphics:HTMLFiles/index_25.gif]

Out[25]=

⁃SurfaceGraphics⁃

Another view

In[32]:=

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

[Graphics:HTMLFiles/index_28.gif]

Out[32]=

⁃SurfaceGraphics⁃

Another example

In[34]:=

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

In[35]:=

f[x, y]

Out[35]=

Sin[x] Sin[y]

In[36]:=

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

[Graphics:HTMLFiles/index_34.gif]

Out[36]=

⁃SurfaceGraphics⁃

The partial derivatives

In[37]:=

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

Out[37]=

Cos[x] Sin[y]

Out[38]=

Cos[y] Sin[x]

A function defining the gradient

In[39]:=

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

Here is the gradient vector field

In[40]:=

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

[Graphics:HTMLFiles/index_41.gif]

Out[40]=

⁃Graphics⁃

Here is the gradient vector field shown with more vectors plotted

In[44]:=

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

[Graphics:HTMLFiles/index_44.gif]

Out[44]=

⁃Graphics⁃

Here is the countour plot of our surface

In[50]:=

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

[Graphics:HTMLFiles/index_47.gif]

Out[50]=

⁃ContourGraphics⁃

The gradient vector field and contour plot are shown together.

In[52]:=

Show[{g2, g1}]

[Graphics:HTMLFiles/index_50.gif]

Out[52]=

⁃Graphics⁃

The norm of the gradient is graphed below

In[59]:=

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

[Graphics:HTMLFiles/index_53.gif]

Out[59]=

⁃SurfaceGraphics⁃

The graph of our function

In[60]:=

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

[Graphics:HTMLFiles/index_56.gif]

Out[60]=

⁃SurfaceGraphics⁃

Here they are shown together

In[61]:=

Show[{g4, g3}]

[Graphics:HTMLFiles/index_59.gif]

Out[61]=

⁃Graphics3D⁃


Created by Mathematica  (October 4, 2004)