Numerical Integration: Riemann Sums, the Midpoint Rule, and the Trapezoidal Method

Foundations of Definite Integrals and Numerical Approximation

  • Partitioning the Integration Interval:

    • An interval on the xx-axis is partitioned into subintervals defined by discrete points: x1,x2,x3,x4,…,xnx_1, x_2, x_3, x_4, \dots, x_n.
    • Subintervals are assumed to be equidistant, with a uniform width defined by Δx\Delta x.
    • Depending on the convention used, indexing may begin at x1x_1 through xnx_n, or from 00 to n−1n-1. Regardless of indexing notation, the core mechanics of calculation remain invariant.
  • The Definite Integral as a Limit of a Riemann Sum:

    • The standard mathematical definition of the definite integral involves taking the limit as the number of subintervals (rectangles) approaches infinity:         ∫abf(x) dx=lim⁡n→∞∑i=1nf(xi) Δx\int_a^b f(x)\,dx = \lim_{n \to \infty} \sum_{i=1}^n f(x_i)\,\Delta x
    • Inside the integral symbol, the integrand function f(x)f(x) and the differential element dxdx directly mirror the terms f(xi)f(x_i) and Δx\Delta x inside the summation.
  • Reformulation in Terms of a Width Parameter Approaching Zero:

    • Conceptually, computing limits where a parameter approaches infinity (n→∞n \to \infty) can be non-intuitive to model numerically on computational hardware.
    • Paralleling the definition of the derivative, where h→0h \to 0, the definite integral can be restructured so that the partition step size h=Δxh = \Delta x approaches zero rather than the count n→∞n \to \infty.
    • In numerical computation, a limit where h→0h \to 0 can be practically interpreted through machine epsilon (ϵmach\epsilon_{\text{mach}}), which represents the smallest positive number that can be stored in floating-point memory such that 1+ϵmach>11 + \epsilon_{\text{mach}} > 1.
    • Letting Δx=h\Delta x = h represent the machine epsilon gives a fundamental theoretical boundary for the finest possible approximation step size on a digital machine (subject to the scale/magnitude of the problem).
    • Given an interval [a,b][a, b] and a uniform step size hh, the total number of subintervals is:         n=b−ahn = \frac{b - a}{h}
    • The definite integral can therefore be equivalently expressed as:         ∫abf(x) dx=lim⁡h→0∑i=1b−ahh⋅f(a+i⋅h)\int_a^b f(x)\,dx = \lim_{h \to 0} \sum_{i=1}^{\frac{b - a}{h}} h \cdot f(a + i \cdot h)
  • Newton's Conceptualization of Calculus:

    • Calculus was formulated by Isaac Newton as an analytical language to describe physical realities that were already known to exist, such as continuous motion in physics.
    • Newton conceptualized geometric and physical quantities not by beginning with an abstract absolute formula, but by constructing an approximation and then driving it to convergence through a limit.
    • The Derivative: The tangent line is initially approximated by a secant line passing through two points; as the points move arbitrarily close, the secant line converges to the tangent line.
    • The Integral: The area under a curve is initially modeled by approximating geometric shapes (rectangles); as the widths of these shapes shrink to zero, the sum of their areas converges to the exact area under the curve.
    • In modern abstract mathematics, approximations are typically derived downwards from abstract principles, whereas foundational calculus developed abstract mathematical structures upward from physical approximations.

Analytical Evaluation of the Test Function

  • Test Function and Objective:

    • The primary function evaluated across various numerical quadrature schemes is:         f(x)=x2+1f(x) = x^2 + 1
    • The domain of integration is chosen over the closed interval [a,b]=[0,2][a, b] = [0, 2].
    • The function is deliberately shifted vertically upward by +1+1 to ensure that the curve remains strictly above the xx-axis across the entire interval, thereby providing a non-zero height and positive area beneath the curve for the very first partition.
  • Antiderivative and Exact Definite Integral:

    • The general indefinite integral (antiderivative) is:         ∫(x2+1) dx=x33+x+C\int (x^2 + 1)\,dx = \frac{x^3}{3} + x + C         where CC is an arbitrary constant of integration.
    • Differentiating this confirms the integrand:         ddx(x33+x+C)=x2+1\frac{d}{dx}\left(\frac{x^3}{3} + x + C\right) = x^2 + 1
    • Evaluating the definite integral from 00 to 22 using the Fundamental Theorem of Calculus:         ∫02(x2+1) dx=[x33+x+C]02=(233+2+C)−(033+0+C)\int_0^2 (x^2 + 1)\,dx = \left[ \frac{x^3}{3} + x + C \right]_0^2 = \left( \frac{2^3}{3} + 2 + C \right) - \left( \frac{0^3}{3} + 0 + C \right)
    • The constants of integration CC cancel out algebraically:         (83+2+C)−C=83+63=143=423≈4.6667\left(\frac{8}{3} + 2 + C\right) - C = \frac{8}{3} + \frac{6}{3} = \frac{14}{3} = 4\frac{2}{3} \approx 4.6667
    • The exact analytical area under the curve is 4234\frac{2}{3}.

Rectangle-Based Riemann Sum Formulations

  • Discretization Parameters:

    • Interval: [a,b][a, b] partitioned into nn equal subintervals (rectangles).
    • Number of boundary points: n+1n + 1 points, labeled x0,x1,x2,…,xnx_0, x_1, x_2, \dots, x_n, where x0=ax_0 = a and xn=bx_n = b.
    • Uniform width of each rectangle: h=Δx=b−anh = \Delta x = \frac{b - a}{n}.
    • Coordinates of partition points: xi=a+i⋅hx_i = a + i \cdot h, for i=0,1,…,ni = 0, 1, \dots, n.
  • The Choice of Height (f(x)f(x)):

    • The primary implementation question for rectangular numerical integration is determining which xx-coordinate within each subinterval [xi,xi+1][x_i, x_{i+1}] evaluates the height of the rectangle.
    • The three standard selections are:
      1. Left Endpoint: Evaluated at the start of each subinterval: xix_i.
      2. Right Endpoint: Evaluated at the end of each subinterval: xi+1x_{i+1}.
      3. Midpoint: Evaluated at the arithmetic mean of the boundary points: mi=xi+xi+12m_i = \frac{x_i + x_{i+1}}{2}.
  • Summation Formulas:

    • Left Endpoint Approximation (LnL_n):Ln=∑i=0n−1h⋅f(xi)L_n = \sum_{i=0}^{n-1} h \cdot f(x_i)
    • Right Endpoint Approximation (RnR_n):Rn=∑i=0n−1h⋅f(xi+1)=∑i=1nh⋅f(xi)R_n = \sum_{i=0}^{n-1} h \cdot f(x_{i+1}) = \sum_{i=1}^n h \cdot f(x_i)
    • Midpoint Approximation (MnM_n):Mn=∑i=0n−1h⋅f(mi)=∑i=0n−1h⋅f(xi+xi+12)M_n = \sum_{i=0}^{n-1} h \cdot f(m_i) = \sum_{i=0}^{n-1} h \cdot f\left(\frac{x_i + x_{i+1}}{2}\right)

Left and Right Endpoint Numerical Computations

  • Left Endpoint Rule Calculations on f(x)=x2+1f(x) = x^2 + 1 over [0,2][0, 2]:

    • n=1n = 1 Rectangle:
      • Width: h=2−01=2h = \frac{2 - 0}{1} = 2.
      • Height: f(x0)=f(0)=02+1=1f(x_0) = f(0) = 0^2 + 1 = 1.
      • Area=2⋅f(0)=2⋅1=2\text{Area} = 2 \cdot f(0) = 2 \cdot 1 = 2.
      • Result: Significantly underestimates the true value of 4234\frac{2}{3}.
    • n=2n = 2 Rectangles:
      • Width: h=2−02=1h = \frac{2 - 0}{2} = 1.
      • Subintervals: [0,1][0, 1] and [1,2][1, 2].
      • Heights: f(0)=1f(0) = 1, f(1)=12+1=2f(1) = 1^2 + 1 = 2.
      • Area=1⋅f(0)+1⋅f(1)=1⋅1+1⋅2=3\text{Area} = 1 \cdot f(0) + 1 \cdot f(1) = 1 \cdot 1 + 1 \cdot 2 = 3.
    • n=4n = 4 Rectangles:
      • Width: h=2−04=0.5h = \frac{2 - 0}{4} = 0.5.
      • Subintervals: [0,0.5][0, 0.5], [0.5,1.0][0.5, 1.0], [1.0,1.5][1.0, 1.5], and [1.5,2.0][1.5, 2.0].
      • Heights and individual rectangle areas:
        • Rectangle 1: Height f(0)=1f(0) = 1, Area=1⋅0.5=12=0.5\text{Area} = 1 \cdot 0.5 = \frac{1}{2} = 0.5.
        • Rectangle 2: Height f(0.5)=(0.5)2+1=1.25f(0.5) = (0.5)^2 + 1 = 1.25, Area=1.252=0.625\text{Area} = \frac{1.25}{2} = 0.625.
        • Rectangle 3: Height f(1)=12+1=2f(1) = 1^2 + 1 = 2, Area=22=1\text{Area} = \frac{2}{2} = 1.
        • Rectangle 4: Height f(1.5)=(1.5)2+1=3.25f(1.5) = (1.5)^2 + 1 = 3.25, Area=3.252=1.625\text{Area} = \frac{3.25}{2} = 1.625.
      • Total Sum: 0.5+0.625+1.0+1.625=3.750.5 + 0.625 + 1.0 + 1.625 = 3.75.
    • n=8n = 8 Rectangles:
      • Width: h=2−08=0.25h = \frac{2 - 0}{8} = 0.25.
      • Evaluates previously calculated heights plus newly sampled quarter-increment coordinates: x=0.25,0.75,1.25,1.75x = 0.25, 0.75, 1.25, 1.75.
      • All values are factored by the width 0.250.25 (or divided by 44).
    • Convergence Behavior: Even scaling up to n=16n = 16 rectangles does not comfortably yield reliable single decimal place precision (fails to reach 4.64.6). For algorithms requiring 1,000,0001{,}000{,}000 rectangles, computing simple rectangular endpoints demands an enormous number of arithmetic operations while remaining inefficient.
  • Right Endpoint Rule Calculations on f(x)=x2+1f(x) = x^2 + 1 over [0,2][0, 2]:

    • n=1n = 1 Rectangle:
      • Width: h=2h = 2.
      • Height: f(x1)=f(2)=22+1=5f(x_1) = f(2) = 2^2 + 1 = 5.
      • Area=2⋅f(2)=2⋅5=10\text{Area} = 2 \cdot f(2) = 2 \cdot 5 = 10.
      • Result: Significantly overshoots the true value of 4234\frac{2}{3}.
    • n=2n = 2 Rectangles:
      • Width: h=1h = 1.
      • Heights: f(1)=2f(1) = 2, f(2)=5f(2) = 5.
      • Area=1⋅f(1)+1⋅f(2)=2+5=7\text{Area} = 1 \cdot f(1) + 1 \cdot f(2) = 2 + 5 = 7.
    • n=4n = 4 Rectangles:
      • Width: h=0.5h = 0.5.
      • Heights: f(0.5)=1.25f(0.5) = 1.25, f(1.0)=2f(1.0) = 2, f(1.5)=3.25f(1.5) = 3.25, f(2.0)=5f(2.0) = 5.
      • Area=0.5⋅(1.25+2+3.25+5)=0.5⋅(11.5)=5.75\text{Area} = 0.5 \cdot (1.25 + 2 + 3.25 + 5) = 0.5 \cdot (11.5) = 5.75.
      • Error Comparison: 5.755.75 is off from the analytical value (4.66674.6667) by approximately +1.0833+1.0833, roughly symmetric in magnitude of error to the n=4n = 4 left endpoint value (3.753.75, off by −0.9167-0.9167).
    • n=8n = 8 Rectangles:
      • Result: Yields 5.185.18.

The Midpoint Rule and Error Cancellation Mechanics

  • Geometric Mechanism of the Midpoint Rule:

    • Instead of evaluating the height at either boundary of a subinterval, the midpoint rule samples the function at the center: mi=xi+xi+12m_i = \frac{x_i + x_{i+1}}{2}.
    • On any subinterval where the function is strictly increasing:
      • Across the left half of the rectangle, the horizontal rectangular top sits above the actual function curve (local overestimation).
      • Across the right half of the rectangle, the horizontal rectangular top sits below the actual function curve (local underestimation).
    • Because one half of the subinterval overestimates while the other half underestimates, the two error areas geometrically cancel each other out to a substantial degree.
    • Similarly, if a function is strictly decreasing over a subinterval, the left half underestimates and the right half overestimates, achieving the same self-canceling error behavior.
  • Midpoint Rule Numerical Calculations on f(x)=x2+1f(x) = x^2 + 1 over [0,2][0, 2]:

    • n=1n = 1 Rectangle:
      • Width: h=2h = 2.
      • Midpoint: m0=0+22=1m_0 = \frac{0 + 2}{2} = 1.
      • Height: f(1)=12+1=2f(1) = 1^2 + 1 = 2.
      • Area=2⋅f(1)=2⋅2=4\text{Area} = 2 \cdot f(1) = 2 \cdot 2 = 4.
      • Comparison: An area of 44 is notably closer to 4234\frac{2}{3} than the left endpoint result (22) or right endpoint result (1010).
    • n=2n = 2 Rectangles:
      • Width: h=1h = 1.
      • Subintervals: [0,1][0, 1] and [1,2][1, 2].
      • Midpoints: m0=0.5m_0 = 0.5, m1=1.5m_1 = 1.5.
      • Heights: f(0.5)=(0.5)2+1=1.25f(0.5) = (0.5)^2 + 1 = 1.25, f(1.5)=(1.5)2+1=3.25f(1.5) = (1.5)^2 + 1 = 3.25.
      • Area=1⋅f(0.5)+1⋅f(1.5)=1.25+3.25=4.5\text{Area} = 1 \cdot f(0.5) + 1 \cdot f(1.5) = 1.25 + 3.25 = 4.5.
      • Comparison: With just n=2n = 2 rectangles, the midpoint rule (4.54.5) achieves a far superior approximation to the analytical value (4.66674.6667) than either the left (3.753.75) or right (5.185.18) endpoint methods achieved with n=8n = 8 rectangles.
    • n=4n = 4 Rectangles:
      • Width: h=0.5h = 0.5.
      • Midpoints: 0.25,0.75,1.25,1.750.25, 0.75, 1.25, 1.75.
      • Area=4.625\text{Area} = 4.625.
      • Comparison: Successfully captures the first decimal digit (4.64.6).
    • n=8n = 8 Rectangles:
      • Width: h=0.25h = 0.25.
      • Midpoints: Values across the interval from 00 to 22 expressed in eighths that do not simplify to fourths:             18,38,58,78,98,118,138,158\frac{1}{8}, \frac{3}{8}, \frac{5}{8}, \frac{7}{8}, \frac{9}{8}, \frac{11}{8}, \frac{13}{8}, \frac{15}{8}
      • Evaluating f(x)f(x) at these eight midpoints and scaling by h=0.25h = 0.25 yields a value exceptionally close to 4234\frac{2}{3}.
  • Monotonicity and Edge Cases:

    • The error cancellation of the midpoint rule is sustained when a function is monotonic (consistently increasing or decreasing) over each subinterval.
    • Potential edge cases arise if a function exhibits an internal local extremum within a single rectangle (e.g., increasing up to a peak and then decreasing). However, as the step size hh decreases, the width of individual rectangles becomes sufficiently small that local turning points occupy an increasingly negligible fraction of the total partition.

The Trapezoidal Rule: Derivation and Mechanics

  • Algorithmic Critique of Constant Approximations:

    • Left and right endpoint methods approximate the curve on each subinterval using a constant function (a degree-0 polynomial, represented by a flat horizontal line):         f(x)≈f(xi)orf(x)≈f(xi+1)f(x) \approx f(x_i) \quad \text{or} \quad f(x) \approx f(x_{i+1})
    • Approximating an arbitrary curve with a constant function fails to model the actual mathematical behavior of the function. Constant approximations are only exact when integrating a flat line (such as y=2y = 2).
    • For a strictly increasing function, a flat left-aligned box must systematically underestimate every subinterval, and a right-aligned box must systematically overestimate every subinterval.
  • Linear Modeling via Secant Lines:

    • Rather than a constant line, the function over [xi,xi+1][x_i, x_{i+1}] is better approximated by a secant line connecting the boundary coordinates (xi,f(xi))(x_i, f(x_i)) and (xi+1,f(xi+1))(x_{i+1}, f(x_{i+1})).
    • As established in Taylor polynomial theory and differential calculus, secant lines approach the tangent line as the width decreases, capturing both the local value and the rate of change of the function.
    • Geometrically, the region beneath a slanted secant line between xix_i and xi+1x_{i+1} forms a trapezoid.
  • Mathematical Derivation of the Trapezoid Area:

    • A trapezoid is half of a rectangle whose height is the sum of the two parallel vertical bases and whose width is the base width hh:         Areatrapezoid=h⋅f(xi)+f(xi+1)2=h2(f(xi)+f(xi+1))\text{Area}_{\text{trapezoid}} = h \cdot \frac{f(x_i) + f(x_{i+1})}{2} = \frac{h}{2} \left( f(x_i) + f(x_{i+1}) \right)
    • When summing across nn consecutive subintervals, every internal boundary wall is shared by two adjacent trapezoids (acting as the right edge of trapezoid ii and the left edge of trapezoid i+1i+1).
    • Consequently, each internal evaluation f(xi)f(x_i) for i=1,…,n−1i = 1, \dots, n-1 appears twice in the sum, whereas the exterior endpoints f(x0)f(x_0) and f(xn)f(x_n) appear only once:         Tn=h2[f(x0)+2f(x1)+2f(x2)+⋯+2f(xn−1)+f(xn)]T_n = \frac{h}{2} \left[ f(x_0) + 2f(x_1) + 2f(x_2) + \dots + 2f(x_{n-1}) + f(x_n) \right]Tn=h2[f(x0)+f(xn)+2∑i=1n−1f(xi)]T_n = \frac{h}{2} \left[ f(x_0) + f(x_n) + 2 \sum_{i=1}^{n-1} f(x_i) \right]
  • Trapezoidal Numerical Calculations on f(x)=x2+1f(x) = x^2 + 1 over [0,2][0, 2]:

    • n=1n = 1 Trapezoid:
      • Width: h=2h = 2.
      • Boundary heights: f(0)=1f(0) = 1, f(2)=5f(2) = 5.
      • Area=22(f(0)+f(2))=1⋅(1+5)=6\text{Area} = \frac{2}{2} \left( f(0) + f(2) \right) = 1 \cdot (1 + 5) = 6.
      • Comparison: While it overshoots the true answer (4234\frac{2}{3}), an area of 66 is vastly superior to the single right-rectangle result of 1010.
    • n=2n = 2 Trapezoids:
      • Width: h=1h = 1.
      • Points: x0=0x_0 = 0, x1=1x_1 = 1, x2=2x_2 = 2.
      • Heights: f(0)=1f(0) = 1, f(1)=2f(1) = 2, f(2)=5f(2) = 5.
      • Trapezoid 1 Area (over [0,1][0, 1]): 12(1+2)=1.5\frac{1}{2} (1 + 2) = 1.5.
      • Trapezoid 2 Area (over [1,2][1, 2]): 12(2+5)=3.5\frac{1}{2} (2 + 5) = 3.5.
      • Total Area: 1.5+3.5=51.5 + 3.5 = 5.
      • Error: The error is exactly 5−423=13≈0.33335 - 4\frac{2}{3} = \frac{1}{3} \approx 0.3333.
    • n=4n = 4 Trapezoids:
      • Width: h=0.5h = 0.5.
      • Evaluating across the points gives approximately 4.754.75 (or zoomed detail displaying 4.68754.6875).

Comparative Synthesis of Quadrature Methods

  • Summary Table for f(x)=x2+1f(x) = x^2 + 1 on [0,2][0, 2] (Exact Value = 423≈4.66674\frac{2}{3} \approx 4.6667):

    • Single Region (n=1n = 1):

      • Left Endpoint: 22
      • Right Endpoint: 1010
      • Midpoint Rule: 44
      • Trapezoidal Rule: 66
    • Two Regions (n=2n = 2):

      • Left Endpoint: 33
      • Right Endpoint: 77
      • Midpoint Rule: 4.54.5
      • Trapezoidal Rule: 55
  • Theoretical Comparison: Midpoint vs. Trapezoid:

    • Accuracy in the Test Problem: For the convex curve f(x)=x2+1f(x) = x^2 + 1, the midpoint method numbers (4.54.5 for n=2n = 2) are numerically closer to 4.66674.6667 than the trapezoid method numbers (55 for n=2n = 2).
    • Underlying Rationales: The midpoint rule benefits from internal geometric cancellation where under-approximations and over-approximations offset each other across each rectangle. By contrast, because f(x)=x2+1f(x) = x^2 + 1 is strictly concave up (f′′(x)>0f''(x) > 0), the secant line sits entirely above the curve, causing the trapezoidal rule to consistently overestimate across all subintervals.
    • Practical Application: The midpoint rule on average performs remarkably well, but specific functions can be engineered where midpoint cancellation breaks down. The trapezoidal rule directly models the dynamic slope of the function on every interval via secant lines. Because their computational performance is within a comparable order of magnitude, both provide solid numerical approximations.

Computational Implementation in GraphState

  • GraphState Software Syntax:

    • int(f, [a, b]): Standard syntax to define and plot a definite integral of function f across the interval [a, b].
    • rn = [count]: Parameter defining the Riemann sum subdivision. For instance, setting rn = 5 renders five discrete rectangles evaluated via the midpoint rule.
    • To plot the continuous, true definite integral without rectangle discretization, the rn metric is omitted.
  • Laboratory Assignment Tasks:

    • Target function: f(x)=sin⁡(x)f(x) = \sin(x) starting from x=0.25x = 0.25.
    • Task 1: Approximate ∫sin⁡(x) dx\int \sin(x)\,dx with 44 regions using the Midpoint rule.
    • Task 2: Approximate ∫sin⁡(x) dx\int \sin(x)\,dx with 44 regions using the Trapezoidal rule.
    • Task 3: Compute the exact analytical solution using the fundamental antiderivative of the sine function:         ∫sin⁡(x) dx=−cos⁡(x)+C\int \sin(x)\,dx = -\cos(x) + C

Questions & Discussion

  • Audience Question / Discussion on Limits and Machine Representation:

    • Question/Prompt: How can one conceptualize or compute a limit approaching infinity when finite machines cannot evaluate out to infinity?
    • Response: Limits to infinity can be algebraically restructured into limits where a parameter approaches zero (h→0h \to 0). In computational terms, the limit as h→0h \to 0 is interpreted via machine epsilon—the smallest increment storeable where 1+ϵmach>11 + \epsilon_{\text{mach}} > 1. Setting Δx\Delta x to this fundamental threshold represents the highest precision approximation attainable on a digital system for that scale.
  • Audience Question / Discussion on Algorithmic Inefficiency of Endpoint Rules:

    • Question/Prompt: Why are left and right endpoint approximations fundamentally inefficient processes for calculating areas under curves?
    • Response: Endpoint algorithms model the function using a constant function (flat horizontal top) over each subinterval. For an increasing function, every single right box overestimates and every left box underestimates. Because constant functions ignore the slope and behavior of non-constant functions entirely, thousands or millions of rectangles are required to attain modest precision.
  • Audience Question / Discussion on Midpoint Accuracy versus Trapezoidal Validity:

    • Question/Prompt: Why would one consider using the trapezoidal method if the midpoint method produced numbers closer to the exact answer (4.54.5 vs. 55 for n=2n = 2)? Is midpoint accuracy merely an artifact of errors canceling out rather than true curve tracking?
    • Response: The midpoint rule is on average exceptionally accurate because internal errors offset. However, functions can be constructed (e.g., oscillating trigonometric functions like sin⁡(x)\sin(x) over selected intervals) where midpoint cancellation does not perform as reliably. The trapezoid method explicitly incorporates the secant line to track function slopes; both methods are closely aligned in utility, and testing across varying intervals confirms their respective strengths.
  • Audience Question / Discussion on GraphState Technical Troubleshooting:

    • Question/Prompt: When pasting the definite integral command into GraphState, a plot fails to appear and returns a line error. What is causing this failure, and does a sine symbol need to be removed?
    • Response: The error is not an issue with duplicate sine syntax, but rather a browser caching bug. When GraphState encounters an error on an initial parse, it caches that parsed state in memory so it does not redundantly re-parse identical input. As a result, updating the input field without refreshing leaves the error active.
    • Resolution Procedure: While a standard hard refresh is typically executed via Ctrl + Shift + R, the command Alt + Shift + R forces GraphState internally to clear its cached parsing tree and completely re-parse the command from scratch, resolving the line error.