Gradient and Directional Derivatives: Formula and Examples
Learn what the gradient vector is, how to compute a directional derivative with a unit vector, and why the gradient points in the direction of steepest ascent.
By the PracticeCalculus team Published 7 min read
On this page
- What is the gradient?
- The directional derivative formula
- Worked example 1: a direction given by a vector
- Worked example 2: a direction given by an angle
- Why the gradient points in the direction of steepest ascent
- Worked example 3: steepest ascent on a hill
- Worked example 4: three variables
- Common mistakes with gradients and directional derivatives
- Quick reference: gradient facts
- Practice until it feels like reading a map
You are standing on a hillside described by z = f(x, y). Partial derivatives tell you how steep the ground is if you walk due east or due north. But what if you walk northeast? And which way should you walk to climb as fast as possible? The gradient answers both questions with a single vector.
This guide explains the gradient, the directional derivative formula, why the gradient points uphill, and works through four complete examples.
What is the gradient?
The gradient of a function is the vector of its first partial derivatives. For two and three variables:
The symbol \nabla is read "del" or "grad." The gradient is a vector-valued function: at each point it gives you a vector that lives in the input plane (the xy-plane), not on the surface.
For example, if f(x, y) = x^2 + 3xy, then f_x = 2x + 3y and f_y = 3x, so
The directional derivative formula
The directional derivative of f at a point in the direction of a unit vector \mathbf{u} is the rate of change of f per unit distance traveled in that direction:
If \mathbf{u} = \langle u_1, u_2 \rangle, that is D_{\mathbf{u}} f = f_x u_1 + f_y u_2.
Partial derivatives are special cases. With \mathbf{u} = \langle 1, 0 \rangle you get D_{\mathbf{u}} f = f_x, and with \mathbf{u} = \langle 0, 1 \rangle you get f_y.
How to compute a directional derivative:
- Find the gradient \nabla f and evaluate it at the point.
- Turn the direction into a unit vector: divide by its length, or use \mathbf{u} = \langle \cos\theta, \sin\theta \rangle for an angle \theta.
- Take the dot product of the two.
Tip: step 2 is the one people skip. If the problem says "in the direction of \langle 3, 4 \rangle," you must use \langle \frac{3}{5}, \frac{4}{5} \rangle.
Worked example 1: a direction given by a vector
Find the directional derivative of f(x, y) = x^2 + 3xy at (1, 2) in the direction of \mathbf{v} = \langle 3, 4 \rangle.
Step 1. Gradient at the point. From above, \nabla f(1, 2) = \langle 2(1) + 3(2),\ 3(1) \rangle = \langle 8, 3 \rangle.
Step 2. Unit vector. |\mathbf{v}| = \sqrt{9 + 16} = 5, so \mathbf{u} = \left\langle \frac{3}{5}, \frac{4}{5} \right\rangle.
Step 3. Dot product.
Moving from (1, 2) toward \langle 3, 4 \rangle, f increases at about 7.2 units per unit of distance.
Worked example 2: a direction given by an angle
Find the directional derivative of f(x, y) = x e^y at (2, 0) in the direction making an angle \theta = \frac{\pi}{3} with the positive x-axis.
Step 1. Gradient. f_x = e^y and f_y = x e^y, so \nabla f(2, 0) = \langle e^0, 2e^0 \rangle = \langle 1, 2 \rangle.
Step 2. Unit vector from the angle.
This is already length 1, so no dividing is needed.
Step 3. Dot product.
Why the gradient points in the direction of steepest ascent
Use the geometric form of the dot product. If \theta is the angle between \nabla f and the unit vector \mathbf{u}, then
Since \cos\theta ranges from -1 to 1, three facts drop out:
- Steepest ascent: the largest value, |\nabla f|, happens at \theta = 0, when \mathbf{u} points along \nabla f.
- Steepest descent: the smallest value, -|\nabla f|, happens at \theta = \pi, when \mathbf{u} points along -\nabla f.
- No change: D_{\mathbf{u}} f = 0 when \theta = \frac{\pi}{2}, that is in directions perpendicular to \nabla f. Those are the directions along the level curve.
That last fact means the gradient is always perpendicular to the level curves of f. On a topographic map, the gradient points straight uphill, crossing the contour lines at right angles.
Worked example 3: steepest ascent on a hill
The height of a hill is f(x, y) = 100 - x^2 - 2y^2. A hiker stands at (3, 1).
Step 1. Gradient. \nabla f = \langle -2x, -4y \rangle, so \nabla f(3, 1) = \langle -6, -4 \rangle.
Step 2. Direction of steepest ascent. It is \langle -6, -4 \rangle, or as a unit vector,
It points uphill, roughly back toward the summit at the origin. (Not exactly at it: the hill is steeper in the y direction, so the gradient heads toward the x-axis more sharply than a straight line to the top would.)
Step 3. Steepest rate. |\nabla f(3, 1)| = \sqrt{52} = 2\sqrt{13} \approx 7.21.
Step 4. Steepest descent. The opposite direction, \langle 6, 4 \rangle, with rate -2\sqrt{13}.
Step 5. Level directions. Perpendicular to \langle -3, -2 \rangle are \langle 2, -3 \rangle and \langle -2, 3 \rangle (check: -6 + 6 = 0). Walking either way keeps the hiker at the same height, at least instantaneously.
The point (3, 1) lies on the level curve x^2 + 2y^2 = 11 (where the height is 89). The graph shows that curve, the gradient direction, and the tangent direction. They meet at a right angle.
Worked example 4: three variables
Find the directional derivative of f(x, y, z) = xyz at P(1, 2, 3) in the direction of the point Q(2, 4, 5).
Step 1. Gradient. \nabla f = \langle yz, xz, xy \rangle, so \nabla f(1, 2, 3) = \langle 6, 3, 2 \rangle.
Step 2. Direction vector. \overrightarrow{PQ} = \langle 1, 2, 2 \rangle, with length \sqrt{1 + 4 + 4} = 3. So \mathbf{u} = \left\langle \frac{1}{3}, \frac{2}{3}, \frac{2}{3} \right\rangle.
Step 3. Dot product.
The maximum possible rate at P is |\nabla f| = \sqrt{36 + 9 + 4} = 7, so this direction gets you \frac{16}{3} \approx 5.33 out of a possible 7.
In three variables the gradient is perpendicular to the level surface f(x, y, z) = c, which is why \nabla f is used as the normal vector for tangent planes.
Common mistakes with gradients and directional derivatives
- Not normalizing the direction. Using \langle 3, 4 \rangle instead of \langle \frac{3}{5}, \frac{4}{5} \rangle makes the answer 5 times too large.
- Using the point instead of the direction. In example 4, the direction is Q - P, not Q itself.
- Forgetting to evaluate the gradient at the point. The answer to a directional derivative question is a number, not a formula.
- Thinking the gradient lives on the surface. For z = f(x, y), \nabla f is a 2D vector in the xy-plane telling you which way to walk.
- Mixing up the maximum rate and its direction. The direction is \nabla f (or its unit vector). The rate is the number |\nabla f|.
Quick reference: gradient facts
| Question | Answer |
|---|---|
| Gradient of f(x, y) | \nabla f = \langle f_x, f_y \rangle |
| Rate of change in direction \mathbf{u} (\lvert\mathbf{u}\rvert = 1) | D_{\mathbf{u}} f = \nabla f \cdot \mathbf{u} |
| Unit vector at angle \theta | \langle \cos\theta, \sin\theta \rangle |
| Direction of fastest increase | \nabla f |
| Fastest rate of increase | \lvert \nabla f \rvert |
| Direction of fastest decrease | -\nabla f |
| Directions of zero change | perpendicular to \nabla f (along the level curve) |
Practice until it feels like reading a map
Once you can compute a gradient in your head and remember to normalize the direction, these problems become quick points on an exam. They are also the key idea behind Lagrange multipliers. The interactive lesson below gives you surfaces, points and directions to work with, and checks every step instantly so you catch a missing unit vector the first time.
Practice it now
The gradient and directional derivatives
Solve real problems on this topic with instant feedback, hints and a step-by-step walkthrough for every answer. Free, no sign-up needed to start.
Practice this lesson