The Gradient Vector and Level Curves

Anytime you are examining a function, you can learn a lot by plotting some of its level curves.

In[157]:=

  Clear[f,x,y]
  f[x_,y_] = 5 - (x^2  + x y  +  y^2)

Out[157]=

       2          2
  5 - x  - x y - y

Here is a plot of some level curves f[x,y] = c.

In[158]:=

  a = -1;
  b = 1;
  levelcurves = ContourPlot[Evaluate[f[x,y]],{x,a,b},{y,a,b},
             ContourSmoothing->Automatic,
             AxesLabel->{"x","y"}];

The lighter shading indicates larger values of f[x,y].

Here is the 3D plot of the surface z = f[x,y]:

In[159]:=

  surfaceplot = 
  ParametricPlot3D[{x,y,f[x,y]},{x,a,b},{y,a,b},
  AxesLabel->{"x","y","f[x,y]"}];

The level curves are plots at the height f[x,y]=c.
Here is f[x,y] = 4.5:

In[160]:=

  c = 4.5;
  Show[levelcurves,Contours->{c},ContourShading->False];

Here is the plot of the intersection of the surface and the plane z = 4.5:

In[161]:=

  c = 4.5;
  plane = ParametricPlot3D[{x,y,c},{x,a,b},{y,a,b},
  PlotPoints->{2,2},DisplayFunction->Identity];
  Show[surfaceplot,plane,DisplayFunction->$DisplayFunction];

Consider f(x,y) = 2 x^2 + x y + y^2.

In[162]:=

  Clear[x,y,f]
  f[x_,y_] = 2 x^2 + x y + y^2;
  grad2[f][x,y]

Out[162]=

  {4 x + y, x + 2 y}

Here is a part of the level curve f[x,y] = f[1.5,2].

In[163]:=

  levelcurve = ContourPlot[f[x,y],{x,0,3},{y,1,4},
  Contours->{f[1.5,2]},ContourShading->False,
  DisplayFunction->$DisplayFunction];

These next two will plot the gradient vector.

In[164]:=

  Clear[x,y,t]
  x0=1.5;
  y0=2;
  {x1,y1}=grad2[f][x,y]/.{x->1.5,y->2};

In[165]:=

  gradientp=vectorPlot[{{x0,y0}},{{x1,y1}}];

This plots the level curve and the gradient at (1.5,2)

In[166]:=

  Show[levelcurve,gradientp,PlotRange->All,
  AspectRatio->Automatic,AxesLabel->{"x","y"},
  DisplayFunction->$DisplayFunction];

Well...

Well... ...

Up to The Gradient