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:

If you want spoilers for what’s to come, see the following table.

Thing18.0118.02
Input𝑓:ℝ→ℝ𝑓:ℝ𝑛→ℝ
Output𝑓′:ℝ→ℝ∇𝑓:ℝ𝑛→ℝ𝑛
Think of asSlope (rise/run)Measures change in each of 𝑛 directions
ApproximationMultiply by small runDot product with small displacement
PictureSlope of tangent in 𝑥𝑦-graphNormal vector to tangent of level curve
Table 6: How to think of ∇𝑓 for multivariable functions, compared to the derivative in 18.01.

§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:

Definition

If 𝑓(𝑥,𝑦) is a two-variable function (so 𝑓:ℝ2→ℝ), the gradient of 𝑓, denoted ∇𝑓, is the function ℝ2→ℝ2 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

∇𝑓(𝑥,𝑦,𝑧)=(𝑓𝑥(𝑥,𝑦,𝑧)𝑓𝑦(𝑥,𝑦,𝑧)𝑓𝑧(𝑥,𝑦,𝑧)).
Type signature

The types are confusing here. To continue harping on type safety:

  • Suppose 𝑓:ℝ2→ℝ accepts points in ℝ2 and outputs scalars in ℝ.
  • Then ∇𝑓:ℝ2→ℝ2 accepts points in ℝ2 and outputs vectors in ℝ2.

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

𝑓(𝑥,𝑦)=𝑥2+𝑦2

at the point 𝑃=(3,4). Way back in Equation 10 (on an earlier page), we computed 𝑓𝑥(𝑃)=2⋅3=6 and 𝑓𝑦(𝑃)=2⋅4=8 and used it to get the approximation

𝑓(𝑃+⟨𝜀𝑥,𝜀𝑦⟩)=𝑓(⟨3,4⟩+⟨𝜀𝑥,𝜀𝑦⟩)=(3+𝜀𝑥)2+(4+𝜀𝑦)2≈25+6𝜀𝑥+8𝜀𝑦.

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 6 and 8 with the vector (68) instead, so that the approximation part just becomes a dot product:

𝑓((34)+𝐯)≈𝑓((34))+(68)⋅𝐯.
Warning: the directional derivative sucks

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:

Memorize: Linear approximation

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

Recipe for calculating the gradient
  1. Compute every partial derivative of the given function.
  2. Output the vector whose components are those partial derivatives.
Sample Question

Consider the six functions

𝑓1(𝑥,𝑦)=𝑥3𝑦2+cos(𝑦),𝑓2(𝑥,𝑦,𝑧)=𝑒𝑥𝑦𝑧𝑓3(𝑥,𝑦)=𝑥2+𝑦2,𝑓4(𝑥,𝑦,𝑧)=𝑥+𝑦+𝑧𝑓5(𝑥,𝑦,𝑧)=𝑥𝑦+𝑦𝑧+𝑧𝑥𝑓6(𝑥,𝑦)=𝑥𝑦

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:

∇𝑓1(𝑥,𝑦)=(3𝑥2𝑦22𝑥3𝑦−sin(𝑦)),∇𝑓2(𝑥,𝑦)=(𝑦𝑧𝑒𝑥𝑦𝑧𝑥𝑧𝑒𝑥𝑦𝑧𝑥𝑦𝑒𝑥𝑦𝑧),∇𝑓3(𝑥,𝑦)=(2𝑥2𝑦),∇𝑓4(𝑥,𝑦,𝑧)=(111),∇𝑓5(𝑥,𝑦,𝑧)=(𝑦+𝑧𝑥+𝑧𝑥+𝑦),∇𝑓6(𝑥,𝑦)=(𝑦𝑥𝑦−1log(𝑥)⋅𝑥𝑦).

(Remember log is the natural log, not base 10.)□

§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.

Recipe for linear approximation

To do linear approximation of 𝑓(𝑃+𝐯) for a small displacement vector 𝐯:

  1. Compute ∇𝑓(𝑃), the gradient of 𝑓 at the point 𝑃.
  2. Take the dot product ∇𝑓(𝑃)⋅𝐯 to get a number, the approximate change.
  3. Output 𝑓(𝑃) plus the change from the previous step.
Sample Question
Let 𝑓(𝑥,𝑦)=𝑥2+𝑦2. Approximate the value of 𝑓(3.01,4.01) by using linear approximation from (3,4).

Solution. Compute the gradient by taking both partial derivatives:

∇𝑓(𝑥,𝑦)=(2𝑥2𝑦).

So the gradient vector at the starting point is given by

∇𝑓(3,4)=(2⋅32⋅4)=(68).

The target point (3.01,4.01) differs from the starting point (3,4) by the displacement 𝐯=(0.01,0.01). So the approximate change in 𝑓 is given by

(68)⏟=∇𝑓(3,4)⋅(0.010.01)⏟=𝐯=(6⋅0.01+8⋅0.01)=0.14.

Therefore,

𝑓(3.01,4.01)≈𝑓(3,4)⏟=25+0.14=25.14.

□

Sample Question
Let 𝑓(𝑥,𝑦)=𝑥3−𝑦3. Approximate the value of 𝑓(2.01,−1.01) by using linear approximation from (2,−1).

Solution. Compute the gradient by taking both partial derivatives:

∇𝑓(𝑥,𝑦)=(3𝑥2−3𝑦2).

So the gradient vector at the starting point (2,−1) is given by

∇𝑓(2,−1)=(3(2)2−3(−1)2)=(12−3).

The target point (2.01,−1.01) differs from the starting point (2,−1) by the displacement 𝐯=(0.01,−0.01). So the approximate change in 𝑓 is given by

(12−3)⏟=∇𝑓(2,−1)⋅(0.01−0.01)⏟=𝐯=(12⋅0.01+(−3)⋅(−0.01))=0.15.

Therefore,

𝑓(2.01,−1.01)≈𝑓(2,−1)⏟=9+0.15=9.15.

□

Sample Question
Let 𝑓(𝑥,𝑦)=𝑒𝑥sin(𝑦)+777. Approximate the value of 𝑓(0.04,0.03) by using linear approximation from the point (0,0).

Solution. Compute the gradient by taking both partial derivatives:

∇𝑓(𝑥,𝑦)=(𝑒𝑥sin𝑦𝑒𝑥cos𝑦).

So the gradient vector at the starting point (0,0) is given by

∇𝑓(0,0)=(𝑒0sin0𝑒0cos0)=(01).

The target point (0.04,0.03) differs from the starting point (0,0) by (0.04,0.03). So the approximate change in 𝑓 is given by

(01)⏟=∇𝑓(0,0)⋅(0.040.03)⏟=𝐯=0⋅0.04+1⋅0.03=0.03.

Therefore,

𝑓(0.04,0.03)≈𝑓(0,0)⏟=777+0.03=777.03.

□

§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:

Question

Let 𝑓(𝑥,𝑦)=𝑥2+𝑦2. Imagine we’re standing at the point 𝑃=(3,4). We’d like to take a step 0.01 away in some direction of our choice. For example, we could go to (2.99,4), or (3,4.01) or (2.992,4.006), or any other point on the circle we’ve marked in the figure below. (For the third point, note that (3−2.992)2+(4−4.006)2=0.01, so that point is indeed 0.01 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 0.01 is too small to be legibly drawn, so the black circle is drawn much larger than it actually is.

Figure 34: Starting from 𝑃=(3,4), we make a step 𝐯 away, where |𝐯|=0.01. Not to scale.

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

∇𝑓(𝑃)⋅𝐯=|∇𝑓(𝑃)||𝐯|cos𝜃

where 𝜃 is the included angle. But |∇𝑓(𝑃)| is fixed (in this example, it’s 62+82=10) and |𝐯| is fixed as well (in this example we chose it to be the small number 0.01).

So actually all we care about is the angle 𝜃! Think about that for a moment. Then remember how the cosine function works:

Translation:

Memorize
  • 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

Memorize
The gradient ∇𝑓(𝑃) is normal to the tangent line/plane at 𝑃. It points towards the direction that increases 𝑓.
Example

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

𝑓(𝑥,𝑦)=1𝑥2+𝑦2.

The point (3,4) lies on the level curve of 𝑓(3,4)=125. The level curve of 𝑓(𝑥,𝑦) with value 125 is also a circle of radius 5, because it corresponds to the equation 1𝑥2+𝑦2=125.

However, the gradient looks quite different: with enough calculation one gets

∇𝑓(𝑥,𝑦)=(−2𝑥(𝑥2+𝑦2)2−2𝑦(𝑥2+𝑦2)2).

Evaluating at (3,4), we get

∇𝑓(3,4)=(−6625−8625).

Hence, for the function 𝑓(𝑥,𝑦)=1𝑥2+𝑦2, 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 1𝑥2+𝑦2 to get larger. See Figure 35.

Figure 35: Similar picture but for 𝑓(𝑥,𝑦)=1𝑥2+𝑦2. It looks very similar to Figure 34, but now the gradient points the other way and has much smaller absolute value, indicating that the value of 𝑓 increases as we go towards the center (but only slightly). Not to scale.
Remark
Back in the 3D geometry in the linear algebra part of the course, we usually neither knew nor cared what the sign and magnitude of the normal vector was. That is, when asked “what is a normal vector to the plane 𝑥−𝑦+2𝑧=8?”, you could answer (1−12) or (−11−2) or even (−100100−200). But this doesn’t apply to the gradient anymore: while it is a normal vector to the tangent line/plane, the magnitude carries additional information we shouldn’t just throw away.

§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:

Recipe: Tangent line/plane to level curve/surface

To find the tangent line/plane to a level curve/surface of a function 𝑓 at point 𝑃:

  1. Compute the gradient ∇𝑓. This is a normal vector, so it tells you the left-hand side for the equation of the line/plane.
  2. Adjust the right-hand side so it passes through 𝑃, like in Section 5.4.
Sample Question
Compute the tangent line to 𝑥2+𝑦2=25 at the point (3,4).

Solution. Let 𝑓(𝑥,𝑦)=𝑥2+𝑦2, so we are looking at the level curve for 25 of 𝑓. We have seen already that

∇𝑓=(2𝑥2𝑦)⟹∇𝑓(3,4)=(68).

Hence, the tangent line should take the form

6𝑥+8𝑦=𝑑

for some 𝑑. To pass through 𝑃=(3,4), we need 𝑑=6⋅3+8⋅4, so the answer is

6𝑥+8𝑦=50.

□

Sample Question
Compute the tangent line to 𝑦=𝑥2+5 at the point (3,14).

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 𝑓(𝑥,𝑦)=𝑦−𝑥2 for the value 5. The gradient is then

∇𝑓=(−2𝑥1)⟹∇𝑓(3,14)=(−6,1).

Hence the tangent line should take the line −6𝑥+𝑦=𝑑 for some 𝑑. We need to pass through (3,14), so we take 𝑑=(−6)⋅3+14=−4 to get the answer

−6𝑥+𝑦=−4.

(Written in 18.01 form this would be 𝑦=6𝑥−4, which shouldn’t be a surprise, because we know the derivative of 𝑥2+5 at 𝑥=3 is 6.)□

§15.7 [RECAP] A recap of Part Echo on Multivariable Differentiation

Let’s summarize the last few sections.

§15.8 [EXER] Exercises

Exercise 15.1. Compute the equation of the tangent plane to the sphere 𝑥2+𝑦2+𝑧2=14 at the point (1,2,3).

Exercise 15.2. The level curve of a certain differentiable function 𝑓(𝑥,𝑦) for the value −7 turns out to be a circle of radius 2 centered at (0,0).

Exercise 15.3. For each part, either give an example of 𝑓:ℝ2→ℝ or show that none exist.

Exercise 15.4 (*). Let 𝑎, 𝑏, 𝑐, 𝑑 be nonzero real numbers and let

𝑓(𝑥,𝑦)=𝑎𝑒𝑥+𝑦+𝑏𝑒𝑥−𝑦.

Suppose the level curve of 𝑓 for the value 𝑐 is tangent to the line 𝑦=5𝑥 at the origin, and also passes through (0,𝑑). Compute 𝑑.