Unit 3, lesson 18

Directional Derivatives and the Gradient

How to measure the rate of change of a function in any direction, why the gradient gives every such rate for differentiable functions, and how it points uphill and stands normal to level sets.

  • Theory part 35
  • Practice part 36
  • Derivatives and Approximation

Standing on a hillside, you can walk north, east, or any direction in between, and the slope you feel depends on which way you face. Partial derivatives give the slope only due east and due north. The directional derivative gives it for every heading, and for a differentiable function a single vector, the gradient, holds all of those slopes at once.

Key terms

Unit direction

Definition A direction in Rn\mathbb{R}^n is given by a unit vector u\mathbf{u}, one with ∥u∥=1\lVert \mathbf{u} \rVert = 1. Any nonzero vector v\mathbf{v} determines the unit direction

u=v∥v∥.\mathbf{u} = \frac{\mathbf{v}}{\lVert \mathbf{v} \rVert}.

The zero vector gives no direction.

In plain words A direction should say which way to go, not how far. Dividing by the length removes the “how far” part.

Example The vector v=⟨3,4⟩\mathbf{v} = \langle 3, 4 \rangle has length 55, so its direction is u=⟨35,45⟩\mathbf{u} = \left\langle \frac{3}{5}, \frac{4}{5} \right\rangle. Using v\mathbf{v} itself in the formulas below would multiply every rate by 55.

Directional derivative

Definition Let ff be real valued on an open set U⊆RnU \subseteq \mathbb{R}^n, let a\mathbf{a} be in UU, and let u\mathbf{u} be a unit vector. The directional derivative of ff at a\mathbf{a} in the direction u\mathbf{u} is the one-variable limit

Duf(a)=lim⁡t→0f(a+tu)−f(a)t,D_{\mathbf{u}} f(\mathbf{a}) = \lim_{t \to 0} \frac{f(\mathbf{a} + t\mathbf{u}) - f(\mathbf{a})}{t},

when it exists. The partial derivatives are the special cases u=ei\mathbf{u} = \mathbf{e}_i, the standard basis vectors. Replacing u\mathbf{u} by −u-\mathbf{u} changes the sign of the limit.

In plain words Walk through a\mathbf{a} along a straight line at unit speed and measure how fast ff changes as you pass. On the graph this is the slope of the curve cut out by a vertical plane through a\mathbf{a} in the direction u\mathbf{u}.

Example For f(x,y)=x2yf(x, y) = x^2 y at (1,2)(1, 2) with u=⟨35,45⟩\mathbf{u} = \left\langle \frac{3}{5}, \frac{4}{5} \right\rangle, expanding gives

f(1+35t, 2+45t)=2+3.2t+1.68t2+0.288t3.\begin{aligned} &f\big(1 + \tfrac{3}{5}t, \ 2 + \tfrac{4}{5}t\big) \\[0.4em] &\quad = 2 + 3.2t + 1.68t^2 + 0.288t^3. \end{aligned}

Subtracting f(1,2)=2f(1, 2) = 2, dividing by t≠0t \ne 0 and letting t→0t \to 0 gives Duf(1,2)=3.2D_{\mathbf{u}} f(1, 2) = 3.2.

Gradient formula for differentiable functions

Definition If all first partial derivatives of ff exist at a\mathbf{a}, the gradient of ff at a\mathbf{a} is the vector

∇f(a)=⟨∂f∂x1(a),…,∂f∂xn(a)⟩.\nabla f(\mathbf{a}) = \left\langle \frac{\partial f}{\partial x_1}(\mathbf{a}), \dots, \frac{\partial f}{\partial x_n}(\mathbf{a}) \right\rangle.

If ff is differentiable at a\mathbf{a}, then the directional derivative exists for every unit vector u\mathbf{u} and

Duf(a)=∇f(a)⋅u.D_{\mathbf{u}} f(\mathbf{a}) = \nabla f(\mathbf{a}) \cdot \mathbf{u}.

This follows from the chain rule applied to t↦f(a+tu)t \mapsto f(\mathbf{a} + t\mathbf{u}). Without differentiability the formula can fail even when every directional derivative exists.

In plain words For a differentiable function you never need the limit again. Compute the partial derivatives once, and every directional rate is a dot product.

Example The polynomial f(x,y,z)=xy+z2f(x, y, z) = xy + z^2 is differentiable everywhere. At (1,2,−1)(1, 2, -1) its gradient is ⟨y,x,2z⟩=⟨2,1,−2⟩\langle y, x, 2z \rangle = \langle 2, 1, -2 \rangle. Toward v=⟨2,−1,2⟩\mathbf{v} = \langle 2, -1, 2 \rangle, of length 33, the unit direction is 13⟨2,−1,2⟩\frac{1}{3}\langle 2, -1, 2 \rangle, and

Duf=4−1−43=−13.D_{\mathbf{u}} f = \frac{4 - 1 - 4}{3} = -\frac{1}{3}.

In the first example the same formula gives ⟨4,1⟩⋅⟨35,45⟩=3.2\langle 4, 1 \rangle \cdot \left\langle \frac{3}{5}, \frac{4}{5} \right\rangle = 3.2, matching the limit.

Steepest ascent

Definition Let ff be differentiable at a\mathbf{a} with ∇f(a)≠0\nabla f(\mathbf{a}) \ne \mathbf{0}. If θ\theta is the angle between u\mathbf{u} and ∇f(a)\nabla f(\mathbf{a}), then

Duf(a)=∥∇f(a)∥cos⁡θ.D_{\mathbf{u}} f(\mathbf{a}) = \lVert \nabla f(\mathbf{a}) \rVert \cos\theta.

So the largest directional derivative is ∥∇f(a)∥\lVert \nabla f(\mathbf{a}) \rVert, reached only for u=∇f(a)/∥∇f(a)∥\mathbf{u} = \nabla f(\mathbf{a}) / \lVert \nabla f(\mathbf{a}) \rVert, the smallest is −∥∇f(a)∥-\lVert \nabla f(\mathbf{a}) \rVert, reached only in the opposite direction, and the rate is 00 exactly for directions perpendicular to the gradient.

In plain words The gradient points in the input space toward the steepest climb, and its length is how steep that climb is. Walking against it is the steepest descent.

Example On the hill h(x,y)=100−x2−2y2h(x, y) = 100 - x^2 - 2y^2 at (3,1)(3, 1) the gradient is ⟨−2x,−4y⟩=⟨−6,−4⟩\langle -2x, -4y \rangle = \langle -6, -4 \rangle, of length 52=213≈7.21\sqrt{52} = 2\sqrt{13} \approx 7.21. The steepest way up is the direction

u=⟨−3,−2⟩13,\mathbf{u} = \frac{\langle -3, -2 \rangle}{\sqrt{13}},

with slope about 7.217.21. Moving along ±⟨2,−3⟩/13\pm\langle 2, -3 \rangle / \sqrt{13} keeps the height unchanged to first order.

Gradient as a level-set normal

Definition Let ff be real valued on an open set U⊆RnU \subseteq \mathbb{R}^n, let cc be a constant, and let a\mathbf{a} lie on the level set f(x)=cf(\mathbf{x}) = c. Suppose ff is differentiable at a\mathbf{a}. If r(t)\mathbf{r}(t), defined on an open interval around 00, is a curve in the level set, differentiable at t=0t = 0 with r(0)=a\mathbf{r}(0) = \mathbf{a}, then differentiating f(r(t))=cf(\mathbf{r}(t)) = c gives

∇f(a)⋅r′(0)=0.\nabla f(\mathbf{a}) \cdot \mathbf{r}'(0) = 0.

If moreover ff is C1C^1 on UU and ∇f(a)≠0\nabla f(\mathbf{a}) \ne \mathbf{0}, then by the implicit function theorem the level set near a\mathbf{a} is a smooth curve (n=2n = 2) or surface (n=3n = 3), namely the graph of a C1C^1 function of n−1n - 1 of the variables. Then ∇f(a)\nabla f(\mathbf{a}) is a normal vector to it, and its tangent line or tangent plane at a\mathbf{a} is ∇f(a)⋅(x−a)=0\nabla f(\mathbf{a}) \cdot (\mathbf{x} - \mathbf{a}) = 0.

In plain words Moving along a level set keeps ff constant, so the rate of change is zero, and that forces the gradient to be perpendicular to the level set. Wherever the gradient is not zero, the steepest way uphill is at right angles to the contour lines.

Example The ellipsoid x2+2y2+3z2=6x^2 + 2y^2 + 3z^2 = 6 is a level set of a polynomial and contains (1,1,1)(1, 1, 1). The gradient there is ⟨2x,4y,6z⟩=⟨2,4,6⟩\langle 2x, 4y, 6z \rangle = \langle 2, 4, 6 \rangle, which is not zero. The tangent plane is ⟨2,4,6⟩⋅⟨x−1,y−1,z−1⟩=0\langle 2, 4, 6 \rangle \cdot \langle x - 1, y - 1, z - 1 \rangle = 0. Dividing by 22 gives

(x−1)+2(y−1)+3(z−1)=0,(x - 1) + 2(y - 1) + 3(z - 1) = 0,

which simplifies to x+2y+3z=6x + 2y + 3z = 6.

Gradient zero

Definition A point a\mathbf{a} where ff is differentiable and ∇f(a)=0\nabla f(\mathbf{a}) = \mathbf{0} is a critical point. There every directional derivative is 00, there is no direction of steepest ascent, and the level set through a\mathbf{a} need not be a smooth curve or surface, so the gradient gives no normal vector there.

In plain words A zero gradient means the surface is flat to first order in every direction. It could be a peak, a pit, or a saddle, and the contour through it can look strange.

Example The function f(x,y)=x2−y2f(x, y) = x^2 - y^2 has ∇f=⟨2x,−2y⟩\nabla f = \langle 2x, -2y \rangle, which is 0\mathbf{0} at the origin. The level set f=0f = 0 is the pair of lines y=xy = x and y=−xy = -x, which cross at the origin, so no single normal line exists there. For g(x,y)=x2+y2g(x, y) = x^2 + y^2 the level set g=0g = 0 is only the point (0,0)(0, 0).

Directional derivatives alone are not enough

Definition A function can have a directional derivative at a\mathbf{a} in every unit direction without being differentiable at a\mathbf{a}, and even without being continuous there. In that case the formula Duf(a)=∇f(a)⋅uD_{\mathbf{u}} f(\mathbf{a}) = \nabla f(\mathbf{a}) \cdot \mathbf{u} may fail. Differentiability must be checked separately, for example through partial derivatives that are continuous on an open set containing a\mathbf{a}.

In plain words Directional derivatives test only straight lines through the point. A function can behave nicely on every line and still jump along a curve.

Example Let f(x,y)=x2yx4+y2f(x, y) = \frac{x^2 y}{x^4 + y^2} for (x,y)≠(0,0)(x, y) \ne (0, 0) and f(0,0)=0f(0, 0) = 0. For a unit vector u=⟨a,b⟩\mathbf{u} = \langle a, b \rangle and t≠0t \ne 0,

f(ta,tb)t=a2bt2a4+b2,\frac{f(ta, tb)}{t} = \frac{a^2 b}{t^2 a^4 + b^2},

which tends to a2b\frac{a^2}{b} if b≠0b \ne 0 and is 00 if b=0b = 0. So every directional derivative exists. For u=⟨35,45⟩\mathbf{u} = \left\langle \frac{3}{5}, \frac{4}{5} \right\rangle it is 920\frac{9}{20}, while ∇f(0,0)=⟨0,0⟩\nabla f(0, 0) = \langle 0, 0 \rangle would predict 00. Along the parabola y=x2y = x^2 with x≠0x \ne 0 the function equals 12\frac{1}{2}, so it is not continuous at the origin and not differentiable there.

Check yourself

  1. Find the rate of change of f(x,y)=xeyf(x, y) = x e^y at (2,0)(2, 0) in the direction of v=⟨1,−1⟩\mathbf{v} = \langle 1, -1 \rangle.
Show the answer

The function has continuous partial derivatives, so it is differentiable. The gradient is ⟨ey,xey⟩=⟨1,2⟩\langle e^y, x e^y \rangle = \langle 1, 2 \rangle at (2,0)(2, 0), and the unit direction is ⟨1,−1⟩/2\langle 1, -1 \rangle / \sqrt{2}. The rate is

Duf(2,0)=1−22=−22.D_{\mathbf{u}} f(2, 0) = \frac{1 - 2}{\sqrt{2}} = -\frac{\sqrt{2}}{2}.
  1. In which unit direction does f(x,y)=x2+xyf(x, y) = x^2 + xy decrease fastest at (1,2)(1, 2), and at what rate?
Show the answer

The gradient is ⟨2x+y,x⟩=⟨4,1⟩\langle 2x + y, x \rangle = \langle 4, 1 \rangle, of length 17\sqrt{17}. The fastest decrease is in the direction −⟨4,1⟩/17-\langle 4, 1 \rangle / \sqrt{17}, at the rate −17≈−4.12-\sqrt{17} \approx -4.12.

  1. Find the tangent plane to the surface xyz=6xyz = 6 at (1,2,3)(1, 2, 3).
Show the answer

With F(x,y,z)=xyzF(x, y, z) = xyz the gradient is ⟨yz,xz,xy⟩=⟨6,3,2⟩\langle yz, xz, xy \rangle = \langle 6, 3, 2 \rangle at (1,2,3)(1, 2, 3), which is not zero. The tangent plane is 6(x−1)+3(y−2)+2(z−3)=06(x - 1) + 3(y - 2) + 2(z - 3) = 0, or 6x+3y+2z=186x + 3y + 2z = 18.

How this lesson connects

It also uses Partial derivatives and differentiability, Dot product and unit vectors and Chain rule.