Practice / Calculus

Riemann sums: left, right, midpoint and trapezoid, and the limit that defines the integral

Ten problems on Riemann sums: left and right sums for x² on [0, 2], the midpoint sum and its error, the trapezoid rule and Simpson's combination, over- and underestimates for 1/x, the limit of right sums for 2x + 1 and for x², a sum recognized as π/4, distance from a table of speeds, how many subintervals an error bound needs, and the area under an ROC curve with uneven steps, with worked solutions and the mistakes of using every point in a left sum, writing x_i = i/n on the wrong interval and assuming equal widths.

Before you start

A definite integral is defined as a limit of sums: cut the interval into thin strips, approximate each strip by a rectangle, add up the rectangles, and let the strips get thinner. Those sums are also how integrals are computed when there is no antiderivative, only a table of measurements or a curve known at a few points, as with the area under an ROC curve. The choice of where each rectangle touches the curve decides whether the sum is too big or too small, and how fast the error shrinks. The work goes wrong in a few places: a left sum that uses every point, xix_i written as if every interval were [0,1][0, 1], the endpoint weights of the trapezoid rule forgotten, equal widths assumed for unequal data, and the belief that a left sum always underestimates. These ten problems cover each of them.

  • Partition. Split [a,b][a, b] into nn equal pieces of width Δx=b−an\Delta x = \dfrac{b - a}{n}, with points xi=a+i Δxx_i = a + i\,\Delta x for i=0,1,…,ni = 0, 1, \dots, n. There are nn strips and n+1n + 1 points.
  • Left, right, midpoint. Ln=Δx∑i=0n−1f(xi)L_n = \Delta x\displaystyle\sum_{i=0}^{n-1} f(x_i), Rn=Δx∑i=1nf(xi)R_n = \Delta x\displaystyle\sum_{i=1}^{n} f(x_i), and Mn=Δx∑i=1nf(xi−1+xi2)M_n = \Delta x\displaystyle\sum_{i=1}^{n} f\Big(\frac{x_{i-1} + x_i}{2}\Big). Each uses exactly nn heights.
  • Trapezoid and Simpson. Tn=Δx2(f(x0)+2f(x1)+⋯+2f(xn−1)+f(xn))=Ln+Rn2T_n = \dfrac{\Delta x}2\big(f(x_0) + 2f(x_1) + \dots + 2f(x_{n-1}) + f(x_n)\big) = \dfrac{L_n + R_n}2. With unequal widths, add 12(xi+1−xi)(f(xi)+f(xi+1))\tfrac12(x_{i+1} - x_i)\big(f(x_i) + f(x_{i+1})\big) strip by strip. 2Mn+Tn3\dfrac{2M_n + T_n}3 is Simpson's rule on 2n2n strips.
  • Over or under. If ff is increasing, LnL_n is too small and RnR_n too big; if decreasing, the other way round. If ff is concave up, MnM_n is too small and TnT_n too big.
  • The definition. ∫abf(x) dx=lim⁡n→∞∑i=1nf(xi) Δx\displaystyle\int_a^b f(x)\,dx = \lim_{n\to\infty}\sum_{i=1}^n f(x_i)\,\Delta x. Useful sums: ∑i=1ni=n(n+1)2\displaystyle\sum_{i=1}^n i = \frac{n(n + 1)}2 and ∑i=1ni2=n(n+1)(2n+1)6\displaystyle\sum_{i=1}^n i^2 = \frac{n(n + 1)(2n + 1)}6.
  • Error bounds. If ∣f′′∣≤K\lvert f''\rvert \le K on [a,b][a, b], then ∣EM∣≤K(b−a)324n2\lvert E_M\rvert \le \dfrac{K(b - a)^3}{24n^2} and ∣ET∣≤K(b−a)312n2\lvert E_T\rvert \le \dfrac{K(b - a)^3}{12n^2}.

Builds on: Evaluating limits: factoring, conjugates and limits at infinity

Problems

  1. ·

    Find the left and right sums L4L_4 and R4R_4 for f(x)=x2f(x) = x^2 on [0,2][0, 2], and compare them with the integral.

  2. ·

    Find the midpoint sum M4M_4 for x2x^2 on [0,2][0, 2], and its error.

  3. ··

    Find the trapezoid sum T4T_4 for x2x^2 on [0,2][0, 2] and its error, and then the combination 2M4+T43\dfrac{2M_4 + T_4}3.

  4. ··

    Find L4L_4 and R4R_4 for f(x)=1xf(x) = \dfrac1x on [1,2][1, 2]. Which is an overestimate?

  5. ··

    Write ∫03(2x+1) dx\displaystyle\int_0^3 (2x + 1)\,dx as a limit of right sums and evaluate the limit.

  6. ···

    Use right sums to show that ∫01x2 dx=13\displaystyle\int_0^1 x^2\,dx = \frac13, and find R10R_{10}.

  7. ··

    Evaluate lim⁡n→∞∑i=1nnn2+i2\displaystyle\lim_{n\to\infty}\sum_{i=1}^n \frac{n}{n^2 + i^2} by recognizing it as a Riemann sum.

  8. ··

    A car's speed is recorded every 1010 seconds: 00, 88, 1414, 1818 and 2020 m/s at t=0,10,20,30,40t = 0, 10, 20, 30, 40. Estimate the distance traveled with left, right and trapezoid sums.

  9. ···

    How many subintervals guarantee an error below 10−410^{-4} for ∫12dxx\displaystyle\int_1^2\frac{dx}x with the midpoint rule, and with the trapezoid rule?

  10. ···

    A classifier's ROC curve passes through the points (false positive rate, true positive rate) (0,0)(0, 0), (0.1,0.5)(0.1, 0.5), (0.3,0.8)(0.3, 0.8), (0.6,0.9)(0.6, 0.9) and (1,1)(1, 1), joined by straight lines. Find the area under the curve.

Worked solutions

Problem 1

Find the left and right sums L4L_4 and R4R_4 for f(x)=x2f(x) = x^2 on [0,2][0, 2], and compare them with the integral.

  1. Δx=2−04=12\Delta x = \tfrac{2 - 0}4 = \tfrac12; points 0,12,1,32,20, \tfrac12, 1, \tfrac32, 2; heights 0,14,1,94,40, \tfrac14, 1, \tfrac94, 4Five points, four strips.
  2. L4=12(0+14+1+94)=12⋅72=74L_4 = \tfrac12\big(0 + \tfrac14 + 1 + \tfrac94\big) = \tfrac12\cdot\tfrac{7}{2} = \tfrac74The first four heights.
  3. R4=12(14+1+94+4)=12⋅152=154R_4 = \tfrac12\big(\tfrac14 + 1 + \tfrac94 + 4\big) = \tfrac12\cdot\tfrac{15}{2} = \tfrac{15}4The last four heights.
  4. ∫02x2 dx=83≈2.667\displaystyle\int_0^2 x^2\,dx = \frac83 \approx 2.667The exact value, for comparison.
  5. L4=1.75L_4 = 1.75, R4=3.75R_4 = 3.75; the integral 83\tfrac83 lies between themx2x^2 is increasing on [0,2][0, 2], so left rectangles sit under the curve and right ones stick out above it. R4−L4=Δx (f(2)−f(0))=2R_4 - L_4 = \Delta x\,\big(f(2) - f(0)\big) = 2.

Problem 2

Find the midpoint sum M4M_4 for x2x^2 on [0,2][0, 2], and its error.

  1. Midpoints 14,34,54,74\tfrac14, \tfrac34, \tfrac54, \tfrac74Halfway along each strip of width 12\tfrac12.
  2. Heights 116,916,2516,4916\tfrac1{16}, \tfrac9{16}, \tfrac{25}{16}, \tfrac{49}{16}, which add to 8416=214\tfrac{84}{16} = \tfrac{21}4Square each midpoint.
  3. M4=12⋅214=218M_4 = \tfrac12\cdot\tfrac{21}4 = \tfrac{21}8Times Δx\Delta x.
  4. M4=2.625M_4 = 2.625, an error of 83−218=124≈0.0417\tfrac83 - \tfrac{21}8 = \tfrac1{24} \approx 0.0417With the same four heights' worth of work, it is much closer than L4L_4 or R4R_4, and slightly low because x2x^2 is concave up.

Problem 3

Find the trapezoid sum T4T_4 for x2x^2 on [0,2][0, 2] and its error, and then the combination 2M4+T43\dfrac{2M_4 + T_4}3.

  1. T4=Δx2(f(0)+2f(12)+2f(1)+2f(32)+f(2))=14(0+12+2+92+4)T_4 = \tfrac{\Delta x}2\big(f(0) + 2f(\tfrac12) + 2f(1) + 2f(\tfrac32) + f(2)\big) = \tfrac14\big(0 + \tfrac12 + 2 + \tfrac92 + 4\big)Interior points count twice, the ends once.
  2. T4=14⋅11=114=2.75T_4 = \tfrac14\cdot11 = \tfrac{11}4 = 2.75, which is also L4+R42\tfrac{L_4 + R_4}2The average of 1.751.75 and 3.753.75.
  3. Error 83−114=−112\tfrac83 - \tfrac{11}4 = -\tfrac1{12}Too big, because the chords of a concave-up curve lie above it. It is −2-2 times the midpoint error 124\tfrac1{24}.
  4. 2⋅218+1143=214+1143=83\dfrac{2\cdot\frac{21}8 + \frac{11}4}{3} = \dfrac{\frac{21}4 + \frac{11}4}{3} = \dfrac83The weights 22 and 11 cancel the two errors.
  5. T4=2.75T_4 = 2.75 with error −112-\tfrac1{12}; 2M4+T43=83\tfrac{2M_4 + T_4}3 = \tfrac83 exactlyThis combination is Simpson's rule, which is exact for every polynomial of degree 33 or less.

Problem 4

Find L4L_4 and R4R_4 for f(x)=1xf(x) = \dfrac1x on [1,2][1, 2]. Which is an overestimate?

  1. Δx=14\Delta x = \tfrac14; points 1,54,32,74,21, \tfrac54, \tfrac32, \tfrac74, 2; heights 1,45,23,47,121, \tfrac45, \tfrac23, \tfrac47, \tfrac1215/4=45\tfrac{1}{5/4} = \tfrac45, and so on.
  2. L4=14(1+45+23+47)≈14⋅3.0381≈0.7595L_4 = \tfrac14\big(1 + \tfrac45 + \tfrac23 + \tfrac47\big) \approx \tfrac14\cdot3.0381 \approx 0.7595The first four heights.
  3. R4=L4−14(1−12)≈0.6345R_4 = L_4 - \tfrac14\big(1 - \tfrac12\big) \approx 0.6345The same sum with f(1)f(1) swapped for f(2)f(2).
  4. ∫12dxx=ln⁡2≈0.6931\displaystyle\int_1^2 \frac{dx}x = \ln2 \approx 0.6931The exact value.
  5. L4≈0.7595L_4 \approx 0.7595 is the overestimate and R4≈0.6345R_4 \approx 0.6345 the underestimate1x\tfrac1x is decreasing, so each strip's left edge is its highest point.

Problem 5

Write ∫03(2x+1) dx\displaystyle\int_0^3 (2x + 1)\,dx as a limit of right sums and evaluate the limit.

  1. Δx=3n\Delta x = \tfrac3n and xi=0+i⋅3n=3inx_i = 0 + i\cdot\tfrac3n = \tfrac{3i}nEqual widths on [0,3][0, 3].
  2. Rn=∑i=1n(2⋅3in+1)3n=18n2∑i=1ni+3n∑i=1n1R_n = \displaystyle\sum_{i=1}^n\Big(2\cdot\frac{3i}n + 1\Big)\frac3n = \frac{18}{n^2}\sum_{i=1}^n i + \frac3n\sum_{i=1}^n 1Split the sum and pull out what does not depend on ii.
  3. =18n2⋅n(n+1)2+3=9(n+1)n+3=12+9n= \dfrac{18}{n^2}\cdot\dfrac{n(n + 1)}2 + 3 = \dfrac{9(n + 1)}n + 3 = 12 + \dfrac9n∑i=1n1=n\sum_{i=1}^n 1 = n.
  4. 12+9n→1212 + \tfrac9n \to 12Take the limit.
  5. 1212Check with an antiderivative: [x2+x]03=12\big[x^2 + x\big]_0^3 = 12. For an increasing function every RnR_n is above the integral, here by exactly 9n\tfrac9n.

Problem 6

Use right sums to show that ∫01x2 dx=13\displaystyle\int_0^1 x^2\,dx = \frac13, and find R10R_{10}.

  1. Δx=1n\Delta x = \tfrac1n, xi=inx_i = \tfrac in, so Rn=∑i=1ni2n2⋅1n=1n3∑i=1ni2R_n = \displaystyle\sum_{i=1}^n\frac{i^2}{n^2}\cdot\frac1n = \frac{1}{n^3}\sum_{i=1}^n i^2Right endpoints on [0,1][0, 1].
  2. Rn=1n3⋅n(n+1)(2n+1)6=(n+1)(2n+1)6n2R_n = \dfrac{1}{n^3}\cdot\dfrac{n(n + 1)(2n + 1)}{6} = \dfrac{(n + 1)(2n + 1)}{6n^2}The sum of squares.
  3. (n+1)(2n+1)6n2=2n2+3n+16n2→26=13\dfrac{(n + 1)(2n + 1)}{6n^2} = \dfrac{2n^2 + 3n + 1}{6n^2} \to \dfrac26 = \dfrac13Divide top and bottom by n2n^2.
  4. ∫01x2 dx=13\displaystyle\int_0^1 x^2\,dx = \frac13; R10=11⋅21600=0.385R_{10} = \frac{11\cdot21}{600} = 0.385This is the calculation Archimedes did for the parabola, with no antiderivative in sight. R10R_{10} is still 0.0520.052 too high.

Problem 7

Evaluate lim⁡n→∞∑i=1nnn2+i2\displaystyle\lim_{n\to\infty}\sum_{i=1}^n \frac{n}{n^2 + i^2} by recognizing it as a Riemann sum.

  1. nn2+i2=1n⋅11+(i/n)2\dfrac{n}{n^2 + i^2} = \dfrac1n\cdot\dfrac{1}{1 + (i/n)^2}Divide top and bottom by n2n^2 to expose in\tfrac in and a factor 1n\tfrac1n.
  2. This is ∑i=1nf(xi) Δx\displaystyle\sum_{i=1}^n f(x_i)\,\Delta x with f(x)=11+x2f(x) = \dfrac1{1 + x^2}, xi=inx_i = \tfrac in, Δx=1n\Delta x = \tfrac1nA right sum on [0,1][0, 1].
  3. ∫01dx1+x2=arctan⁡1−arctan⁡0\displaystyle\int_0^1\frac{dx}{1 + x^2} = \arctan1 - \arctan0The limit of the sums is the integral.
  4. π4≈0.7854\tfrac\pi4 \approx 0.7854At n=1000n = 1000 the sum is about 0.785150.78515, slightly low because 11+x2\tfrac{1}{1 + x^2} is decreasing.

Problem 8

A car's speed is recorded every 1010 seconds: 00, 88, 1414, 1818 and 2020 m/s at t=0,10,20,30,40t = 0, 10, 20, 30, 40. Estimate the distance traveled with left, right and trapezoid sums.

  1. Distance is ∫040v(t) dt\displaystyle\int_0^{40} v(t)\,dt, and Δt=10\Delta t = 10Four strips of 1010 s.
  2. L4=10(0+8+14+18)=400L_4 = 10(0 + 8 + 14 + 18) = 400 mThe speed at the start of each interval.
  3. R4=10(8+14+18+20)=600R_4 = 10(8 + 14 + 18 + 20) = 600 mThe speed at the end of each interval.
  4. T4=102(0+2⋅8+2⋅14+2⋅18+20)=5⋅100=500T_4 = \tfrac{10}2\big(0 + 2\cdot8 + 2\cdot14 + 2\cdot18 + 20\big) = 5\cdot100 = 500 mThe average of L4L_4 and R4R_4.
  5. L4=400L_4 = 400 m, R4=600R_4 = 600 m, T4=500T_4 = 500 mThe car speeds up throughout, so the true distance is between 400400 and 600600 m, and the trapezoid estimate is the best of the three.

Problem 9

How many subintervals guarantee an error below 10−410^{-4} for ∫12dxx\displaystyle\int_1^2\frac{dx}x with the midpoint rule, and with the trapezoid rule?

  1. f′′(x)=2x3≤2f''(x) = \dfrac{2}{x^3} \le 2 on [1,2][1, 2], so K=2K = 2f′′f'' is largest at the left end.
  2. Midpoint: 2⋅1324n2=112n2<10−4\dfrac{2\cdot1^3}{24n^2} = \dfrac{1}{12n^2} < 10^{-4} needs n2>833.3n^2 > 833.3282=78428^2 = 784 is too small and 292=84129^2 = 841 is enough.
  3. Trapezoid: 2⋅1312n2=16n2<10−4\dfrac{2\cdot1^3}{12n^2} = \dfrac{1}{6n^2} < 10^{-4} needs n2>1666.7n^2 > 1666.7402=160040^2 = 1600 is too small and 412=168141^2 = 1681 is enough.
  4. Midpoint: n=29n = 29; trapezoid: n=41n = 41The midpoint bound is half the trapezoid bound, so the trapezoid rule needs about 2\sqrt2 times as many strips. These are guarantees; the actual errors at these nn are smaller still.

Problem 10

A classifier's ROC curve passes through the points (false positive rate, true positive rate) (0,0)(0, 0), (0.1,0.5)(0.1, 0.5), (0.3,0.8)(0.3, 0.8), (0.6,0.9)(0.6, 0.9) and (1,1)(1, 1), joined by straight lines. Find the area under the curve.

  1. The widths are 0.10.1, 0.20.2, 0.30.3 and 0.40.4The steps in the false positive rate are not equal.
  2. 12⋅0.1⋅(0+0.5)=0.025\tfrac12\cdot0.1\cdot(0 + 0.5) = 0.025 and 12⋅0.2⋅(0.5+0.8)=0.13\tfrac12\cdot0.2\cdot(0.5 + 0.8) = 0.13One trapezoid per segment, each with its own width.
  3. 12⋅0.3⋅(0.8+0.9)=0.255\tfrac12\cdot0.3\cdot(0.8 + 0.9) = 0.255 and 12⋅0.4⋅(0.9+1)=0.38\tfrac12\cdot0.4\cdot(0.9 + 1) = 0.38The last two segments.
  4. AUC =0.025+0.13+0.255+0.38=0.79= 0.025 + 0.13 + 0.255 + 0.38 = 0.79With straight lines between the points the trapezoid rule is exact, not an estimate. A random classifier's diagonal has area 0.50.5 and a perfect one 11.

Where this goes wrong

1. Using every point in a left sum

The table of values has n+1n + 1 entries, and all of them go into the sum.

  1. Δx=14\Delta x = \tfrac14 and heights 1,45,23,47,121, \tfrac45, \tfrac23, \tfrac47, \tfrac12 for 1x\tfrac1x on [1,2][1, 2]Right so far: Problem 4, step 1.
  2. “Add the heights and multiply by Δx\Delta x.”The habit that causes the mistake: there are five points but only four strips.
  3. L4=14(1+45+23+47+12)≈0.8845L_4 = \tfrac14\big(1 + \tfrac45 + \tfrac23 + \tfrac47 + \tfrac12\big) \approx 0.8845That counts a fifth rectangle, over [2,94][2, \tfrac94], outside the interval. A left sum uses the first nn heights, giving 0.75950.7595; a right sum uses the last nn. Only the trapezoid rule uses all n+1n + 1, and with the ends halved.

2. Writing x_i = i/n on the wrong interval

xi=inx_i = \tfrac in is the most familiar form, and it gets used for every interval.

  1. ∫03(2x+1) dx\displaystyle\int_0^3 (2x + 1)\,dx with Δx=3n\Delta x = \tfrac3nRight so far: Problem 5, step 1.
  2. “xi=inx_i = \tfrac in.”The habit that causes the mistake: the formula for [0,1][0, 1] used on [0,3][0, 3].
  3. Rn=∑i=1n(2in+1)3n=6+3n→6R_n = \displaystyle\sum_{i=1}^n\Big(\frac{2i}n + 1\Big)\frac3n = 6 + \frac3n \to 6The points in\tfrac in only reach 11, so this is 3∫01(2x+1) dx3\int_0^1(2x + 1)\,dx, not the integral over [0,3][0, 3]. With xi=a+i Δx=3inx_i = a + i\,\Delta x = \tfrac{3i}n the limit is 1212.

3. Giving the endpoints full weight in the trapezoid rule

The trapezoid rule's pattern of 1,2,2,…,2,11, 2, 2, \dots, 2, 1 gets flattened to all ones, or the factor of 12\tfrac12 gets lost.

  1. Speeds 0,8,14,18,200, 8, 14, 18, 20 every 1010 sRight so far: Problem 8.
  2. “The trapezoid rule adds the readings and multiplies by Δt\Delta t.”The habit that causes the mistake: every reading counted once, at full width.
  3. T4=10(0+8+14+18+20)=600T_4 = 10(0 + 8 + 14 + 18 + 20) = 600 mThat is five full rectangles over four intervals. Each end reading belongs to one trapezoid and each interior reading to two, each with half weight: 5(0+16+28+36+20)=5005(0 + 16 + 28 + 36 + 20) = 500 m.

4. Assuming equal widths for unequal steps

Four trapezoids on [0,1][0, 1] suggests a width of 14\tfrac14 each.

  1. Points (0,0)(0, 0), (0.1,0.5)(0.1, 0.5), (0.3,0.8)(0.3, 0.8), (0.6,0.9)(0.6, 0.9), (1,1)(1, 1)Right so far: Problem 10.
  2. “Four strips on [0,1][0, 1], so Δx=0.25\Delta x = 0.25.”The habit that causes the mistake: the equal-width formula used on data that were not sampled evenly.
  3. AUC =0.25(0+0.52+0.5+0.82+0.8+0.92+0.9+12)=0.25⋅2.7=0.675= 0.25\big(\tfrac{0 + 0.5}2 + \tfrac{0.5 + 0.8}2 + \tfrac{0.8 + 0.9}2 + \tfrac{0.9 + 1}2\big) = 0.25\cdot2.7 = 0.675The steep first segment is only 0.10.1 wide and the flat last one is 0.40.4 wide, and equal widths get both wrong. Each trapezoid needs its own width: the area is 0.790.79.

5. Assuming a left sum always underestimates

In the usual picture, of an increasing curve, the left rectangles sit under it.

  1. L4≈0.7595L_4 \approx 0.7595 for ∫12dxx\int_1^2\tfrac{dx}xRight so far: Problem 4, step 2.
  2. “Left sums are too small.”The habit that causes the mistake: a rule for increasing functions applied to every function.
  3. So ln⁡2>0.7595\ln2 > 0.75951x\tfrac1x is decreasing, so each left edge is the strip's highest point and L4L_4 is too big: ln⁡2≈0.6931\ln2 \approx 0.6931. Decide over or under from whether ff increases or decreases on the interval, and if it does both, the sum alone does not tell you.

Print this set: riemann-sums.pdf (problems, answers, and worked solutions on separate pages).

Read the guide: Riemann sums: left, right, midpoint, and trapezoid, and which way each one misses