Numerical Integration
Total Page:16
File Type:pdf, Size:1020Kb
Numerical Integration Thomas Hahn Max-Planck-Institut für Physik München T. Hahn, Numerical Integration – p.1 Restrictions Integration is a wide field. We will concentrate here on Riemann integrals of the form 1 I f := ddx f (~x ) w(~x ) . Z0 The Weight Function w(~x ) is generally known analytically and is used to absorb characteristics of the integrand which are difficult to treat otherwise, e.g. peaks or oscillatory behaviour. For the purposes of numerical integration, we assume that f (~x ) is given as a function/subroutine that can be sampled at arbitrary points ~x [0, 1]d. i ∈ T. Hahn, Numerical Integration – p.2 Quadrature Formulas Task: Find a Quadrature Formula n Qn f := ∑ wi f (~xi) i=1 with Nodes (sampling points) ~xi and Weights wi. Qn f should approximate I f for a large class of functions with as small an Error En f as possible: I f = Qn f + En f , En f “small.” But: For a given Qn, it is always possible to construct an f such that En f becomes arbitrarily large! T. Hahn, Numerical Integration – p.3 Terminology A Quadrature Rule is the vector of nodes and weights, ~x1 ~xn Rn = ,..., ( w1! wn!) and of course completely specifies Qn. Open Rule = all ~x lie strictly inside the unit hypercube. • i Closed Rule = some ~x lie on the boundary. • i Positive Rule = all wi are positive. • (Weights with alternating sign are generally considered inferior because numerical cancellations may occur when adding up.) T. Hahn, Numerical Integration – p.4 Interpolatory Formulas (1 D) (n 1) Idea: Approximate f by a polynomial p − and integrate the latter. Works as far as f is well approximated by polynomials. (n 1) We impose that p − interpolates f at n given points xi: (n 1) ! p − (xi) = f (xi) , i = 1,..., n . The polynomial thus specified is unique and can explicitly be given in terms of Lagrange Polynomials `n 1,i: − n x x (n 1) = (n 1) (n 1) = j p − (x) ∑ f (xi)`i − (x) , where `i − (x) ∏ − . i=1 j=i xi xj 6 − (n 1) = By construction, `i − (xj) δi j . T. Hahn, Numerical Integration – p.5 Interpolatory Formulas (1 D) Weights: 1 (n 1) (n 1) I p − = dx w(x) p − (x) Z0 n 1 = (n 1) ∑ f (xi) dx w(x) `i − (x) . i=1 Z0 wi From the practical point of view,| the weight{z function} w(x) must be chosen such that these integrals can be computed. The Degree of Qn is the degree of the highest polynomial integrated exactly by Qn. T. Hahn, Numerical Integration – p.6 Newton–Cotes (Rectangle) Rules (1 D) The simplest case: take equidistant xi. For w(x) = 1 the lowest-order rules are: = i Closed rules: = i−1 , e.g. Open rules: xi n+1 , e.g. xi n−1 Q = 1 , Q = 1 + 1 f f ( 2 ) 2 f 2 f (0) f (1) , Q = 1 1 + 2 , Q = 1 + 1 + 2 f 2 f ( 3 ) f ( 3 ) 3 f 6 f (0) 4 f ( 2) f (1) , Q f = 1 2 f ( 1 ) f ( 1 ) + 2 f ( 3 ) . Q f = 1 f (0) + 3 f ( 1 ) + 3 f ( 2 ) + f (1) . 3 3 4 − 2 4 4 8 3 3 By construction, deg Q > n 1, but cannot generally be n − expected to be larger than n 1 because the x are prescribed − i and only the n wi have been determined. The Newton–Cotes rules by themselves are not nearly as powerful as the Gauss rules, but they have the advantage that they can be compounded easily, e.g. in Romberg Rules. T. Hahn, Numerical Integration – p.7 Gauss Rules (1 D) Choose the xi such that Qn has the highest possible degree. With 2n degrees of freedom (n xi and n wi), we ought to achieve deg Q = 2n 1. n − By Euclid’s GCD algorithm we write n (2n 1) (n) (n 1) (n 1) (n) p − (x) = q (x) r − (x) + s − (x) , q (x) = ∏(x xi) i=1 − 1 1 (2n 1) (n) (n 1) (n 1) I p − = dx w(x) q (x) r − (x) + dx w(x) s − (x) Z0 Z0 n n (n) (n 1) (n 1) (2n 1) = ∑ wi q (xi) r − (xi) + ∑ wi s − (xi) + En p − i=1 i=1 =0 If the boxed term were zero, the error term would vanish! | {z } T. Hahn, Numerical Integration – p.8 Gauss Rules (1 D) 1 (n) (n 1) dx w(x) q (x) r − (x) = Z0 (n) (n 1) (n) (n 1) q r − = 0 q r − h | i ⇔ ⊥ Choose Orthogonal Polynomials for the q(n), then n 1 (n 1) − (i) (n 1) (i) (n) (n 1) r − = ∑ q r − q and q r − = 0 i= h | i h | i 0 because q(n) q(i<n) = 0 . h | i (n) n Since q (x) = ∏ = (x x ), the x are just the zeros of the i 1 − i i orthogonal polynomials q(n) ! T. Hahn, Numerical Integration – p.9 Gauss Rules (1 D) Gauss rules for particular weight functions have special names, hinting at the orthogonal polynomials used: w(x) = 1 Gauss–Legendre Rules w(x) = 1/√1 x2 Gauss–Chebychev Rules − α x w(x) = x e− Gauss–Laguerre Rules x2 w(x) = e− Gauss–Hermite Rules w(x) = (1 x)α(1 + x)β Gauss–Jacobi Rules − T. Hahn, Numerical Integration – p.10 Error Estimate and Embedded Rules (1D) So far we have no error estimate for our integration rule. Idea: Compare the results of two rules, Qn and Qn+m. Use Embedded Rules with x n x n+m for economy. { i}i=1 ⊂{ i}i=1 But: e.g. Gauss rules of different n have no common xi. Seek to add new points xn+1,..., xn+m such that Qn+m is exact for polynomial of maximum degree expectable from #df, (n+2m 1) p − . This leads to 1 (n) (m) (m 1) dx w(x) v (x) q (x) r − (x) = 0 Z0 (n) n (m) m with v (x) = ∏ = (x x ) , q (x) = ∏ = (x x + ) . i 1 − i i 1 − n i Not all w(x) have solutions since a scalar product with weight function w(x) v(n)(x) cannot in general be defined. T. Hahn, Numerical Integration – p.11 Embedded Rules and Null Rules Some common Embedded Rules in d = 1 are: Gauss–Kronrod Rules add one point between every two • points of a Gauss rule R so that R R + + , e.g. n n ⊂ n n 1 0 1 Patterson Rules add points on top of the Gauss–Kronrod • rules, e.g. there exists a set of rules R R R R R R R R R . 1 ⊂ 3 ⊂ 5 ⊂ 7 ⊂ 15 ⊂ 31 ⊂ 63 ⊂ 127 ⊂ 255 A Null Rule Nm is associated with an integration rule Qn (usually m < n) and is engineered to give zero for all functions that are integrated exactly by Qn. Nm measures the “higher terms” of the integrand that are not exactly integrated by Qn and is also used for error estimation. T. Hahn, Numerical Integration – p.12 Adaptive Algorithms With an error estimate available, adaptiveness can easily be implemented: 1. Integrate the entire region: I E . tot ± tot 2. while Etot > max(εrel Itot, εabs) 3. Find the region r with the largest error. 4. Bisect (or otherwise cut up) r. 5. Integrate each subregion of r separately. = ∑ = ∑ 2 6. Itot Ii, Etot Ei . 7. end while q Examples: QUADPACK’s QAG, CUBA’s Cuhre, Suave. T. Hahn, Numerical Integration – p.13 Extrapolation Extrapolation can be used to accelerate convergence if the functional behaviour of the error term is known. Example: for the Newton–Cotes formulas the error can be shown to vary with the spacing h of the nodes as 1 Q f I f = c h2 + c h4 + ..., h = . n − 2 4 n Idea: Compute Qn f for different h and extrapolate to h = 0. Use Richardson’s Extrapolation to eliminate k powers of h: T0 k k 1 k 1 1 & 4 T − T 0 1 k m+1 m− 0 T2 → T1 T := − , T := Q m−1 f . & & m k m 2 0 1 2 4 1 T3 → T2 → T1 . & & − . .. These are known as Romberg Formulas. T. Hahn, Numerical Integration – p.14 Curse of Dimension Imagine computing the volume of the d-dim. sphere Sd by integrating its characteristic func- χ = 1 tion χ = θ(1 x 2) inside the surrounding − k k d hypercube Cd = [ 1, 1] . − χ = 0 The following table gives the ratio of the volumes: d 2 5 10 50 100 Vol Sd 28 70 .785 .164 .0025 1.5 10− 1.9 10− Vol Cd × × This ratio can in a sense be thought of as the chance that a general-purpose integrator will find the sphere at all! T. Hahn, Numerical Integration – p.15 Product Formulas Easiest method: Iterate one-dimensional rules, e.g. 1 nx ny nz 3 = (x) (y) (z) (x) (y) (z) d x f (~x ) ∑ ∑ ∑ wi wj wk f (xi , xj , xk ) . Z0 i=1 j=1 k=1 d d But: the number of samples increases as N = ∏ = n n . i 1 i ∼ Consider e.g. the Newton–Cotes rules, where the error term is (h2) = (n 2). Convergence is thus: O O − 2/d Q f I f = (N− ) . n − O Even for moderate dimensions (say d & 5), this convergence rate is usually too slow to be useful. T. Hahn, Numerical Integration – p.16 Construction of Polynomial Rules Select orthogonal basis of functions b ,..., b (usually { 1 m} monomials) with which most f can (hopefully) be approximated sufficiently and impose that each bi be integrated exactly by Qn: n 1 ! I b = Q b ∑ w b (~x ) = ddx w(~x ) b (~x ) .