Lesson 10 · Calculus
Adding up the continuous: integrals
The problem
A delivery scooter has a broken odometer. Its speedometer still works, and it logs the speed during every trip. How far did the scooter go?
If the speed never changed, this would be one multiplication: distance = speed × time. But the speed changes all the time. The scooter speeds up, slows down for a red light, waits, and sets off again.
This chapter shows how to add up a changing rate. The tool is the integral, and it has a picture: the area under a curve.
Constant speed: distance is an area
Start with the easy case. Plot speed against time. A scooter riding at a constant speed draws a flat line, and the region under it is a rectangle.
- Its height is the speed, in km/h.
- Its width is the time spent.
- Its area, height × width, is speed × time: the distance.
Watch the units. Our clock runs in minutes but the speed is in km per hour, so km/h × minutes is not yet km. A minute is of an hour, so divide the minutes by 60 before you multiply. This chapter does that every time.
Predict first
The scooter rides at 30 km/h for 10 minutes. What is the area under the speed curve?
Height × width = 24 km/h × 5/60 h
Area = distance = 2 km
This is the whole idea of the chapter: distance is the area under the speed curve. The rest is about curves that aren't flat.
Changing speed: slice it thin
Here is a made-up trip. The scooter speeds up to 36 km/h, then slows down for a red light at minute 5. It waits until minute 6. Then it rides a slower second stretch and stops at the customer's door at minute 12.
The region under this curve isn't a rectangle. But over a short slice of time, the speed hardly changes, so that slice is almost a rectangle. So we:
- cut the 12 minutes into equal slices
- treat the speed as constant within each slice, using its value at the start of the slice
- add up the areas of the rectangles
A total made this way is a Riemann sum.
Worked example
Three slices of 4 minutes
The slices start at 0, 4 and 8 minutes. The speeds there are 0, 23.04 and 24 km/h. Each slice is 4 minutes wide, which is of an hour.
With sum notation, slices of width (read "delta t", a small step in time) give:
Here is the speed at the start of slice , and is in hours.
Now make the slices thinner. Press play to double the number of rectangles at each step.
3 slices, each 4 min wide
Sum of rectangles: 3.136 km
Exact distance 3.8 km · gap 0.664 km
The totals go 3.136, 3.52, 3.67, 3.768, 3.792, 3.798 km. Each doubling shrinks the gap to the true 3.8 km. From 24 slices on, it cuts the gap to about a quarter.
That is faster than rectangles usually manage. It happens here because the speed is 0 at both ends of the trip. Usually, doubling the number of rectangles only halves the gap. You'll see that in the running totals later in the chapter, which don't get this help.
The integral
Keep slicing thinner, forever. The rectangles hug the curve more and more closely. Their total settles on one number: the exact area under the curve. That number is the integral of the speed. We write it like this:
The is a stretched S, for "sum". The and say where the slices start and stop, and is one slice: height times a tiny width . So an integral is a Riemann sum with infinitely thin slices. As a picture, it is the area under the curve. Because our clock runs in minutes, the distance in km is .
The running total
The odometer we lost shows the distance so far at every moment, not only at the end. In area terms, it is the area under the speed curve from 0 up to the current time . Call it :
The letter runs over the times from 0 to . It is a different letter from because is now the end point. A function like this, the area from a fixed start up to , is an accumulation function. It is a running total.
Predict first
The scooter waits at a red light from minute 5 to minute 6. What does the odometer's graph do then?
Press play to sweep time across the trip. The shaded area on top becomes the curve being drawn below.
t = 0 min · speed 0 km/h
Area so far = distance travelled: 0.00 km
Straight line: the running total is climbing 0.000 km per minute = 0 km/h, the speed right now. Dark strip: the next 0.25 min adds about 0 × 0.25/60 = 0.000 km.
Here is what to notice in the Delivery trip tab:
- The odometer climbs fastest at minute 2.5, when the speed peaks at 36 km/h.
- It is flat from minute 5 to 6, at the red light.
- It ends at 3.8 km: 2 km on the first stretch, 1.8 km on the second.
Why integrals undo derivatives
Look at the straight line on the lower curve. It touches the odometer curve, so its slope is the odometer's slope. At every moment, that slope equals the current speed. For example, at minute 3 the speed is 34.56 km/h, and the odometer is climbing at 0.576 km per minute. Multiply by 60 and that is 34.56 km/h.
Why must this be true? Move time forward by a tiny step . The area grows by one thin strip, the dark strip in the figure. The strip is almost a rectangle, with height and width . So:
The left side is the slope of the running total. As shrinks, the strip becomes more and more like a rectangle, and the becomes exact.
This is the fundamental theorem of calculus: the rate of change of the accumulated area is the curve's height. In symbols:
So integrating and differentiating undo each other. That gives a shortcut for exact areas. Find a function whose derivative is the curve. Such a function is called an antiderivative: it is what you get by running a derivative backwards. The area is then how much the antiderivative changes between the start and the end.
Worked example
The exact distance on the second stretch
From minute 6 to 12 the speed is . Let be the minutes since the light turned green. Then , for from 0 to 6.
Area below the axis counts as negative
Now switch the figure to Out and back. The rider sets off, realises the drinks are still at the shop, and rides back. Here we plot velocity: speed with a direction. Positive means away from the shop, negative means back towards it. Speed is the size of the velocity, with the sign dropped.
Predict first
The rider goes out for 6 minutes, then comes back the same way for 6 minutes. What is the total area under the velocity curve, counting area below the axis as negative?
So the integral measures signed area: area above the axis counts as positive and area below counts as negative. With velocity, the signed area is your change in position. To get the distance travelled, integrate the speed instead.
When there's no formula: numerical integration
The worked example needed an antiderivative. Often you can't get one:
- You only have samples. The real speedometer logs a speed every few seconds. There is no formula.
- No neat antiderivative exists. The bell-shaped curve is like this. Its area matters a lot in probability (see the normal distribution), but no combination of the usual functions has it as a derivative.
Then you compute the area from samples. This is numerical integration. Rectangles are one way. A better way joins neighbouring samples with straight lines and adds up the trapezoids underneath. Each trapezoid's area is the average of its two end heights, times its width.
Rectangles: worst gap 0.704 km
Trapezoids: worst gap 0.280 km
Solid curve: exact distance · both end at 3.520 km (exact 3.8)
Use the Rectangles and Trapezoids tabs to switch the top plot, and the slider to change how often the speed is sampled. Here is what to notice:
- Rectangles use one sample per slice, so they are always a step behind when the speed changes. Trapezoids follow the slope between samples.
- Sampling every minute, the rectangles' running total is off by up to 0.336 km. The trapezoids' is off by up to 0.13 km.
- Move the spacing slider from 1 minute to 0.5 minutes. The rectangles' worst gap halves (0.336 to 0.16 km). The trapezoids' gap falls to a quarter (0.13 to 0.0325 km).
Both methods end at the same total here. That's a quirk of this trip. Trapezoids minus rectangles is ½ × width × (last speed − first speed), and both speeds are 0 here.
DigressionIntegrating with random dotsThrow random points at a curve and count. It works in any number of dimensions. · 2 min
Rectangles and trapezoids need a grid of sample points. With one input, that's fine. With 10 inputs and 100 points along each, the grid has points, far too many.
A different trick is to pick random inputs, evaluate the curve there, and average the values. Average height × width gives the area. The more random points you use, the closer the average gets. This is a Monte Carlo estimate, named after the casino.
Roughly, its accuracy depends on how many points you use, not on how many inputs there are. It also depends on how much the values vary. That's why it shows up all over ML and reinforcement learning. The Monte Carlo estimate gets its full explanation in the densities chapter.
Common misconceptions
"The integral is the height of the curve." No. The height is the rate (km/h). The integral is the total (km). The running total's slope is the height.
"Area is always positive." Not for integrals. Area below the axis counts as negative, which is why out and back gives 0.
"More rectangles always means exact." More rectangles get closer, but any finite number is an estimate. For the running totals, trapezoids get closer with the same samples.
"Units don't matter for areas." They do. km/h × minutes is not km until you divide by 60.
Recap
- Distance is the area under the speed curve. In general, an integral adds up a rate into a total.
- A Riemann sum adds thin rectangles. As they get thinner, it settles on the integral.
- The running total of area is the accumulation function, like an odometer.
- Its slope is the curve's height. So integrals and derivatives undo each other.
- Area below the axis counts as negative.
- Without a formula, estimate the integral from samples. Trapezoids beat rectangles.
Section 2 shows how to compute these in PyTorch.