Calculus, Linear Algebra & Discrete Math Integrals & Applications of Integration Riemann integration; composite Newton–Cotes rules

Riemann Sum Calculator (Left, Right, Midpoint)

Enter a function, an interval and a number of subintervals, and this calculator builds the Riemann sum term by term: it shows Δx, every sample point, the height of every rectangle and the area it contributes. It then evaluates the integral to high accuracy so you can see exactly how far the approximation sits from the true value, and how quickly that gap closes when you double n. Left, right and midpoint sampling are all here, plus the trapezoid rule for comparison.

Calculator

This calculator runs in your browser. Enable JavaScript for live results — the inputs, formula and worked example below remain fully readable without it.

Inputs this calculator takes, with typical values
InputWhat to enterExample
Function f(x)Use x as the variable. Available: + - * / ^, sin, cos, tan, exp, ln, log10, sqrt, abs, sinh, cosh, tanh, pi, e.x^2
Lower limit aLeft end of the interval of integration.0
Upper limit bRight end of the interval. Entering b below a gives a negative Δx and a signed sum.1
Number of subintervals nHow many rectangles the interval is cut into.4
Sampling ruleWhich point in each subinterval supplies the rectangle height.Left endpoints

It returns

  • Riemann sum — The total signed area of the rectangles (or trapezoids) for your rule and n.
  • Width of each subinterval Δx
  • Exact integral (high-accuracy) — Composite Simpson's rule on 2,000 panels, used as the reference value.
  • Absolute error
  • Relative error
  • Error when n is doubled — Compare with the absolute error above to see the convergence rate of the rule.

The formula

k=0n1f(xk*)Δx
T=Δx[f(a)2+k=1n1f(xk)+f(b)2]

In plain text: Δx = (b − a)/n; S = Σ f(xₖ*) Δx with xₖ* the left, right or midpoint of subinterval k

  • a, bThe limits of integration
  • nNumber of subintervals (rectangles)
  • ΔxCommon width of each subinterval, (b − a)/n
  • xₖ*The sample point in subinterval k: a + kΔx (left), a + (k+1)Δx (right), or a + (k+½)Δx (midpoint)

The definite integral is the limit of this sum as n → ∞, and for a continuous function on a closed interval that limit exists and is the same for every choice of sample point.

Updated Category Integrals & Applications of Integration Verified against published test cases Reading time 11 min

What a Riemann sum is

A Riemann sum replaces the area under a curve with a row of rectangles. You cut [a, b] into n pieces of equal width Δx = (b − a)/n, pick one point in each piece, use the height of the curve there as the height of a rectangle, and add up the areas. The result approximates ∫f(x)dx, and Riemann's definition of the integral is the statement that as n → ∞ this sum converges to a single number no matter which sample point you choose in each piece.

That last clause is the reason left, right and midpoint sums all appear in a first calculus course. They give different answers for finite n and the same answer in the limit, which is what makes the integral well defined. Watching them converge to a common value is how the definition stops being an abstraction.

The sum is signed. Where f is negative the rectangle height is negative and its contribution subtracts, so a Riemann sum for sin x over [0, 2π] comes out near zero rather than near 4. If you want geometric area rather than signed area, integrate abs(f(x)) — the calculator accepts abs for exactly this reason.

Riemann sums are not only pedagogy. Every quadrature rule in numerical analysis is a weighted sum of function values on a grid, and the left, right, midpoint and trapezoid rules are the simplest members of that family. If you have data rather than a formula, a sum like this is the only integral available to you.

The three sampling rules, and why the midpoint rule wins

Each rule differs only in where xₖ* sits inside subinterval k. The left rule takes xₖ* = a + kΔx for k = 0…n−1; the right rule takes xₖ* = a + kΔx for k = 1…n; the midpoint rule takes xₖ* = a + (k + ½)Δx. The trapezoid rule is not a Riemann sum at all — it averages the two edge heights — but it is the natural comparison because it equals the average of the left and right sums.

On a function that is increasing throughout the interval, the left rule samples the lowest point of each subinterval and therefore underestimates, while the right rule samples the highest and overestimates. Reverse both statements on a decreasing function. Those are the only two monotone cases; on a function that rises and falls the two errors partly cancel and no such rule holds.

The midpoint rule is different in kind. Its error term is −(b − a)h²f''(ξ)/24 against the trapezoid rule's +(b − a)h²f''(ξ)/12, where h = Δx. Two things follow. First, both are second order, so halving Δx divides the error by about four, while the left and right rules are only first order and halve it. Second, the midpoint error is half the size of the trapezoid error and carries the opposite sign — so for a function with a consistent concavity, the midpoint rule is the better of the two and the true value lies between them.

The geometric reason the midpoint rule does so well is worth seeing. Its rectangle has the same area as the trapezoid formed by the tangent line at the midpoint, and the tangent line matches the curve to first order. The rectangle overshoots on one half of the subinterval by nearly exactly what it undershoots on the other, so the linear part of the error cancels and only the curvature term survives.

Worked example: f(x) = x² on [0, 1] with n = 4

Here Δx = (1 − 0)/4 = 0.25 and the grid is 0, 0.25, 0.5, 0.75, 1. The exact integral is [x³/3]₀¹ = 1/3 ≈ 0.333333.

  1. Left sum. Heights at 0, 0.25, 0.5, 0.75 are 0, 0.0625, 0.25, 0.5625. They add to 0.875, and 0.875 × 0.25 = 0.21875. The error is 0.333333 − 0.21875 = 0.114583, an underestimate as expected for an increasing function.
  2. Right sum. Heights at 0.25, 0.5, 0.75, 1 are 0.0625, 0.25, 0.5625, 1, adding to 1.875, and 1.875 × 0.25 = 0.46875. The error is 0.135417, an overestimate.
  3. Check the relationship. Right minus left is 0.46875 − 0.21875 = 0.25, which must equal [f(b) − f(a)]Δx = (1 − 0)(0.25) = 0.25. Every interior height appears in both sums, so only the two ends survive the subtraction — a fast way to get one sum from the other.
  4. Midpoint sum. Sample points are 0.125, 0.375, 0.625, 0.875, with heights 0.015625, 0.140625, 0.390625, 0.765625. They add to 1.3125, and 1.3125 × 0.25 = 0.328125. The error is only 0.005208 — about 22 times smaller than the left sum's error from the same four evaluations.
  5. Trapezoid. The average of the left and right sums is (0.21875 + 0.46875)/2 = 0.34375, with error 0.010417 — exactly twice the midpoint error, and of opposite sign, as the error formulas predict.

Doubling to n = 8 gives a left sum of 0.2734375, with error 0.059896. Compared with 0.114583 at n = 4, that is a factor of 1.91 — heading towards the factor of 2 that first-order convergence promises.

How to read the error and the convergence rate

Read the absolute error first, then the ratio between it and the error at double n. That ratio is the practical signature of the rule: near 2 for left and right sums, near 4 for midpoint and trapezoid, on any function with a continuous second derivative. If your ratio is far from the expected value, something in the problem violates the assumptions — a kink, an infinite derivative at an endpoint, or a wildly oscillatory integrand sampled too coarsely.

A ratio much better than expected is a real phenomenon, not a bug. Integrating a periodic function over an exact whole number of periods makes the trapezoid rule converge spectacularly fast, because the endpoint error terms cancel identically. Symmetry can also make an error vanish outright: the midpoint rule is exact for any straight line, and the trapezoid rule is too.

Relative error is the figure to quote when the integral is large; absolute error is the one that matters when the integral is near zero, where relative error is meaningless or undefined. The calculator returns relative error only when the reference integral is non-zero, which is why that field is blank for a function that integrates to zero over a symmetric interval.

Finally, remember that increasing n is not free and not unboundedly useful. Each doubling doubles the function evaluations, and once the accumulated round-off in summing n terms is comparable with the truncation error, more rectangles stop helping. For a smooth integrand, moving to a higher-order rule — Simpson, Gauss–Legendre — buys far more than more rectangles of a low-order rule.

Reference: approximating ∫₀¹ x² dx = 1/3

Every entry is the calculator's own output for f(x) = x² on [0, 1]. Exact value 0.333333…
nLeftRightMidpointTrapezoid
1010.250.5
20.1250.6250.31250.375
40.218750.468750.3281250.34375
80.27343750.39843750.332031250.3359375
160.3027343750.3652343750.33300781250.333984375
320.31787109380.34912109380.33325195310.3334960938

Left and right errors halve per row; midpoint and trapezoid errors quarter per row. The midpoint error is always half the trapezoid error and on the opposite side of the true value, because x² has constant positive curvature.

Pitfalls and limits of this method

  • Confusing signed area with geometric area. Below the axis the contributions are negative. Integrate abs(f(x)) if you want total area, and expect slower convergence because the absolute value introduces a kink.
  • Sampling a singularity. A left sum of 1/x starting at a = 0 evaluates f(0) and fails. The midpoint rule avoids both endpoints and is the standard fix for an integrable endpoint singularity.
  • Assuming left always underestimates. That holds only where f is increasing. On a function that turns inside the interval, neither the left nor the right sum is a reliable bound.
  • Trusting a small error at small n. Errors can cancel by accident. Convergence is a property of the sequence, so check that the ratio between successive errors matches the rule's order before you believe a single result.
  • Using equal subintervals on a peaked integrand. A function with a narrow spike needs its rectangles concentrated there; a uniform grid either misses the spike or wastes thousands of evaluations on flat regions. Adaptive quadrature exists for this.
  • Forgetting that this calculator uses uniform Δx. Riemann's definition allows unequal widths, provided the largest width tends to zero. Non-uniform partitions matter in theory and in adaptive methods, but not here.

How the reference value is obtained

The "exact integral" shown is composite Simpson's rule on 2,000 panels, not a symbolic antiderivative. For a smooth integrand its error is of order h⁴ with h = (b − a)/2000, which is well below the precision displayed. It is not exact for a function with a singularity or a discontinuous derivative on the interval, and in that case the error figures should be read as indicative. If you need the length of the curve rather than the area under it, use the arc length calculator, which integrates √(1 + f'²) by the same quadrature machinery.

Where Riemann sums lead

Simpson's rule is the next step up and costs nothing extra in evaluations: fit a parabola through each consecutive triple of grid points instead of a line through each pair, and the order jumps from two to four. Combining the trapezoid rule with Richardson extrapolation gives Romberg integration, which reaches machine precision on smooth integrands in a few dozen evaluations. Gauss–Legendre quadrature abandons the uniform grid entirely and places nodes where they do the most good.

Applications of the definite integral all reduce to setting up the right integrand and then approximating it. The volume of a solid of revolution is π∫R² dx, which the disk and washer calculator evaluates; arc length is ∫√(1 + f'²) dx. Both need the same convergence judgement you are making here, and both inherit any roughness in f.

The derivative side of the same coin is the numerical derivative calculator. Compare their behaviour: integration is stable and forgiving because averaging suppresses noise, while differentiation amplifies it by 1/h. That asymmetry is why you can integrate noisy data usefully but should never differentiate it without smoothing first.

If your integrand is a limit rather than a function value — an integrand of the form 0/0 at an endpoint, say — resolve the limit first with the L'Hôpital's rule calculator and integrate the resulting continuous extension. And when the region you want is bounded by a curve you must locate first, find the intersection with the bisection method calculator before choosing the limits.

Frequently asked questions

Which Riemann sum is most accurate?

The midpoint rule, by a wide margin, among the three sampling choices. Its error is second order in Δx while the left and right rules are only first order, so at n = 4 on ∫₀¹x²dx the midpoint error is 0.005208 against the left rule's 0.114583 — about 22 times smaller for the same four function evaluations. The midpoint error is also half the trapezoid error.

Does the left sum always underestimate the integral?

Only when f is increasing across the whole interval. Then each rectangle is drawn at the lowest point of its subinterval, so every term is too small. If f is decreasing, the left sum overestimates instead. On a function that both rises and falls, the two effects partly cancel and no general bound applies.

How many rectangles do I need for a given accuracy?

For the left or right rule, the error is bounded by (b − a)²·max|f'|/(2n), so tenfold accuracy needs ten times as many rectangles. For the midpoint rule the bound is (b − a)³·max|f''|/(24n²), so tenfold accuracy needs only about three times as many. Both formulas need the relevant derivative to be bounded on the interval.

Why is the trapezoid rule not a Riemann sum?

Because it uses two function values per subinterval rather than one, so it is not of the form Σf(xₖ*)Δx. It is included here because it equals the exact average of the left and right sums, which makes it a useful cross-check: if your left and right values are right, their average must match the trapezoid value.

What does a negative Riemann sum mean?

That the signed area is negative — the curve spends more of the interval below the axis, weighted by how far below. This is the correct answer for a definite integral. A negative value can also come from entering b below a, which makes Δx negative and flips the sign of the whole sum, matching the convention ∫ₐᵇ = −∫ᵦᵃ.

Can I use a Riemann sum on data instead of a formula?

Yes, and it is often the only option. With readings at equal spacing h, the trapezoid rule h[y₀/2 + y₁ + … + yₙ₋₁ + yₙ/2] is the standard choice because it uses every point once. The midpoint rule is unavailable unless you have values between your samples. This page needs a formula because it evaluates the function itself.

Why does the error stop improving when n gets very large?

Because adding n floating-point numbers accumulates round-off that grows roughly like √n times machine epsilon times the size of the terms. Once that is comparable with the truncation error, more rectangles cannot help. For a first-order rule this happens far beyond any n you would use by hand; for a fourth-order rule it can appear at a few thousand panels.

Is the midpoint rule exact for anything?

Yes — for every linear function, on every interval and every n. Its error term is proportional to f'', which vanishes for a straight line. The trapezoid rule is exact for linear functions too. Simpson's rule, with an error term in f⁽⁴⁾, is exact for every cubic, which is why it is so much stronger for the same number of nodes.

What if the function is undefined at one endpoint?

Switch to the midpoint rule, which never evaluates f at a or b. That handles an integrable singularity such as 1/√x at the origin, though convergence will be slower than the usual second order because the error bound assumes a bounded second derivative. The reference integral shown on this page uses Simpson's rule, which does evaluate the endpoints, so its value will be blank in that case.

References