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, , at the point , and move in the direction . Its length is , so it is a unit vector: it gives a direction and nothing else.
- Read the height at the start. . Check: plug in and and nothing else.
- Take a small step of length along . The new point is . Check: each coordinate moves by times the matching entry of .
- Read the height at the new point. . Check: and .
- Divide the change in height by the step length. The change is , so the average slope over the step is . This is the difference quotient: change in per unit distance walked. Check: a shorter step should give a slope closer to the true one.
- Shrink the step to . The new point is , the height is , and the quotient is . It is closer to .
- Let shrink to zero. The quotients approach . That limit is the directional derivative of at in the direction .
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 has heights . Plotted against the distance walked, the curve starts at height 7 and bends upward. Its slope at is the directional derivative.
- height along the line
- tangent line at t = 0
The math
Let be a function of several inputs, a point and a unit vector (a vector of length 1, so it encodes only direction). The directional derivative of at in direction is
whenever this limit exists.
Step 1 in symbols: the step
The point lies a distance from , because has length 1. In the example, , and give .
Step 2 in symbols: the average slope
The fraction in the definition is the change in 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 is differentiable, the limit can be computed exactly. Let be the height along the line, as a function of the single variable . Then . The chain rule gives
Here is the Gradient and the last step is a dot product. For the example, at , so , matching the limit in the walkthrough.
Step 4 in symbols: the direction that matters
Write the dot product with the angle between the two vectors (the same cosine used in cosine similarity): . Three facts follow.
- The largest value comes when : step along the gradient. The value is , the steepest rate.
- The value is zero when : moving perpendicular to the gradient does not change to first order.
- The value is negative when : the function falls.
Worked examples
Let and , which points east.
- The partials are and , so everywhere.
- Dot with : .
- Interpretation: walking east raises by 1 for every unit of distance.
- Check from the definition at the point : , so the quotient is for every .
Let , the point and the unit direction .
- Partials: and .
- Gradient: .
- Check that is a unit vector: .
- Dot product: .
- Interpretation: heading north-east from raises at 5.6 per unit of distance.
- Check with a step of : the new point is , the height is , the change is , and the quotient is , close to .
A hill has height metres, where is east and is north, both in kilometres. The hiker stands at and wants to walk the unit direction , which is north-west.
- Height here: metres.
- Partials: and , so metres per kilometre.
- Dot with : metres per kilometre. The ground rises gently in that direction.
- Check with a step of km: the new point is , the height is , and the quotient is , approaching .
Try it: at the same point, what is the directional derivative in the unit direction , which points south-east?
Answer: . 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: . For at the partials are 2 and 3, but the direction gives 3.6, which is neither.
It is tempting to skip normalising the direction. A vector such as has length 5. Dotting it with gives 18, but that is not the rate per unit distance. Divide by the length first, , 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 at the origin the gradient is , yet rises along the -axis and falls along the -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 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 is large and its rounding error grows when 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 changes at per unit distance in direction : the limit of .
- The direction must be a unit vector; divide any other direction by its length first.
- Partial derivatives are the special cases where points along an axis.
- For a differentiable , the value is : the chain rule and a dot product.
- The largest value over all directions is , reached when 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.