Process Model Formulation and Solution, 3E4
Section E, Part B: Numerical IntegrationInstructor: Kevin Dunn [email protected]
Department of Chemical Engineering
Course notes: © Dr. Benoˆıt Chachuat
Why integral calculation?
Example 1
Calculate theheight of liquid after a given time:
z(t) =
Example 2
Calculate the (constant) water flowin a river/canal:
Numerical calculation of integrals:
quadrature
Problem statement:
Given a continuous function f : [a, b] → IR, calculate itsintegralon [a, b],
I = Z b
a
f (x ) dx
usingfunction evaluations only!
General approach:
Approximate: I = Z b
a
f (x ) dx
by the weighted sum: In= n
X
i =1
ωif (xi)
withnodal abscissas xi andweights ωi
Numerical calculation of integrals:
quadrature
Problem statement:
Given a continuous function f : [a, b] → IR, calculate itsintegralon [a, b],
I = Z b
a
f (x ) dx
usingfunction evaluations only! Two possible scenarios
1. Empirical function: Freeto choose the points at which to evaluate the function f
2. Experimental data: Function values available atdiscrete(fixed) data points only!
Outline and recommended readings
Newton-Cotes formulas The trapezoidal rule Simpson’s rules Romberg integration Gauss quadrature
2-point Gauss quadrature Higher-order Gauss quadrature
(Strongly) recommended readings:
I Chapters21.1-21.3and22.1-22.3in: S. C. Chapra, and R. P. Canale, “Numerical
Methods for Engineers”, McGraw Hill, 5th/6th edition
Newton-Cotes
formulas: principles
I Most commonnumerical integration schemes
I Requireequally spacedabscissas – Most useful if:
I f (x ) already available at equally spaced data points x1, ..., xn; or
I f (x ) can be computed at low cost
Main concept:
Approximate the integrand by a polynomial function, pn(x ) = a0+ a1x + · · · + anxn, that is easy to integrate:
I = Z b a f (x ) dx ≈ In= Z b a pn(x ) dx
The
trapezoidal rule: basics
I First Newton-Cotes formula:
I ≈ I1=
Z b
a
p1(x ) dx
with: p1(x ) = a0+ a1x
I Consider the twonodal abscissas: x1= aandx2= b
p1(x1) = f (x1) p1(x2) = f (x2) =⇒ p1(x ) = I Trapezoidal rule: I1= Z b a p1(x ) dx = I Interpretation?
The trapezoidal rule:
application
Use the trapezoidal rule to estimate: I = Z π2 0 sin(x ) dx I I = I I1= I E1= |I − I1| =
The
composite
trapezoidal rule
Concept: Apply the trapezoidal rule in apiecewise fashion
I Divide [a, b] into n panels:
h = b − a n , I = n X k=1 Ik
I For each panel k = 1, . . . , n:
Ik = Z xk+1 xk f (x ) dx ≈ I1k = hf (xk) + f (xk+1) 2 I Overall, I ≈ I1(n)= h 2 n X k=1 [f (xk) + f (xk+1)] = h 2[f (x1) + 2f (x2) + · · · + 2f (xn) + f (xn+1)]
The composite trapezoidal rule:
error
analysis
Error analysis: I − I1(n)= −h 2 12(b − a)f 00(ξ) = −(b − a)3 12n2 f00(ξ), for some ξ ∈ [a, b]
I The error E1(n)= |I − I1(n)| can be decreased by increasing n
I At the limit: lim
n→∞E (n) 1 = 0
How fast does the error go to zero?
E1(n)= |I − I1(n)| =O(h2)
The composite trapezoidal rule:
application
Use thecompositetrapezoidal rule to estimate: I = Z π2 0 sin(x ) dx I I = I I1(2)= I E1(2)= |I − I1(2)| = n I1(n) E1(n) 1 7.8540e-01 2.1460e-01 2 9.4806e-01 5.1941e-02 4 9.8712e-01 1.2884e-02 8 9.9679e-01 3.2148e-03 16 9.9920e-01 8.0332e-04 32 9.9980e-01 2.0081e-04 64 9.9995e-01 5.0200e-05 128 9.9999e-01 1.2550e-05
I Large number of panels required for high accuracy
The composite trapezoidal rule:
coding
Write a function that estimates the integral I =Rb
a f (x )dx using the
Simpson’s rules: basics
I Concept: Develop Newton-Cotes formulas ofhigher order
I How? Use more points!
n = 3: Simpson’s 1/3 rule I ≈ I2= Z b a p2(x ) dx n = 4: Simpson’s 3/8 rule I ≈ I3= Z b a p3(x ) dx
Simpson’s 1/3 rule:
derivation
p2(x ),uniquequadratic polynomial
such that: p2(x1) = f (a) p2(x2) = f a + b 2 p2(x3) = f (b)
Use of Lagrange interpolating polynomials:
I ≈ I2=
h
Simpson’s 1/3 rule:
application
Use Simpson’s 1/3 rule to estimate: I = Z π2 0 sin(x ) dx I I = I I2≈ I E2= |I − I2| ≈ 2.280 × 10−3
Simpson’s 3/8 rule:
derivation
p3(x ),uniquecubic polynomial s.t.
p3(x1) = f (a) p3(x2) = f 2a + b 3 p3(x3) = f a + 2b 3 p3(x4) = f (b)
Use of Lagrange interpolating polynomials:
I ≈ I3=
3h
Simpson’s 3/8 rule:
application
Use Simpson’s 3/8 rule to estimate: I = Z π2 0 sin(x ) dx I I = I I3≈ I E3= |I − I3| ≈ 1.005 × 10−3
Composite
Simpson’s 1/3 rule
Principle: Apply Simpson’s 1/3 rule in apiecewise fashionI Divide [a, b] into n panels, withn even: h = b − a n , I = n/2 X k=1 Ik
I For each pair of panels k = 1, 2, . . . , n/2:
Ik = Z x2k+1 x2k−1 f (x ) dx ≈ I2k = h 3[f (x2k−1) + 4f (x2k) + f (x2k+1)] I Overall, h n/2
Composite
Simpson’s 1/3 rule (cont’d)
Error analysis: I − I2(n)= − h 4 180(b − a)f (4)(ξ) = −(b − a)5 180n4 f(4)(ξ), for some xi ∈ [a, b]
I Simpson’s 1/3 ruleexactfor a polynomial of degree 3 or less Convergence speed (with decreasing h):
E2(n)= |I − I2(n)| =O(h4)
I Halving the stepsize, divides the error by sixteen
I One order higher than expected! Case of an odd number of panels:
I Use Simpson’s 1/3 rule for all but last 3 panels
I Use Simpson’s 3/8 rule for last 3 panels
I ≈h 3 n−3 2 X k=1 [f (x2k−1) + 4f (x2k) + f (x2k+1)] + 3h 8 [f (xn−2) + 3f (xn−1) + 3f (xn) + f (xn+1)]
Composite Simpson’s 1/3 rule:
application
Use thecompositeSimpson’s 1/3 rule to estimate: I = Z π2 0 sin(x ) dx I I = I I2(2)= I E2(2)= |I − I2(2)| = n I1(n) E1(n) 2 1.00227988 2.2799e-03 4 1.00013458 1.3458e-04 8 1.00000830 8.2955e-06 16 1.00000052 5.1668e-07 32 1.00000003 3.2265e-08
I Much smaller number of panels required for high accuracy (compared to composite trapezoidal rule)
Refresher:
Richardson extrapolation
Principle:
Combine two estimatesof a given order, O(hp), in order to obtain a new
estimate of higher-order, O(hp+1)
I Suppose we want to calculate some quantity Q
I Let Qpbe an estimate of Q of order p,
Q = Qp(h) + O(hp)
I Then, an estimate Qp+1 of order p + 1 is given by: Why?
Q = Qp+1(h) + O(hp+1) =
2pQ
p(12h) − Qp(h)
2p+ 1 + O(h p+1)
I Successfully applied to numerical differentiation
Romberg
integration: principles
Principle:
ApplyRichardson extrapolationto numerical integration, starting with thecomposite trapezoidal rule
Starting point: −→ O(h2)
I Composite trapezoidal rule: n panels, h = b−a n I1(h) = h 2 n X k=1 [f (xk) + f (xk+1)] = h 2[f (x1) + 2f (x2) + · · · + 2f (xn) + f (xn+1)] I Truncation error: I − I1(h) = O(h2) = α2h2+ α4h4+ · · · + α2kh2k+ · · ·
Romberg
integration: principles
1st extrapolation: −→ O(h4)
I Combine I1(h) and I1(12h) to eliminate theterm in h2, How?
I = 4I1( 1 2h) − I1(h) 3 | {z } I2(12h) +α04h4+ O(h6)
I Link with the composite Simpson’s 1/3 rule? 2nd extrapolation: −→ O(h6)
I Combine I2(12h) and I2(14h) to eliminate theterm in h4, How?
I = 4 2I 2(14h) − I2(12h) 42− 1 | {z } I3(14h) +α006h6+ O(h8)
Romberg integration:
algorithm
Recursive formula:I
j +1(
21kh) =
4
jI
j(
21kh) − I
j(
1 2k−1h)
4
j− 1
,
j ≥ 1, k ≥ 1
O(h2) O(h4) O(h6)
I1(h) & I1(12h) → I2(12h) = 4I1(12h) − I1(h) 4 − 1 & & 1 1 4I1( 1 22h) − I1(12h) 1 42I2(212h) − I2(12h)
Romberg integration:
application
Use Romberg integration to calculate an O(h8) estimate of: I =
Z π2
0
sin(x ) dx , starting with h = π2 (1 panel)
h I1, O(h2) I2, O(h4) I3, O(h6) I4, O(h8)
π 2 0.7853981633 π 4 0.9480594489 1.0022798774 π 8 0.9871158009 1.0001345849 0.9999915654 π 16 0.9967851718 1.0000082955 0.9999998762 1.0000000081
E 3.2148e-03 8.2955e-06 1.2377e-07 8.1440e-09
I I2(π4) =
I How manyfunction evaluationsare required to get an accuracy of ca. 10−8 for I ?
Romberg integration:
coding
Write a function that estimates the integral I =Rb
a f (x )dx using
Romberg integration; make use of the trapezoidal function (see earlier slide)
Gauss quadrature: principles
Approximate: I = Z b
a
f (x ) dx by the weighted sum: In= n X k=1 ωif (xi) So far, I Newton-Cotes formulas I Romberg integration
The nodal abscissas xi areevenly
spacedin [a, b]
Only the weights ωi are “optimized”
Gauss Quadrature:
Choose the nodal abscissas and the weightssimultaneously in order to achieve maximal accuracy!
2-point
Gauss quadrature
Find the coefficients ω1, ω2, x1, x2such that:
I = Z b
a
2-point
Gauss quadrature
Start with the interval [−1, 1]: I =Z 1
−1
f (x ) dx 0. Condition for aconstant term: f (x ) = 1
Z 1 −1 1 dx = n X k=1 ωi ⇒
1. Condition for alinear term: f (x ) = x Z 1 −1 x dx = n X k=1 ωixi ⇒
2. Condition for aquadratic term: f (x ) = x2
Z 1 −1 x2dx = n X k=1 ωixi2 ⇒
3. Condition for acubic term: f (x ) = x3 Z 1 −1 x3dx = n X k=1 ωixi3 ⇒
2-point
Gauss quadrature (cont’d)
Solution to the nonlinear algebraic equations: Check! ω1= ω2= 1, x1= − 1 √ 3, x2= 1 √ 3
2-point Gauss quadrature on [−1, 1]:
I = Z 1 −1 f (x ) dx ≈ I2= f −√1 3 + f√1 3
2-point Gauss quadrature on a general interval[a, b]: Why?
2-point Gauss quadrature:
application
Use2-point Gauss quadratureto estimate: I = Z π2 0 sin(x ) dx Nodal abscissas: I x1= I x2= Function evaluations: I f (x1) = I f (x2) =
2-point Gauss quadrature estimate:
I = Z π2
0
sin(x ) dx ≈ I2=
E2= |I − I2| ≈ 1.527 × 10−3
I Error of the same magnitude as with Simpson’s 1/3 and 3/8 rule
Higher-order
Gauss quadrature
The good news: exact same procedure as with 2 points!
Find the coefficients ω1, . . . , ωn, x1, . . . , xn such that:
I = Z b a f (x ) dx ≈ n X k=1 ωkf (xk) 2n conditions required:
I Now, require the formula to beexactfor termsup to order 2n
Coefficients for Gauss-quadrature on [−1, 1]
n = 3 ±xk ωk 0.000 000 0.888 889 n = 4 ±xk ωk 0.339 981 0.652 145 n = 5 ±xk ωk 0.000 000 0.568 889
3-point Gauss quadrature:
application
Use3-point Gauss quadratureto estimate: I = Z π2 0 sin(x ) dx Nodal abscissas: I x1= I x2= I x3= Function evaluations: I f (x1) = I f (x2) = I f (x3) =
3-point Gauss quadrature estimate:
I = Z π2
0
sin(x ) dx ≈ I3=