calculus25 min

Riemann Sums

Approximating the area under a curve using rectangles with left, right, and midpoint methods

0/9Not Started

Why This Matters

How do you measure the area of an irregularly shaped region? You cannot use the length-times-width formula for a rectangle. The brilliant idea behind Riemann sums is to approximate the area by filling it with rectangles whose total area you can compute. The more rectangles you use, the better the approximation.

A partition divides the interval into subintervals, and each subinterval gets a rectangle. The width of each rectangle is the subinterval width, and the height is determined by the function value at some point in the subinterval. The choice of that sample point gives rise to different methods: left endpoint, right endpoint, or midpoint Riemann sums.

As the number of rectangles approaches infinity (and each gets infinitely thin), the Riemann sum converges to the exact area -- this limit is the definite integral. Riemann sums are not just a theoretical stepping stone; they are the basis for numerical integration methods used in every scientific computing library.

Define Terms

Visual Model

Function f(x)The curve above the x-axis
Partition [a,b]Divide into n subintervals
Left SumHeight = f(left endpoint)
Right SumHeight = f(right endpoint)
Midpoint SumHeight = f(midpoint)
Area ApproximationSum of rectangle areas
Limit as n -> infinityExact area = definite integral
n RectanglesMore rectangles = better approx

The full process at a glance. Click Start tour to walk through each step.

Riemann sums approximate area with rectangles. Left, right, and midpoint methods converge to the definite integral as n grows.

Code Example

Code
// Riemann sum implementations
function leftRiemannSum(f, a, b, n) {
  const dx = (b - a) / n;
  let sum = 0;
  for (let i = 0; i < n; i++) {
    sum += f(a + i * dx) * dx;
  }
  return sum;
}

function rightRiemannSum(f, a, b, n) {
  const dx = (b - a) / n;
  let sum = 0;
  for (let i = 1; i <= n; i++) {
    sum += f(a + i * dx) * dx;
  }
  return sum;
}

function midpointRiemannSum(f, a, b, n) {
  const dx = (b - a) / n;
  let sum = 0;
  for (let i = 0; i < n; i++) {
    sum += f(a + (i + 0.5) * dx) * dx;
  }
  return sum;
}

// Test: integral of x^2 from 0 to 3 (exact = 9)
const f = x => x * x;
console.log("Integral of x^2 from 0 to 3 (exact = 9):\n");

for (const n of [4, 10, 100, 1000]) {
  const L = leftRiemannSum(f, 0, 3, n);
  const R = rightRiemannSum(f, 0, 3, n);
  const M = midpointRiemannSum(f, 0, 3, n);
  console.log(`n=${String(n).padStart(4)}: left=${L.toFixed(6)}, right=${R.toFixed(6)}, mid=${M.toFixed(6)}`);
}

// Notice: midpoint converges fastest!
// Also: for increasing functions, left < exact < right
console.log("\nFor x^2 (increasing on [0,3]): left underestimates, right overestimates");
console.log("Midpoint is closest to the exact value of 9");

Interactive Experiment

Try these exercises:

  • Compute left, right, and midpoint sums for sin(x) on [0, pi] with n = 10 and n = 100. The exact answer is 2.
  • For a decreasing function like 1/x on [1, 3], which sum overestimates and which underestimates?
  • Try n = 1, 2, 4, 8, 16, 32 for x^2 on [0, 1]. At what n does the midpoint sum have 4 decimal places of accuracy?
  • Compute the Riemann sum for a constant function f(x) = 5 on [0, 3]. Does it equal the exact area regardless of n?
  • Compare convergence rates: how does the error decrease as you double n for each method?

Quick Quiz

Coding Challenge

Compute Left, Right, and Midpoint Riemann Sums

Write a function called `riemannSums` that takes a function f, interval endpoints a and b, and a number of rectangles n. Return an object with three properties: left, right, and mid -- the left, right, and midpoint Riemann sums, each rounded to 4 decimal places.

Loading editor...

Real-World Usage

Riemann sums and numerical integration are used everywhere:

  • Scientific computing: When an integral has no closed-form antiderivative, numerical methods (based on Riemann sums) are the only option. This is common in physics, biology, and finance.
  • Computer graphics: Rendering images often requires integrating light intensity over a pixel area. Monte Carlo integration is a randomized Riemann sum approach.
  • Probability and statistics: Computing probabilities from continuous distributions requires integrating the PDF. Numerical integration handles distributions without closed-form CDFs.
  • Signal processing: Digital audio and image processing convert continuous signals to discrete samples, which is fundamentally a partition-and-sum process.
  • Engineering: Finite element analysis approximates continuous structures with discrete elements, computing stress and strain through numerical integration.

Connections