Numerical Integration
Total Page:16
File Type:pdf, Size:1020Kb
Numerical Integration ChEn 2450 b Given f(x), we want to calculate the integral over some region [a,b]. f(x)dx a Concept: Approximate f(x) locally as a polynomial. 1 Integration.key - October 31, 2014 Midpoint Rule Trapezoid Rule Concept: Approximate f(x) as a Concept: Approximate f(x) as a constant on the interval [a,b]. linear function on the interval [a,b]. b b + a b Triangle area. Rectangle area. f(x)dx (b a) f b a f(x)dx −2 (f(b) f(a)) + f(a) (b a) a ⇥ − 2 ⇥ − − ⇤ ⇥ a b a − [f(a) + f(b)] ⇥ 2 • Requires function • Convenient form for tabular (discrete) data. f(x) value at the midpoint f(x) (can be a problem for • Doesn’t require tabular/discrete data). equally spaced data. a b • ∆x = b-a x a x b Simpson’s 1/3 Rule Simpson’s 3/8 Rule Concept: Approximate f(x) as a Concept: Approximate f(x) as a quadratic on the interval [a,b]. cubic on the interval [a,b]. b b ∆x a+b 3∆x f(x)dx f(a) + 4f + f(b) f(x)dx (f(x0) + 3f(x1) + 3f(x2) + f(x3)) ≈ 3 2 ≈ 8 ⇧a a ⇤ ⇥ ⌅ • Requires four equally spaced • Requires three equally spaced points on interval [a,b]. f(x) points on interval [a,b]. f(x) • ∆x = (b-a)/3 • ∆x = (b-a)/2 • xi = a + i∆x a b a b x x 2 Integration.key - October 31, 2014 Example: Discrete Data Integration CO2 Emissions in US from How much CO2 was emitted from electrical electrical power generation. power generation nation-wide from 1990-2006? | Metric Tons CO2! b b a Year | Emitted in US! Trapezoid rule: f(x)dx − [f(a) + f(b)] ≈ 2 -------+-------------! a 1990 | 1.93125e+09! 1990-1991: 1991 | 1.92804e+09! 1991 1990 1992 | 1.94959e+09! − 1.92804 109 + 1.93125 109 = 1.9296 109 2 ⇥ ⇥ ⇥ 1993 | 2.03314e+09! 9 x 10 1994 | 2.06390e+09! 3 ⇥ 1995 | 2.08351e+09! 1996 | 2.16126e+09! 2.5 1997 | 2.23271e+09! 1998 | 2.32414e+09! 2 1999 | 2.33866e+09! 2000 | 2.44172e+09! 1.5 10 2001 | 2.38975e+09! Total 1990-2006: 3.5923×10 2002 | 2.39505e+09! 1 36 Gigatons 2003 | 2.41568e+09! Metric Tons CO2 Emitted 2004 | 2.45693e+09! 0.5 2005 | 2.51361e+09! 2006 | 2.45980e+09 0 1990 1995 2000 2005 Data from U.S. Department of Energy. Year 3 Integration.key - October 31, 2014 Example: Discrete Data Integration b For a pure gas, the “fugacity,” f, b a Trapezoid rule: f(x)dx − [f(a) + f(b)] may be determined as: ≈ 2 a f p z 1 f 800 z 1 ln = − dp ln = − dp p 0 p 800 p ⇥ ⇤ ⇥ ⌅0 8 pi+1 pi zi+1 1 zi 1 Data for N2 at 25 C: − − + − ⇥ 2 p p i=1 i+1 i p (atm) | z (-)! ⇤ ⇥ 4 x 10 10 ---------+---------! f 10 10−3 0.998 1 1.000 1 ln = − − + − 800 2 10 0.001 0.001 | 1.000! ⇥ ⇥ 8 50 10 0.996 1 0.998 1 + − − + − 10.000 | 0.998! 2 50 10 ⇥ 6 100 50 1.004 1 0.996 1 50.000 | 0.996! + − − + − 2 100 50 4 ⇥ 100.000 | 1.004! 1)/p 200 100 1.057 1 1.004 1 + − − + − (z 2 200 100 2 200.000 | 1.057! ⇥ 300 200 1.146 1 1.057 1 + − − + − 0 300.000 | 1.146! 2 300 200 ⇥ 400 300 1.254 1 1.146 1 400.000 | 1.254! + − − + − 2 2 400 300 ⇥ 600.000 | 1.495! 600 400 1.495 1 1.254 1 4 + − − − 0 200 400 600 800 2 600 400 p 800.000 | 1.723 ⇥ 800 600 1.723 1 1.495 1 + − − − 2 800 600 f = 1220 atm. Determine f at 800 bar. ⇥ Could we apply either Simpson’s rule here? Why/why not? 4 Integration.key - October 31, 2014 Quadrature & Composite Rules (for continuous functions) Concept: Apply a quadrature rule over many subintervals in [a,b]. f(x) f(x) We can add more subintervals to better approximate the integral. a b x a x b b I = f(x)dx Za Trapezoid x2 x1 x3 x2 xn xn 1 rule over n − (f(x2)+f(x1)) + − (f(x3)+f(x2)) + + − − (f(xn)+f(xn 1)) ⇥ 2 2 ··· 2 − intervals. 1 1 If ∆x is constant, I ∆x f(x1)+f(x2)+f(x3)+ + f(xn 1)+ f(xn) ≈ 2 ··· − 2 n 1 f(x ) f(x ) − = ∆x 1 + n + f(x ) Composite 2 2 i " i=2 # Trapezoid rule X ∆x I f(x1) + 4f(x2) + f(x3) + f(x3) + 4f(x4) + f(x5) + + f(xn 2 + 4f(xn 1) + f(xn) Composite ⇥ 3 · · · − − ⇥ ∆x Simpson = [f(x1) + 4f(x2) + 2f(x3) + 4f(x4) + 2f(x5) + + 2f(xn 2) + 4f(xn 1) + f(xn)] 1/3 rule 3 ⇧ ⌅⇤ ⌃ ⇧ ⌅⇤ · · ·⌃ −⇧ −⌅⇤ ⌃ NOTE: could simply use the basic rule on each interval - no real need to use composite rules other than efficiency... 5 Integration.key - October 31, 2014 Miscellaneous Numeric Integration in Adaptive Quadrature MATLAB: Concept: only refine only intervals where needed (not everywhere). q=quad(fname,a,b,tol) One approach: • Numerically evaluate the function on the interval [a,b] using the tolerance tol. 1. Evaluate f(x) using n intervals on [a,b]. • Uses an integrator based on adaptive 2. Evaluate f(x) using 2n intervals on [a,b]. Simpson’s quadrature. 3. Determine which intervals resulted in a q=trapz(x,y) change in the local integral approximation • Integrate discrete data numerically using larger than the tolerance and refine them. the trapezoid method. 4. Continue refining until tolerance is met. 6 Integration.key - October 31, 2014.