mathcalculus
integrals
adding up thin slices: area, distance from speed, and how it undoes the derivative.
cut the region under a curve into thin slices, add them up, and let the slices get thinner. the number the sums close in on is the integral. its slope, as the right end moves, is the curve itself, so one antiderivative evaluates it.
- riemann sum
- Σ f(xᵢ) Δx n slices, a height times a width each
- definite integral
- ∫ₐᵇ f(x) dx what the sums close in on as n grows
- signed
- below the axis counts − sin x over a full turn is 0, though the area is 4
- fundamental theorem
- ∫ₐᵇ f = F(b) − F(a) for any F with F′ = f. the running area has slope f
- power rule, backwards
- ∫ xⁿ dx = xⁿ⁺¹/(n + 1) + C n ≠ −1. for n = −1 it's ln|x|
- + C
- ∫ f(x) dx = F(x) + C an antiderivative is a family. a constant has slope 0
- area under a curve, or between two curves
- a total from a rate: distance from speed, charge from current
- a game loop: position += velocity · dt is one slice a frame
on this page
the idea
how much area is under between and ? the top edge is curved, so no formula for a triangle or a rectangle fits. cut the region into thin vertical slices instead. each slice is nearly a rectangle, and a rectangle’s area is height times width. add them up and the total is close to the area. cut thinner and it’s closer. the number the totals close in on is the integral, written : the is a stretched s, for sum, and is the width of a slice gone to nothing.
the movie reads each slice’s height twice, at its left edge in cyan and at its right edge in amber. one set stays under the curve and the other pokes over it, so the area is trapped between the two sums.
an animation on a square plot of y = x² from x = 0 to 1. first the region under the curve is shaded and marked area?. then it is cut into slices with two rectangles on each: a cyan one as tall as the curve at the slice's left edge, which stays under the curve, and an amber one as tall as it at the right edge, which pokes over it. with 1 slice the sums are 0.0000 and 1.0000. with 2 slices, 0.1250 and 0.6250. with 4, 0.2188 and 0.4688. with 8, 0.2734 and 0.3984. with 16, 0.3027 and 0.3652. with 32, 0.3179 and 0.3491. with 64, 0.3256 and 0.3412. the gap between them is always one slice's width, 1/n, and a number line under the plot shows the two sums closing in on 1/3 ≈ 0.3333 from both sides: the integral.
the same picture answers a different question. if the curve is a speed, the area under it is the distance covered: a thin slice is a speed times a short time, which is a short distance, and the slices add up to the trip. a game loop does the same thing one frame at a time, position += velocity * dt: one slice a frame.
explore
pick a curve, a rule and , and drag the ends of the interval. the readout gives the sum, the exact integral, the error between them, and the area with every part counted positive. slices under the axis are amber: they count negative in the sum.
drag a and b along the axis, or tab to one and use the arrow keys
- from a to b
- 0 to 1
- left sum, n = 8
- 0.2734
- exact: F(b) − F(a), with F(x) = x³/3
- 0.3333
- error, sum − exact
- −0.0599
- area, with the amber counted +
- 0.3333
an interactive chart with a row of buttons for six curves, x², x, x² − 1, sin x, eˣ and 1/x, a row for four rules, left, right, midpoint and trapezoid, a slider for n from 1 to 64, and two points a and b to drag along the x axis, or move with the arrow keys. the slices are drawn under the curve, cyan over the axis and amber under it, with a dot where each height is read. a readout gives the interval, the sum, the exact integral F(b) − F(a) with F named, the error, and the area with every part counted positive. it opens on x² from 0 to 1 with 8 left slices: the sum is 0.2734, the integral 0.3333, the error −0.0599.
- the left and right rules read the height at one edge of each slice. on a curve that only climbs across the interval, left is under and right is over, and the two differ by : one slice’s worth
- the midpoint rule reads the height in the middle, and the trapezoid rule joins the slice’s two corners with a straight edge. both are much closer at the same : their error shrinks to a quarter when doubles, where left and right only halve
- a slice under the axis has a negative height, so it subtracts. the sum is signed, and so is the integral. the last row of the readout counts every slice positive, which is the area
try it
y = x² from 0 to 2, cut into 2 slices, each as tall as the curve at the left edge. what do the slices add up to?
pick one.
a practice exercise with five kinds of question: add up the slices of a drawn riemann sum, pick the antiderivative of a function, evaluate a definite integral with the fundamental theorem, read a signed area off two given areas, and get a distance from a speed. it opens on y = x² from 0 to 2 in 2 left slices, which add to 1, then ∫ 3x² dx, which is x³ + C.
the rules
the riemann sum
cut into slices of width , pick a point in each, and add up the rectangles: height times width . the left edge gives the left sum, the right edge the right sum, the middle the midpoint sum. each term is one rectangle’s area, so the sum is the area of a staircase that follows the curve.
the definite integral
the number the sums close in on as the slices get thinner. for a continuous they do close in, and on the same number whichever point of each slice is picked. why: the lowest and the highest height in each slice give an under-estimate and an over-estimate, every other choice lands between them, and the gap between the two goes to 0, since barely changes across a thin slice. the movie’s cyan and amber sums are that pair for . so the integral depends on , and alone. the limits page is about what closing in means.
the trapezoid rule
each slice is a trapezoid: its two corners on the curve are joined by a straight edge, so its area is the average of its two heights times the width, and over all the slices that’s the average of the left and right sums. every inside point is the right edge of one slice and the left edge of the next, so it counts in full. the two ends count half each.
what carries over from sums
each one holds for every riemann sum, term by term, so it holds for the number the sums close in on. a sum of two heights splits into two sums, a constant factor comes out front, the slices of are the slices of and of together, and running from down to makes every negative, which flips the sign. what doesn’t carry over is a product: isn’t , because a sum of products isn’t a product of sums.
signed area against area
the integral counts a part under the axis negative, because its heights are negative. the area between the curve and the axis counts it positive: that’s , which in practice means splitting the interval where crosses zero and flipping the sign of the negative pieces. which one you want depends on the question. a displacement, a net change, money in and out: the integral. how much paint covers the region, how far a car drove counting the way back: the area.
the fundamental theorem
the running area, from out to a moving right end , is a function of , and its slope is the height of the curve there. moving a small step adds a strip about tall and wide, so grows by about : slope . drag in the figure and watch the two plots: the slope of the lower curve is the height of the upper one. the key idea section works the argument through.
drag x along the axis of the first plot, or tab to it and use the arrow keys
- x
- 1
- f(x), the height there
- 1.0000
- A(x), the area so far
- 0.3333
- the strip, A(x + h) − A(x), h = 0.25
- 0.3177
- the strip over h
- 1.2708
- slope of A at x
- f(x) = 1.0000
two plots, with a row of buttons for f(t) = t² and f(t) = sin t and a slider for the strip width h, 0.5, 0.25, 0.1, 0.05 or 0.01. the first plot draws f in cyan with the area under it shaded from 0 to a point x that drags along the axis, or moves with the arrow keys, and a strip h wide at x drawn as a rectangle f(x) tall. the second traces A(x), the area so far, in white up to x, with a cyan tangent whose slope is f(x). a readout gives x, f(x), A(x), the strip A(x + h) − A(x), the strip over h, and the slope of A. it opens on t² at x = 1 with h = 0.25: f(1) = 1, A(1) = 0.3333, the strip 0.3177, the strip over h 1.2708. with h = 0.01 the strip over h is 1.0100.
the second half, and the one that does the work. and have the same derivative, so they differ by a constant: . then , and is 0, so the difference is : the integral. no slices needed. find one function whose derivative is and take its change across the interval. the bracket is shorthand for .
antiderivatives
is an antiderivative of when , and is the whole family of them. every line here is a rule from the derivatives page, read backwards:
the power rule backwards, for every but : , so dividing by leaves . at that would divide by 0, and fills the gap, since its derivative is on both sides of 0. the : a constant has slope 0, so has the same derivative as , and nothing else does. sums and constant factors carry over from the derivative rules too: . check any antiderivative by differentiating it.
substitution
the chain rule run backwards. , so when the integrand has the shape , an inside function times its derivative, the antiderivative is . in practice: set , replace with , integrate in , and put back. with limits, move them too: becomes . one example:
with and . check: the derivative of is .
worked examples
the movie’s area, in one line
has derivative , so the fundamental theorem gives the number the slices were closing in on. from 0 to 2 the same antiderivative gives : eight times as much for twice the width, since the region is taller as well as wider.
distance from speed
a cart starts from rest and its speed grows as m/s. how far does it go in 4 s? the distance is the area under the speed graph:
the graph is a straight line from 0 to 12 m/s, so the region is a triangle, and agrees. the units work out too: m/s times s is m. with a starting speed, , the integral from 0 to 4 is m: the rectangle under the starting speed plus the same triangle.
signed area
the hump from 0 to has area 2 and the dip from to has area 2 under the axis, and the integral cancels them. the area between the curve and the axis is 4: split at and add the two sizes. the same for from 0 to 2: the integral is , while the area is 2, a dip of below the axis from 0 to 1 and a rise of above it from 1 to 2.
an antiderivative with a condition
a function has slope and passes through . integrate term by term: . the condition fixes : , so and . the is where the known point goes in. in motion it’s the same: a velocity integrates to a position plus a constant, and the constant is where the object started.
a substitution with limits
and . the limits move with it: is and is .
no need to go back to once the limits are in .
1/x, estimated and exact
. with three slices of width 1, the midpoint sum is and the trapezoid sum is : one a little under, the other a little over. that’s how it goes for any curve that bends upward across the interval, this one and included: a straight edge between two points on it lies above it, so the trapezoid overshoots, and the midpoint rectangle has the same area as the tangent trapezoid at the middle, which lies below.
the key idea up close
why the running area has slope f
let , the area from out to . move the right end a small step further. the area gains a thin strip:
across a strip that thin, barely changes. it stays between its smallest and largest values on , call them and , so the strip sits between a rectangle tall and one tall, both wide:
divide by :
as the strip shrinks onto the point , and is continuous, so both and close in on . the quotient is trapped between them, so it goes to too. and the quotient is the slope of :
that’s what the running area figure shows: for at the strip over its width reads 1.2708 with , 1.1033 with and 1.0100 with , against .
the second half follows. if too, then , and a function with slope 0 everywhere is a constant, so . at the area is 0, so . at : . a limit of sums, computed by one subtraction.
in code
riemann.py sums the slices by each rule and, run as a script, prints how the error shrinks for on as doubles from 4 to 128:
"""riemann sums: the area under a curve as n slices of equal width, each a height times the width, with the height read at the slice's left edge, its right edge or its middle, or the slice taken as a trapezoid. a slice under the axis counts negative. run as a script, it doubles n again and again for x² on [0, 1], whose exact area is 1/3, and prints how far each rule is off: the left and right errors about halve each time, the midpoint and trapezoid errors quarter."""
from __future__ import annotations
from dataclasses import dataclass
from typing import Callable
Fn = Callable[[float], float]
RULES = ("left", "right", "midpoint", "trapezoid")
def left_sum(f: Fn, a: float, b: float, n: int) -> float:
"""Return the sum of n rectangles under f on [a, b], each as tall as f at its left edge."""
dx = (b - a) / n
return sum(f(a + i * dx) for i in range(n)) * dx
def right_sum(f: Fn, a: float, b: float, n: int) -> float:
"""Return the sum of n rectangles under f on [a, b], each as tall as f at its right edge."""
dx = (b - a) / n
return sum(f(a + (i + 1) * dx) for i in range(n)) * dx
def midpoint_sum(f: Fn, a: float, b: float, n: int) -> float:
"""Return the sum of n rectangles under f on [a, b], each as tall as f at its middle."""
dx = (b - a) / n
return sum(f(a + (i + 0.5) * dx) for i in range(n)) * dx
def trapezoid_sum(f: Fn, a: float, b: float, n: int) -> float:
"""Return the sum of n trapezoids under f on [a, b]: the average of the left and right sums.
Every inside point is the right edge of one slice and the left edge of the next, so it counts in full;
the two ends count half each.
"""
dx = (b - a) / n
inside = sum(f(a + i * dx) for i in range(1, n))
return (f(a) / 2 + inside + f(b) / 2) * dx
def riemann_sum(f: Fn, a: float, b: float, n: int, rule: str) -> float:
"""Return the sum by the rule named: left, right, midpoint or trapezoid."""
sums = {"left": left_sum, "right": right_sum, "midpoint": midpoint_sum, "trapezoid": trapezoid_sum}
if rule not in sums:
raise ValueError(f"no rule named {rule!r}")
return sums[rule](f, a, b, n)
@dataclass(frozen=True)
class Row:
"""One n of the error table: each rule's error, sum − exact, and its ratio to the row before."""
n: int
error: dict[str, float]
ratio: dict[str, float | None]
def error_table(f: Fn, a: float, b: float, exact: float, ns: list[int]) -> list[Row]:
"""Return how each rule's error shrinks as n grows, one row per n.
The ratio of a row's error to the row before it says how much a step in n buys: with n doubling it
comes to about 1/2 for the left and right rules and 1/4 for midpoint and trapezoid.
"""
rows: list[Row] = []
for n in ns:
error = {rule: riemann_sum(f, a, b, n, rule) - exact for rule in RULES}
prev = rows[-1].error if rows else None
ratio = {rule: error[rule] / prev[rule] if prev else None for rule in RULES}
rows.append(Row(n, error, ratio))
return rows
def format_table(rows: list[Row]) -> str:
"""Return the table as text: n, the four errors to seven places, then the four ratios to three."""
lines = [f"{'n':>4} " + " ".join(f"{rule:>10}" for rule in RULES) + " ratios"]
for row in rows:
errors = " ".join(f"{row.error[rule]:>+10.7f}" for rule in RULES)
ratios = " ".join(" –" if row.ratio[rule] is None else f"{row.ratio[rule]:.3f}" for rule in RULES)
lines.append(f"{row.n:>4} {errors} {ratios}")
return "\n".join(lines)
if __name__ == "__main__":
print(format_table(error_table(lambda x: x * x, 0, 1, 1 / 3, [4, 8, 16, 32, 64, 128])))each row of the table is the four rules’ errors, the sum minus , and the ratio to the row above. the left and right ratios settle toward 0.5: from 64 slices to 128 the left error goes from −0.0078 to −0.0039, a ratio of 0.501. the midpoint and trapezoid ratios read 0.250 at every step: from 64 to 128 the midpoint error goes from −0.0000203 to −0.0000051. for the midpoint error is exactly and the trapezoid error exactly , so doubling quarters both, and the trapezoid sum misses by twice as much as the midpoint sum, the other way. the pattern is general: the one-edge rules have an error that shrinks like . in the midpoint and trapezoid rules, a slice’s over and under parts nearly cancel, and the error left shrinks like .
pitfalls
- the + C. is , not : has the same derivative. drop it and a condition like has nowhere to go. in a definite integral the constant cancels, , which is why it never shows up there
- area against signed area. , and the area between the sine and the axis over that stretch is 4. an integral that comes out 0 or negative isn’t a mistake, it’s a net. for an area, split where crosses zero and add the sizes
- isn’t 0. the two halves look like they cancel, but neither is finite: , which grows without bound as . the fundamental theorem needs across the whole interval, and has no derivative at 0. an integral across a point where blows up has to be taken as a limit, and this one has none
- isn’t . , while . a product needs substitution, or integration by parts, which is the product rule run backwards
- units. the integral of m/s over s is m, of m/s² over s is m/s, of watts over seconds is joules. the carries a unit, and the answer’s unit is the height’s times the width’s. an answer in m/s where you expected m means the was dropped, or a derivative was taken instead
- the limits flip the sign. : reversing the order turns every negative
- the inside’s derivative. is , not : differentiate and the chain rule puts a 2 out front. in the substitution , is where the comes from
practice
-
evaluate .
answer
an antiderivative is . take it at 3, minus at 0: .
-
for on with 4 slices of width 0.5, work out the left, right, midpoint and trapezoid sums, and compare them with the exact value.
answer
left edges 0, 0.5, 1, 1.5 give heights 0, 0.25, 1, 2.25, so the left sum is . right edges give heights 0.25, 1, 2.25, 4: the right sum is . middles 0.25, 0.75, 1.25, 1.75 give heights 0.0625, 0.5625, 1.5625, 3.0625: the midpoint sum is . the trapezoid sum is the average of left and right: . exact: . the left and right sums miss by 0.92 and 1.08, the midpoint sum is under by 0.04 and the trapezoid sum over by 0.08.
-
a cart moves at m/s. how far does it travel from to s, and which area formula checks it?
answer
m. the speed graph is a straight line from 2 to 14 m/s, so the region under it is a trapezoid: .
-
find , and then the area between the curve and the axis over the same interval.
answer
. the curve is under the axis from to 1 and over it from 1 to 2. the piece below: , so of area. the piece above: . the area is . the integral is 0 because the two pieces are the same size and count with opposite signs.
-
and . find .
answer
term by term, . then gives , so . check: , and .
-
evaluate by substitution.
answer
, , and the limits become to . then .