---
title: Directional Derivatives and the Gradient
module: Partial Derivatives
moduleNumber: 11
lessonNumber: 4
order: 1104
summary: >
  The partial derivatives measure slope along the two axes; the directional
  derivative measures slope along any chosen direction, and equals the gradient
  dotted with a unit vector. The gradient points in the direction of steepest
  increase, its length is the greatest rate, and it stands perpendicular to level
  curves and surfaces, which fixes the tangent plane to a level surface.
topics: [Partial Derivatives]
draft: false
sources:
  - book: Stewart
    ref: "Ch. 14 — Partial Derivatives; §14.6 Directional Derivatives and the Gradient Vector"
---

The partials $f_x$ and $f_y$ report the rate of change of $f$ along the $x$- and
$y$-axes. Standing on a hillside, those are the slopes due east and due north.
The rate of change in an arbitrary compass direction is the **directional
derivative**, and collecting the two partials into one vector (the **gradient**)
gives every directional slope at once, including the steepest.

## The directional derivative

Let $\hat u = \langle a, b \rangle$ be a unit vector. Stepping from
$(x_0, y_0)$ a distance $h$ in the direction $\hat u$ lands at
$(x_0 + ha, y_0 + hb)$. The rate of change of $f$ per unit distance along that
direction is the limit of the difference quotient.

> **Definition (Directional derivative).** The directional derivative of $f$ at
> $(x_0, y_0)$ in the direction of a unit vector $\hat u = \langle a, b \rangle$
> is
> $$
> D_{\hat u} f(x_0, y_0) = \lim_{h \to 0}
> \frac{f(x_0 + ha, y_0 + hb) - f(x_0, y_0)}{h},
> $$
> when the limit exists.

The two partials are the special cases along the axes: $\hat u = \langle 1, 0 \rangle$
gives $D_{\hat u} f = f_x$, and $\hat u = \langle 0, 1 \rangle$ gives
$D_{\hat u} f = f_y$. Geometrically, $D_{\hat u} f$ is the slope of the
surface $z = f(x, y)$ in the vertical plane through $P$ carrying the direction
$\hat u$, the slope of the tangent to the curve where that plane meets the
surface.

$$
% caption: The directional derivative is the slope of the surface in the vertical
% plane through $P$ pointing along the unit vector $\mathbf{u}$.
\begin{tikzpicture}[scale=1.0, >=stealth, font=\small]
  \definecolor{acc}{HTML}{4A6FA5}
  % ground plane patch with point and direction
  \draw[black] (-2.4,-0.5) -- (2.4,-0.5) -- (3.4,0.4) -- (-1.4,0.4) -- cycle;
  \fill[black] (0,-0.05) circle (1.7pt);
  \node[anchor=north east, font=\footnotesize] at (-0.05,-0.1) {$P$};
  \draw[->, acc, very thick] (0,-0.05) -- (1.5,0.28);
  \node[acc, anchor=west, font=\footnotesize] at (1.5,0.22) {$\mathbf{u}$};
  % vertical plane cutting up (trace curve)
  \draw[black, fill=acc!6] (0,-0.05) -- (1.5,0.28) -- (1.5,2.5) -- (0,2.2) -- cycle;
  \draw[acc, thick] (0,1.15) .. controls (0.6,1.5) and (1.1,1.9) .. (1.5,2.35);
  % tangent showing slope
  \draw[acc, thick, dashed] (0.15,1.2) -- (1.35,2.05);
  \node[acc, font=\footnotesize, anchor=south east] at (0.9,1.55) {slope $=D_{\mathbf{u}}f$};
\end{tikzpicture}
$$

Computing that limit directly is unnecessary. For a differentiable function it
reduces to the partials.

> **Theorem (Directional derivative formula).** If $f$ is differentiable, then it
> has a directional derivative in every unit direction $\hat u = \langle a, b \rangle$,
> and
> $$
> D_{\hat u} f(x, y) = f_x(x, y)\,a + f_y(x, y)\,b.
> $$

The proof applies the [chain rule](/calculus/partial-derivatives/tangent-planes-and-the-chain-rule)
to $g(h) = f(x_0 + ha, y_0 + hb)$: $g'(0) = f_x a + f_y b$. Writing $\hat u$ by
its angle, $\hat u = \langle \cos\theta, \sin\theta \rangle$, the formula is
$D_{\hat u} f = f_x \cos\theta + f_y \sin\theta$.

> **Worked example.** Find $D_{\hat u} f$ for $f(x, y) = x^3 - 3xy + 4y^2$ at
> $(1, 2)$ in the direction $\theta = \tfrac{\pi}{6}$.
>
> The unit vector is $\hat u = \langle \tfrac{\sqrt{3}}{2}, \tfrac{1}{2} \rangle$,
> and $f_x = 3x^2 - 3y$, $f_y = -3x + 8y$, so
> $$
> D_{\hat u} f = (3x^2 - 3y)\tfrac{\sqrt{3}}{2} + (-3x + 8y)\tfrac{1}{2}.
> $$
> At $(1, 2)$ this is
> $$
> (3 - 6)\tfrac{\sqrt{3}}{2} + (-3 + 16)\tfrac{1}{2}
> = \frac{13 - 3\sqrt{3}}{2} \approx 3.9.
> $$

## The gradient vector

The formula $D_{\hat u} f = f_x a + f_y b$ is a dot product of $\hat u$
with the vector of partials. That vector has a name.

> **Definition (Gradient).** The gradient of $f$ is the vector field
> $$
> \nabla f(x, y) = \langle f_x(x, y),\, f_y(x, y) \rangle
> = \frac{\partial f}{\partial x}\hat\imath + \frac{\partial f}{\partial y}\hat\jmath.
> $$
> For three variables, $\nabla f = \langle f_x, f_y, f_z \rangle$.

With this, the directional derivative is a single dot product,

$$
D_{\hat u} f = \nabla f \cdot \hat u,
$$

the scalar projection of the gradient onto $\hat u$. Every slope in every
direction is read off the one vector $\nabla f$.

> **Worked example.** Compute $\nabla f$ for $f(x, y) = \sin x + e^{xy}$ and
> evaluate at $(0, 1)$.
>
> $$
> \nabla f = \langle \cos x + y e^{xy},\ x e^{xy} \rangle,
> \qquad
> \nabla f(0, 1) = \langle 1 + 1,\ 0 \rangle = \langle 2, 0 \rangle.
> $$

## Steepest ascent

Because $D_{\hat u} f = \nabla f \cdot \hat u = |\nabla f|\,|\hat u|\cos\phi
= |\nabla f|\cos\phi$, where $\phi$ is the angle between $\nabla f$ and
$\hat u$, the directional derivative is largest when $\cos\phi = 1$, that is
when $\hat u$ points along $\nabla f$.

> **Theorem (Maximal rate).** The maximum value of $D_{\hat u} f$ is
> $|\nabla f|$, attained when $\hat u$ has the direction of $\nabla f$. The
> minimum is $-|\nabla f|$, in the direction of $-\nabla f$; and
> $D_{\hat u} f = 0$ when $\hat u \perp \nabla f$.

Three facts, one for each value of $\cos\phi$:

- **Uphill fastest.** $f$ increases most rapidly in the direction of $\nabla f$,
  at rate $|\nabla f|$.
- **Downhill fastest.** $f$ decreases most rapidly in the direction of
  $-\nabla f$, at rate $|\nabla f|$.
- **Level.** $f$ is momentarily unchanging in the directions perpendicular to
  $\nabla f$, the directions along the level curve.

$$
% caption: The directional derivative $D_{\mathbf u}f=|\nabla f|\cos\phi$ is
% greatest along the gradient, zero across it, and least in the opposite
% direction; $\phi$ is the angle from $\mathbf u$ to $\nabla f$.
\begin{tikzpicture}[scale=1.0, >=stealth, font=\small]
  \definecolor{acc}{HTML}{4A6FA5}
  \definecolor{wrm}{HTML}{C0392B}
  \coordinate (O) at (0,0);
  \fill[black] (O) circle (1.6pt);
  \node[anchor=north east, font=\footnotesize] at (-0.05,-0.05) {$P$};
  \draw[->, acc, very thick] (O) -- (30:2.5);
  \node[acc, anchor=west, font=\footnotesize] at (30:2.55) {grad f: greatest rate};
  \draw[->, black, thick] (O) -- (120:1.9);
  \node[black, anchor=south, font=\footnotesize] at (120:2.0) {across: rate 0};
  \draw[->, wrm, thick] (O) -- (210:2.1);
  \node[wrm, anchor=east, font=\footnotesize] at (210:2.2) {opposite: least rate};
  \draw[->, black!70, thick] (O) -- (66:1.7);
  \node[black!70, anchor=south west, font=\footnotesize] at (66:1.62) {$\mathbf{u}$};
  \draw[black] (30:0.75) arc (30:66:0.75);
\end{tikzpicture}
$$

> **Worked example.** In which direction does $f(x, y) = x e^y$ increase fastest
> at $P = (2, 0)$, and how fast?
>
> The gradient is $\nabla f = \langle e^y, x e^y \rangle$, so
> $\nabla f(2, 0) = \langle 1, 2 \rangle$. The fastest increase at $P$ is in the
> direction $\langle 1, 2 \rangle$, at rate
> $$
> |\nabla f(2, 0)| = |\langle 1, 2 \rangle| = \sqrt{5}.
> $$

## Gradient perpendicular to level curves

The last bullet has a geometric consequence. Along a level curve $f(x, y) = k$ the value of $f$ is constant, so its
rate of change in the tangent direction of the curve is zero. But that rate is
$D_{\hat u} f = \nabla f \cdot \hat u$, so $\nabla f$ is orthogonal to the
curve's tangent: the gradient meets every level curve at a right angle, pointing
toward higher values.

$$
% caption: At each point the gradient is perpendicular to the level curve through
% that point and aims toward larger $k$; the closer the curves, the longer the
% gradient.
\begin{tikzpicture}[scale=1.0, >=stealth, font=\small]
  \definecolor{acc}{HTML}{4A6FA5}
  % nested level curves (a hill), off-centre
  \foreach \r in {0.55, 1.1, 1.75, 2.55} {
    \draw[black] (0.4,0) circle (\r);
  }
  \node[fill=white, inner sep=1pt, font=\footnotesize] at (0.4,0) {$k_3$};
  \node[fill=white, inner sep=1pt, font=\footnotesize] at (0.4,-1.1) {$k_2$};
  \node[fill=white, inner sep=1pt, font=\footnotesize] at (0.4,-2.55) {$k_1$};
  % gradient arrows: radial, perpendicular to circles
  \foreach \ang in {35, 90, 150, 215, 300} {
    \draw[->, acc, very thick] ([shift={(0.4,0)}]\ang:1.1) -- ([shift={(0.4,0)}]\ang:1.75);
  }
  \node[acc, font=\footnotesize] at (2.55,1.5) {grad $f$};
\end{tikzpicture}
$$

Two objects follow from this.

**Gradient field.** Plotting $\nabla f(a, b)$ as an arrow based at each point
$(a, b)$ produces the gradient vector field, laid over the contour map. Every
arrow crosses its contour perpendicularly and points uphill; arrows are long
where contours crowd (steep ground) and short where they spread (flat ground).

$$
% caption: The gradient field over a contour map. Arrows cross contours at right
% angles, point toward higher ground, and lengthen where the slope is steep.
\begin{tikzpicture}[scale=1.0, >=stealth, font=\small]
  \definecolor{acc}{HTML}{4A6FA5}
  % contour lines (a slanted ridge): parallel-ish curves
  \foreach \c in {-1.4,-0.7,0,0.7,1.4} {
    \draw[black] (-2.4,{\c-0.5}) .. controls (-0.5,{\c+0.4}) and (0.5,{\c-0.4}) .. (2.4,{\c+0.5});
  }
  % gradient arrows roughly perpendicular to contours (pointing up-left)
  \foreach \x/\y in {-1.6/-0.6, -0.4/-0.1, 0.8/0.15, -1.0/0.7, 0.2/1.0} {
    \draw[->, acc, very thick] (\x,\y) -- ($(\x,\y)+(0.18,0.62)$);
  }
  \node[acc, font=\footnotesize, anchor=west] at (1.4,1.3) {grad $f$};
\end{tikzpicture}
$$

**Steepest-ascent path.** A curve that follows the gradient at every point,
always turning to head straight uphill, cuts across the contours at right angles
everywhere. This is the path of steepest ascent, and the same idea run downhill,
following $-\nabla f$, is the geometry behind gradient-descent optimization.

## Level surfaces and tangent planes

For three variables the picture rises one dimension. If a surface $S$ is a level
surface $F(x, y, z) = k$, take any curve $\vec r(t)$ lying on $S$ through
$P = (x_0, y_0, z_0)$. Then $F(\vec r(t)) = k$ is constant, and differentiating
by the chain rule,
$$
\nabla F(x_0, y_0, z_0) \cdot \vec r\,'(t_0) = 0.
$$
Since $\vec r\,'(t_0)$ is a tangent vector to an arbitrary curve on $S$, the
gradient $\nabla F(x_0, y_0, z_0)$ is perpendicular to every tangent direction at
$P$: it is a **normal vector** to the surface.

$$
% caption: On a level surface $F=k$, the gradient $\nabla F(P)$ is normal to every
% curve through $P$, so it is normal to the tangent plane; each curve tangent
% lies in that plane.
\begin{tikzpicture}[scale=1.0, >=stealth, font=\small]
  \definecolor{acc}{HTML}{4A6FA5}
  \definecolor{wrm}{HTML}{C0392B}
  % level surface patch (kept left to leave room for labels on the right)
  \draw[black, thick, fill=acc!8]
    (-2.6,0.0) .. controls (-1.2,0.7) and (0.4,-0.7) .. (1.6,0.0)
    -- (1.9,1.3) .. controls (0.7,0.6) and (-1.0,2.0) .. (-2.3,1.3) -- cycle;
  \node[black, font=\footnotesize, anchor=north west] at (-2.55,-0.15) {surface $F=k$};
  \coordinate (P) at (-0.2,0.72);
  % tangent plane through P
  \draw[acc, thick] (-1.6,0.55) -- (1.2,0.30) -- (1.7,1.05) -- (-1.1,1.30) -- cycle;
  % gradient normal
  \draw[->, wrm, very thick] (P) -- (0.2,2.3);
  \node[wrm, font=\footnotesize, anchor=west] at (0.22,2.05) {grad F};
  % curve tangent in the plane
  \draw[->, black!75, thick] (P) -- (1.1,0.5);
  % labels to the right, clear of the panel, with leader lines
  \node[acc, font=\footnotesize, anchor=west] at (2.15,1.05) {tangent plane};
  \node[black!75, font=\footnotesize, anchor=west] at (2.15,0.45) {curve tangent};
  \draw[acc, thin] (1.65,1.03) -- (2.1,1.05);
  \draw[black!75, thin] (1.1,0.5) -- (2.1,0.47);
  \fill[black] (P) circle (1.7pt);
  \node[anchor=south east, font=\footnotesize] at (-0.22,0.78) {$P$};
\end{tikzpicture}
$$

> **Theorem (Tangent plane to a level surface).** The tangent plane to
> $F(x, y, z) = k$ at $P = (x_0, y_0, z_0)$ has normal vector
> $\nabla F(x_0, y_0, z_0)$ and equation
> $$
> F_x(P)(x - x_0) + F_y(P)(y - y_0) + F_z(P)(z - z_0) = 0.
> $$
> The normal line through $P$ has direction $\nabla F(P)$.

This subsumes the earlier tangent plane. A graph $z = f(x, y)$ is the level
surface $F(x, y, z) = f(x, y) - z = 0$, whose gradient is
$\langle f_x, f_y, -1 \rangle$; the normal equation reduces to
$z - z_0 = f_x(x_0, y_0)(x - x_0) + f_y(x_0, y_0)(y - y_0)$, the tangent-plane
formula for a graph.

> **Worked example.** Find the tangent plane and normal line to the sphere
> $x^2 + y^2 + z^2 = 3$ at $(1, 1, 1)$.
>
> With $F = x^2 + y^2 + z^2$, $\nabla F = \langle 2x, 2y, 2z \rangle$, so
> $\nabla F(1, 1, 1) = \langle 2, 2, 2 \rangle$. The tangent plane is
> $$
> 2(x - 1) + 2(y - 1) + 2(z - 1) = 0, \qquad\text{i.e.}\qquad x + y + z = 3.
> $$
> The normal line $\langle 1 + t, 1 + t, 1 + t \rangle$ passes through the center,
> as every radius of a sphere does.

> **Worked example.** Find the tangent plane to the ellipsoid
> $\dfrac{x^2}{4} + y^2 + \dfrac{z^2}{9} = 3$ at $(-2, 1, -3)$.
>
> The ellipsoid is the level surface $F = 3$ of
> $F(x, y, z) = \dfrac{x^2}{4} + y^2 + \dfrac{z^2}{9}$, with
> $\nabla F = \left\langle \dfrac{x}{2},\, 2y,\, \dfrac{2z}{9} \right\rangle$. At
> $(-2, 1, -3)$ this is $\left\langle -1,\, 2,\, -\tfrac{2}{3} \right\rangle$, so
> the tangent plane is
> $$
> -1(x + 2) + 2(y - 1) - \tfrac{2}{3}(z + 3) = 0,
> \qquad\text{i.e.}\qquad 3x - 6y + 2z + 18 = 0.
> $$

The perpendicularity of the gradient to level sets is what makes
[constrained optimization](/calculus/partial-derivatives/optimization-and-lagrange-multipliers)
tractable: at a constrained optimum the gradient of the objective and the
gradient of the constraint are parallel, because both are normal to the same
tangent direction.
