TExcelTAD
Home › Calculus in Depth › Directional Derivatives and the Gradient Vector

Directional Derivatives and the Gradient Vector

Partial derivatives give rates of change along the coordinate axes only. The directional derivative gives the rate in any direction, and the gradient vector packages all of them: one dot product with the gradient answers "how fast does ff change if I move this way?". This lesson proves the formula Duf=∇f⋅uD_{\mathbf{u}} f = \nabla f \cdot \mathbf{u}, the steepest-ascent property, and the fact that the gradient is perpendicular to level curves and surfaces.

The survey version is Gradient and Directional Derivative.

The directional derivative

Let u=⟨a,b⟩\mathbf{u} = \langle a, b \rangle be a unit vector. The directional derivative of ff at (x0,y0)(x_0, y_0) in the direction u\mathbf{u} is

Duf(x0,y0)=lim⁡h→0f(x0+ha,y0+hb)−f(x0,y0)hD_{\mathbf{u}} f(x_0, y_0) = \lim_{h \to 0} \frac{f(x_0 + ha, y_0 + hb) - f(x_0, y_0)}{h}

if the limit exists. With u=⟨1,0⟩\mathbf{u} = \langle 1, 0 \rangle it is fxf_x; with u=⟨0,1⟩\mathbf{u} = \langle 0, 1 \rangle it is fyf_y. Geometrically it is the slope of the curve cut from the surface z=f(x,y)z = f(x, y) by the vertical plane through (x0,y0)(x_0, y_0) in direction u\mathbf{u}.

Theorem. If ff is differentiable at (x0,y0)(x_0, y_0), then DufD_{\mathbf{u}} f exists for every unit u\mathbf{u} and

Duf(x0,y0)=fx(x0,y0) a+fy(x0,y0) b=∇f(x0,y0)⋅uD_{\mathbf{u}} f(x_0, y_0) = f_x(x_0, y_0)\,a + f_y(x_0, y_0)\,b = \nabla f(x_0, y_0) \cdot \mathbf{u}

Proof. Put g(h)=f(x0+ha,y0+hb)g(h) = f(x_0 + ha, y_0 + hb). By definition Duf=g′(0)D_{\mathbf{u}} f = g'(0). By the chain rule with x=x0+hax = x_0 + ha, y=y0+hby = y_0 + hb: g′(h)=fxa+fybg'(h) = f_x a + f_y b. Set h=0h = 0.

The gradient is the vector of partials, ∇f=⟨fx,fy⟩\nabla f = \langle f_x, f_y \rangle (or ⟨fx,fy,fz⟩\langle f_x, f_y, f_z \rangle in three variables). It is a vector field: one vector at each point.

Note: If a direction is given by a non-unit vector v\mathbf{v}, use u=v/∥v∥\mathbf{u} = \mathbf{v}/\lVert \mathbf{v} \rVert. Using v\mathbf{v} itself scales the answer by ∥v∥\lVert \mathbf{v} \rVert.

Example: f(x,y)=x2y−3y3f(x, y) = x^2y - 3y^3 at (2,1)(2, 1), direction of v=⟨3,−4⟩\mathbf{v} = \langle 3, -4 \rangle. Then ∇f=⟨2xy,x2−9y2⟩=⟨4,−5⟩\nabla f = \langle 2xy, x^2 - 9y^2 \rangle = \langle 4, -5 \rangle, u=⟨0.6,−0.8⟩\mathbf{u} = \langle 0.6, -0.8 \rangle, and Duf=2.4+4=6.4D_{\mathbf{u}} f = 2.4 + 4 = 6.4.

Maximising the directional derivative

By the dot-product formula, Duf=∥∇f∥∥u∥cos⁡θ=∥∇f∥cos⁡θD_{\mathbf{u}} f = \lVert \nabla f \rVert \lVert \mathbf{u} \rVert \cos\theta = \lVert \nabla f \rVert \cos\theta, where θ\theta is the angle between ∇f\nabla f and u\mathbf{u}.

Theorem. At a point where ∇f≠0\nabla f \ne \mathbf{0}, the maximum value of DufD_{\mathbf{u}} f over unit vectors u\mathbf{u} is ∥∇f∥\lVert \nabla f \rVert, attained when u\mathbf{u} points in the direction of ∇f\nabla f. The minimum is −∥∇f∥-\lVert \nabla f \rVert, in the direction of −∇f-\nabla f, and Duf=0D_{\mathbf{u}} f = 0 exactly for u\mathbf{u} perpendicular to ∇f\nabla f.

So ∇f\nabla f points in the direction of steepest ascent, and its length is the steepest slope.

python · try it yourself
import numpy as np

f = lambda p: p[0]**2 * p[1] - 3 * p[1]**3
grad = lambda p: np.array([2 * p[0] * p[1], p[0]**2 - 9 * p[1]**2])
p = np.array([2.0, 1.0])
g = grad(p)

angles = np.linspace(0, 2 * np.pi, 3600, endpoint=False)
U = np.stack([np.cos(angles), np.sin(angles)], axis=1)
D = U @ g                                    # D_u f for 3600 directions
k = int(np.argmax(D))
print("gradient:", g, " |grad| =", round(float(np.linalg.norm(g)), 4))
print("best direction found:", np.round(U[k], 4), " rate", round(float(D[k]), 4))
print("grad / |grad|       :", np.round(g / np.linalg.norm(g), 4))

h = 1e-6
u = np.array([0.6, -0.8])
print("D_u f toward <3,-4>: formula", round(float(g @ u), 4),
      " numeric", round((f(p + h * u) - f(p - h * u)) / (2 * h), 4))
gradient: [ 4. -5.]  |grad| = 6.4031
best direction found: [ 0.6252 -0.7804]  rate 6.4031
grad / |grad|       : [ 0.6247 -0.7809]
D_u f toward <3,-4>: formula 6.4  numeric 6.4

The direction ⟨3,−4⟩\langle 3, -4 \rangle happens to lie very close to the gradient direction, so its rate 6.4 is almost the maximum 6.4031.

The gradient is normal to level sets

Let r(t)\mathbf{r}(t) be any curve on the level surface F(x,y,z)=kF(x, y, z) = k through PP, with r(t0)=P\mathbf{r}(t_0) = P. Then F(r(t))=kF(\mathbf{r}(t)) = k for all tt, and differentiating with the chain rule gives

∇F(P)⋅r′(t0)=0\nabla F(P) \cdot \mathbf{r}'(t_0) = 0

So ∇F(P)\nabla F(P) is perpendicular to the tangent vector of every curve on the surface through PP. That makes it a normal vector of the surface, and gives:

tangent plane:Fx(P)(x−x0)+Fy(P)(y−y0)+Fz(P)(z−z0)=0normal line:(x,y,z)=P+t ∇F(P)\begin{aligned} &\text{tangent plane:} && F_x(P)(x - x_0) + F_y(P)(y - y_0) + F_z(P)(z - z_0) = 0 \\ &\text{normal line:} && (x, y, z) = P + t\,\nabla F(P) \end{aligned}

In two variables, ∇f\nabla f is perpendicular to the level curves f(x,y)=kf(x, y) = k. On a contour map, the gradient arrows cross the contours at right angles, pointing uphill, and are longest where contours are densest.

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

f = lambda x, y: np.sin(x) * np.cos(y) + 0.2 * x
g = np.linspace(-3, 3, 200)
X, Y = np.meshgrid(g, g)

q = np.linspace(-3, 3, 13)
QX, QY = np.meshgrid(q, q)
GX, GY = np.cos(QX) * np.cos(QY) + 0.2, -np.sin(QX) * np.sin(QY)

plt.figure(figsize=(5.5, 5))
plt.contour(X, Y, f(X, Y), levels=14, cmap="viridis")
plt.quiver(QX, QY, GX, GY, color="crimson", angles="xy")
plt.gca().set_aspect("equal")
plt.title("∇f crosses the level curves at right angles")

# check perpendicularity numerically at one point: tangent of contour = (-f_y, f_x)
x0, y0 = 1.0, 0.5
gx, gy = np.cos(x0) * np.cos(y0) + 0.2, -np.sin(x0) * np.sin(y0)
print("grad · contour tangent =", round(float(gx * (-gy) + gy * gx), 12))
grad · contour tangent = 0.0

Three variables

Everything carries over: Duf=∇f⋅u=fxa+fyb+fzcD_{\mathbf{u}} f = \nabla f \cdot \mathbf{u} = f_x a + f_y b + f_z c for unit u=⟨a,b,c⟩\mathbf{u} = \langle a, b, c \rangle. Example: the tangent plane to the ellipsoid x2/4+y2+z2/9=3x^2/4 + y^2 + z^2/9 = 3 at (2,1,3)(2, 1, 3): ∇F=⟨x/2,2y,2z/9⟩=⟨1,2,2/3⟩\nabla F = \langle x/2, 2y, 2z/9 \rangle = \langle 1, 2, 2/3 \rangle, so

(x−2)+2(y−1)+23(z−3)=0  ⟺  3x+6y+2z=18(x - 2) + 2(y - 1) + \tfrac{2}{3}(z - 3) = 0 \qquad \iff \qquad 3x + 6y + 2z = 18

In software

  • Gradient descent steps along −∇f-\nabla f because, among all directions of the same length, it decreases ff fastest to first order. In ML the "direction" lives in a space with millions of dimensions, but the theorem is the same; see Gradient Descent.
  • Normals of implicit surfaces. Ray marchers and SDF modelling tools compute surface normals as ∇F/∥∇F∥\nabla F/\lVert \nabla F \rVert, usually with central differences of the distance function, because the gradient is perpendicular to the level surface F=0F = 0.
  • Image gradients. Edge detectors (Sobel, Canny) estimate ∇I\nabla I of image brightness; edges run perpendicular to it, and ∥∇I∥\lVert \nabla I \rVert measures edge strength.
  • Adversarial examples. The fast gradient sign method perturbs an input by ε⋅sign⁡(∇xL)\varepsilon \cdot \operatorname{sign}(\nabla_x L): a step in a direction with large directional derivative of the loss.

Exercises

Exercise 1: A directional derivative

Find DufD_{\mathbf{u}} f at (1,2)(1, 2) for f(x,y)=xey−2+y2f(x, y) = x e^{y - 2} + y^2, in the direction making angle π/3\pi/3 with the positive xx-axis.

Answer

1/2+53/2≈4.8301/2 + 5\sqrt{3}/2 \approx 4.830.

Solution
∇f=⟨ey−2,xey−2+2y⟩=⟨1,1+4⟩=⟨1,5⟩at (1,2)u=⟨cos⁡π/3,sin⁡π/3⟩=⟨1/2,3/2⟩Duf=1/2+53/2≈0.5+4.330=4.830\begin{aligned} \nabla f &= \langle e^{y-2}, x e^{y-2} + 2y \rangle = \langle 1, 1 + 4 \rangle = \langle 1, 5 \rangle \quad \text{at } (1, 2) \\ \mathbf{u} &= \langle \cos \pi/3, \sin \pi/3 \rangle = \langle 1/2, \sqrt{3}/2 \rangle \\ D_{\mathbf{u}} f &= 1/2 + 5\sqrt{3}/2 \approx 0.5 + 4.330 = 4.830 \end{aligned}

Exercise 2: Steepest direction

For T(x,y,z)=100−x2−2y2−3z2T(x, y, z) = 100 - x^2 - 2y^2 - 3z^2, find the direction of fastest increase at (1,1,1)(1, 1, 1) and the maximum rate.

Answer

Direction ⟨−2,−4,−6⟩/56=⟨−1,−2,−3⟩/14\langle -2, -4, -6 \rangle/\sqrt{56} = \langle -1, -2, -3 \rangle/\sqrt{14}; maximum rate 56=214≈7.483\sqrt{56} = 2\sqrt{14} \approx 7.483.

Solution
∇T=⟨−2x,−4y,−6z⟩=⟨−2,−4,−6⟩∥∇T∥=4+16+36=56≈7.483\begin{aligned} \nabla T &= \langle -2x, -4y, -6z \rangle = \langle -2, -4, -6 \rangle \\ \lVert \nabla T \rVert &= \sqrt{4 + 16 + 36} = \sqrt{56} \approx 7.483 \end{aligned}

Exercise 3: Tangent plane and normal line

Find the tangent plane and normal line to xyz=6xyz = 6 at (1,2,3)(1, 2, 3).

Answer

Plane 6x+3y+2z=186x + 3y + 2z = 18; line (1,2,3)+t⟨6,3,2⟩(1, 2, 3) + t\langle 6, 3, 2 \rangle.

Solution
∇F=⟨yz,xz,xy⟩=⟨6,3,2⟩6(x−1)+3(y−2)+2(z−3)=0  ⟺  6x+3y+2z=18\begin{aligned} &\nabla F = \langle yz, xz, xy \rangle = \langle 6, 3, 2 \rangle \\ &6(x - 1) + 3(y - 2) + 2(z - 3) = 0 \iff 6x + 3y + 2z = 18 \end{aligned}

Exercise 4: Level-direction (code)

For f(x,y)=x3−3xy+y2f(x, y) = x^3 - 3xy + y^2, find both unit directions at (2,1)(2, 1) in which ff does not change, and check numerically that Duf≈0D_{\mathbf{u}} f \approx 0 in them.

Answer

∇f=⟨9,−4⟩\nabla f = \langle 9, -4 \rangle, so u=±⟨4,9⟩/97≈±⟨0.4061,0.9138⟩\mathbf{u} = \pm\langle 4, 9 \rangle/\sqrt{97} \approx \pm\langle 0.4061, 0.9138 \rangle.

Solution

fx=3x2−3y=9f_x = 3x^2 - 3y = 9, fy=−3x+2y=−4f_y = -3x + 2y = -4. A direction perpendicular to ⟨9,−4⟩\langle 9, -4 \rangle is ⟨4,9⟩\langle 4, 9 \rangle.

python · try it yourself
import numpy as np
f = lambda p: p[0]**3 - 3 * p[0] * p[1] + p[1]**2
p = np.array([2.0, 1.0])
for s in (1, -1):
    u = s * np.array([4.0, 9.0]) / np.sqrt(97)
    h = 1e-6
    print(np.round(u, 4), round((f(p + h * u) - f(p - h * u)) / (2 * h), 8) + 0.0)  # + 0.0 turns -0.0 into 0.0
[0.4061 0.9138] 0.0
[-0.4061 -0.9138] 0.0

Test yourself

∇f(P)=⟨2,−1,2⟩\nabla f(P) = \langle 2, -1, 2 \rangle. What is the largest directional derivative of ff at PP?

  1. 5
  2. 3
  3. 9
  4. 1

Why must u\mathbf{u} be a unit vector in Duf=∇f⋅uD_{\mathbf{u}} f = \nabla f \cdot \mathbf{u}?

  1. Otherwise the dot product is undefined
  2. Otherwise the rate is multiplied by the length of the vector
  3. Because the gradient is a unit vector
  4. It need not be

On a contour map of ff, the gradient at a point is…

  1. tangent to the contour
  2. perpendicular to the contour, pointing uphill
  3. perpendicular to the contour, pointing downhill
  4. always parallel to the xx-axis