Skip to main content

Riemann sums and the definite integral

Cut an interval into equal strips, take a height in each, and add height times width: a Riemann sum. For x squared on [0, 1] the right sums have a closed form that tends to exactly 1/3, and that limit is the definite integral. Compute the sums by hand, by a loop and with NumPy.
M04.5.2L1 Core25 minIn review

Before this lesson

M04.5.1 Distance from speed: adding up thin slices M01.6.4 Contradiction and induction M04.1.3 One-sided limits and infinity

After this lesson you can

  • Compute left, right and midpoint sums by hand for a function on an interval cut into equal strips.
  • Derive the closed form (n + 1)(2n + 1)/(6n^2) of the right sum of x^2 on [0, 1], using the sum of the first n squares proved by induction, and find its limit 1/3.
  • State the definition of the definite integral as a limit of Riemann sums, and name each part of its notation.
  • Write a riemann_sum function with a loop, then vectorise it with NumPy.

In review. The maths and code on this page are checked by automated tests on every change, but an independent reader has not signed it off yet. Spot a mistake or a confusing step? Tell us.

The question

The region under the curve \({y = x^2}\), from \({x = 0}\) to \({x = 1}\), is not a rectangle, a triangle or a circle, so no school formula gives its area. Rectangles can come as close to it as we like, but every finite set of them is a little off. How can rectangles give an exact number? The answer, a limit of sums, is the definite integral. In machine learning it is the quantity behind every probability and every expected value of a continuous variable, and behind the area under an ROC curve.

From the last lesson

1. For a speed that only rises, which of the left and right sums is too small?

Show answer

The left sum, because each of its rectangles uses the lowest speed on its slice.

2. What is the gap between the right and left sums with \(n\) equal slices from \(a\) to \(b\)?

Show answer

\({(v(b) - v(a))\,\Delta t}\) with \({\Delta t = (b - a)/n}\): the rise in height times one slice width.

Intuition

Lesson M04.5.1 trapped a distance between two sums of rectangles. The same rectangles work for any curve. Cut the interval into strips, stand a rectangle on each strip, and add the rectangles’ areas. The thinner the strips, the less each rectangle overshoots or falls short of the curve.

Three graphs of the blue curve y equals x squared from 0 to 1, over a grey region. Each has vermillion rectangles standing on equal strips, as tall as the curve at the right end of each strip, with the part of every rectangle above the curve shaded purple and labelled overshoot in the first panel. The panels have 4, 8 and 16 strips and are titled 4 strips: 0.469, 8 strips: 0.398 and 16 strips: 0.365. A line underneath says: the right sums fall towards 1/3, the area under the curve, as the overshoot shrinks.
M04.5.2-F1

Riemann sums refining. Each time the strips halve, the overshoot shrinks and the sum of the rectangles falls closer to the area under the curve, 1/3.

The sums 0.469, 0.398 and 0.365 are settling down. The number they settle on is the area, and this lesson proves that it is exactly 1/3.

Build it up

Step 1. Strips. Cut the interval from \(a\) to \(b\) into \(n\) strips of equal width

\[ \cparam{\Delta x} = \frac{b - a}{n} . \]

The cut points are \({x_k = a + k\,\Delta x}\) for \({k = 0, 1, \dots, n}\), and strip \(k\) runs from \(x_{k-1}\) to \(x_k\), for \(k\) from 1 to \(n\).

Step 2. A height for each strip. Choose a sample point \(x_k^*\) somewhere in strip \(k\) and use \({f(x_k^*)}\) as the rectangle’s height. The three common choices are the left end, the right end and the middle of the strip.

Three graphs of y equals x squared on 0 to 1, each cut into four strips. In each, a vermillion dot on the curve marks where every strip takes its height, and the rectangles reach exactly that height. Left panel, titled Left end: 0.21875: dots at the left edges, rectangles below the curve. Middle panel, titled Midpoint: 0.328125: dots at the middle of each strip, rectangles that cross the curve, with a pointer saying height taken at the dot. Right panel, titled Right end: 0.46875: dots at the right edges, rectangles above the curve.
M04.5.2-F2

One set of strips, three choices of height. The dot in each strip is its sample point. The midpoint rectangles cross the curve, so their overshoots and shortfalls nearly cancel.

Step 3. The Riemann sum. Add height times width over all the strips:

\[ \sum_{k=1}^{n} f(x_k^*)\,\Delta x . \]

Any sum of this form is a Riemann sum. The left, right and midpoint sums take \({x_k^* = x_{k-1}}\), \({x_k^* = x_k}\) and the middle of the strip. We write them \({\text{Left}(n)}\), \({\text{Right}(n)}\) and \({\text{Mid}(n)}\).

Step 4. Let the strips shrink. For \({f(x) = x^2}\) the curve rises from 0 to 1, so by lesson M04.5.1 the true area lies between the left and right sums, whose gap is \({(f(1) - f(0))\,\Delta x = 1/n}\). To pin the area down we need the value those sums approach as \(n\) grows. For the right sum we can find it exactly.

The formal statement

The right sum in closed form. On \({[0, 1]}\) the strip width is \({\Delta x = 1/n}\) and the right ends are \({x_k = k/n}\). So

\[ \begin{aligned} \text{Right}(n) &= \sum_{k=1}^{n} \Big(\frac{k}{n}\Big)^2 \cdot \frac{1}{n} \\ &= \frac{1}{n^3} \sum_{k=1}^{n} k^2 . \end{aligned} \]

The factor \({1/n^3}\) is the same in every term, so it comes outside the sigma (the constant-factor rule of M01.7.2). What is left is the sum of the first \(n\) squares.

The sum of the first \(n\) squares. For every whole number \({n \ge 1}\),

\[ \sum_{k=1}^{n} k^2 = \frac{n(n + 1)(2n + 1)}{6} . \]

Proof by induction (M01.6.4). At \({n = 1}\) the left side is 1 and the right side is \({1 \times 2 \times 3 / 6 = 1}\). Now suppose the formula holds for some \(n\). Adding the next square,

\[ \begin{aligned} &\sum_{k=1}^{n+1} k^2 \\ &\quad = \frac{n(n + 1)(2n + 1)}{6} + (n + 1)^2 \\ &\quad = \frac{(n + 1)\big[n(2n + 1) + 6(n + 1)\big]}{6} \\ &\quad = \frac{(n + 1)(2n^2 + 7n + 6)}{6} \\ &\quad = \frac{(n + 1)(n + 2)(2n + 3)}{6} . \end{aligned} \]

The third line takes out the common factor \({n + 1}\), the fourth multiplies out the bracket, and the last factorises \({2n^2 + 7n + 6}\) as \({(n + 2)(2n + 3)}\). Since \({(n + 1) + 1 = n + 2}\) and \({2(n + 1) + 1 = 2n + 3}\), the result is the formula with \({n + 1}\) in place of \(n\). So the formula holds for every \(n\).

The closed form and its limit. Put the formula into the right sum. One factor \(n\) cancels:

\[ \begin{aligned} \text{Right}(n) &= \frac{1}{n^3} \cdot \frac{n(n + 1)(2n + 1)}{6} \\ &= \frac{(n + 1)(2n + 1)}{6n^2} . \end{aligned} \]

Multiply out the top, \({(n + 1)(2n + 1) = 2n^2 + 3n + 1}\), and divide each term by \({6n^2}\):

\[ \text{Right}(n) = \frac{1}{3} + \frac{1}{2n} + \frac{1}{6n^2} . \]

As \(n\) grows, \(1/n\) tends to 0 (M04.1.3), so the last two terms vanish and

\[ \lim_{n \to \infty} \text{Right}(n) = \frac{1}{3} . \]

The left sums agree. The gap is \({1/n}\), so \({\text{Left}(n) = \text{Right}(n) - 1/n}\), which also tends to 1/3. Every midpoint height lies between the left and right heights of its strip, so \({\text{Mid}(n)}\) is squeezed between the two and tends to 1/3 as well (the squeeze theorem of M04.1.2).

NoteDefinition: the definite integral

For a function \(f\) on \({[a, b]}\), cut into \(n\) equal strips of width \({\Delta x = (b - a)/n}\) with a sample point \(x_k^*\) in strip \(k\), the definite integral of \(f\) from \(a\) to \(b\) is

\[ \ctruth{\int_a^b f(x)\,dx} = \lim_{n \to \infty} \sum_{k=1}^{n} f(x_k^*)\,\cparam{\Delta x} , \]

when this limit exists and is the same for every choice of sample points.

A diagram in two rows joined by three green arrows marked let n tend to infinity. Top row, in vermillion: a sigma from k equals 1 to n, then x sub k squared, then delta x in orange, captioned add the n strips, the height of strip k, and the width of a strip. Bottom row: an integral sign from 0 to 1, x squared in blue and dx in orange, followed by equals one third, captioned a long S, the sum, with 0 and 1 the limits of integration; the integrand, each strip's height; and the width, shrunk to nothing.
M04.5.2-F5

Each part of the notation comes from a part of the sum. The sigma becomes the integral sign, the height \(x_k^2\) becomes the integrand \(x^2\), and the width \({\Delta x}\) becomes \(dx\).

The parts have names. The function \(f\) is the integrand, \(a\) and \(b\) are the limits of integration, and \(x\) is the variable of integration. Like the index of a sigma, \(x\) is a dummy: \({\int_0^1 x^2\,dx}\) and \({\int_0^1 t^2\,dt}\) are the same number. We have proved

\[ \int_0^1 x^2\,dx = \frac{1}{3} . \]

When does the limit exist? For every continuous function on \({[a, b]}\) it does, and every choice of sample points gives the same limit. This theorem is stated here without proof; its proof uses a property of the real numbers that belongs to analysis. A function with a few jumps, such as a step, has an integral too (lesson M04.6.2 uses one), but every function in this module is continuous where we integrate it.

Go deeper: why every choice of sample point gives the same limit, for a rising function

Suppose \(f\) never falls on \({[a, b]}\). In strip \(k\) any sample point \(x_k^*\) lies between \(x_{k-1}\) and \(x_k\), so \({f(x_{k-1}) \le f(x_k^*) \le f(x_k)}\). Multiply by \({\Delta x > 0}\) and add over the strips:

\[ \begin{aligned} \text{Left}(n) &\le \sum_{k=1}^{n} f(x_k^*)\,\Delta x \\ &\le \text{Right}(n) . \end{aligned} \]

The gap between the outer sums is \({(f(b) - f(a))\,\Delta x}\) (M04.5.1, Result 2), which tends to 0. So once either outer sum is known to tend to a number, every Riemann sum tends to the same number, by the squeeze theorem. For \({x^2}\) on \({[0, 1]}\) we found that number exactly: whatever sample points you choose, the sums tend to 1/3. A function that never rises works the same way, with the inequalities turned round.

Worked example

Find the left, right and midpoint sums of \({f(x) = x^2}\) on \({[0, 1]}\) with four strips.

Here \({\Delta x = 1/4}\) and the cut points are 0, 1/4, 1/2, 3/4 and 1, where the heights are 0, 1/16, 4/16, 9/16 and 16/16. The midpoints are 1/8, 3/8, 5/8 and 7/8, where the heights are 1/64, 9/64, 25/64 and 49/64.

A table built from boxes. Columns: the four strips, 0 to a quarter, a quarter to a half, a half to three quarters and three quarters to 1, then add, then times a quarter. Row left end: 0, 1/16, 4/16, 9/16, adding to 14/16, which times a quarter is 0.21875. Row right end: 1/16, 4/16, 9/16, 16/16, adding to 30/16, giving 0.46875. Row midpoint: 1/64, 9/64, 25/64, 49/64, adding to 84/64, giving 0.328125 in a yellow box. A note underneath: heights, the strip's value of x squared at that point.
M04.5.2-F3

The hand computation, one row per rule: four heights, their total, and the total times the strip width, which gives the sum.

Each sum is the total of its heights times \({\Delta x = 1/4}\):

\[ \begin{aligned} \text{Left}(4) &= \tfrac14 \cdot \tfrac{0 + 1 + 4 + 9}{16} = \tfrac{14}{64} , \\ \text{Right}(4) &= \tfrac14 \cdot \tfrac{1 + 4 + 9 + 16}{16} = \tfrac{30}{64} , \\ \text{Mid}(4) &= \tfrac14 \cdot \tfrac{1 + 9 + 25 + 49}{64} = \tfrac{84}{256} . \end{aligned} \]

As decimals these are 0.21875, 0.46875 and 0.328125. The right sum’s numerator, \({1 + 4 + 9 + 16}\), is the sum of the first four squares, 30, and the closed form agrees: with \({n = 4}\) it gives \({(5 \times 9)/(6 \times 16)}\), which is \({45/96 = 0.46875}\).

Compare with the exact area 1/3, about 0.33333. The left sum is short by 0.115 and the right sum over by 0.135, but the midpoint sum is short by only 0.0052, more than twenty times closer. Its rectangles cross the curve, so in each strip the part that overshoots nearly cancels the part that falls short.

Code

One function covers all three rules. The sample point sits a fraction shift of the way across its strip: 0 for the left end, 0.5 for the middle and 1 for the right end.

def riemann_sum(f, a, b, n, rule="mid"):
    """A Riemann sum of f over [a, b] with n equal strips."""
    dx = (b - a) / n
    shift = {"left": 0.0, "mid": 0.5, "right": 1.0}[rule]
    total = 0.0
    for k in range(n):                  # strip k + 1 runs from a + k dx to a + (k + 1) dx
        x = a + (k + shift) * dx        # its sample point
        total = total + f(x) * dx       # height times width
    return total

def f(x):
    return x ** 2

for rule in ["left", "right", "mid"]:
    print(f"{rule:5s} sum, 4 strips: {riemann_sum(f, 0, 1, 4, rule)}")
left  sum, 4 strips: 0.21875
right sum, 4 strips: 0.46875
mid   sum, 4 strips: 0.328125

Now check the closed form against the loop, and watch the error shrink.

for n in [4, 16, 64, 256, 1024]:
    loop = riemann_sum(f, 0, 1, n, "right")
    closed = (n + 1) * (2 * n + 1) / (6 * n ** 2)
    print(f"n = {n:4d}: loop {loop:.6f}, closed form {closed:.6f}, error {loop - 1 / 3:.6f}")
n =    4: loop 0.468750, closed form 0.468750, error 0.135417
n =   16: loop 0.365234, closed form 0.365234, error 0.031901
n =   64: loop 0.341187, closed form 0.341187, error 0.007853
n =  256: loop 0.335289, closed form 0.335289, error 0.001956
n = 1024: loop 0.333822, closed form 0.333822, error 0.000488

The error is \({1/(2n) + 1/(6n^2)}\), as derived. Each row has four times as many strips as the one before and about a quarter of its error, so doubling \(n\) about halves the error. A loop over a million strips is slow in plain Python. NumPy makes all the sample points at once with numpy.arange and adds the heights with numpy.sum.

import numpy as np

def riemann_sum_np(f, a, b, n, rule="mid"):
    """The same sum, with every sample point made at once."""
    dx = (b - a) / n
    shift = {"left": 0.0, "mid": 0.5, "right": 1.0}[rule]
    x = a + (np.arange(n) + shift) * dx     # all n sample points
    return np.sum(f(x)) * dx

print("NumPy, 4 strips:", riemann_sum_np(f, 0, 1, 4, "mid"))
print(f"NumPy, a million strips: {riemann_sum_np(f, 0, 1, 1_000_000, 'mid'):.10f}")
NumPy, 4 strips: 0.328125
NumPy, a million strips: 0.3333333333

With a million strips the midpoint sum matches 1/3 to at least nine decimal places. The last few digits depend on rounding inside the computer, so the check allows for that.

Explore

M04.5-X1

Riemann refiner

Choose \({y = x^2}\) and the right rule, then press Double the strips a few times: the sum falls towards 1/3 and the error about halves each time. Switch to the midpoint rule and the error is far smaller and falls by about four. Try the cart’s speed and the bell curve, and the cart run on to 5 s, whose curve dips below the axis.

Three graphs of y equals x squared from 0 to 1 with right-end rectangles on 4, 8 and 16 strips, titled 4 strips: 0.469, 8 strips: 0.398 and 16 strips: 0.365. The part of each rectangle above the curve is shaded purple, and it shrinks as the strips multiply.

Pitfalls

Two plots against the number of strips n, at 4, 8, 16, 32 and 64. Left, marked with a cross and titled Forget the width: vermillion dots for the sum of the right-end heights of x squared alone, 1.88, 3.19, 5.84, 11.17 and 21.84, climbing ever faster. Right, marked with a tick and titled Height times width: the same sums multiplied by delta x, 0.469, 0.398, 0.365, 0.349 and 0.341, levelling off just above a dashed line labelled area 1/3.
M04.5.2-F4

Wrong beside right. Without the widths the total grows without limit, roughly like \({n/3}\). With them it settles on the area.

“A Riemann sum adds up the heights.”

It adds up heights times widths. Leave out \({\Delta x}\) and the total grows with the number of strips: for \({x^2}\) the right-end heights alone add to 1.875 with 4 strips and 21.8 with 64. In code this is the missing * dx, a common bug when an expected value or an area is computed from samples.

“The midpoint sum is the average of the left and right sums.”

The average of 0.21875 and 0.46875 is 0.34375, but the midpoint sum is 0.328125. The midpoint rule takes the height of the curve at the middle of each strip, while the average of the two sums averages the heights at the ends. For a curve that bends upwards, like \({x^2}\), the middle height is lower. The average of the two sums is a rule of its own, the trapezoid rule of lesson M04.5.4.

“With enough strips, the sum is the integral.”

A finite sum is in general only an approximation. For \({x^2}\) the right sum is \({1/3 + 1/(2n) + 1/(6n^2)}\), which is above 1/3 for every \(n\). The integral is the limit, the one number that all the sums approach.

Check yourself

Recall

1. What is the strip width for \(n\) equal strips on \({[a, b]}\)?

Show answer

\({\Delta x = (b - a)/n}\).

2. Where does the midpoint rule take each strip’s height?

Show answer

At the middle of the strip, \({(x_{k-1} + x_k)/2}\), where the height is \({f\big((x_{k-1} + x_k)/2\big)}\).

3. What is \({1^2 + 2^2 + \dots + n^2}\) in closed form, and how was it proved?

Show answer

\({n(n + 1)(2n + 1)/6}\), proved by induction: true for \({n = 1}\), and true for \({n + 1}\) whenever it is true for \(n\).

4. Write the definition of \({\int_a^b f(x)\,dx}\).

Show answer

\[ \int_a^b f(x)\,dx = \lim_{n \to \infty} \sum_{k=1}^{n} f(x_k^*)\,\Delta x , \]

with \({\Delta x = (b - a)/n}\) and a sample point \(x_k^*\) in each strip.

5. In \({\int_0^1 x^2\,dx}\), name the integrand and the limits of integration, and give its value.

Show answer

The integrand is \({x^2}\), the limits of integration are 0 and 1, and the value is 1/3.

Exercises

1. ★ Find the left, right and midpoint sums of \({x^2}\) on \({[0, 1]}\) with two strips, and check the right sum against the closed form.

Show solution

With \({\Delta x = 1/2}\) the cut points are 0, 1/2 and 1, with heights 0, 1/4 and 1; the midpoints 1/4 and 3/4 have heights 1/16 and 9/16.

\[ \begin{aligned} \text{Left}(2) &= \tfrac12 \,(0 + \tfrac14) = \tfrac18 , \\ \text{Right}(2) &= \tfrac12 \,(\tfrac14 + 1) = \tfrac58 , \\ \text{Mid}(2) &= \tfrac12 \,(\tfrac{1}{16} + \tfrac{9}{16}) = \tfrac{5}{16} . \end{aligned} \]

As decimals these are 0.125, 0.625 and 0.3125. The closed form gives \({(3 \times 5)/(6 \times 4)}\), which is \({15/24 = 5/8}\), as it should.

2. ★★ Find the smallest \(n\) for which the right sum of \({x^2}\) on \({[0, 1]}\) is within 0.001 of 1/3.

Show solution

The error is \({1/(2n) + 1/(6n^2)}\), and it is more than \({1/(2n)}\). So we need \({1/(2n) < 0.001}\), that is \({n > 500}\). At \({n = 500}\) the error is \({0.001 + 1/1\,500\,000}\), just too big. At \({n = 501}\) it is

\[ \begin{aligned} &\tfrac{1}{1002} + \tfrac{1}{6 \times 501^2} \\ &\quad \approx 0.000998 + 0.0000007 , \end{aligned} \]

which is below 0.001. So \({n = 501}\).

3. ★★★ Show that \({\int_0^b x^2\,dx = b^3/3}\) for any \({b > 0}\), using right sums.

Show solution

Now \({\Delta x = b/n}\) and the right ends are \({x_k = kb/n}\). Each height is \({k^2 b^2 / n^2}\), so

\[ \begin{aligned} \text{Right}(n) &= \sum_{k=1}^{n} \frac{k^2 b^2}{n^2} \cdot \frac{b}{n} \\ &= \frac{b^3}{n^3} \sum_{k=1}^{n} k^2 \\ &= b^3 \, \frac{(n + 1)(2n + 1)}{6n^2} . \end{aligned} \]

The last fraction is the right sum on \({[0, 1]}\), which tends to 1/3. So the right sums tend to \({b^3/3}\). The function rises on \({[0, b]}\), so by the squeeze argument in the formal statement every Riemann sum has the same limit. For \({b = 2}\) the area is \({8/3}\).

Summary card

The definite integral is the limit of Riemann sums as the strips shrink.

The anchor figure again: right-end rectangles under y equals x squared on 4, 8 and 16 strips, their overshoot shrinking, with sums 0.469, 0.398 and 0.365.
M04.5.2-F1

More strips, less overshoot.

  • Riemann sum. Add \({f(x_k^*)\,\Delta x}\) over the strips, where \({\Delta x = (b - a)/n}\).
  • Squares. \({1^2 + \dots + n^2}\) is \({n(n + 1)(2n + 1)/6}\).
  • Right sums of \({x^2}\). They equal \({\tfrac13 + \tfrac{1}{2n} + \tfrac{1}{6n^2}}\), which tends to 1/3.
  • The integral. \({\int_0^1 x^2\,dx = 1/3}\), and \({\int_0^b x^2\,dx = b^3/3}\).

Sources and next steps

Sources. Each was opened and checked against this lesson on 12 November 2026.

  • Gilbert Strang and Edwin Herman, Calculus Volume 1 (OpenStax), Section 5.1, Approximating Areas, which gives the sum of the first \(n\) squares under “Rule: Sums and Powers of Integers” and defines a Riemann sum with any sample point in each subinterval; and Section 5.2, The Definite Integral, which defines the integral as the limit of Riemann sums (equation 5.8), names the integrand, the limits of integration and the variable of integration, a dummy variable, and states without proof that continuous functions are integrable (Theorem 5.1), adding that a function with finitely many jumps is integrable too.
  • David Jerison, MIT OpenCourseWare 18.01SC, Session 46: Riemann Sums, and its lecture notes, which set out the procedure used here: divide \({[a, b]}\) into \(n\) equal pieces, pick any point in each, add height times width, and let \(n\) grow.
  • Matthew Boelkins, Active Calculus, Section 4.2, “Riemann sums”, which introduces the left, right and middle sums, written \(L_n\), \(R_n\) and \(M_n\). This course writes them \({\text{Left}(n)}\), \({\text{Right}(n)}\) and \({\text{Mid}(n)}\) because \(L\) is the loss and \(T_n\) a Taylor polynomial.

Next. M04.5.3 Signed area, properties and the average value lets the curve dip below the axis and derives the rules that make integrals easy to combine.

This lesson unlocks

M04.5.3 Signed area, properties and the average value

Your progress is saved in this browser only. No account, no tracking.
Back to top