Chapter 15. The gradient
The gradient of , denoted , is the single most important concept in the entire “Multivariable differentiation” part. Although its definition is actually quite easy to compute, I want to give a proper explanation for where it comes from.
Throughout this chapter, remember two important ideas:
- The goal of the derivative is to approximate a function by a linear one.
- Everything you used slopes for before, you should use normal vectors instead.
If you want spoilers for what’s to come, see the following table.
| Thing | 18.01 | 18.02 |
|---|---|---|
| Input | ||
| Output | ||
| Think of as | Slope (rise/run) | Measures change in each of directions |
| Approximation | Multiply by small run | Dot product with small displacement |
| Picture | Slope of tangent in -graph | Normal vector to tangent of level curve |
§15.1 [TEXT] The gradient rewrites linear approximation into a dot product
In 18.01, when was a function and was an input, we thought of the single number as the slope to interpret it geometrically. Now that we’re in 18.02, we have different rates of change, but we haven’t talked about how to think of it geometrically yet.
It turns out the correct definition is to take the numbers and make them into a vector. Bear with me for just one second:
If is a two-variable function (so ), the gradient of , denoted , is the function obtained by taking the two partial derivatives as the coordinates:
The case of variables is analogous; for example if is a three-variable function, then
The types are confusing here. To continue harping on type safety:
- Suppose accepts points in and outputs scalars in .
- Then accepts points in and outputs vectors in .
Keep the distinction between points and vectors in mind when drawing pictures. We’ll always draw points as dots, and vectors as arrows.
The reason for defining this gradient is that it lets us do linear approximation with a dot product, and consequently dot products are going to be super important throughout this chapter. Let me show you how. Let’s go back to our protagonist
at the point . Way back in Equation 10 (on an earlier page), we computed and and used it to get the approximation
Now the idea that will let us do geometry is to replace the pair of numbers and with a single “small displacement” vector , and the pair of numbers and with the vector instead, so that the approximation part just becomes a dot product:
In some places you see the abbreviation and the name “directional derivative” for it. I hate this term, because some people have different notations and definitions (according to Wikipedia, some authors require to be a unit vector, etc.).
So I will always just write the dot product instead, which is unambiguous and means you have one less symbol to remember. The gradient does everything directional derivative can do, and does it better.
In full abstraction, we can rewrite linear approximation as:
Suppose is differentiable at a point . Then for small displacement vectors , linear approximation promises that
In other words the net change from to is approximated by the dot product .
Up until now, all we’ve done is rewrite the earlier equation with a different notation; so far, nothing new has been introduced. Why did we do all this work to use different symbols to say the same thing?
The important idea is what I told you a long time ago: anything you used to think of in terms of slopes, you should rethink in terms of normal vectors. It turns out that to complete the analogy to differentiation, the normal vector is going to be that gradient , and we’ll see why in just a moment (spoiler: it’s because of the dot product). For now, you should just know that is going to be the right way to draw pictures of all rates of change at once, although I haven’t explained why yet.
Before going on, let’s write down the recipes and some examples just to make sure the definition of the gradient makes sense, then I’ll explain why the gradient is the normal vector we need to complete our analogy.
§15.2 [RECIPE] Calculating the gradient
- Compute every partial derivative of the given function.
- Output the vector whose components are those partial derivatives.
Consider the six functions
from back in Section 14.2 and Section 14.3. Compute their gradients.
Solution. Take the partial derivatives we already computed and make them the components:
(Remember is the natural log, not base .)
§15.3 [RECIPE] Linear approximation
We actually could have stated an equivalent recipe right after we defined partial derivatives, but conceptually I think it’s better to think of everything in terms of the gradient, so I waited until after I had defined the gradient to write the recipe.
To do linear approximation of for a small displacement vector :
- Compute , the gradient of at the point .
- Take the dot product to get a number, the approximate change.
- Output plus the change from the previous step.
Solution. Compute the gradient by taking both partial derivatives:
So the gradient vector at the starting point is given by
The target point differs from the starting point by the displacement . So the approximate change in is given by
Therefore,
Solution. Compute the gradient by taking both partial derivatives:
So the gradient vector at the starting point is given by
The target point differs from the starting point by the displacement . So the approximate change in is given by
Therefore,
Solution. Compute the gradient by taking both partial derivatives:
So the gradient vector at the starting point is given by
The target point differs from the starting point by . So the approximate change in is given by
Therefore,
§15.4 [TEXT] Gradient descent
At the end of Section 15.1, we promised the geometric definition of the dot product would pay dividends. We now make good on that promise.
The motivating question here is:
Let . Imagine we’re standing at the point . We’d like to take a step away in some direction of our choice. For example, we could go to , or or , or any other point on the circle we’ve marked in the figure below. (For the third point, note that , so that point is indeed away.)
- Which way should we step if we want to maximize the -value at the new point?
- Which way should we step if we want to the -value to stay about the same?
- Which way should we step if we want to minimize the -value at the new point?
You can see a cartoon of the situation in Figure 34. Note that this figure is not to scale, because is too small to be legibly drawn, so the black circle is drawn much larger than it actually is.
To answer the question, we use the geometric interpretation of the dot product now. Remember that the change in is approximated by
The geometric definition of the dot product is that it equals
where is the included angle. But is fixed (in this example, it’s ) and is fixed as well (in this example we chose it to be the small number ).
So actually all we care about is the angle ! Think about that for a moment. Then remember how the cosine function works:
- is the most positive value of the cosine, and that occurs when and point the same direction.
- is the most negative value of the cosine, and that occurs when and point opposite directions.
- If and are perpendicular (so or ), then the dot product is zero.
Translation:
- Move along the gradient to increase as quickly as possible.
- Move against the gradient to decrease as quickly as possible.
- Move perpendicular to the gradient to avoid changing by much either direction.
§15.5 [TEXT] Normal vectors to the tangent line/plane
We only need to add one more idea: keeping about the same should correspond to moving along the tangent line or plane.
Indeed, in the 2D case, the tangent line is the line that “hugs” the level curve the closest, so we think of it as the direction causing to avoid much change. The same is true for a tangent plane to a level surface in the 3D case; the plane hugs the curve near the point . So that means the last bullet could be rewritten as
In the previous example with a level curve, the gradient pointed away from the interior. This is not true in general. For example, imagine instead the function
The point lies on the level curve of . The level curve of with value is also a circle of radius , because it corresponds to the equation .
However, the gradient looks quite different: with enough calculation one gets
Evaluating at , we get
Hence, for the function , drawing the figure analogous to Figure 34 gives something that looks quite similar, except the green arrow points the other way and is way smaller. This makes sense: as you move towards the origin, you expect to get larger. See Figure 35.
§15.6 [RECIPE] Computing tangent lines/planes to level curves/surfaces
At this point, we can compute tangent lines and planes easily. We apply the old recipe in Section 5.4 (finding a plane given a point with a known normal vector) with as the normal vector. To spell it out:
To find the tangent line/plane to a level curve/surface of a function at point :
- Compute the gradient . This is a normal vector, so it tells you the left-hand side for the equation of the line/plane.
- Adjust the right-hand side so it passes through , like in Section 5.4.
Solution. Let , so we are looking at the level curve for of . We have seen already that
Hence, the tangent line should take the form
for some . To pass through , we need , so the answer is
Solution. Isn’t this an 18.01 question? Yes, but the level curves work fine here too. We think of this parabola as the level curve of for the value . The gradient is then
Hence the tangent line should take the line for some . We need to pass through , so we take to get the answer
(Written in 18.01 form this would be , which shouldn’t be a surprise, because we know the derivative of at is .)
§15.7 [RECAP] A recap of Part Echo on Multivariable Differentiation
Let’s summarize the last few sections.
- We replaced the old graphs we used in 18.01 with level curve and level surface pictures in Chapter 13. These new pictures differed from 18.01 pictures because all the variables on the axes are inputs now, and we treat them all with equal respect.
- We explained in Chapter 14 how to take a partial derivative of or , which measures the change in just one of the variables.
- We used these partial derivatives to define the gradient in Chapter 15. This made linear approximation into a dot product, where for a small displacement .
Using the geometric interpretation of a dot product, was a normal vector to the level curve of passing through , and:
- Going along the gradient increases most rapidly
- Going against the gradient decreases most rapidly
- Going perpendicular to the gradient puts you along the tangent line or plane at .
§15.8 [EXER] Exercises
Exercise 15.2. The level curve of a certain differentiable function for the value turns out to be a circle of radius centered at .
- Give an example of one such function .
- What are all possible vectors that could be?
- Do linear approximation to estimate starting from the point .
Exercise 15.3. For each part, either give an example of or show that none exist.
- Can you find a function such that ?
- Can you find a function such that ?
- Can you find a function such that ?
- Can you find a function such that ?
Exercise 15.4 (*). Let , , , be nonzero real numbers and let
Suppose the level curve of for the value is tangent to the line at the origin, and also passes through . Compute .