Numerical Integration: The Left Rectangle Rule
Numerical integration is a fundamental technique in computational mathematics for approximating the definite integral of a function. While analytical integration isn't always possible or practical, numerical methods provide powerful alternatives.
The Problem
Given a continuous function on an interval , we want to approximate:
Geometrically, this represents the area under the curve between and .
The Left Rectangle Rule
The Left Rectangle Rule (also called the Left Riemann Sum) is the simplest numerical integration technique. The idea is to:
- Divide the interval into subintervals of equal width
- Approximate the area under the curve using rectangles
- Use the function value at the left endpoint of each subinterval as the rectangle's height
Mathematical Formulation
Let be the width of each subinterval. The partition points are:
The Left Rectangle Rule approximation is:
Implementation
Here's a simple Python implementation:
def left_rectangle_rule(f, a, b, n):
"""
Approximate the definite integral of f from a to b using n rectangles.
Args:
f: Function to integrate
a: Lower bound
b: Upper bound
n: Number of rectangles
Returns:
Approximation of the integral
"""
dx = (b - a) / n
x_values = [a + i * dx for i in range(n)]
return dx * sum(f(x) for x in x_values)Example: Integrating
Let's approximate (which we know equals ):
import math
# Define the function
def f(x):
return x**2
# Approximate with different values of n
for n in [10, 100, 1000]:
approx = left_rectangle_rule(f, 0, 1, n)
error = abs(approx - 1/3)
print(f"n={n:4d}: approx={approx:.6f}, error={error:.6f}")Output:
n= 10: approx=0.285000, error=0.048000
n= 100: approx=0.328350, error=0.004650
n=1000: approx=0.332834, error=0.000167Convergence and Error Analysis
The error in the Left Rectangle Rule decreases as increases. Specifically, for a well-behaved function, the error is proportional to :
This means doubling roughly halves the error.
More Complex Example: Trigonometric Function
Let's integrate from to :
# Exact value: ∫₀^π sin(x)dx = 2
def sin_func(x):
return math.sin(x)
exact = 2.0
for n in [10, 50, 100, 500]:
approx = left_rectangle_rule(sin_func, 0, math.pi, n)
error_pct = abs(approx - exact) / exact * 100
print(f"n={n:3d}: {approx:.6f} (error: {error_pct:.2f}%)")Advantages and Disadvantages
Advantages:
- Extremely simple to understand and implement
- Requires only function evaluations (no derivatives)
- Works for any continuous function
Disadvantages:
- Relatively slow convergence ( error)
- Can be inaccurate for functions with high curvature
- Better methods exist (Trapezoidal Rule, Simpson's Rule, etc.)
When to Use It
The Left Rectangle Rule is excellent for:
- Educational purposes - understanding the basics of numerical integration
- Quick prototypes - when accuracy isn't critical
- Monotonic functions - provides an upper or lower bound
For production code, consider more sophisticated methods like the Trapezoidal Rule or Gaussian Quadrature.
Conclusion
The Left Rectangle Rule demonstrates the core principle of numerical integration: approximating continuous areas with discrete sums. While it's not the most efficient method, it serves as a foundation for understanding more advanced techniques.
Try implementing this yourself and experimenting with different functions and values of !