Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Partial derivatives and the gradient

After this lesson: I can find the gradient of a function of two variables.

60 min lesson1 commandJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

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

WordWhat it meansExample
Variablean input that can changetemperature
Partial derivativethe slope along one input while the others stay fixed
Gradientthe list of all the partial derivatives
Steepest directionthe way the function rises fastest
Length of a gradienthow 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

TemperatureRain (mm)HiresWhat changedChange in hires
100250The start
110261One more degree+11
101243One 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

PointSlope along xSlope along yGradientLength
(3, 4)68[6, 8]10
(-1, 2)-24[-2, 4]About 4.47
(1, 0)20[2, 0]2
(0, 0)00[0, 0]0

The cells students fill in are left blank in this preview.

Ask SciPy for a gradient

Command
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

Code: 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.

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.