Gradients, Gradient Plots and Tangent Planes

copyright © 2000 by Paul Green and Jonathan Rosenberg

Visualizing Gradients of Functions of Two Variables

The gradient of a function of several variables is the vector-valued function whose components are the partial derivatives of the function. Let us recall the function f from the previous notebook.

syms x y z
f=((x^2-1)+(y^2-4)+(x^2-1)*(y^2-4))/(x^2+y^2+1)^2

The gradient of f can be computed using the function jacobian from the symbolic toolbox. Notice that a vector whose components are the variables is needed as the second argument to jacobian. It is worth noting that jacobian actually has much more general capabilities, which we will be using later in the course. Incidentally, there is also a MATLAB command called gradient, but it produces a numerical approximation to the gradient, not a symbolic form for the gradient.

gradf=jacobian(f,[x,y])

We can plot the gradient of f by using 'gradplot' as an optional argument to genplot. It is particularly interesting to superimpose this on a contour plot of f.

genplot(f,-3:.1:3,-3:.1:3,'contour',20)
hold on
genplot(f,-3:.2:3,-3:.2:3,'gradplot',.5)
hold off

The lengths of the arrows in the gradient plot are determined by both the step size and by the last numerical parameter, which we will refer to as the length parameter. What is happening here is that the numerical 'gradient' function is comparing the values of the function at adjacent points on the grid and making the arrows proportional to the difference times the last parameter. It may be necessary to experiment with a few different sets of step sizes and length parameters to get a useful gradient plot. Notice that the gradient arrows are always normal to the level curves. This is always the case.

Behavior Near Critical Points

A plot such as this one can be interpreted to give information regarding critical points of the function. Critical points are points where the gradient vector vanishes. A critical point is called non-degenerate if behavior of the function near the critical point is controlled by the second derivatives (so that the 'second derivative test' applies). For functions of two variables, there are three kinds of non-degenerate critical points. You can recognize them from the following three kinds of pictures:

genplot(x^2+y^2,-1:.1:1,-1:.1:1,'contour',10)
hold on
genplot(x^2+y^2,-1:.1:1,-1:.1:1,'gradplot',.5)
title('local minimum'), axis equal, hold off


genplot(-x^2-y^2,-1:.1:1,-1:.1:1,'contour',10)
hold on
genplot(-x^2-y^2-1:.1:1,-1:.1:1,'gradplot',.5)
title('local maximum'), axis equal, hold off

genplot(x^2-y^2,-1:.1:1,-1:.1:1,'contour',10)
hold on
genplot(x^2-y^2-1:.1:1,-1:.1:1,'gradplot',.5)
title('saddle point'), axis equal, hold off

In the case of the function f, we can see that there are two local minima and a saddle point on the x-axis. The two minima can be identified from the fact that they are surrounded by closed contours along which the gradient arrows point outward. The saddle point marks the transition from contours consisting of pairs of closed contours around the two minima, to single curves surrounding both minima. Note that the level curve through a saddle point has a self-intersection. By symmetry, it seems safe to conclude that the saddle point is at the origin, and that the minima are symmetrically placed around it. We will continue to pursue this example in the next notebook.

Problem 1:

(a)         Obtain a simultaneous contour and gradient plot of the function , defined in Problem 1 of the previous notebook.

(b)        Explain what information the plot conveys regarding the location and nature of the critical points of the function.

Gradients of Functions of Three Variables,
Tangent Planes to Surfaces

The three-dimensional analogue of the observation that the gradient of a function of two variables is always normal to the level curves of the function is the fact that the gradient of a three dimensional function is always normal to the level surfaces of the function. It follows that the gradient of the function at any point is normal to the tangent plane at that point to the level surface through that point. This can be exploited to plot the tangent plane to a surface at a chosen point.

Let us plot the surface 3z3 - 2x2 - y2 = 0 together with its tangent plane at the point (2,4,2). We begin by checking that the indicated point satisfies the equation.

g=3*z^3-2*x^2-y^2; subs(g,[x,y,z],[2,4,2])

Next we determine the gradient of g at (2,4,2).

gradg=jacobian(g,[x,y,z])
planenormal=subs(gradg,[x,y,z],[2,4,2])

Next we write the function that determines the plane.

planeq=realdot(planenormal,[x-2,y-4,z-2])

Then we solve for z in terms of x and y to make this easier to plot.

planefun=solve(planeq,z)

Finally we plot the surface g = 0 and add a plot of the plane so we can see the tangency.

genplot(g,-5:.1:5,-5:.1:5, 0:.1:3, 0), hold on
genplot(planefun,-5:.1:5,-5:.1:5,'plot3'), hold off

Note that there is another way we could have done this. We could have instead solved for z as a function of x and y in the equation g(x, y, z) = 0 and represented our surface as the graph of a function of two variables: z = h(x, y). Then we could have gotten the equation of the tangent plane from the gradient of h. Here are the details:

h=solve(g,z)

Since h should be real, we want the first solution.

h=h(1)

The equation of the tangent plane is then given by z = (grad h)(2, 4) + h(2, 4) . (x - 2, y - 4).

gradh=simplify(subs(jacobian(h,[x,y]), [x,y], [sym(2),sym(4)]))
planefun=simplify(subs(h,[x,y], [sym(2),sym(4)]))+realdot(gradh,[x-2,y-4])

This is the same answer as before. Again we can produce a plot:

ezmesh(h,[-5,5,-5,5]), hold on, ezmesh(planefun,[-5,5,-5,5]), hold off

Problem 2:

Obtain a plot showing the hyperboloid together with its tangent plane at the point (3,4,5).

Additional Problems:

1.      Obtain simultaneous contour and gradient plots for the function What information does your plot give you regarding the location and classification of the critical points of in the region -1.5 < x, y < 1.5?

2.      Obtain a plot showing the ellipsoid together with its tangent plane at the point (2,3,1).

3.      Find the equation of the tangent plane to the surface x y z = 8 at the point (-2, -2, 2) in two different ways. Check that your two answers agree. Then plot the surface (in a vicinity of this point) along with the tangent plane, so that the tangency is visible (you need only do this once).