CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
Lecture 11 f17
1. Lecture 10—Numerical Integration
Outline
1 Numerical Integration
A Chemical Engineering Example
The Newton-Cotes Formulae
Composite Newton-Cotes Formulae — Trapezoidal/Simpson’s Rule
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 1 / 21
2. Calculating the Enthalpy of a Non-Ideal Gas
Suppose that we are designing a process unit that handles vapor-phase
ethanol at high pressure (e.g., 600 atm), where the ideal gas law fails.
We need to find out how much heat our process unit requires to maintain a
certain temperature, e.g., 400 ◦
C.
For a non-ideal gas, the enthapy is:
H = Hig
+ HR
HR
is the residual enthalpy (a lot like the excess enthaply HE
that applies to
liquids).
An equation of state can be used to calculate HR
:
Z =
PV
RT
= f(T, P, V)
Thermodynamics provides the equation needed to relate Z to HR
:
HR
= −RT2
P
0
dP
1
P
∂Z
∂T P
How do we evaluate such an expression?
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 2 / 21
3. Calculating the Enthalpy of a Non-Ideal Gas
We start with an expression for Z. The Peng-Robinson Equation of State does
an excellent job with many compounds, including ethanol:
Z =
V
V − b
−
a(T)V
RT(V + b)(V + σb)
= 1 + β − qβ
Z − β
(Z + β)(Z + σβ)
Z ≡
PV
RT
β =
bP
RT
q ≡
a
bRT
a =
ΨαR2
T2
c
Pc
b =
ΩRTc
Pc
α = 1 + (0.37464 + 1.54226ω − 0.26992ω
2
) 1 − (T/Tc )
1/2 2
Ω = 0.0778 Ψ = 0.45724
σ = 1 +
√
2 1 −
√
2
For ethanol:
ω = 0.2 Pc = 37.96 Tc = 425.1
0 200 400 600
.75
1
1.25
P, atm
Z
Compressibility of ethanol at T = 400 ◦
C
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 3 / 21
4. Calculating the Enthalpy of a Non-Ideal Gas
Write the equation of state out in standard cubic form:
Now we can generate Z vs. P data by:
1 The solution as a Matlab function:
function Z = Z_ethanol(T,P)
% Given a T and list of P’s, find ethanol’s compressibility factor
R = 82.06; omega = 0.2; Pc = 37.96*1.01325; Tc = 425.1;
e = 1-sqrt(2); o = 1+sqrt(2); Omega = 0.0778; Psi = 0.45724;
5 alpha=@(T) (1 + polyval([-0.26992,1.54226,0.37464],omega).*(1-sqrt(T./Tc))).^2;
q = @(T) Psi .* alpha(T) ./ ( Omega .* (T ./ Tc));
beta = @(T,P) Omega .* (P ./ Pc) ./ (T ./ Tc);
eos = @(T,P) [ -1
1+beta(T,P) .* (1-o-e)
10 beta(T,P).^2 .* (o+e-o.*e) + beta(T,P).*(o+e-q(T))
beta(T,P).^2 .* (q(T) + o.*e .* (1+beta(T,P)) ) ];
for ii = 1:length(P)
Z(ii) = max(eos(T,P(ii))); % The vapor phase volume is the largest root
end
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 4 / 21
5. Calculating the Enthalpy of a Non-Ideal Gas
Write the equation of state out in standard cubic form:
Now we can generate Z vs. P data by:
1 The solution as a Matlab function:
function Z = Z_ethanol(T,P)
% Given a T and list of P’s, find ethanol’s compressibility factor
R = 82.06; omega = 0.2; Pc = 37.96*1.01325; Tc = 425.1;
e = 1-sqrt(2); o = 1+sqrt(2); Omega = 0.0778; Psi = 0.45724;
5 alpha=@(T) (1 + polyval([-0.26992,1.54226,0.37464],omega).*(1-sqrt(T./Tc))).^2;
q = @(T) Psi .* alpha(T) ./ ( Omega .* (T ./ Tc));
beta = @(T,P) Omega .* (P ./ Pc) ./ (T ./ Tc);
eos = @(T,P) [ -1
1+beta(T,P) .* (1-o-e)
10 beta(T,P).^2 .* (o+e-o.*e) + beta(T,P).*(o+e-q(T))
beta(T,P).^2 .* (q(T) + o.*e .* (1+beta(T,P)) ) ];
for ii = 1:length(P)
Z(ii) = max(eos(T,P(ii))); % The vapor phase volume is the largest root
end
2 Specify some P values between 0 and 600 atm.
>> P = linspace(1e-15,600,15);
>> Z = Z_ethanol(400+273.15,P);
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 4 / 21
6. Calculating the Enthalpy of a Non-Ideal Gas
Now we need to convert our Z vs. P data into ∂Z
∂T
vs. P data, using
derivatives through finite differences.
1 For each P, pick a small value of ∆T
dT = .1; % .1 K is very small compared to our temperature of 673.15 K
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 5 / 21
7. Calculating the Enthalpy of a Non-Ideal Gas
Now we need to convert our Z vs. P data into ∂Z
∂T
vs. P data, using
derivatives through finite differences.
1 For each P, pick a small value of ∆T
dT = .1; % .1 K is very small compared to our temperature of 673.15 K
The smaller the value, the more accurate the derivative,
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 5 / 21
8. Calculating the Enthalpy of a Non-Ideal Gas
Now we need to convert our Z vs. P data into ∂Z
∂T
vs. P data, using
derivatives through finite differences.
1 For each P, pick a small value of ∆T
dT = .1; % .1 K is very small compared to our temperature of 673.15 K
The smaller the value, the more accurate the derivative,
But too small and numerical error will be a problem
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 5 / 21
9. Calculating the Enthalpy of a Non-Ideal Gas
Now we need to convert our Z vs. P data into ∂Z
∂T
vs. P data, using
derivatives through finite differences.
1 For each P, pick a small value of ∆T
dT = .1; % .1 K is very small compared to our temperature of 673.15 K
The smaller the value, the more accurate the derivative,
But too small and numerical error will be a problem
2 We can use central finite differences:
∂Z
∂T P
≈
1
2∆T
Z(P,T+∆T) − Z(P,T−∆T)
function dZ = dZdT(P,T,c)
dT = .1; % This is the h value
T1 = T + dT;
T2 = T - dT;
5 Z1 = P./(8.314*T1) .* Z_ethanol(P,T1); % Solve the PR-EOS
Z2 = P./(8.314*T2) .* Z_ethanol(P,T2); % Solve the PR-EOS
dZ = (Z1-Z2) ./ (2 * dT);
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 5 / 21
10. Calculating the Enthalpy of a Non-Ideal Gas
This is the partial derivative we need:
0 300 600
0
1
2
P, atm
∂Z
∂T
P
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 6 / 21
11. Calculating the Enthalpy of a Non-Ideal Gas
The residual enthalpy of ethanol at P = 600 atm and T = 673.15 K is:
HR
= −RT2
Itrue
0 300 600
0
.8
1.6
I
true
=
600 atm
0
1
P
∂Z
∂T P
dP
P, atm
1
P
∂Z
∂TP
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 7 / 21
12. Outline
1 Numerical Integration
A Chemical Engineering Example
The Newton-Cotes Formulae
Composite Newton-Cotes Formulae — Trapezoidal/Simpson’s Rule
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 8 / 21
13. The Newton-Cotes Formulae
The basic idea behind numerical integration is to start with a function that
we cannot integrate analytically:
The function might not have an analytic solution (like finding HR)
The function might be represented only by measured data
The Newton-Cotes Formulae are based on the integrals of interpolating
polynomials forced to pass through the function to integrate at a few given
data points.
The Newton-Cotes Formulae are closed formulae, meaning that if we are
interested in
b
a
f(x)dx
we must know what f(a) and f(b) are (the values at the endpoints).
Additionally, given np points at which f(x) is known, the Newton-Cotes
Formulae require that these points are equally spaced.
Nomenclature:
Lower Limit a = x1
Upper Limit b = xn+1
# of intervals n
Interval width h = b−a
n
# of evenly spaced data points np = n + 1
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 9 / 21
14. The Newton-Cotes Formulae
The 1st-Order Formula
If we have only np = 2 data points on the function (n = 1 interval), then we
imagine that the line connecting them has the same integral as the
underlying function:
0 300 600
0
.8
1.6
P
1 (x)
a
b
P, atm
1
P
∂Z
∂T
I1 =
b
a
P1(x) =
b − a
2
(f(a) + f(b))
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 10 / 21
15. The Newton-Cotes Formulae
The 2nd
Order Formula
If we have np = 3 data points on the function (n = 2 intervals), then we
imagine that the parabola connecting them has the same integral as the
underlying function:
0 300 600
0
.8
1.6
P
2 (x)
h = b−a
2
h
(x1 = a, f1)
(x3 = b, f3)
(x2 = a + h, f2)
P, atm
1
P
∂Z
∂T
I2 =
b
a
P2(x) =
h
3
f(x1) + 4f(x2) + f(x3)
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 11 / 21
16. The Newton-Cotes Formulae
The 3rd
Order Formula
If we have np = 4 data points on the function (n = 3 intervals), then we
imagine that the cubic polynomial connecting them has the same integral
as the underlying function:
0 300 600
0
.8
1.6
P
3 (x)
h
h
h
(x1, f1)
(x2, f2)
(x3, f3)
(x4, f4)
P, atm
1
P
∂Z
∂T
I3 =
b
a
P3(x) =
3h
8
f1 + 3f2 + 3f3 + f4
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 12 / 21
17. The Newton-Cotes Formula
Summary
All of these quadrature formulae are based on evenly spaced points
np n h I =
b
a
f(x)dx Error
2 1 (a − b) h
2
f1 + f2 O(h3
)
3 2 (a−b)
2
h
3
f1 + 4f2 + f3 O(h5
)
4 3 (a−b)
3
3h
8
f1 + 3f2 + 3f3 + f4 O(h5
)
5 4 (a−b)
4
h
15
7f1 + 32f2 + 12f3 + 32f4 + 7f5 O(h7
)
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 13 / 21
18. Outline
1 Numerical Integration
A Chemical Engineering Example
The Newton-Cotes Formulae
Composite Newton-Cotes Formulae — Trapezoidal/Simpson’s Rule
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 14 / 21
19. The Composite Rules
The basic Newton-Cotes rules are only useful for the first few orders.
In practice, nothing higher than the 4-point rule (integral of the interpolating
cubic polynomial) is commonly used.
Just like we used splines to do a better job of interpolating a large number of
data points, we typically break functions we wish to integrate into
subintervals, and then use a lower-order Newton-Cotes Rule on each
subinterval.
The resulting formulae are called the composite Newton-Cotes Formulae
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 15 / 21
20. The Newton-Cotes Formulae
The Composite Trapezoidal Rule
If we have 8 data points on the function, then we divide it into 7 intervals,
and sum the integrals of the 7 trapezoids :
0 300 600
0
.8
1.6
P, bar
105
×
1
P
∂Z
∂T
,1
barK
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 16 / 21
21. The Newton-Cotes Formulae
The Composite Trapezoidal Rule
If we have np = 8 data points on the function, then we divide it into n = 7
intervals, and sum the integrals of the 7 trapezoids :
I1,c =
h
2
f1 + f2
+ f2 + f3
+ f3 + f4
+ f4 + f5
+ f5 + f6
+ f6 + f7
+ f7 + f8
=
h
2
f1 + 2f2 + 2f3 + 2f4 + 2f5 + 2f6 + 2f7 + f8
=
h
2
(f1 + fn+1) + h
n
i=2
fi
The accuracy of the Composite Trapezoidal Rule is O h2
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 17 / 21
22. The Newton-Cotes Formulae
The 1/3 Simpsons Rule
If we have 7 data points on the function, then we divide it into 6 intervals,
and sum the integrals of the 3 parabolas that span these intervals :
0 300 600
0
.8
1.6
P, bar
105
×
1
P
∂Z
∂T
,1
barK
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 18 / 21
23. The Newton-Cotes Formulae
The 1/3 Simpsons Rule
If we have 7 data points on the function, then we divide it into 6 intervals,
and sum the integrals of the 3 parabolas that span these intervals :
0 300 600
0
.8
1.6
Parabola 1
P, bar
105
×
1
P
∂Z
∂T
,1
barK
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 18 / 21
24. The Newton-Cotes Formulae
The 1/3 Simpsons Rule
If we have 7 data points on the function, then we divide it into 6 intervals,
and sum the integrals of the 3 parabolas that span these intervals :
0 300 600
0
.8
1.6
Parabola 1
Parabola 2
P, bar
105
×
1
P
∂Z
∂T
,1
barK
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 18 / 21
25. The Newton-Cotes Formulae
The 1/3 Simpsons Rule
If we have 7 data points on the function, then we divide it into 6 intervals,
and sum the integrals of the 3 parabolas that span these intervals :
0 300 600
0
.8
1.6
Parabola 1
Parabola 2
Parabola 3
P, bar
105
×
1
P
∂Z
∂T
,1
barK
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 18 / 21
26. The Newton-Cotes Formulae
The 1/3 Simpsons Rule
If we have np = 7 data points on the function, then we divide it into n = 6
intervals, and sum the integrals of the 3 parabolas that span these intervals :
I1,c =
h
3
f1 + 4f2 + f3
+ f3 + 4f4 + f5
+ f5 + 4f6 + f7
=
h
3
f1 + 4f2 + 2f3 + 4f4 + 2f5 + 4f6 + f7
=
h
3
(f1 + fn+1) + 4
n
i=2,even
fi + 2
n−1
i=3,odd
fi
This formula is used when we have an odd number of data points.
The accuracy of the Composite Simpson’s Rule is O h4
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 19 / 21
27. The Newton-Cotes Formulae
The 1/3 + 3/8 Simpsons Rule
If we have np = 8 data points on the function, then we divide have n = 7 intervals.
0 300 600
0
.8
1.6
P, bar
105
×
1
P
∂Z
∂T
,1
barK
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 20 / 21
28. The Newton-Cotes Formulae
The 1/3 + 3/8 Simpsons Rule
If we have np = 8 data points on the function, then we divide have n = 7 intervals.
We need 2 intervals for each 1/3 Simpsons Rule.
0 300 600
0
.8
1.6
Parabola 1
Parabola 2
P, bar
105
×
1
P
∂Z
∂T
,1
barK
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 20 / 21
29. The Newton-Cotes Formulae
The 1/3 + 3/8 Simpsons Rule
If we have np = 8 data points on the function, then we divide have n = 7 intervals.
We need 2 intervals for each 1/3 Simpsons Rule.
For the final 3 intervals use the 3/8 (4-point) Simpson’s Rule
0 300 600
0
.8
1.6
Parabola 1
Parabola 2
Cubic Polynomial
P, bar
105
×
1
P
∂Z
∂T
,1
barK
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 20 / 21
30. The Newton-Cotes Formulae
The 1/3 + 3/8 Simpsons Rule
If we have np = 8 data points on the function, then we divide it into n = 7
intervals, and sum the integrals of the 2 parabolas and single cubic that
span these intervals :
I1,c =
h
3
f1 + 4f2 + f3
+ f3 + 4f4 + f5
+
3h
8
f5 + 4f6 + 4f7 + f8
=
h
3
f1 + 4f2 + 2f3 + 4f4 + f5 +
3h
8
(f5 + 3f6 + 3f7 + f8)
=
h
3
(f1 + fn−2) + 4
n−3
i=2,even
fi + 2
n−4
i=3,odd
fi
+
3h
8
(fn−2 + 3fn−1 + 3fn + fn+1)
This formula is used when we have an even number of data points.
The accuracy of the Composite Simpson’s Rule is O h4
Che 310 | Chapra 19 | Numerical Integration 10 — Numerical Integration November 9, 2017 21 / 21