Integration COS 323 Numerical Integration Problems • Basic 1D numerical integration: – Given ability to evaluate f (x) for any x, find – Goal: best accuracy with fewest samples • Other problems (future lectures): – – – – Improper integration Multi-dimensional integration Ordinary differential equations Partial differential equations b f ( x) dx a Trapezoidal Rule • Approximate function by trapezoid f(b) f(a) a b Trapezoidal Rule f(b) b a f (a) f (b) f ( x) dx (b a) 2 f(a) a b Extended Trapezoidal Rule f(b) b a f (a) f (b) f ( x) dx (b a) 2 f(a) a b a b Divide into segments of width h: b a f ( x) dx h 12 f (a) f ( x1 ) f ( xn 1 ) 12 f (b) Trapezoidal Rule Error Analysis • How accurate is this approximation? b a (b a) f (a) f (b) E f ( x) dx 2 • Start with Taylor series for f (x) around a f ( x) f (a) ( x a) f (a) 12 ( x a)2 f (a) Trapezoidal Rule Error Analysis • Expand LHS: b f ( x) dx (b a) f (a) 12 (b a) 2 f (a) 16 (b a)3 f (a) a • Expand RHS (b a) f (a) f (b) E 12 (b a) f (a) 2 2 3 1 1 1 ( b a ) f ( a ) ( b a ) f ( a ) ( b a ) f (a) E 2 2 4 Trapezoidal Rule Error Analysis • So, E 121 (b a)3 f (a) • In general, error for a single segment proportional to h3 • Error for subdividing entire ab interval proportional to h2 – “Cubic local accuracy, quadratic global accuracy” Determining Step Size • Change in integral when reducing step size is a reasonable guess for accuracy • For trapezoidal rule, easy to go from h h/2 without wasting previous samples a b Simpson’s Rule • Approximate integral by parabola through three points f(b) f(a) a b a h f ( x) dx f (a) 4 f (a h) f (b) O(h5 ) 3 • Better accuracy for same # of evaluations b Richardson Extrapolation • Better way of getting higher accuracy for a given # of samples • Suppose we’ve evaluated integral for step size h and step size h/2: Fh F h 2 h 4 Fh / 2 F h2 h2 2 4 • Then 4 3 Fh / 2 13 Fh F O(h 4 ) Richardson Extrapolation • This treats the approximation as a function of h and “extrapolates” the result to h=0 • Can repeat: Fh –1/3 –1/15 4/3 Fh / 2 16/15 –1/63 64/63 Fh / 4 Fh / 8 O(h 2 ) O(h 4 ) O(h6 ) O ( h8 ) Open Methods • Trapezoidal rule won’t work if function undefined at one of the points where evaluating – Most often: function infinite at one endpoint 1 dx 0 x 2 • Open methods only evaluate function on the open interval (i.e., not at endpoints) Midpoint Rule • Approximate function by rectangle evaluated at midpoint f ( a 2 b ) a b Extended Midpoint Rule b f ( x) dx (b a) f ( a 2 b ) a a b a b Divide into segments of width h: b a f ( x) dx h f (a h2 ) f (a 32h ) f (b h2 ) Midpoint Rule Error Analysis • Following similar analysis to trapezoidal rule, find that local accuracy is cubic, quadratic global accuracy • Formula suitable for adaptive method, Richardson extrapolation, but can’t halve intervals without wasting samples 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 • Locally-adaptive methods: do not subdivide all intervals equally, focus on those with large error (estimated from change with a single subdivision)