The gradient
Lesson 4.1.2 measured the slope of a surface along the two axes. The partial derivative is the slope of the slice with held fixed, and the slope with held fixed. The gradient collects both into one vector.
Rule: Gradient
Read as "grad f". It is a vector at each point , written as a column like the vectors of Level 3. Each component is in units of per unit of its own variable.
Take , a tilted bowl. Its partials are and . At the point the height is , and
So, heading along the -axis from , the surface falls 2 units per unit step. Heading along the -axis it rises 2 per unit step.
Three or more variables
A function of three variables has three partials, so its gradient has three components. For at , the partials are , and . So .
With inputs, the gradient is a vector in .
Try it
gradient estimates each partial by a central difference (lesson 4.1.2): it bumps one input at a time by , both ways. It works for any number of inputs.
np.asarray(p, dtype=float) matters. In an array of whole numbers, a bump of 0.00001 would be rounded away.
The slope in any direction
From a point you can walk off in any direction, not only along the axes. Give the direction as a unit vector (lesson 3.1.2). The points then form a straight line through , each at distance from it.
The height along that line is a function of one number, . Its slope at is the directional derivative of at in the direction , written .
For the tilted bowl at , take , a vector of length 1. Here are chord slopes (lesson 2.2.1) from both sides:
| step | 0.1 | 0.01 | 0.001 |
|---|---|---|---|
| ahead, to | 0.548 | 0.4148 | 0.40148 |
| behind, to | 0.252 | 0.3852 | 0.39852 |
Both rows settle on 0.4. Algebra agrees: expanding gives . So the chord slope is , which tends to 0.4.
The dot product gives the same number in one line: .
Rule: Directional derivative
The slope of per unit of distance in the direction of the unit vector . A step along moves by and by . Each move changes at the rate of its own partial, and the two changes add.
The partials are special cases. The direction gives , and gives . In three variables the dot product has three terms: for above, along the slope is .
The direction must be a unit vector
A direction often comes as an angle or as an arrow of any length. For an angle (alpha) from the positive -axis, use , a point on the unit circle. For an arrow such as , normalise it first (lesson 3.1.2): .
Skipping that step is costly. , five times the slope, because a step of along covers a distance of .
The slope is in units of per unit of distance. Distance in the plane only makes sense when and are measured in the same units.
Try it
The loop prints the chord slopes of the table, ahead and behind. The next line is , from the gradient .
The last line shows the mistake: dotting with instead of its unit vector gives 2, not 0.4.
Steepest ascent and steepest descent
Since has length 1, the angle rule of lesson 3.1.2 gives . Here (theta) is the angle between and . So the slope depends only on how far turns away from the gradient.
At the gradient points at 135° from the -axis. Its length is . Turning round the circle gives these slopes:
| direction | 0° | 45° | 90° | 135° | 180° | 225° | 270° | 315° |
|---|---|---|---|---|---|---|---|---|
| slope | 0 | 2 | 2.8284 | 2 | 0 |
The largest slope is at 135°, along the gradient. The most negative is at 315°, straight against it. Halfway between, at 45° and 225°, the slope is 0.
Rule: Steepest slopes
The gradient points straight uphill, and its length is the steepest slope. Against it is steepest descent, with slope , and at right angles the slope is 0. The bounds are the Cauchy–Schwarz inequality of lesson 3.1.2.
The slope at in every direction. It peaks at 2.83, the length of , in the direction of , and is 0 along the level curve, at right angles to it.
Directions the same angle either side of the gradient have the same slope. 90° and 180° both give 2, since each is 45° from 135°.
Lesson 4.4.2 uses steepest descent to find minima: take a small step against the gradient, then repeat.
The gradient crosses level curves at right angles
A level curve (lesson 4.1.1) joins points of equal height. Walking along it, does not change, so the slope along the curve is 0. By the rule, the curve's direction is at right angles to .
At the level curve leaves at 45° and 225°, the two zeros in the table. It is a closed curve round the bottom of the bowl, and passes through too, where is also 1.
A second example. Take the bowl at . There and . The level curve is an ellipse, and at it heads in the direction : the nearby point is still at height 6.
The dot product confirms the right angle. The steepest slope is , in the direction .
Uphill does not have to point straight away from the bottom. The bottom of this bowl is , so straight away from it is the direction ; the gradient points along .
Gradient arrows on a grid of points of the tilted bowl, all drawn at the same scale. Each points uphill at right angles to the level curves around it. The arrows are longest where the curves crowd together, and vanish at the lowest point .
Try it below. The dark arrow is the gradient and the dashed arrow your direction .
Turn the direction until the slope peaks, and check that the peak is the gradient's length. Then turn it along the highlighted level curve, where the slope is 0, and move the point to try again.
Explore
The arrow is the gradient: it points uphill, at right angles to the level curve. Turn the direction and watch the slope in that direction: largest along the gradient, zero along the curve.
- Point
- (0, 1), f = 1
- Gradient ∇f (dark arrow)
- (−2, 2), length 2.828
- Slope at 0°
- ∇f · u = −2: downhill
Try it
The first line steps along the ellipse from : each point keeps height 6. Then the dot product of the gradient with the curve's direction, and the steepest slope with its direction.
np.linalg.norm is the length of lesson 3.1.2.
Where the gradient is zero, and where there is none
A zero gradient. For the tilted bowl, and both hold at , so . Then in every direction: the surface is flat there, with no uphill direction at all.
That point is the bottom of the bowl, where . A flat point can also be a top or a saddle; lesson 4.3.2 tells them apart.
No gradient. The cone gives each point's distance from the origin. Its slice along is , which has a corner at 0 (lesson 2.2.2). So does not exist at the origin, and neither does the gradient.
Walking out of the origin, the cone rises with slope 1 in every direction. No vector can do that through a dot product: if , then .
Away from the origin there is no corner. By the chain rule (lesson 2.3.3), and . At that gives , of length 1: the steepest slope of a cone is 1.
Try it
The first line is the gradient of the tilted bowl at , by central differences: zero, after rounding.
Then the cone: from the origin, the slope out along four directions is 1 each time. The last line is the slice : slope 1 to the right of 0 and to the left.
The gradient appears again in the rest of this module. Lesson 4.2.2 follows along a curved path with it. Case study 4.2.4 uses the same first-order slopes to explain a day's change in an option's value.
Common mistakes
- Normalise the direction. Along , the tilted bowl at has slope 0.4, not .
- The steepest slope is the gradient's length, not the sum of its components. At it is , not .
- Put the point into the partials. is the gradient everywhere; at it is the vector .
- Steepest descent is along , with slope : the opposite direction, not a different one.
- The gradient crosses level curves; it never runs along them. Along a level curve the slope is 0.
- Uphill need not point away from the bottom. On at the gradient is along , not .
- No gradient at a corner. The cone rises with slope 1 out of the origin in every direction, but has no gradient there.
The gradient and directional derivatives
0 of 11 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- The gradient stacks the partial derivatives into a vector at each point; in variables it has components.
- The slope in the direction of a unit vector is . Normalise an arrow first; for an angle , .
- : steepest ascent along with slope , steepest descent against it with slope , and slope 0 at right angles.
- Along a level curve the slope is 0, so crosses level curves at right angles.
- Where the surface is flat in every direction. At a corner, such as the cone at its tip, there is no gradient.