Computational Methods in the
Introductory Courses to
Chemical Engineering (KETA01) and
Biotechnology (KKKA05)
Carmen Arévalo
Lund University
carmen@[Link]
Lecture 7
Arévalo KETA01 & KKKA05
Open Rubric
Integration
Z b
• Area: f (x)dx
a
Z b
1
• Average value of a function: f (ξ) = f (x)dx
b−a a
Z b
• Distance: d = v(t)dt (v is velocity)
a
Z Z Z
• Mass: M = c(x, y, z) dx dy dz (c is concentration)
Z Z
• Heat transfer: f (x, y)dx dy (f is flux in cal/(cm2s))
Arévalo KETA01 & KKKA05 1
Numerical Integration or Quadrature
Quadrature is the approximation of definite integrals,
Z b
I(f ) = f (x)dx
a
I(f ) may be interpreted as the area between the curve y = f (x) and the
x-axis.
Areas above the x-axis are positive; those below the x-axis are negative.
Arévalo KETA01 & KKKA05 2
Approximation of integrals
Steps to approximate the integral as an area:
1. The interval [a, b] is divided into subintervals.
2. The area in each subinterval is approximated.
3. The areas of all subintervals are added up.
Arévalo KETA01 & KKKA05 3
Arévalo KETA01 & KKKA05 4
Quadrature formulas
To integrate a function in the interval [a, b], the integral is approximated by
I = (b − a) × average height
Depending on how the average height is approximated, we get different
quadrature formulas.
Arévalo KETA01 & KKKA05 5
The Trapezoidal Rule
The trapezoidal rule approximates the integral below the curve as the area
of the trapezoid
f(x)
f(a)
f(b)
x
a h b
b
b−a
Z
f (x) dx ≈ [f (a) + f (b)]
a 2
Arévalo KETA01 & KKKA05 6
The trapezoidal rule in MATLAB: trapz
Z = trapz(X,Y)
Integrating tabulated data:
1 2 3.25 4.5 6 7 8 8.5 9.3 10
5 6 5.5 7 8.5 6 6 7 7 5
>> t=[1 2 3.25 4.5 6 7 8 8.5 9.3 10];
>> v=[5 6 5.5 7 8.5 6 6 7 7 5];
>> d=trapz(t,v)
d =
58.4250
Arévalo KETA01 & KKKA05 7
Integrating a Function
Z 4
(1 − e−2t)dt
0
>> x = 0:4;
>> y = 1-exp(-2*x);
>> m = trapz(x,y)
m =
3.3437
or
>> x=0:4;
>> f=@(t) 1-exp(-2*t);
>> m=trapz(x,f(x))
m =
3.3437
Arévalo KETA01 & KKKA05 8
Simpson’s rule
Instead of a trapezoid, the ”top” is taken as a parabola (quadratic polyno-
mial) passing through 3 points on the curve.
Simpson rule: Area approximated with a parabola
3
2.5
1.5
0.5
0
0 0.5 1 1.5 2
Arévalo KETA01 & KKKA05 9
Divide the interval into 2 subintervals.
Stepsize: h = (b − a)/2,
Nodes: a, (a + b)/2, b
Z b
h a+b
f (x)dx ≈ Q(f ) = f (a) + 4f ( ) + f (b)
a 3 2
Arévalo KETA01 & KKKA05 10
Truncation error
If the spacing is uniform and we call its length h, the error of approximating
the integral of f in the interval [a, b] by the trapezoidal rule is
1
ET (f ; h) = − (b − a)h2f 00(c), c ∈ [a, b]
12
Taking a spacing half as large will reduce the error by one fourth (roughly).
For Simpson’s rule:
1
ES (f ; h) = − (b − a)h4f (4)(ξ), ξ ∈ [a, b]
180
Taking a spacing half as large will reduce the error by 1/16-th (roughly).
Arévalo KETA01 & KKKA05 11
Comparison between Simpson’s and Trapezoidal Rules
1 2 f (4)(ξ2)
ES (f ) = h ET (f )
15 f 00(ξ1)
When h is small Simpson’s has smaller error than Trapezoidal rule.
• For same h, Simpson’s rule is more accurate than the Trapezoidal rule
• To get the same accuracy, Simpson’s rule needs less computations
Arévalo KETA01 & KKKA05 12
Adaptive quadrature methods
They automatically take into account the behavior of f . The user supplies
f , [a, b] and EPS. With a quadrature rule Q,
1. Approximate the integral using Q
2. Estimate the error of the approximation
3. If E ≤ EP S, stop. Otherwise, divide the interval in two and apply Q to
each subinterval.
4. If |E1| ≤ EP S/2 and |E2| ≤ EP S/2, stop. Otherwise, if the error in
a subinterval is too big, divide that subinterval in two, apply Q to each
of these subintervals and check if the errors are less than EP S/4.
5. Continue until the sum of all errors is less than EP S.
Arévalo KETA01 & KKKA05 13
Example: Adaptive Simpson
Z 1
1 −6
Adaptive Simpson for dx with EP S = 10
0 1 + 100x2
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0 0.06250.1250.1875 0.25 0.375 0.5 0.75 1
Arévalo KETA01 & KKKA05 14
Adaptive Simpson’s Rule in Matlab: quad
q = quad(fun,a,b)
approximates the integral of function fun from a to b to within an error of
10−6 using adaptive Simpson quadrature.
q = quad(fun,a,b,tol)
uses an absolute error tolerance tol instead of the default which is 10−6.
[q,fnce] = quad(fun,a,b,tol)
also shows the number of function evaluations
Arévalo KETA01 & KKKA05 15
>> f=@(t) 8*exp(-0.1*t.^2);
>> [m,fnce]=quad(f,0,24,1e-3)
m =
22.42002755915631
fnce =
25
>> [m,fnce]=quad(f,0,24,1e-6)
m =
22.41996524229996
fnce =
89
>> [m,fnce]=quad(f,0,24,1e-9)
m =
22.41996486570505
fnce =
325
Arévalo KETA01 & KKKA05 16
Precision of quadrature rules
Suppose the heat capacity of a material is given by
c(T ) = 0.132 + 1.56 × 10−4T + 2.64 × 10−7T 2
We will compute the heat required to raise 1000 g of this material from
-100 to 200 degrees (C) using the equation
Z T2
∆H = m c(T ) dT
T1
1)analytically, 2)with Simpson’s rule and 3)with the Trapezoidal rule.
Arévalo KETA01 & KKKA05 17
Analytical integration
c(T ) = 0.132 + 1.56 × 10−4T + 2.64 × 10−7T 2
Z T2
∆H = m c(T ) dT
T1
Z 200
∆H = 1000 (0.132 + 1.56 × 10−4T + 2.64 × 10−7T 2) dT
−100
∆H = 1000(0.132 T + 1.56 × 10−4T /2 + 2.64 × 10−7T 3/3))|200
−100
= 42732 cal
Arévalo KETA01 & KKKA05 18
Numerical integration with Simpson’s rule
>> c=@(t)1000*(0.132+1.56e-4*t+2.64e-7*t.^2)
>> qs=quad(c,-100,200,1e-6)
qs =
42732
Notice that even if we have a very large tol
>> qs=quad(c,-100,200,1e+2)
qs =
42732
the result is exact.
This is because the function c is a quadratic polynomial, and Simpson’s rule
is exact for all polynomials of degree 3 or less.
Arévalo KETA01 & KKKA05 19
Numerical integration with the Trapezoidal rule
We take 5, 50 and 500 nodes in the interval [−100, 200]:
>> t=linspace(-100,200,5);
>> qt=trapz(t,c(t))
qt =
42806.25000000000
>> t=linspace(-100,200,50);
>> qt=trapz(t,c(t))
qt =
42732.49479383590
>> t=linspace(-100,200,500);
>> qt=trapz(t,c(t))
qt =
42732.00477106517
The Trapezoidal rule is exact for polynomials of degree 1 or less.
Arévalo KETA01 & KKKA05 20
M-file functions in quad
• Anonymous function: af = @(x) sin(x) + 2 ∗ cos(x)
Z b
(sin(t) + 2 cos(t)) dt ≈ quad(af,a,b)
a
• M-file function: a file called mf.m containing
function f=mf(y)
f=sin(y)+2*cos(y);
Z b
(sin(t) + 2 cos(t)) dt ≈ quad(@mf,a,b)
a
Arévalo KETA01 & KKKA05 21
The integral of a spline
After creating the structure, one can use the ppval built-in function together
with quad:
x = [0.97 1.12 2.92 3.00 3.33 3.97 6.10 8.39 8.56 9.44];
y = [2.58 0.43 0.06 5.74 7.44 8.07 6.38 2.51 1.44 6.52];
s = spline(x,y);
f = @(x)ppval(s,x);
int = quad(f,x(1),x(end))
Arévalo KETA01 & KKKA05 22
The integral as a function: cumtrapz
cumtrapz computes the cumulative integral.
Z t2
To evaluate M = QC dt at several points,
0
t 0 10 20 30 35 40 45 50
Q 4 4.8 5.2 5 4.6 4.3 4.3 5
c 10 35 55 52 40 37 32 34
>> t=[ 0 10 20 30 35 40 45 50];
>> Q = [ 4 4.8 5.2 5 4.6 4.3 4.3 5];
>> c = [ 10 35 55 52 40 37 32 34];
>> M=cumtrapz(t,Q.*c)
M =
0 1040 3310 6040 7150 8007.8 8749.5 9518.5
Arévalo KETA01 & KKKA05 23