Learning Objectives
By the end of this section, you will be able to:
- Define left Riemann sums, right Riemann sums, and the midpoint rule
- Compute each type of Riemann sum for a given function and interval
- Visualize how rectangles approximate the area under a curve
- Compare the accuracy of different approximation methods
- Explain why the midpoint rule is typically more accurate than endpoints
- Apply numerical integration in scientific computing and machine learning
- Implement these methods in Python code
The Big Picture: From Rectangles to Integrals
"The integral is nothing but the limit of a sum." — Bernhard Riemann
In the previous section, we discovered that the area under a curve can be approximated by filling the region with rectangles. But we glossed over a critical question: where exactly should we sample the function to determine each rectangle's height?
This question leads to three fundamental approaches, each with its own geometric interpretation and accuracy characteristics:
Why This Matters
These three methods form the foundation of numerical integration — the art of computing integrals using algorithms rather than analytical formulas. Every scientific computing library, from NumPy to TensorFlow, relies on these concepts.
Understanding why certain methods are more accurate than others prepares you for more advanced techniques like Simpson's Rule and Gaussian quadrature.
Historical Context: Riemann's Revolutionary Idea
Bernhard Riemann (1826-1866) was a German mathematician who, in just 39 years of life, revolutionized multiple areas of mathematics. His 1854 habilitation lecture on geometry laid the groundwork for Einstein's general relativity.
Riemann's approach to integration was fundamentally different from his predecessors. Rather than starting with antiderivatives (the approach of Newton and Leibniz), he defined the integral directly as the limit of sums. This allowed mathematicians to integrate functions that have no elementary antiderivative.
The Riemann Integral Definition
Riemann showed that if we partition into subintervals and sample any point in each subinterval, the sum converges to the same value as . This value is the definite integral.
The left, right, and midpoint rules are simply different choices for where to place the sample point . All three converge to the same answer — but at different rates.
Partitioning the Interval
Before we can approximate an integral, we need to set up the machinery. Given a function on an interval :
Step 1: Divide into Subintervals
We partition into equal subintervals, each of width:
Step 2: Identify the Partition Points
The endpoints of the subintervals are:
In general:
| Symbol | Meaning | Formula |
|---|---|---|
| n | Number of subintervals (rectangles) | User-specified positive integer |
| Δx | Width of each subinterval | (b - a) / n |
| xᵢ | The i-th partition point | a + i · Δx |
| [xᵢ₋₁, xᵢ] | The i-th subinterval | Length = Δx |
Left Riemann Sums
In a left Riemann sum, we evaluate the function at the left endpoint of each subinterval to determine the rectangle's height.
Definition: Left Riemann Sum
Geometric Interpretation
For an increasing function, the left Riemann sum underestimates the true area. Each rectangle lies entirely below the curve.
For a decreasing function, the left Riemann sum overestimates the true area. Each rectangle extends above the curve.
Example: Left Riemann Sum of f(x) = x² on [0, 2] with n = 4
Step 1: Calculate Δx = (2 - 0) / 4 = 0.5
Step 2: Identify left endpoints: x₀ = 0, x₁ = 0.5, x₂ = 1, x₃ = 1.5
Step 3: Calculate function values:
- f(0) = 0² = 0
- f(0.5) = 0.5² = 0.25
- f(1) = 1² = 1
- f(1.5) = 1.5² = 2.25
Step 4: Sum the areas:
The exact value is , so the left sum underestimates by about 0.92 (34% error).
Right Riemann Sums
In a right Riemann sum, we evaluate the function at the right endpoint of each subinterval.
Definition: Right Riemann Sum
Geometric Interpretation
For an increasing function, the right Riemann sum overestimates the true area. Each rectangle extends above the curve.
For a decreasing function, the right Riemann sum underestimates the true area.
Example: Right Riemann Sum of f(x) = x² on [0, 2] with n = 4
Step 1: Δx = 0.5 (same as before)
Step 2: Identify right endpoints: x₁ = 0.5, x₂ = 1, x₃ = 1.5, x₄ = 2
Step 3: Calculate function values:
- f(0.5) = 0.25
- f(1) = 1
- f(1.5) = 2.25
- f(2) = 4
Step 4: Sum the areas:
The right sum overestimates by about 1.08 (41% error).
Left + Right Bound the True Area
For a monotonic function, the true integral lies between the left and right sums:
In our example: 1.75 ≤ 2.667 ≤ 3.75 ✓
The Midpoint Rule
The midpoint rule evaluates the function at the center of each subinterval. This seemingly simple change leads to dramatically improved accuracy.
Definition: Midpoint Rule
where is the midpoint of the i-th subinterval.
Why Is Midpoint More Accurate?
The key insight is error cancellation. Consider what happens at a single rectangle:
- If the function is concave up (like x²), the tangent line at the midpoint lies below the curve, causing a slight underestimate.
- If the function is concave down, the tangent line lies abovethe curve, causing a slight overestimate.
The crucial difference: these errors are second-order — they scale with rather than .
Example: Midpoint Rule for f(x) = x² on [0, 2] with n = 4
Step 1: Δx = 0.5
Step 2: Identify midpoints: 0.25, 0.75, 1.25, 1.75
Step 3: Calculate function values:
- f(0.25) = 0.0625
- f(0.75) = 0.5625
- f(1.25) = 1.5625
- f(1.75) = 3.0625
Step 4: Sum the areas:
The midpoint gives 2.625 with only 1.6% error — dramatically better than left (34%) or right (41%) with the same number of rectangles!
Interactive Riemann Sum Explorer
Use this interactive tool to visualize how left, right, and midpoint Riemann sums approximate the area under various functions. Watch how the approximation improves as you increase the number of rectangles.
| Metric | Value |
|---|---|
| Left Riemann Sum (n = 4) | 5.906250 |
| Exact Area (definite integral) | 9.000000 |
| Error | 3.093750 (34.38%) |
As n increases, the Riemann sum approaches the exact area under the curve. Try increasing n to 50+ and watch the error shrink.
Try These Experiments
- Start with n = 4 and compare left vs right vs midpoint for the same function
- Use the "Animate Convergence" button to watch all methods approach the true value
- Try a decreasing function (like 1/(1+x²)) and notice how left/right swap behaviors
- Observe that midpoint consistently shows smaller error with the same n
Error Analysis: How Wrong Are We?
Understanding the error in numerical integration is crucial for scientific computing. The error bounds depend on the derivatives of the function:
| Method | Error Bound | Order | Behavior |
|---|---|---|---|
| Left Riemann | O(1/n) | First-order; doubling n halves error | |
| Right Riemann | O(1/n) | First-order; doubling n halves error | |
| Midpoint | O(1/n²) | Second-order; doubling n quarters error! |
Here and .
The Power of Second-Order Methods
A second-order method like the midpoint rule converges quadratically — each time you double the number of rectangles, the error decreases by a factor of 4, not 2.
To achieve 0.01% error: Left/Right might need 10,000 rectangles, but midpoint might only need 100!
Convergence to the Definite Integral
The magic of Riemann's approach: regardless of which sampling method you choose, all Riemann sums converge to the same value as :
This limiting value is the definite integral — Riemann's definition. The integral exists if and only if this limit exists (and is the same for any choice of sample points).
Let's compute the area under from to . The exact answer is . Watch how different methods converge as we increase the number of rectangles.
Applications in Scientific Computing and Machine Learning
Numerical integration is ubiquitous in computational science. Here are key applications:
🧮 Physics Simulations
Computing trajectories, forces, and energy requires integrating differential equations. Monte Carlo methods in quantum physics use these techniques extensively.
📊 Probability
Computing expected values and probabilities from PDFs requires integration. Many distributions (like the normal) have no closed-form CDF.
🤖 Machine Learning
Bayesian inference, normalizing flows, and variational autoencoders all require numerical integration to compute marginal likelihoods and evidence.
📈 Financial Math
Option pricing (Black-Scholes), risk assessment, and portfolio optimization involve integrating probability distributions over possible outcomes.
From Riemann to Monte Carlo
In high dimensions, Riemann sums become impractical (the "curse of dimensionality"). Monte Carlo integration — which randomly samples points instead of using a regular grid — becomes essential. But the core idea of summing f(sample) × (volume) remains the same!
Python Implementation
Let's implement all three methods and compare their convergence:
Visualizing the Three Methods
Here's code to create side-by-side visualizations:
Common Pitfalls
Pitfall 1: Confusing Indexing
Left sums use , while right sums use . The indices differ by one. Draw a picture if confused!
Pitfall 2: Assuming Error Direction
Left sums only underestimate for increasing functions. For decreasing functions, left sums overestimate. For non-monotonic functions, you can't easily predict whether the sum is too high or too low.
Pitfall 3: Expecting Midpoint to Always Win
The midpoint rule is generally more accurate, but the error bound involves . If the second derivative is very large (highly curved function), the midpoint advantage diminishes. For linear functions (f'' = 0), the midpoint rule is exact even with n = 1!
Numerical Precision Considerations
For very large n, floating-point errors can accumulate. In practice, n = 1000 to n = 100,000 is usually sufficient. Beyond that, more sophisticated quadrature methods (Simpson's Rule, Gaussian quadrature) are preferred.
Test Your Understanding
In a left Riemann sum, where is the height of each rectangle determined?
Summary
We've explored three fundamental methods for approximating definite integrals using rectangles, each with distinct characteristics and trade-offs.
The Three Methods at a Glance
| Method | Sample Point | Error Order | Best For |
|---|---|---|---|
| Left Riemann | Left endpoint xᵢ₋₁ | O(1/n) | Simple bounds on increasing functions |
| Right Riemann | Right endpoint xᵢ | O(1/n) | Simple bounds on decreasing functions |
| Midpoint Rule | Center x̄ᵢ | O(1/n²) | General use — more accurate! |
Key Takeaways
- All three methods approximate the integral as a sum of rectangle areas:
- The choice of sample point () affects accuracy but not the limiting value as
- Midpoint is more accurate because errors from overestimating and underestimating tend to cancel
- Midpoint error scales as , while left/right scale as
- For monotonic functions, left and right sums bound the true integral from below and above (or vice versa)
- These methods form the foundation for all numerical integration techniques in scientific computing
Coming Next: In the next section, we'll formalize the notation for Riemann sums using sigma notation, giving us a precise and compact way to express these sums mathematically.