Search

Search papers and lessons

Directional Derivative

Level 2 · 11 min read

The directional derivative is how fast a multivariable function changes when you move in one chosen direction; it equals the gradient dotted with a unit vector that gives that direction.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Linear algebra
  • ReadReady to readNot yet

TL;DR

A directional derivative measures how fast a function changes when you move through its inputs in one chosen direction. The partial derivatives are the special case where the direction runs along one axis. You compute it by taking the Gradient and dotting it with a unit direction vector, which turns a question about every direction into one number per direction.

Builds on: Derivative, Multivariable Function, Gradient, Dot Product · Leads to: Functional Derivative

Where it's used

Choosing how far to step in training. When a model is trained by gradient descent, the optimizer first picks a direction in which to move the weights. Before it decides how far to go, it needs to know how fast the loss falls along that direction. That rate is a directional derivative. The search for a good step length along a fixed direction, called a line search, reads this rate at its starting point.

Walking across a slope. Suppose a hiker stands on a hill whose height is measured in metres. The hiker can ask how steeply the ground rises if they walk north-west, which is not the steepest direction. The answer is the directional derivative of the height along that bearing. Route planning over terrain can use the same quantity to compare paths.

Sensors that cannot follow an axis. A thermometer carried in a straight line across a room reads a temperature that changes as it moves. The rate of that change, per metre travelled along the line, is the directional derivative of the temperature field in the line's direction. Engineers use it when a sensor cannot be placed along the x or y axis of a room.

How it works

Take one tiny function, f(x,y)=x2+3yf(x, y) = x^2 + 3y, at the point (1,2)(1, 2), and move in the direction u=(0.6,0.8)\mathbf{u} = (0.6, 0.8). Its length is 0.36+0.64=1\sqrt{0.36 + 0.64} = 1, so it is a unit vector: it gives a direction and nothing else.

  1. Read the height at the start. f(1,2)=1+6=7f(1, 2) = 1 + 6 = 7. Check: plug in x=1x = 1 and y=2y = 2 and nothing else.
  2. Take a small step of length h=0.1h = 0.1 along u\mathbf{u}. The new point is (1+0.06,2+0.08)=(1.06,2.08)(1 + 0.06, 2 + 0.08) = (1.06, 2.08). Check: each coordinate moves by hh times the matching entry of u\mathbf{u}.
  3. Read the height at the new point. f(1.06,2.08)=1.1236+6.24=7.3636f(1.06, 2.08) = 1.1236 + 6.24 = 7.3636. Check: 1.062=1.12361.06^2 = 1.1236 and 3×2.08=6.243 \times 2.08 = 6.24.
  4. Divide the change in height by the step length. The change is 7.3636−7=0.36367.3636 - 7 = 0.3636, so the average slope over the step is 0.3636/0.1=3.6360.3636 / 0.1 = 3.636. This is the difference quotient: change in ff per unit distance walked. Check: a shorter step should give a slope closer to the true one.
  5. Shrink the step to h=0.01h = 0.01. The new point is (1.006,2.008)(1.006, 2.008), the height is 7.0360367.036036, and the quotient is 0.036036/0.01=3.60360.036036 / 0.01 = 3.6036. It is closer to 3.63.6.
  6. Let hh shrink to zero. The quotients approach 3.63.6. That limit is the directional derivative of ff at (1,2)(1, 2) in the direction u\mathbf{u}.
flowchart LR
  A[Start at the point x] --> B[Pick unit direction u]
  B --> C[Step h along u]
  C --> D[Change in f divided by h]
  D --> E[Let h go to zero: the limit]

The line through the start point in direction u\mathbf{u} has heights g(t)=f(x+tu)g(t) = f(\mathbf{x} + t\mathbf{u}). Plotted against the distance tt walked, the curve starts at height 7 and bends upward. Its slope at t=0t = 0 is the directional derivative.

−1−0.500.511.52051015t (distance along u)f
  • height along the line
  • tangent line at t = 0
Along the line through (1, 2) in direction u, the height is a parabola. Its slope at t = 0 is 3.6, the directional derivative.

The math

Definition: Directional derivative

Let ff be a function of several inputs, x\mathbf{x} a point and u\mathbf{u} a unit vector (a vector of length 1, so it encodes only direction). The directional derivative of ff at x\mathbf{x} in direction u\mathbf{u} is

Duf(x)=lim⁡h→0f(x+hu)−f(x)hD_{\mathbf{u}} f(\mathbf{x}) = \lim_{h \to 0} \frac{f(\mathbf{x} + h\mathbf{u}) - f(\mathbf{x})}{h}

whenever this limit exists.

Step 1 in symbols: the step

The point x+hu\mathbf{x} + h\mathbf{u} lies a distance ∣h∣|h| from x\mathbf{x}, because u\mathbf{u} has length 1. In the example, x=(1,2)\mathbf{x} = (1, 2), u=(0.6,0.8)\mathbf{u} = (0.6, 0.8) and h=0.1h = 0.1 give x+hu=(1.06,2.08)\mathbf{x} + h\mathbf{u} = (1.06, 2.08).

Step 2 in symbols: the average slope

The fraction in the definition is the change in ff divided by the step length. It is the average slope over the step, which is the number we read off in step 4 of the walkthrough.

Step 3 in symbols: the gradient formula

If ff is differentiable, the limit can be computed exactly. Let g(t)=f(x+tu)g(t) = f(\mathbf{x} + t\mathbf{u}) be the height along the line, as a function of the single variable tt. Then Duf(x)=g′(0)D_{\mathbf{u}} f(\mathbf{x}) = g'(0). The chain rule gives

g′(0)=∑i=1n∂f∂xi(x) ui=∇f(x)⋅ug'(0) = \sum_{i=1}^{n} \frac{\partial f}{\partial x_i}(\mathbf{x})\, u_i = \nabla f(\mathbf{x}) \cdot \mathbf{u}

Here ∇f\nabla f is the Gradient and the last step is a dot product. For the example, ∇f=(∂f/∂x,∂f/∂y)=(2x,3)=(2,3)\nabla f = (\partial f/\partial x, \partial f/\partial y) = (2x, 3) = (2, 3) at (1,2)(1, 2), so Duf=2(0.6)+3(0.8)=1.2+2.4=3.6D_{\mathbf{u}} f = 2(0.6) + 3(0.8) = 1.2 + 2.4 = 3.6, matching the limit in the walkthrough.

Step 4 in symbols: the direction that matters

Write the dot product with the angle θ\theta between the two vectors (the same cosine used in cosine similarity): Duf=∥∇f∥cos⁡θD_{\mathbf{u}} f = \|\nabla f\| \cos\theta. Three facts follow.

  • The largest value comes when θ=0\theta = 0: step along the gradient. The value is ∥∇f∥\|\nabla f\|, the steepest rate.
  • The value is zero when θ=90°\theta = 90°: moving perpendicular to the gradient does not change ff to first order.
  • The value is negative when θ>90°\theta > 90°: the function falls.

Worked examples

Example
1. A plane, one axis

Let f(x,y)=x+2yf(x, y) = x + 2y and u=(1,0)\mathbf{u} = (1, 0), which points east.

  1. The partials are ∂f/∂x=1\partial f/\partial x = 1 and ∂f/∂y=2\partial f/\partial y = 2, so ∇f=(1,2)\nabla f = (1, 2) everywhere.
  2. Dot with u\mathbf{u}: (1)(1)+(2)(0)=1(1)(1) + (2)(0) = 1.
  3. Interpretation: walking east raises ff by 1 for every unit of distance.
  4. Check from the definition at the point (0,0)(0, 0): f(h,0)−f(0,0)=hf(h, 0) - f(0, 0) = h, so the quotient is h/h=1h / h = 1 for every hh.
Example
2. A product of two inputs

Let f(x,y)=x2yf(x, y) = x^2 y, the point (2,1)(2, 1) and the unit direction u=(0.6,0.8)\mathbf{u} = (0.6, 0.8).

  1. Partials: ∂f/∂x=2xy=2⋅2⋅1=4\partial f/\partial x = 2xy = 2 \cdot 2 \cdot 1 = 4 and ∂f/∂y=x2=4\partial f/\partial y = x^2 = 4.
  2. Gradient: ∇f=(4,4)\nabla f = (4, 4).
  3. Check that u\mathbf{u} is a unit vector: 0.36+0.64=10.36 + 0.64 = 1.
  4. Dot product: 4(0.6)+4(0.8)=2.4+3.2=5.64(0.6) + 4(0.8) = 2.4 + 3.2 = 5.6.
  5. Interpretation: heading north-east from (2,1)(2, 1) raises ff at 5.6 per unit of distance.
  6. Check with a step of h=0.001h = 0.001: the new point is (2.0006,1.0008)(2.0006, 1.0008), the height is 4.005602284.00560228, the change is 0.005602280.00560228, and the quotient is 5.602285.60228, close to 5.65.6.
Example
3. Walking across a hill

A hill has height h(x,y)=100−x2−2y2h(x, y) = 100 - x^2 - 2y^2 metres, where xx is east and yy is north, both in kilometres. The hiker stands at (3,1)(3, 1) and wants to walk the unit direction u=(−0.6,0.8)\mathbf{u} = (-0.6, 0.8), which is north-west.

  1. Height here: 100−9−2=89100 - 9 - 2 = 89 metres.
  2. Partials: ∂h/∂x=−2x=−6\partial h/\partial x = -2x = -6 and ∂h/∂y=−4y=−4\partial h/\partial y = -4y = -4, so ∇h=(−6,−4)\nabla h = (-6, -4) metres per kilometre.
  3. Dot with u\mathbf{u}: (−6)(−0.6)+(−4)(0.8)=3.6−3.2=0.4(-6)(-0.6) + (-4)(0.8) = 3.6 - 3.2 = 0.4 metres per kilometre. The ground rises gently in that direction.
  4. Check with a step of 0.010.01 km: the new point is (2.994,1.008)(2.994, 1.008), the height is 100−8.964036−2.032128=89.003836100 - 8.964036 - 2.032128 = 89.003836, and the quotient is 0.38360.3836, approaching 0.40.4.

Try it: at the same point, what is the directional derivative in the unit direction v=(0.6,−0.8)\mathbf{v} = (0.6, -0.8), which points south-east?

Answer: (−6)(0.6)+(−4)(−0.8)=−3.6+3.2=−0.4(-6)(0.6) + (-4)(-0.8) = -3.6 + 3.2 = -0.4. The ground falls 0.4 metres per kilometre going south-east, so the hiker walks downhill that way.

Common mistakes and tradeoffs

It is tempting to think the directional derivative is just a partial derivative. In fact a partial derivative is the directional derivative along one axis, and any other unit direction mixes the partials: Duf=∑iui ∂ifD_{\mathbf{u}} f = \sum_i u_i \, \partial_i f. For f=x2+3yf = x^2 + 3y at (1,2)(1, 2) the partials are 2 and 3, but the direction (0.6,0.8)(0.6, 0.8) gives 3.6, which is neither.

It is tempting to skip normalising the direction. A vector such as (3,4)(3, 4) has length 5. Dotting it with (2,3)(2, 3) gives 18, but that is not the rate per unit distance. Divide by the length first, 18/5=3.618 / 5 = 3.6, to get the rate.

It is tempting to think a zero gradient means a peak. In fact a zero gradient means every directional derivative is zero there, which is a flat spot to first order. It does not mean a maximum. For f=x2−y2f = x^2 - y^2 at the origin the gradient is (0,0)(0, 0), yet ff rises along the xx-axis and falls along the yy-axis.

When to use it. Use the gradient formula when you need the rate of change along a specific path or search direction, and when the function is differentiable at the point. When not to use it. A directional derivative can exist in every direction at a point where the function is not differentiable. There the formula ∇f⋅u\nabla f \cdot \mathbf{u} can give the wrong answer, so check differentiability first. Alternatives. A finite difference such as the walkthrough's quotient needs only function values, but its error grows when hh is large and its rounding error grows when hh is tiny. Automatic differentiation computes the directional derivative of a program directly, without a step size.

History

The idea grew out of the calculus of several variables, which was developed through the 1700s and 1800s. Once the partial derivatives were known, the natural next question was how a function changes in any direction, not only along the axes. René Gâteaux later extended the notion to functions on infinite-dimensional spaces in the 1910s. He died in the First World War in 1914, and his work appeared after his death. That generalisation is why the term Gâteaux derivative is used as an alias. The idea caught on in optimisation and, much later, in machine learning, where the rate along a search direction decides how far to move.

Recap

  • A directional derivative is the rate at which ff changes at x\mathbf{x} per unit distance in direction u\mathbf{u}: the limit of (f(x+hu)−f(x))/h(f(\mathbf{x} + h\mathbf{u}) - f(\mathbf{x}))/h.
  • The direction must be a unit vector; divide any other direction by its length first.
  • Partial derivatives are the special cases where u\mathbf{u} points along an axis.
  • For a differentiable ff, the value is ∇f(x)⋅u\nabla f(\mathbf{x}) \cdot \mathbf{u}: the chain rule and a dot product.
  • The largest value over all directions is ∥∇f∥\|\nabla f\|, reached when u\mathbf{u} points along the gradient; perpendicular directions give zero.

Read next: Functional Derivative, which applies the same idea to functions of functions.

Check yourself

QuestionWhat does the directional derivative of f at x in direction u measure?Show answer

It measures the rate at which f changes at x per unit of distance moved in the direction u. It is the limit of the average slope (f(x + hu) - f(x)) / h as h goes to zero.

QuestionLet f(x, y) = 3x + y^2 at the point (1, 2), and let u = (0, 1). Find the directional derivative of f in direction u.Show answer

The gradient is (3, 2y) = (3, 4) at y = 2. Dotting with u = (0, 1) gives 3(0) + 4(1) = 4. So the answer is 4.

QuestionWhy must the direction vector u have length 1 in the definition, and what goes wrong if it does not?Show answer

The definition measures change per unit of distance walked. A vector of length 1 makes the step length equal to h, so the quotient is a rate per unit distance. If u has length L, the step length is L times h, so the quotient is L times the true rate. Dividing u by its length first fixes this.

QuestionA hiker stands where the gradient of the height is (0, 5) metres per kilometre, with north pointing up the page. Which direction gives the fastest climb, and what is the rate?Show answer

Walk due north. The directional derivative is largest when u points along the gradient, and its value is the length of the gradient, which is 5. The fastest climb is 5 metres per kilometre, going north.

Sources