quadrature rules 1michael sokolov / numerical methods for chemical engineers / numerical quadrature...

23
Quadrature rules 1 Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften ETH Hönggerberg / HCI F123 – Zürich E-Mail: [email protected] http://www.morbidelli-group.ethz.ch/education/index ()d b a fx x

Upload: percival-dean

Post on 05-Jan-2016

228 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Quadrature rules

Michael Sokolov

ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften

ETH Hönggerberg / HCI F123 – Zürich

E-Mail: [email protected]

http://www.morbidelli-group.ethz.ch/education/index

( )db

a

f x x

Page 2: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

2Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Quadrature methods

Single Step Trapezoidal Rule

Composite Trapezoidal Rule

Page 3: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

3

Composite Midpoint Rule

Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

a x1 x2xn-1 b

h 1

b a

hN

1

1

( )d ( )2

b Ni i

ia

x xf x x h f

1( )2

a xA hf

Constant function

for each step

Page 4: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

4

Composite Trapezoidal rule

Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

a x1 x2xn-1 b

1 1 2 1( )d ( ( ) ( )) ( ( ) ( )) ( ( ) ( ))2 2 2

b

N

a

h h hf x x f a f x f x f x f x f b

h

1

1

( )d ( ( ) ( )) ( )2

b N

iia

hf x x f a f b h f x

1( ( ) ( ))2

hA f a f x

Linear function

for each step

Page 5: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

5Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Composite Simpson rule

The interval is split up and the areas are integrals of quadratic functions

a x1 x2 xn-1 b

Parabola through f(a), f(x1), f(x2)

11

1 1

( )d ( ) ( ) 2 ( ) 46 2

b N Nj j

jj ja

x xhf x x f a f b f x f

Page 6: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

6Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Gauss QuadratureDepending on the polynomial order n nodes xj and weights wj are used

To approximate the area under a function.

n = 3

Page 7: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

7Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Degree of exactness

Trapezoids are areas under linear functions Linear functions are approximated exactly; q = 1

Simpson uses the area under quadratic functions Polynomials up to order three are approximated exactly! q = 3 Even degree interpolation polynomials get one degree of exactness

for free

Example

10

0

d

Nf x x

I f x x

Page 8: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

8Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Degree of exactness vs. order of accuracy

When a non-exact result is obtained, the error is proportional to the step size to a certain power s, the order of accuracy

It can be shown that s = q + 1 for sufficiently smooth f log logsh s h b

Page 9: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

9Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Solution of Nonlinear Functions

( ) 0f x

Michael Sokolov

ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften

ETH Hönggerberg / HCI F123 – Zürich

E-Mail: [email protected]

http://www.morbidelli-group.ethz.ch/education/index

Page 10: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

10Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Zero of a Nonlinear Function

Problem definition: Find the solution of the equation f(x) = 0

for scalar valued f and x; Look for the solution either in An interval, generally –∞ < x < ∞ In the uncertainty interval [a, b], where f(a)f(b) < 0

Types of algorithms available:1. Bisection method2. Substitution methods3. Methods based on function approximation

Assumptions: In the defined intervals, at least one solution exists We are looking for one solution, not all of them

Page 11: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

11Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Bisection Method

1. Define starting interval [a,b] (check that f(a)*f(b) < 0)

2. Compute x = mean([a, b])

3. Redefine the interval Set either a = x or b = x so that f(a)*f(b) < 0 is still fulfilled

4. Iterate 2 and 3 until the requestedprecision is reached

Advantages After n iterations, the interval is reduced

by 2n

Final precision can be predicted a priori

Disadvantages Function characteristics are not used

to speed up the algorithm 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3-1.5

-1

-0.5

0

0.5

1

1.5

2

2.5

3

ab

xa

Page 12: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

12Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Newton Method The Newton method is based on Taylor expansion

Advantages Theoretically fastest

convergence

Disadvantages Convergence is not guaranteed

even if the uncertainty intervalis known

If the derivative must becalculated numerically, the secant method is more convenient

20 0 0

00 1

0

( ) ( ) ( )( ) ( )

( ) ( )( ) 0

( ) ( )k

k kk

f x f x f x x x O x

f x f xf x x x x x

f x f x

xxf

xxf

2)(

1)( 2

x0x1

Page 13: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

13Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Secant Method The Secant is based on the same principles as the Newton

method, but it approximates the derivative numerically

Advantages Does not require the analytical

first order derivative

Disadvantages Convergence is not assured

even if the uncertainty intervalis known

Convergence is slower

1

1

1

( )

( )

( ) ( )( )

kk k

k

k kk

k k

f xx x

f x

f x f xf x

x x

x0 = 1.8

x1 = 1.7

Secant Newton

Page 14: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

14Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

How does Matlab do it? Nonlinear Functions

fzero finds the zero of a scalar valued function; It uses a combination of bisection, secant, and inverse quadratic interpolation methods

roots finds all the roots of a polynomial function;It computes the eigenvalues of the companion matrix, which correspond to the roots of the polynomial A = diag(ones(n-1,1),-1);A(1,:) = -c(2:n+1)./c(1);eig(A);

Where c is the vector of the polynomial coefficients1 1

1 2 1n n

n np c x c x c x c

3 12 4

1 1 1 1

1 0 0 0

0 1 0 0

0 0 0 1 0

nc cc c

c c c c

A

Page 15: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

15Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Matlab Syntax Hints

x = fzero(fun, x0); fun is a function taking as input a scalar x, returning as output the

scalar function value f(x) x0 is either an initial guess (if it has length 1) or an uncertainty

interval (if it has length 2, then f(x0(1))*f(x0(2)) < 0 must be fulfilled)

x = roots(c); c is a vector containing the polynomial coefficients in the order

1 11 2 1

n nn np c x c x c x c

Page 16: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

16Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Assignment 1: Quadrature method comparison Consider the function and its integral in the range [-2,2]

Using a discretization xk = a + hk with h = (b-a)/(N-1) and k = 0,1,…,N, the quadratures are given by

The Gaussian quadrature is defined for the polynomial order n as:

( ) xf x e2

2

xA e dx

Midpoint

Trapezoidal

Simpson

10

1

[ ]2

N

N k k

k

x xQ f h f

1

11

1 1[ ]

2 2

N

Nk

k

Q f h f a f x f b

1

12

1 1

[ ] 2 46 2

N N

N k kk

k k

h x xQ f f a f x f f b

1

[ ]2 2 2

n

n j jj

b a b a a bG f w f x

(1) (2)

(3)

(4)

(5)

(6)

Page 17: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

17Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Assignment 1: Quadrature method comparison1. Define a new function of the form gauss_int(f,N,a0,b0) where you

discretize x and evaluate the overall area as a sum of subareas evaluated with the Gaussian quadrature above. Note that for n = 3 and .

2. In your main file, vary N between 10 and 105 using logspace(1, 5, 100), and calculate the relative absolute error of the four approximations compared to the analytical solution of (2) for each h.

3. Plot h vs. the relative errors using loglog for the four methods.

4. The order of accuracy can be determined as the slope of the double-logarithmic plot. Use polyfit to obtain the corresponding slope for each of the methods.

a. In the case of non-linear behavior reduce the fitting to the linear area. Why can the non-linear behavior at very small relative errors be neglected?

b. Compare your results with the rules from the lecture.

5. Provide the output in an appropriate format using fprintf.

6. Think of an alternative way to implement the Gaussian quadrature for a given number of approximation points N.

Page 18: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

18Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Exercise: CSTR Multiple Steady States

Consider a CSTR where a reaction takes place

We assume the following V = const., i.e. Qin = Qout = const. Perfect coolant behavior, i.e. TC,in = TC,out = const. Constant density and heat capacity of the reaction mixture Constant reaction enthalpy Constant feed, i.e. cA,in = const., Tin = const.

, ,in in inAc Q T

, ,outic Q T

CT( )A Bk T

0( ) exp AEk T kRT

Page 19: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

19Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

CSTR Mass and Energy Balances

The mass and energy balances read

With the T-dependent reaction rate constant

Total Reaction In Out Cool

d( )

dd

( )d

( )

inAA A A

BA B

in Cool CoolP A R P

cV Vc k T Qc Qc

tc

V Vc k T Qct

Q Q Q Q Q

dTV c Vc k T H Q c T T K T T

dt

0( ) exp AEk T kRT

Page 20: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

20Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Dimensionless Mass and Energy Balances

If we define

We get a dimensionless form

1 1 ( )

( )

( ) 1

AA

BA B

C CA

duu

ddu

u udd

u Kd

ii in

A

in

cu

c

T

TQt

V

CoolC

P

CoolC

in

Vk

Q

KK

Q c

T

T

inR A

inP

Ain

H c

c T

E

RT

,0 ,0

0

0

0

1

( ) exp

A Bu u

Page 21: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

21Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

CSTR Temperature Equilibrium

The steady state concentration of A reads

The temperature in steady state is therefore given by

Reaction

( ) 1

In Out Cool

SS C CA

Q Q Q Q

u K

1

1 ( )ssAu

0

( )1 0

1 ( )

( ) exp

C CK

Page 22: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

22Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Assignment 21. Plot the total heat flow from and to the reactor vs. the

dimensionless reactor temperature Use α = 49.46; κ0 = 2.17e20; KC = 0.83; η = 0.33 and θC = 0.9.

2. Implement and use the secant method to find the three steady state temperature of the CSTR. Use a function of the formfunction [x,xvec] = secantRoot(f,x0)

Also return the x-values calculated as a vector xvec. The calculation steps of the secant method can be found on slide 7 The secant method uses two starting guesses; from x0, calculate x1

= (1+ε)*x0. Suggest a value for ε (not too small). Loop while abs(xk – xk-1) > 1e-8 and f(xk) > 1e-6 and n < 1e5

You will have to store two x-values at any given iteration, that is xk and xk-1

Page 23: Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und

23Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Assignment 2 (continued)

3. In what range of x0 can you converge to the intermediate solution? What feature of the function determines which solution is found?