numerical differentiation and quadrature (integration) 1michael sokolov / numerical methods for...

15
Numerical Differentiation and Quadrature (Integration) 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() , ()d d b a fx fx x x

Upload: grace-parrish

Post on 14-Dec-2015

239 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Numerical Differentiation and Quadrature (Integration) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov

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

Numerical Differentiation andQuadrature (Integration)

d ( ), ( )d

d

b

a

f xf x x

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 2: Numerical Differentiation and Quadrature (Integration) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov

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

Numerical Differentiation

Problem:Though an analytical derivative can be found for all differentiable functions, it is often impractical to calculate it

Solution:Approximate the derivative numerically

Method of forward finite differences:

Remember that:

Therefore: for small h

0

0 0

0

( ) ( )d ( )lim

d hx x

f x h f xf x

x h

0

0 0( ) ( )d ( )

d x x

f x h f xf x

x h

Page 3: Numerical Differentiation and Quadrature (Integration) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov

3

Numerical Quadrature (Integration)

Problem:Generally, it is not possible to find the antiderivative (Stammfunktion) of a function f(x) in order to solve a definite integral in the interval [a,b]

Solution:Approximate the area under the curve numerically

Trapezoidal rule:Divide the interval into sub-intervals and approximate the integral by the sum of the areas of the resulting trapezoids

Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Page 4: Numerical Differentiation and Quadrature (Integration) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov

4

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 b a

hn

1

1

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

b n

iia

hf x x f a f b h f x

1( ( ) ( ))2

hA f a f x

Page 5: Numerical Differentiation and Quadrature (Integration) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov

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

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: Numerical Differentiation and Quadrature (Integration) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov

6Michael 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 7: Numerical Differentiation and Quadrature (Integration) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov

7Michael 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 8: Numerical Differentiation and Quadrature (Integration) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov

8

How does Matlab do it?

quad: Low accuracy, non-smooth integrands, uses adaptive recursive Simpson rule

quadl: High accuracy, smooth integrands, uses adaptive Gauss/Lobatto rule (degree of integration routine related to number of points)

quadgk: High accuracy, oscillatory integrands, can handle infinite intervals and singularities at the end points, uses Gauss/Konrod rule (re-uses lower degree results for higher degree approximations)

Degree q of an integration rule = Polynomials up to order q can be integrated accurately (assuming there is no numerical error)

Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Page 9: Numerical Differentiation and Quadrature (Integration) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov

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

Matlab Syntax Hints

All the integrators use the syntaxresult = quadl(int_fun, a, b, ...); int_fun is a function handle to a function that takes one input x and

returns the function value at x; it must be vectorized

Use parametrizing functions to pass more arguments to int_fun if neededf_new = @(x)int_fun(x, K); f_new is now a function that takes only x as input and returns the

value that int_fun would return when K is used as second input Note that K must be defined in this command

This can be done directly in the integrator call:result = quadl(@(x)int_fun(x, K), a, b);

Matlab 2012a and newer: integral(...)

Page 10: Numerical Differentiation and Quadrature (Integration) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov

10

Assignment 1 Consider the function

1. Use the method of forward finite differences to approximate the derivative of f(x) at x = 1. Vary h between 10-15 and 10-1 using logspace(-15, -1, 200), and calculate the error of the finite differences approximation compared to the analytical solution for each h.

2. Plot the error vs. h using loglog. What do you observe? What could be the cause for this behavior?

3. Repeat the calculations of 1. and 2. using the method of centered finite differences. Compare the two loglog plots.

Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

( ) logf x x0 0

d ( ) 1

d x x

f x

x x

0

0 0d ( ) ( ) ( )

d

x x

f x f x h f x

x h

0

0 0d ( ) ( ) ( h)

d 2

x x

f x f x h f x

x h

Page 11: Numerical Differentiation and Quadrature (Integration) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov

11

Exercise

Mass Transfer into Semi-Infinite Slab Consider a liquid diffusing into a solid material The liquid concentration at the interface is constant The material block is considered to be infinitely long, the

concentration at infinity is therefore constant and equal to the starting concentration inside the block

Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

c(z, t)

0 z

c0 = const. c∞ = const.

Page 12: Numerical Differentiation and Quadrature (Integration) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov

12

Exercise (continued) Using a local mass balance, we can formulate an ODE

where j is the diffusive flux in [kg m-2 s-1] With Δz 0, we arrive at a PDE in two variables

Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Accumulation Mass In Mass Out

d d d

d d dd

d

z z z

z z z

m c cV A z A j A j

t t tj jc

t z

c j

t z

z z+Δz

jin jout

Page 13: Numerical Differentiation and Quadrature (Integration) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov

13

Exercise (continued)

By combining this local mass balance with Fick’s law, a PDE in one variable is found:

The analytical solution of this equation (found by combination of variables) reads:

Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

2

2

d

d

c c cj D D

z t z

20

0 0

2erf exp d

4

c cs s

c c

z

Dt

Page 14: Numerical Differentiation and Quadrature (Integration) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov

14

Assignment 21. Write a Matlab program to calculate and plot the

concentration profile in the slab Use the following values:

c∞ = 0; c0 = 1 Create a vector zeta = linspace(1e-6, 3), calculate the

value of c for each zeta, then plot c vs. zeta Use integral or quadl for the integration

2. Create a function which calculates an integral with the trapezoidal rule Use the form: function F = trapInt(f, n, a, b) Where f is a function handle to the function that is to be integrated,

n is the number of points and a and b denote the interval Provide the mean error of the two methods (1. and 2.) using fprintf

Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature

Page 15: Numerical Differentiation and Quadrature (Integration) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov

15

Assignment 2 (continued)

3. Improve your function by adding a convergence check: In addition to computing the integral with n points, simultaneously

calculate it with 2n points while the results differ by more than 10-6, double n and iterate the

calculation Terminate the calculation and issue a warning if n exceeds 106

Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature