Riemann Sum Calculator
Approximate a definite integral by five different rules, see the panels each one actually uses drawn on the curve, and watch the error shrink as the subintervals double.
The approximation
Each panel
| Panel | Interval | Height | Area |
|---|---|---|---|
| 1 | [0.0000, 0.1250] | f(0.0625) = 0.003906 | 0.00048828 |
| 2 | [0.1250, 0.2500] | f(0.1875) = 0.035156 | 0.00439453 |
| 3 | [0.2500, 0.3750] | f(0.3125) = 0.097656 | 0.01220703 |
| 4 | [0.3750, 0.5000] | f(0.4375) = 0.191406 | 0.02392578 |
| 5 | [0.5000, 0.6250] | f(0.5625) = 0.316406 | 0.03955078 |
| 6 | [0.6250, 0.7500] | f(0.6875) = 0.472656 | 0.05908203 |
| 7 | [0.7500, 0.8750] | f(0.8125) = 0.660156 | 0.08251953 |
| 8 | [0.8750, 1.0000] | f(0.9375) = 0.878906 | 0.10986328 |
How fast it converges
Midpoint is a rule of order 2, so halving the step should divide the error by 4. The order column below is measured from the errors rather than quoted from the textbook, so you can watch it approach 2 — or fail to, which is itself informative.
| n | Approximation | Error | Error ratio | Observed order |
|---|---|---|---|---|
| 1 | 0.2500000000 | 8.333 × 10^−2 | — | — |
| 2 | 0.3125000000 | 2.083 × 10^−2 | 4.00 | 2.00 |
| 4 | 0.3281250000 | 5.208 × 10^−3 | 4.00 | 2.00 |
| 8 | 0.3320312500 | 1.302 × 10^−3 | 4.00 | 2.00 |
| 16 | 0.3330078125 | 3.255 × 10^−4 | 4.00 | 2.00 |
| 32 | 0.3332519531 | 8.138 × 10^−5 | 4.00 | 2.00 |
| 64 | 0.3333129883 | 2.035 × 10^−5 | 4.00 | 2.00 |
| 128 | 0.3333282471 | 5.086 × 10^−6 | 4.00 | 2.00 |
| 256 | 0.3333320618 | 1.272 × 10^−6 | 4.00 | 2.00 |
The picture is the point
A Riemann sum is taught by drawing it. The whole idea — that you can find the area under a curve by filling it with shapes whose areas you already know, and then using more and smaller shapes — is a visual one, and a calculator that returns 0.33203125 with no picture has removed the part that does the teaching.
So each rule here is drawn as the shape it genuinely uses. The endpoint and midpoint rules get rectangles. The trapezoidal rule gets trapezia with slanted tops, because that is the entire difference between it and a rectangle of the same area. Simpson’s rule gets parabolic arcs, because it fits a parabola through three points and drawing rectangles under a Simpson heading teaches the opposite of what the rule does.
The sample points are marked on the curve as well. Once you can see that the left rule always takes its height from the low side of a rising curve, its systematic underestimate stops being a fact to memorise.
The five rules
| Rule | Height taken from | Order | Error when the step halves |
|---|---|---|---|
| Left endpoint | The left edge of each strip | 1 | Halves |
| Right endpoint | The right edge of each strip | 1 | Halves |
| Midpoint | The centre of each strip | 2 | Quarters |
| Trapezoidal | Both edges, joined by a straight line | 2 | Quarters |
| Simpson’s | Both edges and the centre, joined by a parabola | 4 | Divides by sixteen |
The two endpoint rules are first order and nobody uses them for real computation. They are on the list because they are how the definition is introduced, and because seeing their error fall only half as fast is what makes the others worth the extra work.
Measuring the order, rather than quoting it
The convergence table is the part of this tool that does not exist elsewhere. It runs the chosen rule with 1, 2, 4, 8, … subintervals, compares each result against a high-accuracy value computed by an entirely different method, and reports the ratio of consecutive errors.
For a rule of order p, that ratio tends to 2p. So the midpoint rule shows ratios settling near 4, the trapezoidal rule near 4, and Simpson’s near 16 — and the measured order, which is the base-2 logarithm of the ratio, settles near 2, 2 and 4. You do not have to take the textbook’s word for any of it.
The early rows are usually off, and that is worth understanding rather than ignoring. The error formula is the leading term of a series, and when the step is large the other terms are not yet negligible. The order emerges as the step shrinks, which is exactly what “asymptotic” means in this context.
Once the error reaches about fifteen significant figures the table stops reporting an order at all. Below that level the difference between the approximation and the exact value is floating-point noise, and a ratio of one piece of noise to another is not a measurement. Most tools would print a number anyway.
Where the exact value comes from
The error column needs something to compare against. Using a finer Riemann sum would be circular — the same method grading its own homework — so the exact value comes from the double-exponential quadrature built for the derivative calculator, which is a completely different approach and typically accurate to machine precision.
When that method cannot converge, which happens for genuinely hard integrands, no error is shown at all rather than an error measured against a number that is itself wrong. The approximation is still reported, because the sum is still the sum.
Four mistakes this will catch
- Using n points instead of n strips. A left sum over n subintervals evaluates the function n times, at x0 through xn-1 — not at all n + 1 grid points. The panel table lists every evaluation, so an off-by-one is visible.
- Forgetting the h. The sum of the heights is not the area. Each has to be multiplied by the strip width, and the width column is shown for that reason.
- Using an odd n with Simpson. The rule pairs strips, so an odd count cannot work. This rounds up and tells you, rather than producing a number for a rule it did not run.
- Treating a negative area as a mistake. Below the axis the panels contribute negative area, and they are drawn in a different colour for that reason. An integral that comes out near zero over a symmetric interval is usually correct, not broken.
What it does not do
- Improper integrals — infinite limits, or a pole inside the interval.
- Gaussian quadrature and other rules with unevenly spaced points.
- Adaptive subdivision, which puts more strips where the function moves fastest.
- Error bounds from the derivative of the function, as opposed to the measured error.
- More than 500 subintervals, beyond which the drawing stops being readable.
Questions
Why does the midpoint rule beat the trapezoidal rule when it uses half as many points?
Because its errors cancel. On a curve that bends one way throughout, the midpoint rectangle cuts the corner on one side of the sample point and overshoots on the other, and the two almost cancel. A trapezium has both its corners on the curve and its straight top always falls on the same side of the arc, so nothing cancels. Both are second order, but the midpoint error is about half the trapezoidal one and has the opposite sign — which is where Simpson comes from, since a weighted combination of the two cancels the leading error term entirely.
What does "order of convergence" mean, and why is the measured one different from the textbook one?
A rule of order p has an error roughly proportional to hᵖ, so halving the step divides the error by 2ᵖ. The order column here is measured from the errors themselves rather than quoted, and it approaches the textbook value from above or below as n grows. Early rows are often off because the error formula is only the leading term of a series, and the other terms still matter when the step is large. Watching it settle is the useful part.
Why do some rows have no order at all?
Because the error has dropped to the limit of what a computer can represent, around fifteen significant figures. Below that the difference between the approximation and the exact value is rounding noise rather than method error, and the ratio of one piece of noise to another measures nothing. Printing a number there would be inventing precision, so the cell is left blank.
Simpson's rule gave the exact answer for my cubic. Is that a bug?
No, and it is the most interesting thing the rule does. Simpson's error term is proportional to the fourth derivative, and the fourth derivative of any cubic is zero everywhere — so the rule is exact on cubics despite only fitting parabolas. It is the standard demonstration that a method can be better than the argument used to derive it.
Why does it refuse when my function has a vertical asymptote in the interval?
Because the rule genuinely cannot be applied. If a sample point lands where the function is undefined, the sum contains an undefined term. Some tools skip that term and return a finite-looking number, which is not an approximation of the integral — it is the sum of the other rectangles, which answers no question anyone asked. The integral may still exist as an improper integral; the derivative calculator handles those properly.
Does the number of subintervals have to be even?
Only for Simpson's rule, which fits one parabola across each pair of subintervals and so needs them in pairs. If you ask for an odd number it is rounded up and the result says so, rather than quietly answering a different question.