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

Free lesson

Rate of change and slope

After this lesson: I can estimate how fast something changes from numbers.

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

What is a rate of change?

A rate of change says how fast one measurement changes when another one changes. A bike shop asks it every day: for each extra degree of warmth, how many more bikes are hired?

In this lesson you will estimate a rate of change from a table of numbers. You will also see why a small step gives a better estimate than a big one.

By the end of this lesson

Note

I can estimate how fast something changes from numbers.

Key words

WordWhat it meansExample
Functiona rule that turns one number into another numberf(x) = x * x
Slopehow steeply a line or curve rises at a point
Rate of changethe change in the output for one unit of change in the input
Stepa small change in the input, written h
Difference quotientthe change in the output divided by the step

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

A straight line has one slope everywhere

The shop notes how many bikes were hired at three temperatures. For each step of 5 degrees, the hires rise by 55.

The slope is the rise divided by the run. Here the rise is the change in hires and the run is the change in temperature. The slope of this line is 11 hires for each degree, and it is the same wherever you measure it.

Worked example: the slope of a line

Temperature (degrees)HiresRun from the row aboveRise from the row aboveRise divided by run
10250
1530555511
2036055511

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

A curve's slope changes at every point

Most real relationships bend. On a curve, rise over run depends on where you stand and how big a step you take.

To estimate the slope of f(x) = x * x at x = 3, take a small step h to the right. The difference quotient is (f(3 + h) - f(3)) / h. Shrink h and watch the answer settle.

Worked example: smaller steps settle on 6

Step hf(3 + h)f(3 + h) minus f(3)Divided by h
11677
0.19.610.616.1
0.019.06010.06016.01
0.0019.0060010.0060016.001

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

Run the steps in the lab

Command
docker exec jupyter python3 -c "for h in [1, 0.1, 0.01, 0.001]: print(h, round(((3 + h) ** 2 - 9) / h, 3))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Stepping both ways gives a better estimate

The forward estimate is always a little off, because the curve bends away over the step. Stepping one way and the other cancels most of that.

The centred estimate is (f(x + h) - f(x - h)) / (2 * h). For x * x at 3 with a step of 1, it is (16 - 4) / 2, which is exactly 6. The forward estimate with the same step gave 7.

Try small steps in Python

Code: python
# The slope of x squared at 3 is 6. How close does each step get?
def square(x):
    return x * x

for h in [0.1, 0.0001, 0.00000001, 0.0000000000000001]:
    forward = (square(3 + h) - square(3)) / h
    print(h, round(forward, 4))
# Now change the line that finds forward to
# (square(3 + h) - square(3 - h)) / (2 * h)
# and run it again. Which steps give 6.0 now?

A common mistake

Warning

Many people think the smaller the step, the better the estimate, with no limit. A computer keeps only about 16 digits. When h is tiny, f(x + h) and f(x) look the same, and the answer collapses to 0. Use a step near 0.0001.

Going further

In your own notebook, estimate the slope of x * x * x at 2 with steps of 1, 0.1 and 0.01, using the centred estimate.

The true slope is 12. Work out how far each estimate is from 12, and say which step you would trust.

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.