Integration COS 323
Integration
COS 323
Last time
• FFT Wrap-up • Polynomial interpolation
– Monomial basis, Lagrange interpolation
• Intro to numerical integration
Today
• Review numerical integration – Formulation, central considerations
• Interpolatory Quadrature – Review formulation; error analysis
• Newton-Cotes Quadrature – Midpoint, Trapezoid, Simpson’s Rule
• Error analysis for trapezoid, midpoint rules • Richardson Extrapolation • Gaussian Quadrature
Numerical Integration Problems
• Basic 1D numerical integration – Given ability to evaluate f (x) for any x, find
– Goal: best accuracy with fewest samples – Classic problem – even analytic functions not
necessarily integrable in closed form
Quadrature
1. Sample f(x) at a set of points 2. Approximate by a friendly function 3. Integrate approximating function
• Choices: – Which approximating function? – Which sampling points? (“nodes”)
• Even vs. uneven spacing?
– Fit single function vs. multiple (piecewise)?
Today’s topics
• Newton-Cotes methods: Estimating function over integral with a single polynomial and evenly-spaced points (subset of interpolatory quadrature)
• Piecewise application of several polynomials • Richardson extrapolation: iteratively improving a
result
• Adaptive quadrature: choosing node positions according to function
• Gaussian quadrature: “best” polynonial interpolation using optimally-chosen points (independent of function)
Trapezoidal Rule
• Approximate function by trapezoid
• Approximate integral by parabola through three points
Simpson’s Rule
Quadrature: General Formulation
• n-point qudarature rule:
• Open if a < x1 and xn < b, closed if a = x1, xn = b
Quadrature: General Formulation
• n-point qudarature rule:
• Open if a < x1 and xn < b, closed if a = x1, xn = b
Interpolatory Quadrature
• Use n points to approximate f using a polynomial of degree n-1 – Yields a quadrature rule “of degree n-1”
• For a “smooth” function f:
Newton-Cotes family of methods
• n-point Newton-Cotes: n evenly-spaced points in interval (open or closed)
• 1 point, open interval: midpoint rule
• 2 points, closed interval: trapezoidal rule
• 3 points, closed interval: Simpson’s rule
• Approximate integral by parabola through three points
Simpson’s Rule
Midpoint rule error analysis
Midpoint rule error analysis
• In general, error for a single segment proportional to h3 – Note that only odd powers of h (even terms of
Taylor expansion) remain in error expression
Midpoint vs Trapezoid
1. Error(T) > Error(M) (factor of 2!) 2. Halving interval error decreases by 1/8 3. Observation 2: Can use M(f) and T(f) to
learn about error
Simpson’s Rule Error
• Better accuracy than midpoint or trapezoid – Global error O(h4), exact for cubic (!) functions
• Higher-order polynomials (Newton-Cotes): – Global error O(hk+1) for k odd, O(hk+2) for k even – Fits polynomial of degree k for k points, k odd – Or polynomial of degree k-1 for k points, k even
Surprise benefts of odd-point rules
• Errors cancel exactly if true function is polynomial of degree n. (only if n odd!)
• Simpson’s rule is usually preferred over trapezoid & midpoint
Why not just keep increasing n?
€
Qn ( ˆ f ) −Qn ( f ) ≤ wii=1n
∑( ) ˆ f − f ∞where wi = b − a∑i.e., arbitrarily bad conditioning can result if some wi negative
Newton-Cotes:
€
n ≥11 means some wi < 0
and wi →∞ as n∑ →∞
Composite Quadrature
Extended/Composite/Multiple-Application Trapezoidal Rule
Divide into segments of width h, piecewise trapezoidal approximation
Extended Midpoint Rule
Divide into segments of width h:
Error analysis
• Halving interval length reduces error by 1/8 • BUT twice as many intervals needed effect
is error reduction by ¼
• Composite midpoint or trapezoid: O(h2) – Compare to h3 for a single trapezoid – Midpoint still better constant terms than trapezoid
• Composite Simpson’s: O(h4)
How small should h be?
• Can’t necessarily compute f(2) , so can’t compute error directly
• Can estimate error: 1. I(h1)= quadrature with width h1 2. I(h2)= quadrature with width h2=.5h1 3. Estimate error ≅ I(h2) – I(h1)
Progressive Qudrature
• Re-use nodes from Qn1 to compute Qn2 • Trapezoid: Halve intervals • Midpoint:
– Can cut interval into thirds:
Caution: Round-off error lurks
Adaptive Quadrature
Richardson Extrapolation &
Romberg Interpolation
Using knowledge of error to improve a solution
€
I = I h( )+ E h( )I(h1) + E(h1) = I(h2) + E(h2)
E ≅ − b − a12
h2 ʹ′ ʹ′ f
E(h1) ≅ E(h2)h1h2
⎛
⎝ ⎜
⎞
⎠ ⎟
2
If using trapezoidal rule and h2 =12
h1 :
I ≅ 43
I(h2) −13
I(h1)
and the new estimate has error O(h4 ) insetead of O(h2)
Example
€
Integrate f (x) = 0.2 + 25x − 200x 2 + 675x 3 − 900x 4 + 400x 5
from a = 0 to b = 0.8 (true value ≈1.640533)# of trapezoids
h Integral
1 .8 0.1728 2 .4 1.0688 4 .2 1.4848
€
ˆ I 1 =43
(1.0688) − 13
(0.1728) =1.367467
ˆ I 2 =43
(1.4848) − 13
(1.0688) =1.623467
Romberg Interpolation
• Can repeat:
€
Fh
Fh / 2
Fh / 4
Fh / 8O(h2)k =1
O(h4 )k = 2
O(h6)k = 3
O(h8)k = 4
€
I j,k ≈4 k−1I j+1,k−1 − I j ,k−1
4 k−1 −1
Richardson Extrapolation
• This treats the approximation as a function of h and “extrapolates” the result to h=0
• (When using trapezoid and halving intervals, this is Romberg Interpolation)
Gaussian Quadrature
Gaussian Quadrature
• When free to choose node locations, yields degree 2n-1 for n nodes!
• Apply n-point G.Q. nodes from “rules”
Example: 2-point Gaussian Quadrature
• Integration equation:
• When x1, x2 are fixed, use moment equations to derive polynomial of degree 1:
Example: 2-point Gaussian Quadrature
Solution:
• Resulting polynomial is of degree 3! • Tables exist with weights and nodes for GQ
of degree n, integrated over [-1,1]
• In practice, transform function to rescale x in [a, b] to [-1, 1]
Other Techniques
Limits at Infinity
• Usual trick: change of variables
• Works with a, b same sign, one of them infinite – Otherwise, split into multiple pieces
• Also requires f to decrease faster than 1/x2 – Else need different change of variables, if
possible!
€
f (x)dxa
b
∫ = 1t 2 f 1t( )dt1/ b
1/ a
∫
Example: Standard normal distribution
€
=12π−∞
z∫ e−x 2 / 2dx
Discontinuities
• All the above error analyses assumed nice (continuous, differentiable) functions
• In the presence of a discontinuity, all methods revert to accuracy proportional to h – In general, if the k-th order derivative is discontinuous,
can do no better than O(hk+1)
Discussion
• Newton-Cotes: – Simple, often effective – Not highest degree possible for given number of nodes – Progressive, can be used for adaptive
• Gauss: – Not for tabular data, requires linear change of function, best
degree for given number of nodes, can get very high accuracy
• Romberg: – Richardson based on trapezoid; not for tabular data, can get
very high accuracy
Matlab functions
• trapz: trapezoidal with consistent spacing • polyint: integrate polynomial analytically • quad: Adaptive Simpson quadrature • quadgk: Adaptive Gauss-Kronrod • Also: quadl, cumtrapz, dblquad, triplequad