Gradients and Vector fields
Department of Mathematics, Statistics and Computer Science
University of Illinois at Chicago
by Paul Brown, Heidi Burgiel, Marc Culler, Heather Dye and John Wood
Copyright © 1999, The University of Illinois at Chicago
Maple is a registered trademark of Waterloo Maple Software
Introduction
In past labs we discussed using contour maps to indicate the values of a function of two variables, and using tangent planes to approximate the values of a function of two variables in a small region. But most of the information given by a contour map or tangent plane can also be obtained by studying the gradient vectors of a function, as we see in this lab.
The gradient vector of a function f(x,y) is the vector defined by the partial derivatives of f . The direction of the gradient vector is the direction in which the values of f increase fastest. Maple can compute the gradient of a function, but requires that you first load the linalg package and provide a vector indicating which partial derivatives to compute, as demonstrated in the example below.
> with(linalg):
> f := (x,y) -> x^2 + y^2;
> grad(f(x,y),vector([x,y]));
The Gradient Vector
If F is a function of two variables x and y, the gradient of F is the vector function whose first component is the partial derivative of F with respect to x and whose second component is the partial derivative of F with respect to y.
> grad (F(x,y), vector([x,y]));
Notice that the gradient vector of a function of two variables is itself a function of two variables. In fact, it's a function which takes two variables, x and y , as input, and which outputs a vector! Vector valued functions of two variables can be hard to comprehend at first, so we will start by discussing the gradient vector of a familiar function, f(x,y) = x^2 + y^2 .
> plot3d(f(x,y), x=-1..1, y=-1..1, axes=BOX, scaling=constrained);
The gradient vector of f at point (x,y) is given by the partial derivatives of f . For the function x^2 + y^2 the gradient vector is [2x, 2y] . Also, the gradient vector always points directly away from the origin. The gradient vector points in direction of greatest increase of f , which is away from the origin. As x and y increase, the gradient vector increases in length. The length of the gradient vector indicates the steepness of the graph of f , which increases as (x,y) moves away from the origin. Shown below is a (red) contour plot of f together with some of the (blue) gradient vectors of f . Notice that the vectors get longer and the contours get closer together as the graph of f gets steeper away from (0,0).
> with(plots):
> fcplot := contourplot(f(x,y), x=-4..4, y=-4..4):
> ffplot := fieldplot([2*x,2*y], x=-4..4, y=-4..4, color=BLUE, arrows=SLIM):
> display({fcplot, ffplot});
Exercise 1
Shown below are some gradient vectors of the function f1(x,y) = .3*x^3 - x + y^2 . Where do you think the function is increasing the fastest? (Give (x,y) coordinates or a region, like "top left corner".) Where is it changing the least?
> fieldplot(grad(.3*x^3 - x + y^2, vector([x,y])), x=0..4, y=-2..2, color=BLUE, arrows=SLIM);
The Gradient and the Chain Rule
Earlier in this course, you learned how to apply the chain rule to functions of multiple variables. This let you answer questions like: "The strength of the magnetic field at a point (x,y) is given by B(x,y) = 1/(x^2 + y^2) . A particle moves so that its position at time t is given by p(t) = (t/3, (t-1)/2) . How is the magnetic attraction of the particle changing at time t =1?" You learned that in the derivative of B(p(t)) with respect to t can be obtained by taking the sum of products of various derivatives. (Look up the chain rule in your text or notes if this doesn't sound familiar!)
But that sum of products of derivatives is just the dot product of the gradient vector of B with the velocity vector of p(t) ! This makes sense -- the gradient vector describes how B is increasing and the velocity vector describes how the particle is moving. The dot product of these two vectors describes how B is increasing in the direction of motion of the particle, taking into account the speed of the particle.
> B := (x,y) -> 1/sqrt(x^2 + y^2);
> Bfield := fieldplot(grad(B(x,y), vector([x,y])), x=-1..1, y=-1..1, color=BLUE, arrows=SLIM, grid=[12,12]):
> particlepath := plot([t/3, (t-1)/2, t=-1..3], color=GREEN):
> display({Bfield, particlepath}, scaling=CONSTRAINED);
The plot above shows the gradient field of B with the path p(t) superimposed. At time t =1, the position of the particle is (1/3, 0), where the gradient is [-9, 0].
> subs(x=1/3, y=0, grad(B(x,y), vector([x,y])));
By looking closely at the plot (or at the definition of B ), we see that the gradient of B always points toward the origin, and increases in length (a lot!) close to the origin. At (1/3,0), the position of the particle at time t =1, the gradient vector [-9, 0] points toward the origin, while the particle is moving away from the origin with velocity vector [1/3, 1/2]. We conclude that the effects of the magnetic field on the particle are weakening at time t =1; this is confirmed by the fact that the dot product of the gradient vector and the velocity vector (which is the derivative of B(p(t) ) with respect to t ) is negative:
> dotprod([-9,0],[1/3,1/2]);
Exercise 2
A temperature sensor moves at speed 1 clockwise along the path shown in green below. The gradient vectors of a temperature function are shown in blue. At what point on the path is the temperature increasing the fastest? When is it decreasing the fastest? Give a point on the path at which the temperature registered is neither increasing or decreasing.
> tempplot := fieldplot([0,2], x=-3/2..3/2, y=-3/2..3/2, arrows=SLIM, color=BLUE, grid=[10,10]):
> sensorpath := plot([sin(t), cos(t), t=0..7], color=GREEN):
> display({tempplot, sensorpath}, scaling=CONSTRAINED);
The Gradient and the Contour Plot
The dot product of a gradient vector and a velocity vector gives the rate of change of the function observed by a moving object. When moving along a contour line of the function, the value of the function neither decreases nor increases, and so the dot product of the velocity vector and the gradient vector must be zero. By definition, the velocity vector must be tangent to the contour line, so the gradient vector must be perpendicular to the contour line in order for the dot product to equal zero.
The gradient vectors of a function are perpendicular to the contours of that function. We saw an example of this earlier:
> display({fcplot, ffplot});
This makes sense. The contour lines show paths in the plane along which the function is constant, so the direction of greatest increase of a function should be directly away from the contour lines.
Exercise 3
Write down the letters of the plots below for which the (blue) vectors shown could be the gradients of the function whose (red) contour plot is superimposed. HINTS: Gradient vectors should be perpendicular to contours. Gradient vectors are longer when the function increases (or decreases) rapidly. Contours are closest together where the function increases rapidly.
> display({fieldplot([2,0], x=-2..2, y=-2..2, color=BLUE, arrows=SLIM, grid=[10,10]), contourplot(2*y, x=-2.2..2.2, y=-2.2..2.2, thickness=2)}, title=A, scaling=CONSTRAINED);
> display({fieldplot([x,y], x=-2..2, y=-2..2, color=BLUE, arrows=SLIM, grid=[10,10]), contourplot(sqrt(x^2 + y^2), x=-2..2, y=-2..2)}, title=B, scaling=CONSTRAINED);
> display({fieldplot([x/sqrt(x^2+y^2),y/sqrt(x^2+y^2)], x=-2..2, y=-2..2, color=BLUE, arrows=SLIM, grid=[10,10]), contourplot(sqrt(x^2 + y^2), x=-2..2, y=-2..2)}, title=C, scaling=CONSTRAINED);
> display({fieldplot([-.9*x^2+1, -2*y], x=-2..2, y=-2..2, color=BLUE, arrows=SLIM), contourplot(-.3*x^3 + x - y^2, x=-2..2, y=-2..2)}, title=D, scaling=CONSTRAINED);
> display({fieldplot([.9*x^2-1, 2*y], x=-2..2, y=-2..2, color=BLUE, arrows=SLIM), contourplot(.3*x^3 - x + y^2, x=-2..2, y=-2..2)}, title=E, scaling=CONSTRAINED);
> display({fieldplot([-y,x], x=-2..2, y=-2..2, color=BLUE, arrows=SLIM, grid=[12,12]), contourplot(x^2 - y^2, x=-2..2, y=-2..2)}, title=F, scaling=CONSTRAINED);
Gradient Vectors, Tangent Planes and Directional Derivatives
The length of a gradient vector at a point indicates the steepness of the graph of the function at that point, and so determines the slope of the tangent plane at that point. The direction of the gradient vector tells you the direction in which the function is increasing most rapidly, and so tells you the orientation of the tangent plane. So, the gradient vector of a function at a point together with the value of the function at that point is all you need to know to determine the tangent plane to the graph of the function at that point.
The directional derivative of a function at a point is just the slope of the tangent plane at that point in that direction. It is also the dot product of the direction vector and the gradient vector. (IMPORTANT: direction vectors always have length one!) This is also the rate of change of the function with respect to a particle that is moving with speed one in that direction at that point.
The most important application of this fact is in finding the direction of steepest increase (or decrease) of a function. This is very useful when trying to find maxima and minima of a function whose formula is not known, e.g. in a situation in which the only data available describes increases and decreases, but not values, of a function. The directional derivative is greatest when its dot product with the gradient vector is greatest, which is exactly when the two vectors are pointing in the same direction! So to find the maximum value of a function, you travel in the direction of the gradient vectors of the function. To find a minimum value of a function, you travel in the direction opposite the gradient vectors of the function.
Shown below are some gradient vectors of the function x^2 + y^2 . If we start at the point (2,2) and follow the gradient vectors backward, as indicated by the green curve, we reach the minimum of the function at (0,0). The green curve is called a curve of steepest descent because it always follows the direction in which the function is decreasing most rapidly.
> decreasingfield := fieldplot([x,y], x=0..2, y=0..2, color=BLUE, grid=[10,10], arrows=slim):
> steepestdescent := plot([x,x], x=0..2, color=GREEN):
> display({steepestdescent, decreasingfield}, scaling=CONSTRAINED);
Exercise 4
Sketch a path that starts at the point (-1/2, 2) and moves in the direction of greatest increase to a local maximum of the function whose gradient vectors are shown below.
> fieldplot([-.9*x^2+1, -2*y], x=-2..2, y=-2..2, color=BLUE, arrows=SLIM, scaling=CONSTRAINED);
Exercise 5
Match each of the sets of gradient vectors plotted below with the graph of a function by writing the letter of the plot next to the name of the graph.
> plot3d(x/5 - y/4, x=-2..2, y=-2..2, shading=ZHUE, axes=BOX, title="plane");
> plot3d(x^2+y^2, x=-2..2, y=-2..2, axes=BOX, shading=ZHUE, title=parabaloid);
> plot3d(0.3*x^3-x+y^2, x=-2..2, y=-2..2, axes=box, shading=ZHUE, title=cubic);
> plot3d(sqrt(x^2+y^2), x=-2..2, y=-2..2, axes=box, shading=ZHUE, title=cone);