Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Part 2: Quadrature - From Photon Counts to Dark Matter Halos

Module 2: Static Problems & Quadrature | ASTR 596

San Diego State University

Learning Outcomes

By the end of this section, you will be able to:


The Fundamental Challenge

While differentiation is a local operation (needing only nearby points), integration is global—we must consider the entire domain. Quadrature (numerical integration) transforms the continuous integral:

I=abf(x)dxI = \int_a^b f(x) dx

into a discrete sum:

Ii=0nwif(xi)I \approx \sum_{i=0}^n w_i f(x_i)

The art lies in choosing:

Physical Motivation: Why Astronomers Integrate

Integration is everywhere in astronomy because we observe integrated quantities:

  1. Spectroscopy: We don’t see individual photons but integrated flux

    F=λ1λ2FλdλF = \int_{\lambda_1}^{\lambda_2} F_\lambda d\lambda
  2. Photometry: CCD pixels integrate photons over exposure time

    Nphotons=0TΦ(t)dtN_\text{photons} = \int_0^T \Phi(t) dt
  3. Structure: Mass distributions require volume integrals

    M(<r)=0r4πr2ρ(r)drM(<r) = \int_0^r 4\pi r'^2 \rho(r') dr'
  4. Cosmology: Distances involve integrating through expanding space

    dL=(1+z)0zcdzH(z)d_L = (1+z) \int_0^z \frac{c\,dz'}{H(z')}

Each context demands different accuracy, and the function properties (smooth vs oscillatory, bounded vs singular) determine the best method.


Building Integration Methods: From Rectangles to Optimality

The Riemann Sum Foundation

The definition of the Riemann integral suggests the simplest approximation:

abf(x)dx=limni=0n1f(xi)Δxi\int_a^b f(x)dx = \lim_{n \to \infty} \sum_{i=0}^{n-1} f(x_i^*) \Delta x_i

where xi[xi,xi+1]x_i^* \in [x_i, x_{i+1}] and Δxi=xi+1xi\Delta x_i = x_{i+1} - x_i.

Different choices of xix_i^* give different methods:

Four geometric approaches to numerical integration. Each method approximates the area under f(x) = e^{-x/2}\cos(2x) + 1 from 0 to 2 using different geometric shapes: Rectangle rule (top-left) uses constant function values at left endpoints, showing systematic error; Midpoint rule (top-right) uses rectangles centered at interval midpoints for better accuracy; Trapezoidal rule (bottom-left) connects function values with straight lines, capturing linear trends; Simpson’s rule (bottom-right) uses parabolic segments through three points, achieving fourth-order accuracy for smooth functions. Each panel displays the true integral value, approximation, and relative/absolute errors. Notice how the colored geometric approximations progressively approach the true area as interpolation becomes more sophisticated—this visual intuition directly corresponds to the mathematical convergence orders.

Figure 1:Four geometric approaches to numerical integration. Each method approximates the area under f(x)=ex/2cos(2x)+1f(x) = e^{-x/2}\cos(2x) + 1 from 0 to 2 using different geometric shapes: Rectangle rule (top-left) uses constant function values at left endpoints, showing systematic error; Midpoint rule (top-right) uses rectangles centered at interval midpoints for better accuracy; Trapezoidal rule (bottom-left) connects function values with straight lines, capturing linear trends; Simpson’s rule (bottom-right) uses parabolic segments through three points, achieving fourth-order accuracy for smooth functions. Each panel displays the true integral value, approximation, and relative/absolute errors. Notice how the colored geometric approximations progressively approach the true area as interpolation becomes more sophisticated—this visual intuition directly corresponds to the mathematical convergence orders.


Method 1: Rectangle Rule - The Starting Point

Why we care: The simplest possible approximation — helps understand error analysis that applies to all methods.

Mathematical Formulation

For uniform spacing h=(ba)/nh = (b-a)/n:

Ihi=0n1f(a+ih)(left rectangle)I \approx h \sum_{i=0}^{n-1} f(a + ih) \quad \text{(left rectangle)}

Error Analysis via Taylor Series

On each subinterval [xi,xi+1][x_i, x_{i+1}]:

xixi+1f(x)dx=xixi+1[f(xi)+f(xi)(xxi)+f(ξ)2(xxi)2]dx\int_{x_i}^{x_{i+1}} f(x)dx = \int_{x_i}^{x_{i+1}} \left[f(x_i) + f'(x_i)(x-x_i) + \frac{f''(\xi)}{2}(x-x_i)^2\right]dx

Evaluating:

=f(xi)h+f(xi)h22+O(h3)= f(x_i)h + f'(x_i)\frac{h^2}{2} + O(h^3)

The rectangle rule uses only f(xi)hf(x_i)h, so the local error is:

Elocal=h22f(ξ)E_{local} = \frac{h^2}{2}f'(\xi)

Over nn intervals:

Etotal=nO(h2)=bahO(h2)=O(h)E_{total} = n \cdot O(h^2) = \frac{b-a}{h} \cdot O(h^2) = O(h)

This is a first-order method—halving hh halves the error.

Implementation

def rectangle_rule(f, a, b, n):
    """Left rectangle rule"""
    h = (b - a) / n
    total = 0
    for i in range(n):
        x = a + i * h
        total += f(x)
    return h * total

Why Not to Use Rectangle Rule


Method 2: Trapezoidal Rule - The Workhorse

Why we care: The workhorse of experimental data integration—robust and simple.

Geometric Intuition

Instead of rectangles, connect consecutive points with straight lines, creating trapezoids. The area of each trapezoid averages the function values at its endpoints.

Mathematical Formulation

The area of a trapezoid with parallel sides f(xi)f(x_i) and f(xi+1)f(x_{i+1}) and height hh is:

Ai=h2[f(xi)+f(xi+1)]A_i = \frac{h}{2}[f(x_i) + f(x_{i+1})]

Summing over all intervals:

Ii=0n1h2[f(xi)+f(xi+1)]=h[f(a)2+i=1n1f(xi)+f(b)2]I \approx \sum_{i=0}^{n-1} \frac{h}{2}[f(x_i) + f(x_{i+1})] = h\left[\frac{f(a)}{2} + \sum_{i=1}^{n-1} f(x_i) + \frac{f(b)}{2}\right]

Error Analysis

Using Taylor expansion around the midpoint xi+h/2x_i + h/2:

f(xi)=f(xi+h/2)h2f(xi+h/2)+h28f(xi+h/2)+O(h3)f(x_i) = f(x_i + h/2) - \frac{h}{2}f'(x_i + h/2) + \frac{h^2}{8}f''(x_i + h/2) + O(h^3)
f(xi+1)=f(xi+h/2)+h2f(xi+h/2)+h28f(xi+h/2)+O(h3)f(x_{i+1}) = f(x_i + h/2) + \frac{h}{2}f'(x_i + h/2) + \frac{h^2}{8}f''(x_i + h/2) + O(h^3)

The trapezoidal approximation on [xi,xi+1][x_i, x_{i+1}]:

h2[f(xi)+f(xi+1)]=hf(xi+h/2)+h312f(xi+h/2)+O(h4)\frac{h}{2}[f(x_i) + f(x_{i+1})] = hf(x_i + h/2) + \frac{h^3}{12}f''(x_i + h/2) + O(h^4)

Since the exact integral is hf(xi+h/2)+O(h3)hf(x_i + h/2) + O(h^3):

Local error: Elocal=h312f(ξ)E_{local} = -\frac{h^3}{12}f''(\xi)

Global error: Etotal=(ba)h212f(ξ)\boxed{E_{total} = -\frac{(b-a)h^2}{12}f''(\xi)} for some ξ[a,b]\xi \in [a,b]

This is a second-order method!

Common Failure Modes and Fixes

ProblemSymptomSolution
SingularitiesInfinite resultSubtract singularity analytically
OscillationsPoor convergenceIncrease n or use adaptive spacing
Noisy dataErratic resultsSmooth first or use fewer points
DiscontinuitiesWrong answerSplit integral at discontinuity

Method 3: Simpson’s Rule - Parabolic Perfection

Why we care: When your function is smooth and you want high accuracy with moderate computational cost.

The Key Insight

Instead of linear interpolation (trapezoids), use quadratic interpolation (parabolas) through consecutive triplets of points.

Derivation via Interpolation

For three equally-spaced points x0,x1,x2x_0, x_1, x_2 with x1=x0+hx_1 = x_0 + h and x2=x0+2hx_2 = x_0 + 2h, the unique parabola through (xi,f(xi))(x_i, f(x_i)) is:

p(x)=f(x0)(xx1)(xx2)2h2f(x1)(xx0)(xx2)h2+f(x2)(xx0)(xx1)2h2p(x) = f(x_0)\frac{(x-x_1)(x-x_2)}{2h^2} - f(x_1)\frac{(x-x_0)(x-x_2)}{h^2} + f(x_2)\frac{(x-x_0)(x-x_1)}{2h^2}

Integrating this parabola from x0x_0 to x2x_2:

x0x2p(x)dx=h3[f(x0)+4f(x1)+f(x2)]\int_{x_0}^{x_2} p(x)dx = \frac{h}{3}[f(x_0) + 4f(x_1) + f(x_2)]

These are the famous Simpson weights: 1, 4, 1.

Composite Simpson’s Rule

For nn intervals (must be even):

Ih3[f(a)+4i=1,3,5...n1f(xi)+2i=2,4,6...n2f(xi)+f(b)]\boxed{I \approx \frac{h}{3}\left[f(a) + 4\sum_{i=1,3,5...}^{n-1} f(x_i) + 2\sum_{i=2,4,6...}^{n-2} f(x_i) + f(b)\right]}

The pattern of weights is:

Error Analysis and the Fourth-Order Miracle

Through careful Taylor series analysis, the local error for Simpson’s rule on interval [xi,xi+2][x_i, x_{i+2}] is:

Elocal=h590f(4)(ξ)E_{local} = -\frac{h^5}{90}f^{(4)}(\xi)

Global error: Etotal=(ba)h4180f(4)(ξ)\boxed{E_{total} = -\frac{(b-a)h^4}{180}f^{(4)}(\xi)}

Simpson’s rule is fourth-order accurate despite using only parabolas!

Why Simpson’s Rule is Fourth-Order

The “miracle” of Simpson’s fourth-order accuracy comes from symmetry. When we integrate the interpolating parabola, the f(ξ)f'''(\xi) term (which would give O(h3)O(h^3) error) has odd symmetry about the midpoint and integrates to zero:

x0x2(xx1)3dx=0\int_{x_0}^{x_2} (x-x_1)^3 dx = 0

This cancellation is automatic—we get fourth-order accuracy “for free” by using symmetric intervals! Since the error term involves f(4)f^{(4)}, and the fourth derivative of any cubic polynomial is zero, Simpson’s rule integrates cubic polynomials exactly.

When Simpson’s Rule Shines

Common Failure Modes and Fixes

ProblemSymptomSolution
Odd nWon’t workAdd one interval or use trapezoid for last
Noisy dataAmplifies noiseUse trapezoidal instead
Discontinuous f⁽⁴⁾Poor convergenceSplit at discontinuity
Sharp peaksMisses featuresUse adaptive refinement
The power of higher-order methods. Comparing error scaling for the integral \int_0^1 e^x dx reveals why Simpson’s rule dominates for smooth functions. Left panel: The log-log plot shows dramatically different convergence slopes—trapezoidal rule’s error decreases as O(h^2) (slope = -2) while Simpson’s rule achieves O(h^4) (slope = -4). Right panel: The efficiency comparison is striking—to achieve 10^{-10} accuracy, Simpson’s rule needs ~100× fewer points than the trapezoidal rule! This quadratic advantage in computational efficiency explains why high-order methods are preferred for smooth integrands, despite their greater complexity. The machine precision floor (\epsilon \approx 10^{-16}) eventually limits both methods, but Simpson’s rule reaches this limit far more efficiently.

Figure 2:The power of higher-order methods. Comparing error scaling for the integral 01exdx\int_0^1 e^x dx reveals why Simpson’s rule dominates for smooth functions. Left panel: The log-log plot shows dramatically different convergence slopes—trapezoidal rule’s error decreases as O(h2)O(h^2) (slope = -2) while Simpson’s rule achieves O(h4)O(h^4) (slope = -4). Right panel: The efficiency comparison is striking—to achieve 10-10 accuracy, Simpson’s rule needs ~100× fewer points than the trapezoidal rule! This quadratic advantage in computational efficiency explains why high-order methods are preferred for smooth integrands, despite their greater complexity. The machine precision floor (ϵ1016\epsilon \approx 10^{-16}) eventually limits both methods, but Simpson’s rule reaches this limit far more efficiently.


Method 4: Gaussian Quadrature - Optimal Point Placement

Why we care: Sometimes being clever about WHERE you sample beats taking MORE samples.

The Revolutionary Idea

All previous methods used equally-spaced points. But what if we could choose both the points AND weights optimally?

Gauss’s insight: For nn points, we can make the method exact for all polynomials up to degree 2n12n-1 by choosing specific locations. These points are the roots of Legendre polynomials, which have special orthogonality properties that maximize accuracy.

Example: 2-Point Gaussian Quadrature

Instead of evaluating at equally-spaced points, Gaussian quadrature uses special points. For 2 points on [1,1][-1,1]:

This seemingly arbitrary choice makes the method exact for all cubic polynomials!

In Practice

You won’t derive Gauss points yourself. Instead:

from scipy.special import roots_legendre

# Get n Gauss points and weights for [-1,1]
points, weights = roots_legendre(n)

# Transform to general interval [a,b]
x_gauss = (b-a)/2 * points + (a+b)/2
w_gauss = (b-a)/2 * weights

# Compute integral
integral = sum(w * f(x) for x, w in zip(x_gauss, w_gauss))

When Gaussian Quadrature Matters

Key Takeaway: This principle—optimal placement beats more samples—appears throughout computational physics, from choosing timesteps in orbit integration to placing grid points in PDE solvers.


Method 5: Monte Carlo Integration - When Dimensions Explode

Why we care: The only practical method for high-dimensional integrals that appear in statistical mechanics, quantum mechanics, and Bayesian inference.

Understanding Dimensions in Integration

When we talk about “dimensions” in Monte Carlo integration, we mean the number of integration variables, not spatial dimensions:

For example, computing the partition function for 100 atoms requires a 300-dimensional integral (3 position coordinates per atom)!

The Curse of Dimensionality

For a dd-dimensional integral with nn points per dimension, deterministic methods need ndn^d evaluations:

This exponential growth makes grid-based methods impossible for d>5d > 5.

The Monte Carlo Solution

Randomly sample NN points xi\vec{x}_i in the domain Ω\Omega:

I=Ωf(x)dxI^=V(Ω)Ni=1Nf(xi)I = \int_\Omega f(\vec{x})d\vec{x} \approx \hat{I} = \frac{V(\Omega)}{N}\sum_{i=1}^N f(\vec{x}_i)

where V(Ω)V(\Omega) is the volume of the domain.

Error Analysis

For the Monte Carlo estimator I^=V(Ω)Ni=1Nf(xi)\hat{I} = \frac{V(\Omega)}{N}\sum_{i=1}^N f(\vec{x}_i):

Step 1: The expected value of our estimator is the true integral:

E[I^]=V(Ω)Ni=1NE[f(xi)]=V(Ω)E[f]=I\mathbb{E}[\hat{I}] = \frac{V(\Omega)}{N} \sum_{i=1}^N \mathbb{E}[f(\vec{x}_i)] = V(\Omega) \cdot \mathbb{E}[f] = I

Step 2: The variance of the estimator is:

Var[I^]=Var[V(Ω)Ni=1Nf(xi)]=V(Ω)2N2NVar[f]=V(Ω)2σf2N\text{Var}[\hat{I}] = \text{Var}\left[\frac{V(\Omega)}{N}\sum_{i=1}^N f(\vec{x}_i)\right] = \frac{V(\Omega)^2}{N^2} \cdot N \cdot \text{Var}[f] = \frac{V(\Omega)^2 \sigma_f^2}{N}

where σf2=Var[f]=E[f2]E[f]2\sigma_f^2 = \text{Var}[f] = \mathbb{E}[f^2] - \mathbb{E}[f]^2 is the variance of ff over the domain.

Step 3: The standard error (standard deviation of the estimator) is:

σI=Var[I^]=V(Ω)σfN\boxed{\sigma_I = \sqrt{\text{Var}[\hat{I}]} = \frac{V(\Omega)\sigma_f}{\sqrt{N}}}

Key insight: Error N1/2\propto N^{-1/2} independent of dimension dd!

This assumes:

When Monte Carlo Wins

DimensionGrid Points for 1% ErrorMonte Carlo Points for 1% Error
110010,000
210,00010,000
31,000,00010,000
510¹⁰10,000
1010²⁰10,000

Above ~4 dimensions, Monte Carlo becomes superior!

Monte Carlo integration: slow convergence but dimension-independent. Left panel: Unlike grid-based methods, Monte Carlo uses random sampling—here 200 visible points (from 1000 total) sample the 2D paraboloid function f(x,y) = 4(1-x^2-y^2) over the unit square. Points are colored by function value using the same colormap as the background contours, ranging from f = +4 (yellow, maximum at origin) to f = -4 (purple, minimum at corners). The integral estimate equals the domain area times the average function value. Right panel: Statistical convergence analysis from 50 different sample sizes (10 to 100,000), each tested with 20 independent Monte Carlo runs. The standard error (blue points) follows the universal N^{-1/2} scaling (dashed line) perfectly—this is the fundamental Monte Carlo convergence rate that never depends on dimension d, making it revolutionary for high-dimensional integrals in Bayesian inference, quantum mechanics, and statistical mechanics.

Figure 3:Monte Carlo integration: slow convergence but dimension-independent. Left panel: Unlike grid-based methods, Monte Carlo uses random sampling—here 200 visible points (from 1000 total) sample the 2D paraboloid function f(x,y)=4(1x2y2)f(x,y) = 4(1-x^2-y^2) over the unit square. Points are colored by function value using the same colormap as the background contours, ranging from f=+4f = +4 (yellow, maximum at origin) to f=4f = -4 (purple, minimum at corners). The integral estimate equals the domain area times the average function value. Right panel: Statistical convergence analysis from 50 different sample sizes (10 to 100,000), each tested with 20 independent Monte Carlo runs. The standard error (blue points) follows the universal N1/2N^{-1/2} scaling (dashed line) perfectly—this is the fundamental Monte Carlo convergence rate that never depends on dimension dd, making it revolutionary for high-dimensional integrals in Bayesian inference, quantum mechanics, and statistical mechanics.

Common Failure Modes and Fixes

ProblemSymptomSolution
Infinite varianceNo convergenceUse importance sampling
Rare eventsPoor samplingUse stratified sampling
CorrelationSlower convergenceUse quasi-random sequences
Small NLarge uncertaintyCan’t fix—need more samples

Adaptive Methods and Richardson Extrapolation

Adaptive Quadrature

When functions vary rapidly in some regions but are smooth elsewhere:

def adaptive_simpson(f, a, b, tol):
    """Recursively refine where needed"""
    # Compute with n and 2n points
    I1 = simpson(f, a, b, n=10)
    I2 = simpson(f, a, b, n=20)
    
    if abs(I2 - I1) < tol:
        return I2
    else:
        mid = (a + b) / 2
        left = adaptive_simpson(f, a, mid, tol/2)
        right = adaptive_simpson(f, mid, b, tol/2)
        return left + right

Richardson Extrapolation

If your method has error I(h)=Iexact+Chp+O(hp+1)I(h) = I_{exact} + Ch^p + O(h^{p+1}), compute at two resolutions:

Ibetter=2pI(h/2)I(h)2p1I_{better} = \frac{2^p I(h/2) - I(h)}{2^p - 1}

This cancels the leading error term! For trapezoidal rule (p=2):

Ibetter=4I(h/2)I(h)3I_{better} = \frac{4 I(h/2) - I(h)}{3}

This gives fourth-order accuracy from a second-order method!


Real Application: Galaxy Luminosity from Spectrum

Astronomers measure galaxy spectra as discrete samples at wavelengths λi\lambda_i with fluxes FiF_i. The total luminosity requires integration:

L=4πd2λminλmaxFλdλL = 4\pi d^2 \int_{\lambda_{min}}^{\lambda_{max}} F_\lambda d\lambda

Challenges

  1. Irregular wavelength spacing from different instruments

  2. Noise in flux measurements

  3. Missing data from atmospheric absorption

  4. Emission lines need high resolution

Solution Strategy

For irregular spacing, use trapezoidal rule with variable h:

def integrate_spectrum(wavelengths, fluxes):
    """Integrate spectrum with irregular spacing"""
    total = 0
    for i in range(len(wavelengths) - 1):
        dλ = wavelengths[i+1] - wavelengths[i]
        f_avg = (fluxes[i+1] + fluxes[i]) / 2
        total += dλ * f_avg
    return total

For emission lines: ensure ~10 points across line width or fit Gaussian profile and integrate analytically.


Choosing the Right Integration Method

Method Comparison Summary

MethodOrderProsConsBest For
RectangleO(h)O(h)SimpleInaccurateTeaching only
TrapezoidO(h2)O(h^2)Robust, handles irregular spacingModerate accuracyExperimental data, noisy functions
SimpsonO(h4)O(h^4)Very accurate for smooth functionsNeeds even n, amplifies noiseSmooth theoretical functions
GaussianO(h2n)O(h^{2n})Optimal for polynomialsNeed special pointsWhen you control sampling
Monte CarloO(N1/2)O(N^{-1/2})Dimension-independentSlow convergenceHigh dimensions (d > 4)

Practical Guidelines

  1. Start simple: Try trapezoidal first—it often suffices

  2. Check smoothness: Plot your function before choosing

  3. Test convergence: Double n and see if result changes

  4. Consider cost: Is f(x) expensive to evaluate?

  5. Remember libraries: Use scipy.integrate.quad for production


Debugging Integration

Common integration bugs and their diagnosis:

SymptomLikely CauseFix
Error doesn’t decrease with nWrong implementationCheck weights and indices
Error plateaus at ~10⁻¹⁴Round-off dominanceUse fewer points or extended precision
Oscillating convergenceUnder-resolving featuresIncrease n or use adaptive
Simpson gives weird resultsOdd nEnsure even intervals
Negative result for positive fOverflow in sumUse compensated summation
Monte Carlo not convergingInfinite varianceCheck if σ_f is finite

Bridge to Part 3: The Unity of Numerical Methods

You’ve now mastered two fundamental classes of problems: finding where functions equal zero and measuring areas under curves. These aren’t isolated techniques—they’re connected by deep mathematical principles.

In Part 3, we’ll explore these connections. You’ll see how root finding and integration are inverse operations, how the same error analysis principles govern both, and how to combine methods for complex problems. Most importantly, you’ll develop the intuition to choose the right method for any computational challenge.

The synthesis ahead will prepare you for the dynamic problems of Module 3, where these static methods become building blocks for simulating evolution through time.

Next: Part 3 - Synthesis