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 change if I move this way?". This lesson proves the formula , 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 be a unit vector. The directional derivative of at in the direction is
if the limit exists. With it is ; with it is . Geometrically it is the slope of the curve cut from the surface by the vertical plane through in direction .
Theorem. If is differentiable at , then exists for every unit and
Proof. Put . By definition . By the chain rule with , : . Set .
The gradient is the vector of partials, (or in three variables). It is a vector field: one vector at each point.
Note: If a direction is given by a non-unit vector , use . Using itself scales the answer by .
Example: at , direction of . Then , , and .
Maximising the directional derivative
By the dot-product formula, , where is the angle between and .
Theorem. At a point where , the maximum value of over unit vectors is , attained when points in the direction of . The minimum is , in the direction of , and exactly for perpendicular to .
So points in the direction of steepest ascent, and its length is the steepest slope.
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 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 be any curve on the level surface through , with . Then for all , and differentiating with the chain rule gives
So is perpendicular to the tangent vector of every curve on the surface through . That makes it a normal vector of the surface, and gives:
In two variables, is perpendicular to the level curves . On a contour map, the gradient arrows cross the contours at right angles, pointing uphill, and are longest where contours are densest.
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: for unit . Example: the tangent plane to the ellipsoid at : , so
In software
- Gradient descent steps along because, among all directions of the same length, it decreases 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 , usually with central differences of the distance function, because the gradient is perpendicular to the level surface .
- Image gradients. Edge detectors (Sobel, Canny) estimate of image brightness; edges run perpendicular to it, and measures edge strength.
- Adversarial examples. The fast gradient sign method perturbs an input by : a step in a direction with large directional derivative of the loss.
Exercises
Exercise 1: A directional derivative
Find at for , in the direction making angle with the positive -axis.
Answer
.
Solution
Exercise 2: Steepest direction
For , find the direction of fastest increase at and the maximum rate.
Answer
Direction ; maximum rate .
Solution
Exercise 3: Tangent plane and normal line
Find the tangent plane and normal line to at .
Answer
Plane ; line .
Solution
Exercise 4: Level-direction (code)
For , find both unit directions at in which does not change, and check numerically that in them.
Answer
, so .
Solution
, . A direction perpendicular to is .
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
. What is the largest directional derivative of at ?
- 5
- 3
- 9
- 1
Why must be a unit vector in ?
- Otherwise the dot product is undefined
- Otherwise the rate is multiplied by the length of the vector
- Because the gradient is a unit vector
- It need not be
On a contour map of , the gradient at a point is…
- tangent to the contour
- perpendicular to the contour, pointing uphill
- perpendicular to the contour, pointing downhill
- always parallel to the -axis