Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss...

33
Process Model Formulation and Solution, 3E4 Section E, Part B: Numerical Integration Instructor: Kevin Dunn [email protected] Department of Chemical Engineering Course notes: Dr. Benoˆ ıt Chachuat 08 November 2010 1

Transcript of Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss...

Page 1: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Process Model Formulation and Solution, 3E4Section E, Part B: Numerical Integration

Instructor: Kevin Dunn [email protected]

Department of Chemical Engineering

Course notes: © Dr. Benoıt Chachuat08 November 2010

1

Page 2: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Why integral calculation?

Example 1

Calculate the height of liquidafter a given time:

z(t) =

Example 2

Calculate the (constant)water flow in a river/canal:

F =

2

Page 3: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Numerical calculation of integrals: quadrature

Problem statement:

Given a continuous function f : [a, b] → IR, calculate its integral on [a, b],

I =

∫ b

a

f (x) dx

using function evaluations only!

General approach:

Approximate: I =

∫ b

a

f (x) dx

by the weighted sum: In =n∑

i=1

ωi f (xi )

with nodal abscissas xi and weights ωi

I Rules of quadrature differ in the choice of the xi ’s and/or ωi ’s

I All rules are derived from polynomial interpolation of the integrand f 3

Page 4: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Numerical calculation of integrals: quadrature

Problem statement:

Given a continuous function f : [a, b] → IR, calculate its integral on [a, b],

I =

∫ b

a

f (x) dx

using function evaluations only!

Two possible scenarios

1. Empirical function: Free to choose the points at which to evaluatethe function f

2. Experimental data: Function values available at discrete (fixed)data points only!

4

Page 5: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Outline and recommended readings

Newton-Cotes formulasThe trapezoidal ruleSimpson’s rules

Romberg integration

Gauss quadrature2-point Gauss quadratureHigher-order Gauss quadrature

(Strongly) recommended readings:

I Chapters 21.1-21.3 and 22.1-22.3 in: S. C.Chapra, and R. P. Canale, “NumericalMethods for Engineers”, McGraw Hill, 5th/6thedition

5

Page 6: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Newton-Cotes formulas: principles

I Most common numerical integration schemesI Require equally spaced abscissas – Most useful if:

I f (x) already available at equally spaced data points x1, ..., xn; orI f (x) can be computed at low cost

Main concept:

Approximate the integrand by a polynomial function,pn(x) = a0 + a1x + · · ·+ anx

n, that is easy to integrate:

I =

∫ b

a

f (x) dx ≈ In =

∫ b

a

pn(x) dx

6

Page 7: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

The trapezoidal rule: basics

I First Newton-Cotes formula:

I ≈ I1 =

∫ b

a

p1(x) dx

with: p1(x) = a0 + a1x

I Consider the two nodal abscissas: x1 = a and x2 = b{p1(x1) = f (x1)p1(x2) = f (x2)

=⇒ p1(x) =

I Trapezoidal rule:

I1 =

∫ b

a

p1(x) dx =

I Interpretation?I Link to the general formula: I ≈

Pωi f (xi )?

7

Page 8: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

The trapezoidal rule: application

Use the trapezoidal rule to estimate: I =

∫ π2

0

sin(x) dx

I I =

I I1 =

I E1 = |I − I1| =

8

Page 9: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

The composite trapezoidal rule

Concept: Apply the trapezoidalrule in a piecewise fashion

I Divide [a, b] into n panels(evenly-spaced!):

h =b − a

n, I =

n∑k=1

I k

I For each panel k = 1, . . . , n:

I k =

∫ xk+1

xk

f (x) dx ≈ I k1 =

h

2

(f (xk) + f (xk+1)

)I Overall,

I ≈ I(n)1 =

h

2

nXk=1

[f (xk) + f (xk+1)] =h

2

»f (x1)+2f (x2)+ · · ·+2f (xn)+ f (xn+1)

9

Page 10: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

The composite trapezoidal rule: error analysis

Error analysis:

I − I(n)1 = −h2

12(b − a)f ′′(ξ) = − (b − a)3

12n2f ′′(ξ), for some ξ ∈ [a, b]

I The error E(n)1 = |I − I

(n)1 | can be decreased by increasing n

I At the limit: limn→∞

E(n)1 = 0

How fast does the error go to zero?

E(n)1 = |I − I

(n)1 | = O(h2)

I Halving the stepsize, quarters the error

10

Page 11: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

The composite trapezoidal rule: application

Use the composite trapezoidal rule to estimate:

I =

∫ 1.0

10.0

− 1

0.5CAdCA =

Vtotal

F

This was for a first-order reaction in a plug-flow reactor. Inlet ofCA = 10 mol/L and leaving at 1.0 mol/L (90% conversion), with rateconstant k = 0.5.

n I(n)2 E

(n)2 Relative error [%]

2 6.58636 1.98119 43.02107

4 5.25844 0.65327 14.18563

8 4.79547 0.19030 4.13240

16 4.65590 0.05073 1.10168

32 4.61812 0.01295 0.28123

64 4.60843 0.00326 0.07071

128 4.60599 0.00082 0.01770

I Large number of panels required for high accuracyI Can be problematic for computationally-costly functions

11

Page 12: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

The composite trapezoidal rule: coding

A function that estimates the integral I =∫ b

af (x)dx using the composite

trapezoidal rule: on course website

function In = trapezcomp(f, a, b, n)

% Initialization

h = (b - a)/n;

x = a;

% Composite rule

In = f(a);

for k = 2:n

x = x + h;

In = In + 2 * f(x);

end

In = (In + f(b))*h*0.5;

% end of function

------

>> f = @(CA) -1/(0.5 * CA);

>> I1 = trapezcomp(f, 2.0, 1, 1) % 1.5

>> I2 = trapezcomp(f, 2.0, 1, 2) % 1.4167 12

Page 13: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Simpson’s rules: basics

I Concept: Develop Newton-Cotes formulas of higher order

I How? Use more points!

n = 3: Simpson’s 1/3 rule

I ≈ I2 =

∫ b

a

p2(x) dx

n = 4: Simpson’s 3/8 rule

I ≈ I3 =

∫ b

a

p3(x) dx

13

Page 14: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Simpson’s 1/3 rule: derivation

p2(x), unique quadratic polynomialsuch that:

p2(x1) = f (a)

p2(x2) = f

(a + b

2

)p2(x3) = f (b)

Use of Lagrange interpolating polynomials:

I ≈ I2 =h

3[f (x1) + 4f (x2) + f (x3)]

= (b − a)f (a) + 4f ( a+b

2 ) + f (b)

6

14

Page 15: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Simpson’s 1/3 rule: application

Use Simpson’s 1/3 rule to estimate: I =

∫ π2

0

sin(x) dx

I I =

I I2 ≈

I E2 = |I − I2| ≈ 2.280×10−3

15

Page 16: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Simpson’s 3/8 rule: derivation

p3(x), unique cubic 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

8[f (x1) + 3f (x2) + 3f (x3) + f (x4)]

= (b − a)f (a) + 3f ( 2a+b

3 ) + 3f ( a+2b3 ) + f (b)

8

16

Page 17: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Simpson’s 3/8 rule: application

Use Simpson’s 3/8 rule to estimate: I =

∫ π2

0

sin(x) dx

I I =

I I3 ≈

I E3 = |I − I3| ≈ 1.005×10−3

17

Page 18: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Composite Simpson’s 1/3 rule

Principle: Apply Simpson’s 1/3rule in a piecewise fashion

I Divide [a, b] into n panels,with n even:

h =b − a

n, I =

n/2∑k=1

I k

I For each pair of panels k = 1, 2, . . . , n/2:

I k =

∫ x2k+1

x2k−1

f (x) dx ≈ I k2 =

h

3[f (x2k−1) + 4f (x2k) + f (x2k+1)]

I Overall, for an even number of panels, n:

I ≈ I(n)2 =

h

3

n/2Xk=1

[f (x2k−1) + 4f (x2k) + f (x2k+1)]

=h

3[f (x1) + 4f (x2) + 2f (x3) + 4f (x4) + · · ·+ 2f (xn−1) + 4f (xn) + f (xn+1)]

18

Page 19: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Composite Simpson’s 1/3 rule (cont’d)

Error analysis:

I − I(n)2 = − h4

180(b − a)f (4)(ξ) = − (b − a)5

180n4f (4)(ξ), for some ξ ∈ [a, b]

I Simpson’s 1/3 rule exact for a polynomial of degree 3 or less

Convergence speed (with decreasing h):

E(n)2 = |I − I

(n)2 | = O(h4)

I Halving the stepsize, divides the error by sixteenI One order higher than expected!

Case of an odd number of panels:I Use Simpson’s 1/3 rule for all but last 3 panelsI Use Simpson’s 3/8 rule for last 3 panels

I ≈h

3

n−32X

k=1

[f (x2k−1) + 4f (x2k ) + f (x2k+1)] +3h

8[f (xn−2) + 3f (xn−1) + 3f (xn) + f (xn+1)]

19

Page 20: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Composite Simpson’s 1/3 rule: application

Use the composite Simpson’s 1/3 rule to estimate:

I =

∫ 1.0

10.0

− 1

0.5CAdCA =

Vtotal

F

This was for a first-order reaction in a plug-flow reactor. Inlet ofCA = 10 mol/L and leaving at 1.0 mol/L (90% conversion), with rateconstant k = 0.5.

n I(n)2 E

(n)2 Relative error [%]

2 5.48182 0.87665 19.03617

4 4.81580 0.21063 4.57381

8 4.64115 0.03598 0.78132

16 4.60938 0.00421 0.09143

32 4.60553 0.00036 0.00775

64 4.60520 0.00002 0.00054

128 4.60517 0.00000 0.00003

Contrast with trapezoidal method: I(16)1 = 4.6559; relative error = 1.10%

20

Page 21: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Refresher: Richardson extrapolation

Principle:

Combine two estimates of a given order, O(hp), in order to obtain a newestimate of higher-order, O(hp+1)

I Suppose we want to calculate some quantity QI Let Qp be 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) =2pQp(

12h)− Qp(h)

2p + 1+ O(hp+1)

I Successfully applied to numerical differentiation

I So, why not try to apply it to numerical integration?

21

Page 22: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Romberg integration: principles

Principle:

Apply Richardson extrapolation to numerical integration, starting withthe composite trapezoidal rule

Starting point: −→ O(h2)

I Composite trapezoidal rule: n panels, h = b−an

I1(h) =h

2

nXk=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 + α4h

4 + · · ·+ α2kh2k + · · ·

22

Page 23: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Romberg integration: principles

1st extrapolation: −→ O(h4)

I Combine I1(h) and I1(12h) to eliminate the term in h2, How?

I =4I1(

12h)− I1(h)

3︸ ︷︷ ︸I2(

12h)

+α′4h4 + 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 the term in h4, How?

I =42I2(

14h)− I2(

12h)

42 − 1︸ ︷︷ ︸I3(

14h)

+α′′6h6 + O(h8)

23

Page 24: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Romberg integration: algorithm

Recursive formula:

Ij+1(12k h) =

4j Ij(12k h)− Ij(

12k−1h)

4j − 1, j ≥ 1, k ≥ 1

O(h2) O(h4) O(h6)

I1(h)↘

I1(12h) → I2(

12h) =

4I1(12h)− I1(h)

4− 1↘ ↘

I1(122 h) → I2(

122 h) =

4I1(122 h)− I1(

12h)

4− 1→ I3(

122 h) =

42I2(122 h)− I2(

12h)

42 − 1. . .

. . .. . .

24

Page 25: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Romberg integration: application

Use Romberg integration to calculate an O(h8) estimate of:

I =

∫ π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 many function evaluations are required to get an accuracy ofca. 10−8 for I?

25

Page 26: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Romberg integration: coding

Write a function that estimates the integral I =∫ b

af (x)dx using

Romberg integration; make use of earlier trapezoidal function

function I = romberg(f, a, b, p)

for k = 1:p

% Composite trapezoidal rule for 2^k panels

I(k, 1) = trapezcomp(f, a, b, 2^(k -1));

% Romberg recursive formula

for j = 1:k-1

I(k, j+1) = (4^j * I(k,j) - I(k-1,j)) / (4^j - 1);

end

disp(I(k, 1:k)); % display intermediate results

end

% end of function

--

>> f = @(x)sin(x);

>> format long

>> romberg(f, 0, pi/2, 4);

0.785398163397448

0.948059448968520 1.002279877492210

0.987115800972775 1.000134584974194 0.999991565472993

0.996785171886170 1.000008295523968 0.999999876227286 1.00000000814402026

Page 27: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Gauss quadrature: principles

Approximate: I =

∫ b

a

f (x) dx by the weighted sum: In =n∑

k=1

ωi f (xi )

So far,

I Newton-Cotes formulas

I Romberg integration

The nodal abscissas xi are evenlyspaced in [a, b]Only the weights ωi are “optimized”

Gauss Quadrature:Choose the nodal abscissas and the weights simultaneously in order toachieve maximal accuracy!

27

Page 28: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

2-point Gauss quadrature

Find the coefficients ω1, ω2, x1, x2 such that:

I =

∫ b

a

f (x) dx ≈ ω1f (x1) + ω2f (x2)

4 conditions required:

I Require the formula to be exact for constant, linear, quadratic andcubic terms

28

Page 29: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

2-point Gauss quadrature

Start with the interval [−1, 1]: I =

∫ 1

−1

f (x) dx

0. Condition for a constant term: f (x) = 1∫ 1

−1

1 dx =n∑

k=1

ωi ⇒

1. Condition for a linear term: f (x) = x∫ 1

−1

x dx =n∑

k=1

ωi xi ⇒

2. Condition for a quadratic term: f (x) = x2∫ 1

−1

x2 dx =n∑

k=1

ωi x2i ⇒

3. Condition for a cubic term: f (x) = x3∫ 1

−1

x3 dx =n∑

k=1

ωi x3i ⇒

29

Page 30: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

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 =

∫ 1

−1

f (x) dx ≈ I2 = f(− 1√

3

)+ f

(1√3

)

2-point Gauss quadrature on a general interval [a, b]: Why?

I Make a change of variables: x = b+a2 + b−a

2 ξ, ξ ∈ [−1, 1]

I =

∫ b

a

f (x) dx ≈ I2 =[f

(b+a2 − 1√

3b−a

2

)+ f

(b+a2 + 1√

3b−a

2

)]b − a

2

30

Page 31: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

2-point Gauss quadrature: application

Use 2-point Gauss quadrature to estimate: I =

∫ π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 =

∫ π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

I Yet, only with 2 points – instead of 3 and 4 points, respectively

31

Page 32: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

Higher-order Gauss quadrature

The good news: exact same procedure as with 2 points!

Find the coefficients ω1, . . . , ωn, x1, . . . , xn such that:

I =

∫ b

a

f (x) dx ≈n∑

k=1

ωk f (xk)

2n conditions required:

I Now, require the formula to be exact for terms up to order 2n

Coefficients for Gauss-quadrature on [−1, 1]n = 3

±xk ωk

0.000 000 0.888 8890.774 597 0.555 556

n = 4

±xk ωk

0.339 981 0.652 1450.861 136 0.347 855

n = 5

±xk ωk

0.000 000 0.568 8890.538 469 0.478 6290.906 180 0.236 927

I Apply the same transformation for a general integral∫ b

af (x) dx

32

Page 33: Process Model Formulation and Solution, 3E4 · 2010. 11. 10. · Romberg integration Gauss quadrature 2-point Gauss quadrature Higher-order Gauss quadrature (Strongly) recommended

3-point Gauss quadrature: application

Use 3-point Gauss quadrature to estimate: I =

∫ π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 =

∫ π2

0

sin(x) dx ≈ I3 =

E3 = |I − I3| ≈ 8.497× 10−6

33