What is a gradient?
A gradient is the list of slopes of a function, one for each input. It tells you which way the function climbs fastest.
Bike hires depend on both temperature and rain, so one slope is not enough. In this lesson you will find the slope in each direction and combine them into a gradient.
By the end of this lesson
Note
I can find the gradient of a function of two variables.
Key words
| Word | What it means | Example |
|---|---|---|
| Variable | an input that can change | temperature |
| Partial derivative | the slope along one input while the others stay fixed | |
| Gradient | the list of all the partial derivatives | |
| Steepest direction | the way the function rises fastest | |
| Length of a gradient | how steep the ground is, found with the square root of the squares added |
The cells students fill in are left blank in this preview.
A partial derivative holds one input still
Suppose hires are 140 + 11 times the temperature minus 7 times the rain. To find the slope for temperature, keep the rain fixed and change only the temperature.
This is a partial derivative. Each input has its own. Here the one for temperature is 11 and the one for rain is -7.
Worked example: changing one input at a time
| Temperature | Rain (mm) | Hires | What changed | Change in hires |
|---|---|---|---|---|
| 10 | 0 | 250 | The start | |
| 11 | 0 | 261 | One more degree | +11 |
| 10 | 1 | 243 | One more millimetre of rain | -7 |
The cells students fill in are left blank in this preview.
The gradient points straight uphill
Put the partial derivatives in a list and you have the gradient. Picture a bowl with height x * x + y * y. At the point (3, 4) the slope along x is 2 times 3, and along y is 2 times 4.
The gradient is [6, 8]. It points up the steepest direction. Its length, the square root of 36 + 64, is 10, and that says how steep the bowl is there.
Worked example: the gradient of a bowl
| Point | Slope along x | Slope along y | Gradient | Length |
|---|---|---|---|---|
| (3, 4) | 6 | 8 | [6, 8] | 10 |
| (-1, 2) | -2 | 4 | [-2, 4] | About 4.47 |
| (1, 0) | 2 | 0 | [2, 0] | 2 |
| (0, 0) | 0 | 0 | [0, 0] | 0 |
The cells students fill in are left blank in this preview.
Ask SciPy for a gradient
docker exec jupyter python3 -c "from scipy.optimize import approx_fprime; f = lambda p: p[0] ** 2 + p[1] ** 2; print(*approx_fprime([3.0, 4.0], f, 1e-6).round(2))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think the gradient points towards the lowest point. It points uphill. To go downhill, step the opposite way, along minus the gradient. At the very bottom of the bowl the gradient is zero.
Estimate each slope to find a gradient
When you cannot differentiate by hand, estimate each partial derivative with a centred step. Move x a little and keep y still. Then move y and keep x still.
This costs two function calls for each input. It is the same idea as lesson 1, applied once for each direction.
Estimate a gradient in Python
# The gradient of a function of two inputs, one slope for each.
def bowl(x, y):
return x * x + y * y
def gradient(f, x, y, h):
dx = (f(x + h, y) - f(x - h, y)) / (2 * h)
dy = (f(x, y + h) - f(x, y - h)) / (2 * h)
return [round(dx, 3), round(dy, 3)]
print(gradient(bowl, 3, 4, 0.001))
# Add def tilted(x, y): return x * x + 5 * y
# and print gradient(tilted, 3, 4, 0.001).
# Which of the two slopes changed, and why?
Going further
In your own notebook, estimate the gradient of x * x * y at (2, 3).
Work out the two partial derivatives by hand first: the slope along x treats y as a plain number. They should be 12 and 4.