This action might not be possible to undo. Are you sure you want to continue?
8 Interpolation of Equally Spaced Points
177
We replace the derivatives of f(x) with the differential operators to obtain
The oddorder differential operators in Eq. (3.127) are replaced by averaged central differences and the evenorder differential operators by central differences, all taken from Table 3.3. Substituting these into Eq. (3.127) and regrouping of terms yield the formula
By applying Eq. (3.121) into Eq. (3.128), we obtain the final form of Stirling's interpolation formula
The general formula for determining the higherorder terms containing odd differences in the above series is
where k = 1,3, . . ., and the formula for terms with even differences is
where k = 2,4, . . . Other forms of Stirling's interpolation formula exist, which make use of base points spaced at half intervals (i.e., at h12). Our choice of using averaged central differences to replace the odd differential operators eliminated the need for having base points located at the midpoints. The central differences for Eq. (3.129) are tabulated in Table 3.6.
Table 3.6 Central difference table' i
Xt
f(x0
p6f(xj)
a2f(x0
3 2
x3h x2h xh x x+h x+2h x+3h
f(x3h) f(x2h) f(xh) .f(x) f(x+h) f(x+2h) f(x+3h) %lf(xh)f(x3h)l %lf(x)f(x2h)l %Lf(x+h)f(xh)l %Lf(x+Zh)f(x)] %Lf(x+3h)f(x+h)] f(xh)Zf(x2h)+f(x3h) f(x)2f(xh)+,f(xZh) f(x+h)2f(x)+f(xh) f(xc2h)Zf(x+h)+f(x) f(x+3h)Zf(x+Zh)+f(x+h)
1
0
1
2 3

~p
*Read this table from left to right, starting with top section and continuing wilh middle and bottom sections
3.9 Interpolation of Unequally Spaced Points
179
In this section, we will develop two interpolation methods for unequally spaced data: the Lagrange polynomials and spline interpolation.
3.9.1 Lagrange Polynomials
Consider a set of unequally spaced base points, such as those shown in Fig. 3.3a. Define the polynomial
which is the sum of the weighredvalues of the function at all (n + I) base points. The weights p,(x) are nthdegree polynomial functions corresponding to each base point. Eq. (3.132) is actually a linear combination of nthdegree polynomials; therefore, P,(x) is also an nthdegree polynomial. In order for the interpolating polynomial to fit the function exactly at all the base points, each particular weighting polynomial p,(x) must be chosen so that it has the value of unity when x =xk, and the value of zero at all other base points, that is,
The Lagrange polynomials, which have the form
satisfy the first part of condition (3.133), because there will be a term (xi  xi) in the product series of Eq. (3.134) whenever x =xi. The constant Ck is evaluated to make the Lagrange polynomial satisfy the second part of condition (3.133): Ch
=
1
n
i=O
ilk
(3.135)
(xh  x;)
Although the interpolating polynomial passes through all the base points. Figure 3. which can be obtained from Eq.(x) has a remainder term.135) gives the Lagrange polynomials The interpolating polynomial P.137) 3. it is not able to predict the value of the function satisfactorily in between these points.180 Finite Difference Methods and Interpolation Chapter 3 Combination of Eqs.x. a series of lowerdegree interpolating polynomials may beused toconnect smaller number of base points. (3. (3. .5a. thirddegree splines shown in Fig.5 (a) Fluctuation of highdegree interpolating polynomials between base points.5b shows the result of such interpolation using thirddegree (or cubic) splines.functions. (b) Cubic spline interpolation. Fig.134) and (3.2 Spline Interpolation When we deal with a large number of data points. These sets of interpolating polynomials are called spline. In order to avoid such an undesired behavior of the highdegree interpolating polynomial.5b provide a much more acceptable approximation. 3. < xn (3. =a (X .5a. Compared with the higherorder interpolation illustrated in Fig. highdegree interpolating polynomials are likely to fluctuate between base points instead of passing smoothly through them. This situation is illustrated in Fig. 3. 3. 3.) f'"'I1(S) (n + l ) ! no < t.9.6): RJX) = n .
This is called the natural condition. Because there would be an infinite number of thirddegree polynomials passing through each pair of points. Each spline passes from the base points of the edge of its interval (2n equations). a cubic polynomial is used to approximate the curve between each two adjacent base points. The cubic spline of the interval [ x i .139) it can he concluded that the second derivative of the interpolating ..138) is a line From Eq.138) and n such polynomials for the whole range of data points [x. This method is described in detail in this section.. However.. additional constraints are necessary to make the spline unique. Therefore. In this method. (3. for programming purposes. (3. b. c. The latter is called notaknot condition. xi] can be given by the firstorder Lagrange polynomial at any point in the interval [xi. (3. interpolation formula The expression for the spline can be obtained by twice integrating Eq. Therefore.3.]. The required equations come from the following conditions: a.1 equations). The first derivative of the splines are continuous across the interior base points (n . x.9 Interpolation of Unequally Spaced Points 181 The most common spline used in engineering problems is the cubic spline. The second derivative of the splines are continuous across the interior base points (n . there are 4n unknown coefficients and we need 4n equations to evaluate these coefficients.1 equations). The second derivative of the end splines are zero at the end base points (2 equations). These conditions imply that the slope and the curvature of the spline polynomials are continuous across the base points. Another commonly used condition is to set the third derivative of the end splines equal to the third derivative of the neighboring splines.140): . it is set that all the polynomials should have equal first and second derivatives at the base points. Simultaneous solution of the above 4n linear algebraic equations results in the determination of all cubic interpolating polynomials.xi] has the following general form There are four unknown coefficients in Eq. (3. d. The second derivative of the Eq.1) algebraic equations. there is an alternative method of determination of the coefficients that needs simultaneous solution of only (n .
from the conditions (3. (3. and C (3. and yi .141) are evaluated from the following b o u n d q conditions: . (3. that is. we find the following cubic equation: Note that Eqs.143) is a morecomplicated expression than Eq.144): AlthoughEq. . (3. (3. we apply the condition of continuity of the first derivative of splines at the interior base points. and further rearrangement. By evaluating the constants C.182 Finite Difference Methods and Interpolation Chapter 3 where the constants C.138). In order to determine the y" values.143) are equivalent and the relations between their coefficients are given by Eq. (3.138) and (3.142).141). namely yi. it contains only two unknowns. and C . in Eq. substituting them into Eq.
.1 and yo = y.143) can be used for interpolating the value of the function in every interval. results in where i = 1.1)order tridiagonal set of simultaneous equations.145). followed by rearranging of the terms. (3. which in matrix form becomes Eq. Eq. . . 2. (3. n .9 Interpolation of Unequally Spaced Points 183 Differentiating Eq. . = 0 (natural spline). (3.3.146) represents an (n . (3. Eq.143) and applying the resulting expression in the condition (3.147): After calculating the values of the second derivatives at each base point. .
(3. In order to he time efficient. The terms of summation (3.132) are then calculated. The last part of the function is interpolation itself.136).(x) subpolynomials are calculated according to Eq. it checks the inputs and sets the order of interpolation if necessary. finally. In this sectiou. the interpolation is done by the firstorder Lagrange polynomial (linear interpolation).143). The cuhic spline interpolation is done based on Eq. In the second part of the function. (3. This program should call general MATLAB functions for interpolating unequally spaced data using Lagrange polynomials and cuhic splines. the required number of independent and dependent variables are arranged in two interim matrices at each location.2.132). The MATLAB function Natura1SPLINE.132) and (3. are calculated from Eq.m also consists of three parts.2: The Lagrange Polynomials and Cubic Splines for Interpolation of Unequally Spaced Data. Program Description: The general MATLAB function Lagrange. (3.184 Finite Difference Methods and Interpolation Chapter 3 Example 3.p. instead of forming the interim matrices. Method of Solution: The Lagrange interpolation is done based on Eqs. locations of all the points at which the values of the function are to he evaluated are found in between the base points. The first and second parts are more or less similar to those of Lagrange. These matrices are used at the interpolation section for doing the interpolation in vector form. However. assuming a natural spline. Write a program to estimate pressure drop of the filter at any flow rate within the experimental range.m performs the nthorder Lagrange interpolation. all these calculations are done in vector form and at all the required points simultaneously.147). The pressure drop of a haskettype filter is measured at different flow rates as shown in Table E3. (3. If not introduced to the function. The values of the second derivatives at base points. Because matrix operations are much faster than elementhyelement operations in MATLAB. Table E3.m. This function consists of the following three parts: At the beginning. the function value is determined based on Eq. The order of interpolation is an input to the function.2 Pressure drop of a baskettype filter Flow Rate (Us) Pressure drop (kpa) Flow rate Pressure drop (kpa) (Us) .136). the interpolation locations are kept in a vector. (3. and.
q = linspace(min(0) . YI = LagrangelX.q. % % % % % % YI = Lagrange(X.N) applies the Nthorder Lagrange interpolation to find YI.XI. It uses Lagrange and cubic spline % interpolations to find the pressure drop of a filter at any % point in between the experimental data. dP2 = NaturalSPLINE(Q .147).dPl.Y.') xlabel I 'Flow Rate (lit. dP .y. n). % Interpolation dP1 = Lagrange(Q . and the values of the second derivatives at the base points are evaluated.dP.'.Example 3.xi. INTERPIQ 8 % % % . based on Eq. 'Natural Spline Interpolation'. q): % Plotting the results plotIQ.m % Example32.2 Lagrange Polynomials and Cubic Splines 185 In the last section of the function.n) %Lagrange One dimensional interpolation.'Lagrange Interpolation'. the values of the underlying function Y at the points in the vector XI. GregoryNewton. disp( ' ) n = inputl' Order of the Lagrange interpolation = ' 1 .2) Lagrange. (3. clc clear clf % Input data Q = input(' Vector of flow rates = dP = input ( ' Vector of pressure drops = ' ) .143).'o'.dP2.Y. dP . See also NATURALSPLINE. max(Q)) . in the vector form. The vector X specifies the points at which the data Y is given. q . Program Example32.m function yi = Lagrange(x.2. The interpolation is then performed. (3. INTERPI.XI)is equivalent to the linear interpolation.m % Solution to Example 3. / s )' ) ylabel I 'Pressure Drop (kPa) ') 1egendl'Experimental Data'. SPLINE.q. the matrix of coefficients and the vector of constants are built according to Eq.
ny = length(y) . xtemp(l:n+l.x. if nx = ny error('x and Y vectors are not the same size. end if loc i 1 loc = 1.\n'. end n = floor(n).n) fprintf('\n%2dorderinterpolation will be performed instead.186 % % Finite Difterence Methods and Interpolation & Chapter 3 ( c ) by N. % Locating xi [dxrn . deltax(1.m) = y(loc:loc+n). end if loc+n > nx lac = nx . if n >= nx fprintf('\nNot enough data points for %2doredr interpolation.') end : . % Locating the required number of base points for m = 1:lxi d x = xi(m) . Mostoufi January 1.n . % Make sure it's a column vector nx = length(x).:) = ones(1.1. nx1) n = nx . % locating the first base point if dx(1oc) < 0 loc = loc . end deltax(2:n+2.m) = x(loc:loc+n): ytemp(l:n+l. end % Check the order of interpolation if nargin == 3 / n c 1 n = 1. % Make sure it's a column vector x = x y = y : . Constantinides % Initialization if nargin < 3 error('Inva1id number of inputs. . 1999 A.m) = dx(loc:loc+n).'). end lxi = length(xi).'.lxi). locl = min(abs(dx)).1.
:) = p(k. 1 9 9 9 % Initialization if nargin c 3 error('Inva1id number of inputs. xi = ( x i ( : ' . Natura1SPLINE. See also Lagrange. SPLINE % (c) by N.:) . INTERPI.y.xtemp(m. .') end x = x : . if nx = ny error('X and Y end % Make sure it's a column vector % Make sure it's a column vector % Make sure it's a column vector vectors are not the same size..:) = ones(1.:l. s(k. GregoryNewton. the values of the underlying function Y at the points in the vector XI. nx = length(x1. end end p(k. Mostoufi & A. 9.2 Lagrange Polynomials and Cubic Splines 187 end 8 Interpolation for k = 1 : n+l for m = 1 : n+l if k = m den(m.:). lxi = lengthlxil. else denlrn. * ytemp(k.:). end yi = sum(s1.lxil. INTERPQ.:) = prod([deltax(l:k. / prod(den1.Y. ny = length(y).xi) %NATURALSPLINE One dimensional interpolation.m function yi = NaturalSPLINE(x.x. The vector X specifies the points at which the data Y is given.:) = xtemp(k.Example 3. % % % % % % YI = NATURALSPLINE(X. y = ( y : .:) .XI) applies the natural spline interpolation to find YI.:lll.'). Constantinides % January 1. % Locating the required number of base points for m = 1:lxi d = xi(m1 . deltax(k+2:n+2.
188
% %
Finite Difference Methods and Interpolation
Chapter 3
Locating xi
[dm , loc(m)I = min(abs(d1 1 ;
locating the first base point if d(loc(m) < 0 1 loc(m) == nx loc (m) = loc(m)  1; end if loc(m) c 1 loc(m) = 1; end end dx = diff(x) ; dy = diff (y); yox = dy . / dx; % Matrix of coefficients A = 2 * diag(x(3:nx)x(1:nx2))+ . . . [zeros(nx2,l) ldiag(dx(2:nx2)) ; zeros(1,nx3)11 + . . . [zeros(l,nx2) ; Ldiag(dx(2:nx2)) zeros(nx3,l)Il; % Vector of constants c = 6 * (yox(2:nx1)  yox(1:nx2)); % Solution of the set of linear equations y2 = [O; inv(A) * c; O];% Interpolation yi = (1/6) * ((xi  x(loc+l)).^3 . / (x(10c)  x(loc+l)) . . .  (x(10c)  x(loc+l)) . * (xi  x(loc+l))) . * y2(loc) . . . + (1/6) * ((xi  x(loc)).^3 . / ix(loc+l)  x(loc)) . . .  (x(loc+l)  x(loc)) . * (xi  x(10c))) . * y2(loc+l) . . . + (xi  x(loc+l)) . / (x(loc)  x(loc+l)) . * y(10c) . . . + (xi  x(10c) ) . / (x(loc+l)  x(1oc)) . * y(loc+l);
Input and Results
Vector of flow rates = [O, 10.80, 16.03, 22.91, 28.24, 32.56, 36.76, 39.88. 43.681 Vector of pressure drops = LO, 0.299, 0.576, 1.036, 1.383, 1.781, 2.432, 2.846, 3.3041 order of the Lagrange interpolation = 3
Discussion of Results: Order of the Lagrange interpolation is chosen to be three for comparison of the results with that of the cubic spline, which is also thirdorder interpolation. Fig. E3.2 shows the results of calculations. There is no essential difference between the two methods. The cubic spline, however, passes smoothly through the base points, as expected. Because the Lagrange interpolation is performed in the subsets of four base points with no restriction related to their neighboring base points. it can be seen that the slope of the resulting curve is not continuous through most of the base points.
3.10 Orthogonal Polynomials
189
0
5
10
15
20
25
30
35
40
45
Flow Rate (litls)
Figure E3.2 Lagrange polynomials and cubic splines.
Orthogonal polynomials are a special category of functions that satisfy the following orthogonality condition with respect to a weighting function w(x) 2 0, on the interval [a, b]:
This orthogonality condition can be viewed as the continuous analog of the orthogonality property of two vectors (see Chap. 2 )
in ndimensional space, where n becomes very large, and the elements of the vectors are represented as continuous functions of some independent variable.
190
Finite Difference Methods and Interpolation
Chapter 3
There are many families of polynomials that obey the orthogonality condition. These are generally known by the name of the mathematician who discovered them: Legendre, Chebyshev, Hermite, and Laguerre polynomials are the most widely used orthogonal polynomials. In this section, we list the Legendre and Chehyshev polynomials. The Legendre polynomials are orthogonal on the interval 11, 11 with respect to the weighting function w(x) = 1. The orthogonality condition is
They also satisfy the recnrrence relation
Starting with P,(x) = 1 and P,(x) = x, the recnrrence formula (3.150) or the orthogonality condition (3.149) can be used to generate the Legendre polynomials. These are listed inTahle 3.7 and drawn on Fig. 3.6. The Chebyshev polynomials are orthogonal on the interval [I, 11 with respect to the weighting function w(x) = 1 1 . Their orthogonality condition is
I
and their recurrence relation is Tn+,  2xTn
+
i f n t m
Tn.,
=
0
(3.152)
Starting with T,(x) = 1 and T,(x) =x, the recurrence formula (3.152) or orthogonality condition (3.151) can be used to generate the Chebyshev polynomials listed in Table 3.8 and drawn on Fig. 3.7. It should he noticed from Figs. 3.6 and 3.7 that these orthogonal polynomials have their zeros (roots) more closely packed near the ends of the interval of integration. This property can be used to advantage in order to improve the accuracy of interpolation of unequally spaced points. This can be done in the case where the choice of base points is completely free. The interpolation can be performed using Lagrange interpolation method described in Sec. 3.9.1, but the base points are chosen at the roots of the appropriate orthogonal polynomial. This concept is demonstrated in Chap. 4 in connection with the development of Gauss quadrature.
10 Orthogonal Polynomials Table 3.o ( 2 n .6 The Legendre orthogonal polynomials.7 Legendre polynomials 191 Inn.m ) ! ( n . .(x) = C . Figure 3.2 m ) ! 'The notation [n/21represents the integer part of n/2..' n P.Zm)! Xn2m 2"m!(n .3.
.7 The Chebyshev orthogonal polynomials.8 Chebyshev polynomials Chapter 3 * The notation [nIZ] represents the integer part of nI2 Figure 3.192 Finite Difference Methods and Interpolation Table 3.
Write the functionin ageneral fashion so that n can be any positive integer. 3. Write the function in a general fashion so that n can be any positive integer. and evaluate the function at x = 10.50. Also write a MATLAB script that reads the data and shows how this . Also write a MATLAB script that reads the data and shows how this MATLAB function fits the data.130. (b) Develop the backward difference table. 50.3 to verify the program.4 Write a MATLAB function that uses the GregoryNewton backward interpolation formula to evaluate the function f(x) from a set of (n + 1) equally spaced input values.90. 130. 170.1 Show that all the interpolating formulas discussed here reduce to the same formula when a firstorder interpolation is used.3 Using the experimental data in Table P3. and 190. 170. (b) Apply Stirling's interpolation formula to evaluate the function a t x = 10.2 Derive the Grego~yNewton backward interpolation formula. 170.130. 90. 3. 3.6 Write a MATLAB function which uses the Stirling's interpolation formula to evaluate the function f(x) fromaset of (n + 1) equally spaced input values. 50. (c) Apply the GregoryNewton interpolation formulas to evaluate the function at x = 10. Table P3. and 190. 3. 3. (a) Develop the central difference table. Use the experimental data of Table 3. and 190.Problems 193 PROBLEMS 3.3.3 Data of penicillin fermentation Time (h) Penicillin concentration (units/mL) Time (h) Penicillin concentration (units1mL) 3.90 .3: (a) Develop the forward difference table.5 Using the experimental data of Prob.
3. and 11. 170.8 Vapor pressure of lithium chloride Pressure (mm Hg) Temperature ("C) Pressure (mm ~ 9 ) Temperature ('C) 1 5 783 883 932 9 87 1045 60 100 200 400 760 1081 1129 1203 1290 1382 10 20 40 .8 Vapor pressure of lithium chloride is given in Table P3. Table P3.7 use Lagrange polynomials and spline interpolation to evaluate the function at x = 2.91 to determine the isoelectric points of silica in the presence of 10'M Pb(NO.194 Finite Difference Methods and Interpolation Chapter 3 MATLAB function fits the data. 8.4. 3. Use data from Rashchi et al. 9.e. 3.130. and evaluate the function at x = 10. 5. The isoelectric point (i.90. (b) From 10 kPa to 100 kPa at 10 kPa increment.8 [41.3 to verify the program. [5] [Table P3..96) by utilizing the relationship E = eh". Use these data to present the vapor pressure of lithium chloride in the following tables: (a) From 800°C to 1350°C at 50°C increment.) refers to the pH where zetapotential is zero. Use the experimental data of Table 3. and 190.7 With the set of unequally spaced data points in Table P3.9 The zetapotential of particles in a suspension is an indication of the sign and the density of the surface charge of the particles.p.7 3.97) directly from the solution of the differential equation (3. 50.).10 Obtain the solution of the difference equation (3. Table P3.
1998. Delhi. Z. 132. M. Rashchi.74 5. 159. 1961. Hindustan Publishing Corp." Colloids and Su$aces A: Physicochemical and Engineering Aspects... 1. U. Englewood Cliffs. NJ. vol. 1997. Ordinary Differential and Difference Equations. as a function of pH 195 pH zetapotential (mV) pH zetapotential (mV) 1. Xu. G. O. and Maloney.. Prentice Hall. 1965. Gel'fond. Systems. and Finch. Salvadori. London.9 Zetapotential of silica in the presence of IO'M Pb(NO.. Van Nostrand.and CaSO. W.References Table P3. "Adsorption of SilicainPb.)..00 33. Calculus of Finite Differences.. and Baron. 3.K.CO. India.. . L. 5. M. 7th ed.2 REFERENCES 1. D. Numerical Methods in Engineering. O. 1971.. Chorlton.. A. of the third Russian edition.. Green. McGrawHill. New York. F. A. p. F.. 2.. 4.3 6. J. Perry's Chemical Enxineers' Handhook. English trans.
C H A P T E R 4 Numerical Differentiation and Integration T h e solution of many engineering problems requires calculationof the derivative of afunction at a knownpoint or integration of the derivative over a known range of the independent variable. 1. which needs calculation of the derivative of the function in each iteration (see Sec. The simplest example of such problems is rootfinding by the NewtonRaphson method. In the following examples. it is more convenient to obtain it numerically if the function is complicated andlor the calculation is done by a computer program. there is no algebraic expression for the experimental data. 197 . in some cases. the analytical derivative of the function may he derived. therefore. numerical differentiation or integration is inevitable. Although.6). or analytical integration does not exist for the function.
solids velocity fluctuation is calculated by . a radioactive tracer is being followed for several time intervals. In this technique. x. A is the exposed surface area for drying. the initial reaction rate can be calculated from differentiation of concentration with respect to time at the beginning of the reaction: If the reaction rate could be expressed by then taking the logarithm of both sides of this equation at t = 0 results in (4.is the velocity of the tracer in direction i. The steadystate velocity profile of solid particles in the reactor is calculated by averaging the instantaneous velocities in small compartments inside the reactor.is the ith component of the coordinate of the tracer.198 Numerical Differentiation and Integration Chapter 4 In chemical reaction kinetics. Ekperimental determination of the rate of drying of a given material can be done by placing the moist material in a tray that is exposed to the drying air stream. Once the velocity profile is determined. which is being recorded at different time intervals. the velocity profiles of solids may technique be determined experimentally by Radioactive Particle Tracking (RPT) velocimet~y [I]. v. In this method. the reaction staas with different initial concentrations of the reactant A. In the study of hydrodynamics of multiphasereactors. and dt is the time increment used at the time of data acquisition. and changes in the concentration of A with time are measured. For each initial concentration. Instantaneous velocity of the tracer can be calculated then from dx. one of the methods for determination of the order of a chemical reaction is the method of initial rates. and coordinates of this tracer are evaluated at each time interval.3) The reaction order can he obtained by calculation of the slope of the line in( rAo) versus ln(CAJ. W is the mass of the moist material. during drying.= 2 I dt where V. A balance indicates the weight of the moist material. The drying rate is calculated for each point by A0 In(r ) = Ink + nlnCAo where R is the drying rate. and t is time.
4. AH. The integral in Eq. enthalpies betweenH. and is enthalpy of moist air at saturation. T is the temperature of the reactor where the conversion is X .1 Introduction 199 where V. k. E.) may he obtained from the Lagrangian autocorrelation integral of velocity fluctuations: The height of a cooling tower is calculated from the following equation: where z is height of tower. M is molecular weight of air. K. For example. at the outlet. Having the above information. may be read from the psychrometric chart. taking place in an adiabatic plug flow reactor.. consider the first order reaction A B in liquid phase. is a reference temperature. and H. Calculation of the volume of a nonisothermal chemical reactor usually needs the use of numerical integration. and it is desired to have the conversion X.8) should be calculated from H. at its outlet. (4. at inlet of the tower to H. CPA and CPB are heat capacities of A and B. and T . PureA enters the reactor. R is the ideal gas constant. For an adiabatic plug flow reactor. His enthalpy of moist air.> is the average velocity (a function of position). We must relate X and Tthrough the energy balance to carry out this integration. is the heat of the reaction. In order to calculate this integral. The volume of this reactor is given by  where Vis the volume of the reactor. (4. P i s pressure.9). v. is the activation energy of the reaction.a is overall mass transfer coefficient. and To is a reference temperature. T is given by In this equation. is the inlet volumetric flow rate of A. assuming constant heat capacities for both A and B. G is dry air mass flow.' is velocity fluctuation (a function of time) and <V. turbulent eddy diffusivity of solids (D. both in idirection. one has to divide the . respectively. In order to calculate the volume of the reactor from Eq. is the rate constant at the temperature T.
10) the temperature in each increment can be evaluated.64.1 FirstOrder Derivative in Terms of Backward Finite Differences with Error of Order h Rearrange Eq. these formulas may have any degree of accuracy desired. the function in the denominator of the integral in Eq. after rearrangement.10. This concept will he demonstrated in the remainder of this section. 4.24.11). (4. enable us to develop a variety of formulas expressing derivatives of functions in terms of backward finite differences. X] into small AXs first. is given as In this chapter we deal with numerical differentiation in Secs. (4. the volume of the reactor can be calculated from Eq. Knowing both X and T. Finally. (4. using a numerical technique for integration. In addition to calculating definite integrals.9) is calculated.9).1. 4. and vice versa. In addition.32) to solve for the differential operator D: Apply this operator to the function y at i: .5 and integration in Secs. The relationships between backward difference operators and differential operators.2. numerical integration can also be used to solve simple differential equations of the form Solution to the differential equation (4. and from Eq. 4. which are summarized in Table 3. (3.200 Numerical Differentiation and Integration Chapter 4 interval [0. provided that a sufficient number of terms is retained in the manipulation of these infinite series.
It is obvious then.4. O(h"). formulas with higherorder error terms have larger truncation errors and are lessaccurate approximations of derivatives.2 SecondOrder Derivative in Terms of Backward Finite Differences with Error of Order h Rearrange Eq. retaining only the first term. It should be emphasized that for h < 1. i. On the other hand.16).0 and the function is smooth and continuous. (4. have smaller truncation errors. they are more accurate approximations of derivatives.2 Differentiation by Backward Finite Differences 201 Truncate the series..therefore.0: Therefore.0.2. 5 and 6.e. The term O(h) is used to represent the order of the first term in the truncated portion of the series. 4. formulas with higherorder error term. enables us to evaluate the firstorder derivative ofy at position i in rerms of backward finite differences. when h < 1.36) to solve for D2: Apply this operator to the function y at i: . the first term in the truncated portion of the series is the predominant term. (3.0: Therefore. This concept will be discussed in detail in Chaps. When h < 1. when h > 1. and show the order of the truncation error: Express the differential and backward operators in terms of their respective definitions: Eq. that the choice of step size h is very important in determining the accuracy and stability of numerical integration and differentiation.
36) to solve for hZD2: h2D2 = v2 + 7 h 3 D 3 . and apply this operator to the function y at i: Truncate the series.3 FirstOrder Derivative in Terms of Backward Finite Differences with Error of Order h2 Rearrange Eq.202 Numerical Differentiation and Integration Chapter 4 Truncate the series. (3. retaining only the first term. (3.32) to solve for hD: Rearrange Eq.h4D4 12 i Combine these two equations to eliminate h2D2: Divide through by h. with error of order h. and express the operators in terms of their respective definitions: . retaining only the first two terms. 4. in terms of backward finite differences. and express the operators in terms of their respective definition: This equation evaluates the secondorder derivative of y at position i.2.
2 Differentiation by Backward Finite Differences 203 In this section. and express the operators in terms of their respective definitions: . Eq. For the case where h < 1.37) to solve for h"': h3D3 = + .4.24) is a more accurate approximation of the first derivative than Eq. = 1 v2yi hZ + 1 3y. (4. the first derivative of y is obtained with error of order h2. Combine these two equations to eliminate h ' ~ ' : = 11 V2 + V3 + h4D4 12  . v3 + 3 h4D4 2  5 h5D5 + 4 . 4. (3.36) to solve for h ' ~ ~ : 7 h 2 D 2 = V2 + h 3 D 3 . + 11 V h2~4yi h2 12  .0. (3. Truncate the series... a larger number of terms is involved in the calculation. . Divide through by h2 and apply the operator to the function y at i: D 2y..4 SecondOrder Derivative in Terms of Backward Finite Differences with Error of Order h2 Rearrange Eq. To obtain the higher accuracy.16). (4.h4D4 12 Rearrange Eq.2.. however. retaining only the first two terms..
204 Numerical Differentiation and Integration Chapter 4 It should be noted that this same equation could have been derived using Eq. as shown in Chaps. Table 4.42) and an equation for V 4 (not shown here). It can be concluded from these examples that any derivative can be expressed in terms of finite differences with any degree of accuracy desired. The formulas for the first. are summarized in Table 4. these same formulas may he used in the numerical integration of differential equations.and fourthorder derivative. These formulas may be used to differentiate the function y(x) given a set of values of this function at equally spaced intervals of x. (3. which can be solved utilizing both sets of equations shown in Table 3.1 Derivatives in terms of backward finite differences Error of order h Error of order h2 . together with those of the third.and secondorder derivatives.1.1. 5 and 6. developed in the preceding four sections. Conversely. such as a set of experiment data. This statement applies to all these examples.
4.3 Differentiation by Forward Finite Differences
205
The relationships between forward difference operators and differential operators, which are summarized in Table 3.2, enable us to develop a variety of formulas expressing derivatives of functions in terms of forward finite differences and vice versa. As was demonstrated in Sec. 4.2, these formulas may have any degree of accuracy desired, provided that a sufficient number of terms are retained in the manipulation of these infinite series. A set of expressions, parallel to those of Sec. 4.2, will he derived using the forward finite differences.
4.3.1 FirstOrder Derivative in Terms of Forward Finite Differences with Error of Order h
Rearrange Eq. (3.53) to solve for the differential operator D:
Apply this operator to the function y at i:
Truncate the series, retaining only the first term: Dyi
=
1 Ayi h
+
O(h)
(4.32)
Express the differential and forward operators in terms of their respective definitions:
Eq. (4.33) enables us to evaluate the firstorder derivative of y at position iin terms of forward finite differences with error of order h.
206
Numerical Differentiation and Integration
Chapter 4
4.3.2 SecondOrder Derivative in Terms of Forward Finite Differences with Error of Order h
Rearrange Eq. (3.57) to solve for DZ:
Apply this operator to the function y at i: D2yi = AZyi hZ
1

hD3yl

7 h2D4yi 12

.. ,
(4.35)
Truncate the series, retaining only the first term, and express the operators in terms of their respective definitions:
This equation evaluates the secondorder derivative of y at position i, in terms of forward finite differences, with error of order h.
4.3.3 FirstOrder Derivative in Terms of Forward Finite Differences with Error of Order h 2
Rearrange Eq. (3.53) to solve for hD:
Rearranging Eq. (3.57) to solve for h2D2: h2~2 =
A2
_ h3~3 _
l h 4 D 4
12
4.3 Differentiationby Forward Finite Differences
207
Combine these two equations to eliminate h2DZ:
Divide through by h, and apply this operator to the function y at i:
Tmncate the series, retaining only the first two terms, and express the operators in terms of their respective definitions:
4.3.4 SecondOrder Derivative in Terms of Forward Finite Differenceswith Error of Order h2
Rearrange Eq. (3.57) to solve for h2D2:
Rearrange Eq. (3.58) to solve for h3@:
h3D3 = A '  3 h 4 D 4  5h 5 D 5 2 4
Combine these two equations to eliminate h3D3:
.
which are summarized in Table 3.78) to solve for D: . From a comparison between Tables 4. we conclude that derivatives can be expressed in their backwardor forward differences. The relationships between central difference operators and differential operators. and express the operators in terms of their respective definitions: The formulas developed in these sections for the first.2. 4. 5 and 6. This is a rule of thumb that applies to all such combinations of finite differences. This will be discussed further in Chaps.1 FirstOrder Derivative in Terms of Central Finite Differences with Error of Order h2 Rearrange Eq. will be used in the following sections to develop a set of formulas expressing the derivatives in terms of central finite differences. (3.1 and 4. with formulas that are very similar to each other in the number of terms involved and in the order of truncation error.3. It should be pointed out that all the finite difference approximations of derivatives obtained in this section and the previous section have coefficients that add up to zero. together with those of the third.208 Numerical Differentiation and Integration Chapter 4 Divide through by h2 and apply the operator to the function y at i: Truncate the series.and secondorder derivatives are summarized in Table 4.4. These formulas will have higher accuracy than those developed in the previous two sections using backward and forward finite differences. The choice between using forward or backward differences will depend on the geometry of the problem and its boundary conditions.2.and fourthorder derivatives. retaining only the first two terms.
h4~Syi 1 ~ 6 ~. Zh' + + 14~.(2yz+.5?.) + + O(h2)  d4Y. h 1 + O(hZ) .. 3 ~ ~ O () h2) + + Apply this operator to the function y at i: Dy. d3?. h 6 120 = Truncate the series. = p6y. dx3 I (3yG+. . + Z~Y.2 Derivatives in terms of forward finite differences Error of order h 209 Error of order h2 .4 Differentiation by Central Finite Differences Table 4. retaining only the first term: Dy.+% ~ X Y . .~I ~ Y .~ 24y. 1 dx4 hh" I ~ Y . .+ . 24).+ . ~. h2~'y. ~ .4.. .
Comparing this equation with Eq.210 Numerical Differentiation and Integration Chapter 4 Express the differential and averaged central difference operators in terms of their respective definitions: Eq.50) enables us to evaluate the firstorder derivative of y at position i in terms of central finite differences. (4. h2 + O(h2) (4.2 SecondOrder Derivative in Terms of Central Finite Differences with Error of Order h2 Rearrange Eq.78) to solve for hD: .53) Express the differential and central difference operatorsin terms of theirrespective definitions: 4.4. (3.4. (4. retaining only the first term: D 2 yi = 1 6'~. (4. for the same number of terms retained.81) to solve for D2: Apply this operator to the function y at i: Truncate the series. (3.3 FirstOrder Derivative in Terms of Central Finite Differences with Error of Order h4 Rearrange Eq. 4.33) reveals that use of central differences increases the accuracy of the formulas.16) and Eq.
82) to solve for h3D3: Combine these two equations to eliminate h 3 ~ ) : Divide through by h and apply this operator to the function y at i: Truncate the series. and express the operators in terms of their respective definitions: 4.83) to solve for h4D4: .4. (3. retaining only the first two terms.4 SecondOrder Derivative in Terms of Central Finite Differences with Error of Order h4 Rearrange Eq. (3. (3.4 Differentiation by Central Finite Differences 21 1 Rearrange Eq.4.12 360 Rearrange Eq.81) to solve for h2D2: h 4 ~4h'~' h2~2 = 62 .
retaining only the first two terms. The unsteadystate concentration profile for this problem. assuming no air flow at the top of the vessel. Example 4.and secondorder derivatives are summarized in Table 4.and fourthorder derivatives.4.212 Numerical Differentiation and Integration Chapter 4 Combine these two equations to eliminate h4D4: ' and apply this operator to function y at i: Divide through by h Truncate the series. Development of formulas with higher accuracy and for the higherorder derivatives are left as exercises for the reader (see Problems). Apply this function to evaluate the unsteadystate flux of water evaporated into air at 1 atm and 25°C from the top of an open vessel. Consider the distance between water level and the open top of the vessel to be 0. 4. along with those for the third. 0. is the flux of water vapor at level z.3. is obtained from . D is the diffusion coefficient of water vapor in air.14. and express the operators in terms of their respective definitions: The formulas derived in Sec..4 for the first. and x . x. [2]: where N .2.1.. The flux of water vapor at a level z above the level of water is given by Bird et al.4. and 0. x i s the mole fraction of water vapor. Develop a MATLAB function for numerical differentiation of a function f (x)over the range [x.1: Mass Transfer Flux from an Open Vessel. c i s the total concentration of the gas phase.3 m.] with truncation error of order h2. indicates the value of x at z = 0.
For the airwater system. = I 1 + [&(I + efcp)cpexpcp2]' Table 4.3 Derivatives in terms of central finite differences Error of order h 2 Error of order h4 .Example 4.1 Mass Transfer Flux from an Open Vessel 213 where where t is time and cp is obtained from the solution of the following nonlinear equation: x.
The program uses . The first part of the program is initialization. and then it switches to the proper section according to the order of differentiation. If the method of finite difference is not determined by the user.214 Numerical Differentiation and Integration Chapter 4 Method of Solution: In order to calculate the flux of water vapor. The user may specify the value of 1.1.14. its derivatives at the water surface level ( z = 0) and any desired level above the surface of water can be evaluated. the equation for derivation with truncation error of order h2 will be employed. (4). Any additional argument will be canied directly to the mfile that represents the function and may contain parameters (such as constants) needed for the function. eps.3. According to the chosen method of finite difference. The independent variable of this function z and other variables necessary to evaluate the function are entered as parameters. The function Ex41grofrle. first we have to determine the value of q from the solution of the nonlinear Eq. Introducing these two inputs to the function is optional. respectively. named Example4l. The program then switches to a different part of the program. Having the concentration profile. or forward finite difference. It calculates and plots the unsteady % flux of water vapor from the open top of a vessel. (1).m is the nonlinear function from which the value of rp is calculated [Eq. The fourth and fifth input arguments are optional andrepresent the increment of the independent variable and the method of finite differences. If the assigned value for h is smaller than the floating point relative accuracy. does the necessaly calculations and plots the results. ( 4 ) ] . (2) and (3)l.001 if this minimum is zero. The default value of h is 11100 of the minimum value of the independent variable or 0. the mass transfer flux is calculated from Eq.m evaluates the derivative of a function. (2) and (3). The main program. To solve the problem posed in this example. Program Description: The MATLAB function fder. Finally. or + I for method of finite difference if it is required to evaluate the derivative based on backward. Program Example41. The third input argument may be a scalar or a vector at which the derivative is to be evaluated.m % Exampled1.m. The defaultmethod is the central difference. Differentiation of the function y =f(x) is done based on equations shown in Tables 4. the function assumes the relative accuracy as the differentiation increment. central. according to the choice of method of finite difference. which has to be less than or equal to 4.m represents the concentration profile of this problem [Eqs. The function Ex41ghi. where inputs to the function are examined and default values for differentiation increment ( h )and method of finite difference are applied. if required.m % This program solves Example 4.O. central finite difference will be used for differentiation. three more MATLAB programs are written. The first input argument should be a string variable giving the name of the mfile that contains the function whose derivative is to be evaluated. The second input argument is the order of derivation. The concentration of water vapor is then obtained from Eqs. respectively.
D. z = D = T = P = t = input(' Vector of time (s) = ' ) . profile = input(' Name of the mfile containing the concentration profile = ' 1 . % Solving the nonlinear equation for phi phi = fzero(phifile.0.phi).: ) = fder(profile.l.le6.:) = c*~*dxdz(k. 0 = 0.l.2) .) 0 ylabel('Mo1e fraction of the Vapor') legend('zl'.l.'z2'. for k = 1 : length(z) % concentration gradient dxdz(k. % Concentration gradient at z = z 0 dxdzO = fder(profile.Example 4.phi): % Molar flux ~z(k. % Gas constant c = P/(R*T).t.*dxdzO.phi).:)x(k. input(' Diffusion coefficient of the vapor in air (m2/s) = ' 1 . [ I . Psat = input(' Vapor pressure at the system temperature (Pa) = I ) .Nz*3600*18*1OOO) xlabel ( ' t (min.t. [ I . ' 1 ylabel('Nz (gr/m2. 273.: ) = feval(profile.xO.z(k).'z2'.D.hr) ' ) legend('zpl'.1 Mass Transfer Flux from an Open Vessel % the function 215 FDER to obtain the concentration gradient. clear c1c clf % Input data 7 .:)*c*D/(lxO). % Gas concentration phifile = input(' Name of the mfile containing the equation for phi = I ) .xO). XO = ~sat/P. [ I .t. input(' System pressure (Pa) = ' ) . % Mole fraction R = 8314.x) xlabel('t (min.'z3'.zfl.'z3'.z(k).xO.1) figure(2) plot(t/60.xO. input(' Vector of axial positions (m) = ' ) .15 + input(' System Temperature (deg C) = ' ) .D. end % Plotting the results figure (1) plot(t/6fl. % Male fraction profile x(k. [I .
% % % % % % % % % % % See also DERIV % (c) N.1./sqrt(4*D*t). X = (1erf (Zphi) 1 / (l+erf(phi) ) .m function phif = Ex4_lqhi(x .H. varargin) %FDER Evaluates nthorder derivative (n<=4) of a function % with truncation error of the order hA2.m function df = fder(fnctn. fder. METHOD is the finite difference method used Use METHOD = 1 for backward finite difference Use METHOD = 0 for central finite difference Use METHOD = 1 for forward finite difference PARRMETER is a scalar or a vector of parameters that are passed to the function F.m % Nonlinear equation for calculation of phi in Example 4.N.PARAMETER) evaluates Nth order derivative of the function using H as increment of X used in differentiation.N.D.m % Concentration profile evaluation in Example 4.001: end if h < eps . Mostoufi & A . h = min(abs (x) )/loo. Pass an empty matrix for H or METHOD to use the default values. xO) % Function Ex41shi. X may be a scalar or a vector. if h == 0 h = 0.METHOD. FDER('F1.M at X. Constantinides % January 1.X) evaluates Nth order derivative of the function described by the Mfile F.X. %Dimensionless axial position % Dimensionless concentration % Mole fraction Ex42hi.M.phi) % Function Ex41qrofile.t.xO. method. order. h.216 Numerical Differentiation and Integration Chapter 4 Ex41grofile.m functlon x = Ex4lqrofile(z.3 Z = z. x = xO*X. x. 1999 % Initialization if nargin == 3 1 isempty(h) method = 0. phif=l/(l+l/(sqrt(pi)*(l+erf (x) )*x*exp(xA2)) )xo. % % % % % % FDER('F1.
varargini:]). if nargini5 1 isemptyimethod) method = 0. varargini:)). x3*h . varargin{:ll. varargin{:]). varargini:)). df = (S*yi18*yim1+24*yim214*yim3+3*yim4). case 2 % 2nd order derivative yi = fevallfnctn . switch order case 1 % 1st order derivative df = iyiplyiml)/ (2*h) . x . df = (3*yi14*yim1+26*yim224*yim3+11*yim42*yim5)/hA4.1 Mass Transfer Flux from an Open Vessel h end end = 217 eps. x3*h . x+h . x . varargini:)). varargin{:l). x+2*h . yim4 = feval(fnctn . case 4 % 4th order derivative yim3 = feval(fnctn . x3*h . df = (yipl2*yi+yimll/hA2. yim2 = feval(fnctn . df = (2*yi5*yiml+4*yim2yim3)/h^2. df = (3*yi4*yiml+yim2) case 2 % 2nd order derivative yim3 = fevallfnctn . x4*h . yip1 = feval(fnctn . yiml = feval(fnctn . end if (order < 1) 1 (order > 4) error('1nvalid input. df = (yip22*yipl+2*yimlyim2)/(2*h^3): case 4 % 4th order derivative yim3 = fevalifnctn . varargini:)).') break end 1 (h <= 0) switch method % Backward finite difference case 1 yi = feval(fnctn . x2*h . varargini:)). varargini:)). end case 0 % Central finite difference yiml = fevalifnctn . x3*h . varargini:)]. varargin{:)). x5*h . varargin{:l). yim4 = feval(fnctn . case 3 % 3rd order derivative yim2 = feval(fnctn . varargin{:)). yip2 = feval(fnctn . yim5 = feval(fnctn . xh . varargini:)). case 3 % 3rd order derivative yim3 = feval(fnctn . x4*h . x2*h . .Example 4. xh . switch order case 1 % 1st order derivative / (2*h) .
E4. end end .m.la shows the unsteady diffusive mass transfer flux from the open top of the vessel. df = (yip3+12*yip239*yipl+56*yi399yim1+12*yim2yim3]l(6*h~4] end % Forward finite difference case 1 yi = feval(fnctn . yip2 = feval(fnctn . E4. varargini:)). varargin{:)). . results far three difierent levels chosen here are illustrated by different signs in Figs. yip4 = feval(fnctn . varargini:)).2e5 System Temperature (deg C ) = 25 System pressure (Pa) = 101325 Vapor pressure at the system temperature (Pa) = 3161 Name of the mfile containing the equation for phi = 'Ex4Llqhi Name of the mfile containing the concentration profile = 'Ex4lgrofile' Discussion of Results: Fig. x+h . 0. solution results will be shown on the screen by solid lines of different color. x+3*h varargini:)). x . switch order % 1st order derivative case 1 df = (yip2+4*yip13*yi) / (2*h) . 0. . x+3*h . varargini:)).la and b in order to be discriminated.2. df = (3*yip4+14*yip324*yip2+18*yipl5*yi)/(2*h~3). x2*h .1. varargini:)). varargini:)).' The concentration profiles with respect to time are also plotted in ' When running Example4_l. yip3 = feval(fnctn . x varargini:)). yip2 = feval(fnctn . yip5 = feval(fnctn . varargini:)). df = (yip3+4*yip25*yipl+Z*yi)/h% case 3 % 3rd order derivative yip3 = feval(fnctn . x+3*h . x+3*h . varargini:)): yip1 = feval(fnctn . case 2 % 2nd order derivative yip3 = feval(fnctn . x+2*h . Howevcr. yip4 = feval(fnctn . x+4*h . case 4 % 4th order derivative yip3 = feval(fnctn . x+2*h .31 Diffusion coefficient of the vapor in air (m2/s) = 2. yi = feval(fnctn . x+4*h . varargini:)). df = (2*yip5+ll*yip424*yip3+26*yip214*yipl+3*yi]lh~4.218 Numerical Differentiationand Integration Chapter 4 yim2 = feval(fnctn . x+5*h . Input and Results Vector of time ( s ) = eps:3600 Vector of axial positions (m) = [0. varargini:)). varargini:)).
) Figure E4.Example 4.1 Mass Transfer Flux from an Open Vessel 219 . Figure E4.lb Mole fraction versus time. .1a Flux versus time. t (rn. .".
2. l b .001. that is: . Assume fully developed solids flow in the riser and neglect wall shear and solids stress. forward.14.2: Derivative of Vectors of Equally Spaced Points. The densities of gas and solids phases are 1.respectively.3 are used to differentiate the columns of the matrix y with the desired order of truncation error. In the programExample4l. Differentiation is done based on equally spaced segments of the independent variable. Fig. defaults are usedfor choice of method of finite difference and differentiation increment. there is dry air above the water level. l ~ shows that the mass flux rises at the beginning and reaches a maximum. However. D . This behavior is due to the fact that at the beginning of the process. Write a general MATLAB function to calculate first. Example 4.to fourthorder derivatives of a series of data presented numerically in a matrix whose columns represent vectors of dependent variable. and mass transfer is taking place faster because of high driving force. or central differentiation as well as the order of the truncation error.m. The flux of water vapor then decreases constantly. which is the concentration gradient. the mass transfer rate decreases. we find that pressure drop in the abovementioned conditions is balanced by the weight of the bed. E 4 . The defaults are central finite difference and h = 0.2 Axial position (m) Pressure (kPa g) Method of Solution: The equations in Tables 4. Apply this function to calculate the solids volume fraction in a riser of a benchscale gassolid fluidized bed whose axial pressure profile is given in Table D. Vapor concentration decreases with increasing height. after enough time has passed and the water vapor concentration moves closer to saturation and the driving force decreases. Table E4.220 Numerical Differentiation and Integration Chapter 4 Fig.2 kg/m3 and 2650 kg/m3. The user should be able to choose between backward. respectively. Writing the momentum balance equation for the twophase flow. The reader is encouraged to repeat the calculation using the other methods of finite differences and with different increments.
Program Description: The MATLAB function deriv. Program Example4_2. and p.)g The solids volume fraction profile can be calculated from Eq. E . respectively. where the function evaluates the first two elements of the vector of derivatives by forward differences and the last two elements of the vector of derivatives by backward differences. The third one is the order of derivative. or I as the fourth input argument results in calculation of the derivative based on backward. it is important to note that. At the end. If onlyy is given as input to the function. The first part of the program is initialization in which the values of h.m % Example42. order of derivative. or forward finite differences.2 Derivative of Vectors of Equally Spaced Points 221 where P is the pressure.m asks the reader to input the data from the keyboard. the last element of the returned derivative vector is calculated by backward differences.Example 4. It then applies the function deriv. (2). The second input argument is the increment of the independent variable.m is written to calculate first. However.m to evaluate the pressure gradient and calculates the solids volume fraction from Eq. in firstorder differentiation with the forward finite difference method with truncation error O(h).p. with tmncation error of the order O(h2). some elements at one or both ends of the derivative vector are evaluated by a different method of differentiation.m has the same number of elements as the vector of input data itself. central. and g is the gravitational acceleration.m % This program solves Example 4.p.: ( F d P l d z ) . the program calculates the central finite differences of y as the output. (2) once the pressure gradient is extracted from the data tabulated in Table E4. p. the program plots the result of the above calculations. For example.O. depending on the method of finite difference used.g €$ = (2) (P. Another example is the calculation of the secondorder derivative of a vector by the central finite difference method with truncation error 0(h2). are the densities of gas and solids. is the volume fraction of the solids. method of finite difference used.to fourthorder derivatives of a matrix of input data. The main program Example42. respectively. It calculates and plots . The fifth argument is the value of the order of truncation error (1 or 2 for backward and forward differences and 2 or 4 for central differences).2. . A value of 1. The reader should pay special attention to the fact that when the function calculates the derivative by the central finite difference method with the truncation error of the order O(h4). The derivative matrix returned by the functionderiv. Eq. (1) can be solved for E.2. the starting and ending rows of derivative values are calculated by forward and backward finite differences. z is the axial position. and order of truncation error are assigned if not entered as input to function.
order. ) .. . clear % Input data P = input(' Vector of pressures (kPa) = ')*le3: dz = input(' Axial distance between pressure probes (m) = ' 1 . % Pressure gradient dP = deriv(P. .lOO*epsilons) xlabel ( 'a (m) ') ylabel('So1ids volume fraction ( t i ) ' ) denv. ERR may be 1 or 2 for backward and forward finite difference and 2 or 4 for central finite difference. epsilons = (dPO*rhogeg) fprintf('\nAverage solids concentration = %4. % The program uses the function DERIV to obtain the pressure % gradient.m function dy = deriv(y.ORDER. .dz). DERIV(Y.ERR) of columns of the matrix Y where H is the independent variable interval ORDER is the order of differentiation (up to 4th order) METHOD is the finite difference method used Use METHOD = 1 for backward finite difference Use METHOD = 0 for central finite difference Use METHOD = 1 for forward finite difference ERR is the order of error of calculation. returns the derivative DY = DERIV(Y.METHOD. rhos = input(' Density of solids (kg/rn3) = ' 1 . method. rhog = input(' Density of the gas (kg/m?) = . plot(z. lOO*mean(epsilons)) % Plotting the results z = [O:length(P)11 *dz. g = 9. h.2f%% in'.H) calculated the firstorder derivative of Y by central finite differences using H as the independent variable interval.H.81.222 Numerical Differentiation and Integration Chapter 4 % the volume fraction profile in a gassolid fluidized bed. % Solids concentration / (g*(rhosrhog)). err) %DERIVDifferentiates a matrix of data numerically % DERIV(Y) calculates the central differences of each vector of matrix Y.
DIFF % (c) N. end if nargin == 4 if method == 0 err = 2. end if nargin < 3 order = 1. end 1 isempty(order) if nargin < 4 1 isempty(method1 method = 0. % Differentiation switch method % Backward finite differences case 1 switch err case 1 % Olh) switch order .2 Derivative of Vectors of Equally Spaced Points % See also FDER. c1 = size(y1. c). warning(' Order of truncation error is set to 1 . % Make it a column vector r = c. warning(' Order of truncation error is set to 2. 1 9 9 9 % Initialization if nargin == 1 1 isemptylh) h = 1. err = 2. end end if absimethod) == 1 & (err < 1 1 err > 2) err = 1. Constantinides % January 1.') end [r . if r == 1 % If y is a row vector y = y'. ' ) end if method ==0 & (err = 2 1 err = 4 ) err = 2. % Number of points d y = zeros(r . Mostoufi & A.Example 4. end n = r. else err = 1. c = 1.
: ) = l3*y(n . : ) . 1 + y(l:4 .24*y(3:n3 . :l=lyl5:n . case 4 % 4th order derivative dyl6:n . : ) . : ) .4*y(2:5 . :) . :)=(y(5:8 .2*y(l:n5 . : ) . . :)+2*y(l . . :))/(2*h). : I )/(2*h). :)=(y(3:4 . :)+4*y(3 . : ) + y(n2 . . end end case 0 I. :))/(2*hA3). :)=(3*yl6:n . . : ) .3*y(3:5 .y l : n 2 . . . : ) + 3*y(l:5 .y(1:n3 .y(1:n3 . dy(n .4*y(4:n1 . dy(l:5 . :)=l3*y(3:n . . :)+ll*y(5:9 . . : ) + 3*y(2:4 . :))/h. : ) + 14*y(4:7 . :))/hA4. case 2 8 2nd order derivative dy(4:n . . : l . : .4*y(2:n1 . : ) = (yl3 . : ) + 6*y(3:6 . . case 2 % 2nd order derivative dy(1 . . :))/hA3. :l=(2*yI6:10. : ) . :)5*y(3:n1. : ) + 3*y(2:n2 .224 Numerical Differentiation and Integration Chapter 4 case 1 % 1st order derivative dy(2:n .24*y(4:8 . . : l + 3*yll:n4 . . . :))/hA2. Central finite differences switch err case 2 % O(h"2) switch order case 1 % 1st order derivative dyll . .2*y(2:n1 . : ) . :))/h"3. . : ) + 6*y(3:n2 . :))/(2*h). . .14*y(2:6 . :))/hA4. : ) . : ) + 4*y(2:n2 . :)=(y(4:6 . :)5*y(2 . : l .24*y(3:6 . . :))/hA2. + 2*y(l:3 . end % O(h2) case 2 switch order case 1 % 1st order derivative dy(3:n . :))/hA2. :) . :)=Iy(4 .y(l . : ) . dy(l:4 . 1 . :)+y(l:n2. case 3 % 3rd order derivative dy(4:n . :))/hA2.4*y(4:7 .5*y(l:4 . : . :)18*y(4:n1. dyll:2 . : . : 1 + ll*yl2:n4 . :114*y(5:n1. : ) = (y(2:n .y(1:n1 . .y(1:3 . : ) . . : . :))/(2*h). :)=(y(3:n . :12*y(2:n1. dy(l:4 . :)=Iy(3:4 . case 3 % 3rd order derivative dyl5:n .4*y(2:n3 . : 1 . dy(l:3 . . :)=(y(4:n . + 26*yl3:7 .3*y(l . dy(l:3 . :))/(2*hA3). dy(2:n1 . : ) + y(1:n4 . :)=I3*y(5:8 . . :))/hA4. .3*yl3:n1 . : ) . :)=IS*y(S:n . :))/hA2. 1 . : .4*y(n1 . :) + y(1:n2 . . : ) = (y(4:6 . :))/hA4. :1+26*y(4:n2 . : 1 + 4*y(2:3 .3*y(l:2 . :)=l2*y(4:n . case 2 % 2nd order derivative dyl3:n.2*y(2:3 . + 18*y(2:5 . :l)/hA2. . . : ) . . 1 . 1 = ( ~ ( 2 .14*yl2:n3 . :l=(y(3:n. dy(2:n1 . : ) . : ) + 4*y(2 . :)+24*y(3:n2. : ) .5*y(2:4 . :))/(2*h): dy(l:2 . . : ) + y(l:2 . : ) + 4*y(3:5 . :)+y(l:n2 . dy(1 . :))/h. : 1 = (y(3:n : . case 4 % 4th order derivative dy(5:n . : ) .
case 2 dy(l:2 . : ) . :I=(y(5:n. . . : ) + y(1:n6 . :)+8*y(6:n1 .2*y(n6:n5 :))/hA4. dy(3:n2 . : ) . . : . : ) . : ) . : ) ) / (2*h). :))/I2*hA3). : ) + 6*y(3:n2 . .. + 24*y(n3:n2.4*y(4:n1.2 Derivative of Vectors of Equally Spaced Points 225 :) dy(n : ) = 12*y(n . : ) + 26*y(3:5 . : ) . .24*y(3:5 . + 4*y(n3:n2 . + 2*y(l:2 . :)=(yl5:n. : ) .8*y(2:n3.5*y(1:2 .. : ) . : ) . : ) . .y(n3 . :))/hA2. + 3*y(n6:n4.24*y(4:5 . :)=ly(5:n. :)=(y(3:4. :))/hA4. : ) . . : I . + 26*y(3:4 .. .13*y(S:n2 . :))/hA4. +v(n3:n2 . : ) . . : ) . :))/I2*hA3). + 13*~(3:n4 : ) . ..14*y(n4:n3 . : ) = (5*y(n2:n . + ll*y(n5:n4 . .. . : I = (y(4:5. : ) .18*y(n3:n1 . : ) + 4*y(2:3 . dy(n2:n . : ) = (5*y(n1:n. : ) . . :)4*y(n2:n1. : ) + 11*y(5:7 : ) . dy(3:n2 .: ) . .y(n4:n3 . : ) + 3*y(l:3 . . . + 4*y(n2 . : . : .y(l:n4 . : ) . : ) (2*hA3): case 4 dy(l:3 . : ) . . . :)=(3*y(n1:n . :)=(2*y(6:7 .. : ) . :))/hA2: . . : ) . : ) . . :))/(8*hA3). . : ) .24*y(3:4 . :))/hA2. : ) . :))/(2*hA3). : = (2*y(nl:n. . : ) .. : ) . : ) . : ) = (3*y(n2:n . . : ) . : ) . . : ) . + 3*y(n5:n4 .14*y(2:4 . . : ) .5*y(n1 . . . : ) .24*y(4:6 .5*y(l:3 . : ) . . . . dy(n1:n . + 12*y(2:n5 . .3*y(l:2 . . :)30*y(3:n2. : ) . case 3 % 3rd order derivative dy(1:2 . : . : .39*y(3:n4 .y 1 : n . :))/hA4. dy(n1:n . + 16*y(2:n3 . :))/112*hA2). : ) . case 4 % 4th order derivative dy(l:2 .18*y(n2:n1 .24*y(n4:n3 . : . : ) . : ) = (3*y(n1:n. + y(1:n4 . .. . : ) + 14*y(4:6 .14*y(n3:n1.2*y(4:n1 . dy(n2:n . dy(n1:n .. dy(3:n2 .. + 24*y(n4:n2. .14*y(2:3 . : ) . : ) . : ) . . + 18*y(2:4 . :)+8*y(4:n1. : )+16*y(4:n1. : ) + 2*y(2:n3 . dy(4:n3 : ) = (y(7:n. . case 3 dyll:3 . + 18*y(2:3 . . .4*y(2:n3 . : . . :I=(3*y(5:6. + 26*y(n3:n2. .Example 4. :))/(2*hA3): dy(4:n3 :I=(y(7:n. end case 4 % O(hA4) switch order case 1 dy(1:Z .. . : ) + 12*y(6:n1 .8*y(2:n5 . : ) + 3*y(1:2 .5*y(2:3 . : ) + y(1:n4 . . . :)=I3*yl5:7. :))/(6*hA4). : ) + 4*y(3:4 . : ) .. : ) + 14*y(4:5 .14*y(n2:n1 . :I=(y(5:n . :))/(2*h). . : ) + 56*y(4:n3 : ) .y1l:n4 . :))/(12*h): d y n 1 : n . : I . .14*y(n5:n3 . .5*y(n2:n1 . dy(3:n2 . : = (2*y(6:8.39*y(5:n2 . : + ll*y(5:6 .
:))/hA2. + 3*y(n4:n2 .2*y(n2:n1 . . . : ) ) / (2*h) . : . : ) . case 4 8 4th order derivative dy(1:n4 . . . : ) . : ) . case 4 % 4th order derivative dy(1:n5 .24*y(4:n2 . : ) . : ) )/h. .14*y(n6:n3 . : ) .. .. . . dy(n3:n . . : ) . .2*y(2:n1 . : ) + 4*y(2:n1 . : ) . : + y(n7:n4 . . :))/hA2. : ) . : ) .14*y(n5:n1 . : ) + y(1:n4 . + 6*y(n5:n2 . : ) . : ) + 26*y(3:n3 : ) . dy(n2:n . : .. : ) .. : ) . + 3*y(2:n2 . . : ) + 14*y(4:n1 . : ) . : ) . : ) = (2*y(n2:n. : = (3*y(n4:n. + 4*y(n4:n2 .. dy(nl:n : ) = (3*y(nl:n .3*y(3:n1 . : ) . :))/(2*hA3).5*y(l:n4 .4*y(n2:n1 .. : ) = (y(2:n . : ) = (y(3:n. .4*y(n6:n3 . : ) = (2*y(6:n. + y(n3:n2 .y(1:n3 . : ) . : ) . :))/hA4. : ) . :I=(y(4:n. : ) = (y(nl:n . end end case 1 % Forward finite differences switch err case 1 % 0(h) switch order % 1st order derivative case 1 dy(1:n1 . . : ) .. : . : ) ..24*y(n5:n3 . : ) + ll*y(5:n1 . dy(n . :))/hA3.4*y(2:n3 .y(n1 . :))/hA4.. .y(1:n1 . dy(nl:n . :))/hA4. . . : ) + 6*y(3:n2 . : ) . . : . : . : ) . . :)5*y(2:n2. + y(n3:n2 .24*y(3:n2 .5*y(n3:n1 . + 3*y(n7:n4 . : ) ) / (2*h) . : = (y(n . case 2 % 2nd order derivative dy(1:n2 . :)=(y(5:n. : ) . . dy(n3:n .4*y(4:n1 . . : ) . : ) = (5*y(n3:n. .14*y(2:n4 . :))/h. . : ) = (3*y(5:n. :)+4*y(3:n1 . : ) .3*y(l:n2 . dy(n2:n . dy(n4:n .3*y(n3:n1 . . . . . . case 2 % 2nd order derivative dy(1:n3 .18*y(n4:n1.y(n5:n3 . : ) . : ) + 18*y(2:n3 .2*y(n7:n5 . + 3*y(l:n5 . case 3 % 3rd order derivative dy(1:n4 . :))/hA4. end case 2 % O(hA2) switch order case 1 % 1st order derivative d y l : n 2 . + ll*y(n6:n4 . . :))/hA3. + 24*y(n5:n2 . : ) . : ) ) lhA2. : ) . : ) . .226 + Numerical Differentiation and Integration Chapter 4 26*y(n4:n2 . : ) = (y(n2:n.4*y(n4:n1 . . . : ) ) / (2*hA3) . + 2*y(l:n3 . : ) . : ) = (y(n3:n . . :)=(y(3:n : ) .y(n5:n3 . . case 3 8 3rd order derivative dy(1:n3 . :))/hA2. : ) + y(1:n2 . : ) = (y(4:n .
2. end end end :) 227 :  .8 2 1 z (rn) Figure E4. .4 1. 0.181 Axial distance between pressure probes (mj = 0. The value of E.6 1.09.38. Input and Results Vector of pressures (kPa) = r1. : ) . :j)/hA4.80. It can be seen from this figure that the solids fraction does not change appreciably with height. . 1. E4.2 Solids volume fraction versus height. varies between 2.2 shows the results graphically.8 1 1.2 1.5 Density of the gas (Kg/m3j = 1.2 0.26% Discussion of Results: Fig.7% and 3.Example 4. 2*y(n9:n5 . + ll*y(n8:n4 .6 0 0. 0. 1.63.6 0.24*y(n7:n3 .2 Density of solids (Kg/m3) = 2650 Average solids concentration = 3.4 0.8% (approximately) with its mean value at 3.2 Derivative of Vectors of Equally Spaced Points + 26*y(n6:n2 . . This confirms the assumption made at the beginning that the measurements are done in the fully developed zone where the solids move with a constant velocity.26%.
and it is desired to evaluate the derivative of the function at a point (or points) between the tabulated values.228 Numerical Differentiation and Integration Chapter 4 In some situations.147).m is able to give the piecewise polynomial coefficients from which the derivative of the function can be evaluated. The reader can easily modify the MATLAB functionNatura1SPLINE. Note that if a natural spline interpolation is employed. its derivative at any point x in the interval [xi. tabulated function values are available. As mentioned before.66) .65). cubic splines have the advantage of continuity of the first derivative through all base points. (4. Eq. It is enough to replace the formula of the interpolation section with the differentiation formula. (3.xi] can be calculated from differentiating Eq. Among different interpolating polynomials. This operation is represented by Xn I = f(x)dx 1 x<> (4. instead of their algebraic expression. .2) in order to calculate at any point the first derivative of a function from a series of tabulated data. (3.. A good example of applying such a method can be found in Hanselman and Littlefield [3]. Also. A practical method in this situation is to interpolate the base points first and calculate the value of the derivative from differentiating the interpolating polynomial. the second derivatives for the first and the last intervals are equal to zero. the values of the second derivative at the base points should be calculated from Eq. (4. the MATLAB function spline.m (see Example 3.65). In the following sections we develop the integration formulas.143): Prior to calculating the derivative from Eq. By cubic spline interpolation of the function.m applies notaknot algorithm for calculating the polynomial coefficients. sp1ine.
this is a very impractical and inaccurate way of evaluating integrals. In the next two sections. the function f ( x ) is very complicated. 4 . Any portion of the area that is below the x axis is counted as negative area (Fig.1 Graphical representation of the integral. the integral in Eq. If the function f ( x ) is such that it can variable x. Under these circumstances. A more accurate and systematic way of evaluating integrals is to perform the integration numerically. in many cases. evaluated between the limits x = x be integrated analytically.4. such as experimental data. (4. one way of evaluating the integral is to plot the function graphically and then simply measure the area enclosed by the function. . we derive NewtonCotes integration formulas for equally spaced intervals and Gauss quadrature for unequally spaced points. or integrand. with respect to the independent . However. the numerical methods are not needed for this problem. (a) Positive area only (b)Positive and negative areas. to x = x . This operation is known as numerical quadrature. 4 . However.66) must be developed numerically. or the function is only a set of tabulated values of x and y. Therefore. l a . as shown in Fig. It is known from differential calculus that the integral of a function f ( x ) is equivalent to the area between the function and thexaxis enclosed within the limits of integration. l b ) . . Figure 4.6 Integration Formulas 229 which is the integral of the function y =f ( x ) .
. = x. we divide the total interval of integration [x. In the next step. each of width h. x. + h): Because this interpolation formula fits the function exactly at a finite number of points ( n + 1).. Eq. (3. the integral equation becomes .. This is tantamount to fitting polynomial through two points (x. yo) and (x.. We retain the first two terms of the GregoryNewton polynomial (up to.] into n segments.1 The Trapezoidal Rule In developing the first NewtonCotes formula. 4. (4. These are developed in the next three sections. 4. each of width h. each of width h. In each case.) (see Fig.. the first forward finite difference) and group together the rest of the terms of the polynomial into remainder term.122)]. This operation yields the wellknown NewtonCotes formulas of integration. a straight line between these points.67) equal to the number of segments of integration. and including.7. (4..2). the interval [x. by using Eq. Simpson's 113 rule and Simpson's 318 rule..230 Numerical Differentiation and Integration Chapter 4 This method is accomplished by first replacing the function y = f(x) with a polynomial approximation. respectively.] is being divided into several segments. x. In practice. The first three of the NewtonCotes formulas are also known by the names trapezoidal rule. we retain a number of finite differences in the finite series of Eq. such as the GregoryNewton forward interpolation formula [Eq. and the GregoryNewton forward interpolation formula becomes (note that x. we use one segment of width h and fit the y. Thus.67). (4.66) can be integrated. The upper limits of integration can be chosen to include an increasing set of segments of integration.
57) and (3. = y. . A3.y o .4. are replaced by their equivalent in terms of differential operators [Eqs. to obtain The remainder term is evaluated as follows: /R~(x)~x The forward difference operators.7 NewtonCotes Formulas of Integration 231 The first integral on the righthand side is integrated with respect to x and the first forward difference is replaced with its definition of Ay. . .2 Enlargement of segment showing the application of the trapezoidal rule. .58)1. . (3. and the remainder term becomes Figure 4. . A2.
Eq. In this case. (4. because D2f(S. The trapezoidal rule in the form of Eq. It was mentioned earlier that fitting a polynomial through only two points is equivalent to fitting a straight line through these points.) is essentially the formula for calculating the area of a trapezoid.68)must be applied over each of the n segment (with the appropriate limits of integration) to obtain the following series of equations: Addition of all these equations over the total interval gives the multiplesegment trapezoidal rule: "I yo + 2 x y i + y.) = 0.therefore. (4. 4. shown as the shaded area in Fig. Eq. This causes the shape of the integration segment to be a trapezoid. (4.2. because the term (h/2)& + y.232 Numerical Differentiation and Integration Chapter 4 The remainder series can he replaced by one term evaluated at t.. If the function f(x) is actually linear. i=l .73) gives the integral of only one integration segment of width h. then the trapezoidal rule calculates the integral exactly.. This is a term of order h3 and is abbreviated by O(h3). To obtain the total integral. The area between y = f ( x ) and the straight line represents the truncation error of the trapezoidal rule.69) can be written as This equation is known as the trapezoidal rule. the segment of integration is a trapezoid standing on its side. which causes the remainder term to vanish. Therefore.
which is . each ti being specific for that interval of integration. (x.3) and fit the polynomial through three points. the term containing h4 in the remainder has a zero coefficient. (4.). The integral equation becomes Integration of Eq..2 Simpson's 113 Rule In the derivation of the second NewtonCotes formula of integration we use two segments of width h (see Fig. and including. This is only an approximation because the remainder term includes the secondorder derivative of y evaluated at unknown values of 5 .4.. the repeated application of the trapezoidal rule over multiple segments has lowered the error term by approximately one order of magnitude. y. the error term has been shown as nO(h3).. Because n is inversely proportional to h: the error term for the multiplesegment trapezoidal rule becomes That is. (x. The final form of the second NewtonCotes formula. 4. 4. but its relative magnitude can be measured by the order of the term.7. thus giving this fortuitous result. y. However.7 NewtonCotes Formulas of Integration 233 For simplicity. We retain the first three terms of the GregoryNewton polynomial (up to. We would have expected to obtain an error term of ~ ( h "because three terms were retained in the GregoryNewton polynomial. and (x.79) and substitution of therelevant finite difference relations simplify this equation to The error term is of order hSand may be abbreviated by O(h5).). This is equivalent to fitting a parabola through these points. The absolute value of the error term cannot be calculated. the second forward finite difference) and group together the rest of the terms of the polynomial into the remainder term.. y.). A more rigorous analysis of the truncation error is given in the next chapter.
.3 Application of Simpson's 113 rule over two segments of integration. 4.82) sums up the oddsubscripted terms. The order of error of the multiplesegment Simpson's 113 rule was reduced by one order of magnitude to O(h4)for the same reason as in Sec. and the second summation adds up the evensubscripted terms. therefore the total interval must be subdivided into an even number of segments. gives the multiplesegment Simpson's 113 rule: Simpson's 113 rule fits pairs of segments. is This equation calculates the integral over two segments of integration. and summation of all formulas over the total interval. (4.7. Repeated application of Simpson's 113 rule over subsequent pairs of segments.1.234 Numerical Differentiation and Integration Chapter 4 better known as Simpson's 113 rule. Figure 4. Simpson's 113 rule is more accurate than the trapezoidal rule but requires additional arithmetic operations. The first summation term in Eq.
. (x. This. in fact.4 Application of Simpson's 318 rule over three segments of integration. yo).4) and fit the polynomial through four points. y..3 Simpson's 318 Rule In the derivation of the third NewtonCotes formula of integration we use three segments of width h (see Fig. (x. and (x. and including.). the third forward finite difference) and group together the rest of the terms of the polynomial into the remainder term.). . 4. We retain the first four terms of the GregotyNewton polynomial (up to. y.). y..7 NewtonCotes Formulas of Integration 235 4. is equivalent to fitting a cubic equation through the four points. (x. The integral equation becomes Figure 4.7..4.
is given by 1 8 rule is obtained by repeated application of Eq. with the latter being only slightly more accurate. because it achieves the same order of accuracy with three points rather than the four points required by the 318 rule.236 Numerical Differentiation and Integration Chapter 4 Integration of Eq. For this reason. The evaluation of the integral is performed: This results in a formula of the general form: h].4. (4.(x) polynomial to (n + 1) base points. The weights wi are where the xj are (n + 1) equally spaced base points in the interval [a. In the derivation of the NewtonCotes formulas. Simpson's 113 rule is usually preferred. the function y =f (x) is approximated by the GregolyNewton polynomial P.7. 4. The final form of this equation. The integral is exact. that is. .4 Summary of NewtonCotes lntegration The three NewtonCotes formulas of integration derived in the previous sections are summarized in Table 4.(x). which is better known as Simpson's 318 rule.83) and substitution of the relevant finite difference relations simplify the equation to The error term is of order h5and may be abbreviated by O(hS).83) The multiplesegment Simpson's 3 over triplets of segments and summation over the total internal of integration: Comparison of the error terms of Simpson's 113 rule and Simpson's 318 rule shows that they are both of the same order.(x) of degree n with remainder R. (4. determined by fitting the P.
7 NewtonCotes Formulas of Integration 237 for any function y = f(x) that is of polynomial form up to degree n.m. over the interval [a.m.m. because the derivative Pt is zero for polynomials of degree i n .y. the error term O[h"'2. a. bl by Simpson's 113 rule. a . The function quad8('file_name'.m from a to b using 8interval (9point) NewtonCotes formula.Dn*'fi~)] . There are three functions in MATLAB. trapz. b) evaluates the integral of the function introduced in the mfilefilename. thus. The function quad('file_name'. and quad8. that numerically evaluate the integral of a vector or a function using different NewtonCotes formulas: The function trapz(x. + O[h"'2.4. 9uad. y ) calculates the integral of y (vector of function values) with respect to x (vector of variables) using the trapezoidal rule.4 Summary of the NewtonCotes numerical integration formulas Simpson's 113 rule Simpson's 318 rule In General quadrature formula Sydx X" = 20 w. if(<) Table 4. P t j f ( 4 ) ] vanishes.m. h) evaluates the integral of the function represented in the rnfilefile_name.
and the flow rates. 2. which is a good barometer of the metabolic activity of the microorganism. calculate the total amounts of carbon dioxide produced and oxygen consumed during this 10h period of fermentation. Table E4. temperature. Table E4. In addition.3: Integration formulasTrapezoidal and Simpson's 113 Rules.238 Numerical Differentiation and Integration Chapter 4 Example 4. and pressure of these gases. Using Simpson's 113 rule.3a Fermentation data Time of fermentation Carbon dioxide evolution rate Oxygen uptake rate (h) (9lh) (Wh) .3a shows a set of rates calculated from the fermentation of Penicillium chlysogenum. These total amounts form the basis of the material balancing technique described in Sec.1. Compare the results of this function and the existing MATLAB function trapz (trapezoidal rule) for solution of the following problem: Two very important quantities in the study of fermentation processes are the carbon dioxide evolution rate and the oxygen uptake rate. Write a general MATLAB function for integrating experimental data using Simpson's 113 rule. Repeat this using the trapezoidal rule and compare the results obtained from the two methods. These are calculated from experimental analysis of the inlet and exit gases of the fermentor. The ratio of carbon dioxide evolution rate to oxygen uptake rate yields the respiratory quotient. which produces penicillin antibiotics. the above rates can be integrated to obtain the total amounts of carbon dioxide produced and oxygen consumed during the fermentation.
the carbon dioxide evolution rate data and the oxygen uptake rate data are integrated separately. ) . the function adds the value of the integral for the last segment according to Eq.mlC02) fprintf('\n Total carbon dioxide evolution the Simpson 1/3 rule)'. m102 = trapz(t.3.4£ (evaluated by = %9. Program Description: The MATLAB function Simps0n. we can use either the trapezoidal rule or Simpson's 113 rule for this integration. % output fprint£('\n Total carbon dioxide evolution the trapezoidal rule) '. for the last interval. r_C02 = input(' Carbon dioxide evolution rate (g/h) = ' 1 .3 Integration FormulasTrapezoidal and Simpson's 113 Rules 239 Method of Solution: In this problem.75).Example 4. therefore. The user should pay special attention to this case because the truncation errors for Simpson's 113 rule and trapezoidal rule are not of the same order. (4. Program Example43.m first tests the input arguments. which are the vector of independent variable (x) and the vector of function values (y). the function calculates the value of the integral up to the point ( n . calls the functions trapz and Simpson for integration.r02).m % % % % Example43. and displays the results. % Integration mlC02 = trapz (t. r02 = input(' Oxygen uptake rate (g/h) = . the function calculates the value of the integral based on Eq.m asks the user to input the data from the keyboard. If necessiuy. There are 11 data points (10 intervals) for each rate.1) and adds the value of the integral. as the problem specifies. approximated by the trapezoidal rule. Also.m Solution to Example 4. m2C02 = Simpson(t. m202 = Simpson (t. clear C1C % Input data t = input(' Vector of time = ' ) . (4. We first use Simpson's 113 rule and then repeat using the trapezoidal rule. These two vectors should he of the same length.rC02). If the vectors contain an even number of elements (odd numher of intervals).m2C02) = %9. It calculates carbon dioxide evolved and oxygen uptaken in a fermentation process using TRAPZ (trapezoidal rule) and SIMPSON (Simpson's 1/3 rule) functions. r_C02 ) . the numher of elements of these vectors (n) should he odd (even number of intervals).82). r02) .4£ (evaluated by . After checking the above conditions. The main program Example43. Elements of vector x have to be equally spaced values.
QUAD8. Constantinides % January 1. maxi = max([min(abs(x))IlOOO .ml02) fprintf('\nTotal oxygen uptake the Simpson 113 rule)\n'.') break end h = dx(1). if max(dx)min(dx) > maxi error('X is not equally spaced. If length of Y is even. y) %SIMPSON Numerical evaluation of integral by Simpson's 113 rule. else n = points.m202) = %9. Trapezoidal rule will be used for the last interval. Mostoufi & A. the function calculates the integral for [LENGTH(Y)11 points by Simpson's 113 rule and adds to it the value of the integral for the last interval by trapezoidal rule.4f (evaluated by function Q = Simpson(x . QUAD .2) == 0 warning('0dd number of intervals.1. le101). end . if length(y1 = points error('x and y are not of the same length') break end dx = diff (x).2). X is the vector of equally spaced independent variable. % SIMPSON(X.240 Numerical Differentiation and Integration Chapter 4 fprintf('\nTotal oxygen uptake the trapezoidal rule)'. end % Integration yl = y(2 : 2 : n . See also TRAPZ . Length of Y has to be odd (even number of intervals). if n = points Q = Q + (~(points) + y(n)) * h 1 2 . if mod(points.4f (evaluated by = %9. GAUSSLEGENDRE % (c) N.1).') n = points . 1999 points = length(x).Y) numerically evaluates the integral of the vector of function values Y with respect to X by Simpson's 1/3 rule. Q = ( ~ ( 1+ ) 4 * sum(y1) + 2 * sum(y2) + y(n)) * h 13. y2 = y(3 : 2 : n .
15. 15. yield the total amounts of carbon dioxide and oxygen shown in Table E4. It has been suggested by Chapra and Canale [4] that a combination of the trapezoidal rule with Simpson's rules may he feasible for integrating certain sets of unevenly spaced data points.74.16. if experimental dataare to be integrated.19. we have assumed that the interval of integration could be divided into segments of equal width.68. This is usually possible when integratingcontinuous functions. (g) 168. 17. 15. 17. 14. However.23.3450 145. using both Simpson's 113 rule and the trapezoidal rule.56. 13.35.9733 (evaluated by the = = = Discussion of Results: The integration of the experimental data.9733 168. 16.53. 14.6633 144. they may be chosen to be the locations of the roots . 18. (g) Total 0. This method uses the Lagrange polynomial to approximate the function and then applies orthogonal polynomials to locate the loci of the base points.36 Total CO. Table E4. suchdata may be used with a variablewidth segment. 12. 14. 15. 17.21.5200 In the development of the NewtonCotes formulas.4.42. 17.951 Oxygen uptake rate (g/h) = [ 1 5 . If no restrictions are placed on the location of the base points.511 Total carbon dioxide evolution trapezoidal rule) Total carbon dioxide evolution Simpson 1/3 rule) Total oxygen uptake trapezoidal rule) Total oxygen uptake Simpson 1/3 rule) = 168.75.74.36. Gauss quadrature is a powerful method of integration that employs unequally spaced base points. 16.20.8 Gauss Quadrature 241 Input and Results >>Exampled3 Vector of time = [140:1501 Carbon dioxide evolution rate (g/h) = [15. 14.29.3b. 14.72. 16. 17.13. 4 9 .60.3450 (evaluated by the 168.39.5200 (evaluated by the 144.6633 (evaluated by the 145.
and we are able to estimate the integral from the area under a straight line that joins any two points on the curve. As aresult.5. as illustrated in Fig.8. a straight line that balances the positive and negative errors can be drawn. This concept is used in the Gauss quadrature method. .242 Numerical Differentiation and Integration Chapter 4 of certain orthogonal polynomials in order to achieve higher accuracy than the NewtonCotes formulas for the same number of base points.1 TwoPoint GaussLegendre Quadrature In order to illustrate the approach. for the trapezoidal rule that approximates the integral by taking the area under the straight line connecting the function values at the ends of the integration interval. 4. 4. X. Xo Figure 4.5 Application of twopoint Gauss quadrature to improve integral estimation. By choosing these points in proper positions.2. which is discussed in this section. consider the case that the restriction of fixed points is withdrawn. we obtain an improved estimate of the integral. Now. In NewtonCotes method. the location of the base points is determined. This is shown in Fig. 4. we first develop the integration formula for the twopoint problem. and integration is done based on the values of the function at these base points.
this equation becomes Using Eq. b] and z in interval [c.95) where use of Y (instead of y) indicates that the function value at the variable z (rather than x) . 11. 4 is the following: For converting to the interval [I. The general transformation equation for converting between x in interval [a.93). the interval [a. the function y =f(x) is replaced by a linear polynomial and a remainder: b The integral (y dx is evaluated by Without loss of generality.4. The weights w .8 Gauss Quadrature 243 In order to derive the twopoint Gauss quadrature. 11. the transformed integral is given by I 1 J Ydz = wnYn + wlYl + j"R(z)dz (4. (4. and wl are calculated from should be used. b] is changed to [I.
5. (4.94) and (4.98) imply that instead of evaluating the function at z. The Gauss quadrature method goes a step beyond this in order to make the error term in Eq. Laguerre. 3. and z .10): The values of z. such as Chebychev. 4.8. This is roughly equivalent to the application of fivepoint trapezoidal mle. which is the case in the trapezoidal rule. the development of this method is equivalent to that of the trapezoidal rule. the integral of the error term is expanded in terms of 2nddegree Legendre polynomial (see Sec. This choice of roots causes the error term to vanish. 3. Therefore. = 11J3 should be used in the Gauss quadrature method.95) vanish.1) and its remainder. The Gauss quadrature formula developed in this section is known as the GaussLegendre quadrature because of the use of the Legendre polynomials.95) becomes Calculation of the integral through Eqs.244 Numerical Differentiation and Integration Chapter 4 Up to this point.9. = I (using function values at base points).2 HigherPoint GaussLegendre Formulas The function y = f(x) is replaced by the Lagrange polynomial (see Sec. Eq. where . (4. = 1lJ3 and z. Other orthogonal polynomials. To do so. = 1 and z. as illustrated in Fig. 4. or Hermite. that is. may be used in a similar manner to develop a variety of Gauss quadrature formulas. function values at z. (4. are chosen as the root of the 2nddegree Legendre polynomial. This results in improving the precision of calculation.
The Gauss quadrature method goes a step beyond this in order to make the error term [Eq. I] through Eqs. (4. The values of zj are chosen as the roots of the (n + 1)stdegree Legendre polynomial. (4.92) and (4.(z) and n 2" (z  zi) are polynomials of degree n and (n + I).103) becomes S Ydz 1 I = x i0 wiY. .103) where the weights wi are calculated from if i and the error term is given by The q.93). the development of this method is different from that of the NewtonCotes formulas in only one respect: the use of Lagrange interpolation formula for unequally spaced points instead of the GregoryNewton formula. Eq. Therefore. .10).8 Gauss Quadrature 245 b The integral Sy dx is evaluated by Converting the interval from [a.4.105)] vanish.(z)dz 1 (4. respectively. 3. (4. the transformed integral is given by 1 j" Ydz 1 = x i=O 1 wiYi + /R. the two polynomials in the error term are expanded in terms of Legendre orthogonal polynomials (see Sec.149)l of the Legendre polynomials causes the error term to vanish. (3. To do so.bl to [I. This choice of roots combined with the orthogonality property [Eq." Up to this point.
we get . the judicious choice of the (n + 1) base points at the (n + 1) roots of the Legendre polynomial has increased the accuracy of the integration from n to (2n + 1). It is desired to know the temperature profile of the fluid as a function of y and z. Table 4.7. The solid wall is maintained at a temperature (T. near the wall.4: Integration Formulas . the increase in accuracy has been obtained at the cost of having to perform a larger number of arithmetic calculations. The partial differential equation that describes the temperature of the liquid for this problem is where p is the density of the liquid.106) yields the integral of the function Y exactl)~ when Y is a polynomial of degree (2n + 1) or less. k is the thermal conductivity of the liquid. the velocity simplifies to: Putting this velocity profile into the energy balance equation. [2]: where 6 is thickness of the film.5 lists the roots and weights of the GaussLegendre quadrature for selected values of n. and is the viscosity of the liquid. initially at temperature To. The error of GaussLegendre formulas is given by [5] The roots zi of the Legendre polynomials can be evaluated after calculating the coefficients of the polynomial from the formula given in Table 3. In effect. (4. g is gravity acceleration.) higher than that of the falling film. The values of the weights w. I]. Therefore. Write a general MATLAB function for integrating a function using a general GaussLegendre quadrature. is falling down (in zdirection) a vertical solid wall (xzplane). and Tis the temperature of the liquid. where y << 6.GaussLegendre Quadrature.246 Numerical Differentiation and Integration Chapter 4 Since the vanishing error term was of degree (n + I). corresponding to these roots have been calculated for the integration interval [1. The velocity profile of the falling liquid is given by Bird et al. however. is the velocity of the liquid. Example 4. As usual. C. is heat capacity of the liquid. near the wall. Apply this function for the solution of the following problem: A cold liquid film. Eq. v.
555555555555555 0.861 I36311594053 0 _+0.171324492379170 0.865063366688985 k0.433395394129247 k0.149451349150581 0.652145154862546 0.932469514203152 Tenpoint formula (n+l=10) k0.219086362515982 0.Example 4.GaussLegendre Quadrature Table 4.360761573048139 0.066671344308688 Fourpoint formula (n + l = 4) Fivepoint formula (n + 1 = 5) Sixpoint formula (n + 1 = 6) k0.467913934572691 0.) Weight factors (w.238619186083197 *0.4 Integration Formulas .661209386466265 *0.478628670499366 0.) Twopoint formula (n + 1 = 2) k0.000000000000000 Threepoint formula (n + 1 = 3) 0 k0.5 Roots of Legendre polynomials P.148874338981631 k0.347854845137454 0.339981043584856 k0.906179845938664 0.236926885056189 0.973906528517172 Fifteenpoint formula (n+l=15) .269266719309996 0.295524224714753 0.568888888888889 0.57735026918926 1.679409568299024 k0.774596669241483 t0..888888888888888 0.(r) and the weight factors for the GaussLegendre quadrature p~ 247 Number of points Roots (2.538469310105683 k0.
The program checks the inputs to the function to be sure that they have valid values. the integral is numerically evaluated by Eq.m. the default value for the number of points of the GaussLegendre formula is two. It calculates and plots the temperature " .m % Solution to Example 4. Method of Solution: In order to evaluate the temperature profile (O). Also. we may write the b o u n d q conditions as Atz=O.m % Example44 . the main program Example44. and the Gamma function r(x) is defined as Using GaussLegendre quadrature calculate the above temperature profile and plot it against q. In order to solve the problem described in this example.) and evaluates the weight factors. The function to be integrated is introduced in the MATLAB function Ex44jimc. If no value is introduced for the integration step. (4. we first have to integrate the function e for several values of q > 0. Once these coefficients a e calculated. to L.104). using the values of zi and w . the program evaluates the roots of the Legendre polynomial (z. (4.248 Numerical Differentiation and Integration Chapter 4 in which P = pklp2C. T=To T=T T = To fory>O for z > 0 for z finite The analytical solution to this problem is [2] where q = y / 3 fiis a dimensionless variable. Then.) using the MATLAB function roots. then.m numerically evaluates the integral of a function by npoint GaussLegendre quadrature. The next step in the function is the calculation of the coefficients of the nth degree Legendre polynomial. The temperature profile itself.g6. Program Example44.m is written to calculate the temperature profile for specific range of the dimensionless number q. At y = m. as defined in Eq. Aty=O. the function calculates the coefficients of the Lagrange polynomial terms (L.4. to z. Program Description: The function GaussLegendre.106). Finally. wj. can be calculated from the equation described above. For short contact time. the function sets it to the integration interval.
[].4 integration Formulas GaussLegendre Quadrature  249 % profile of a liquid falling along a wall of different temperature. .B.a. I calculates the integral using interval spacing H=BA and N=2 and also allows parameters PI.B. using interval spacing H. .P1.m function y = Ex4_4Lfunc(xl % Function Ex44func. .theta / gamma(4/3). % The program uses GAUSSLEGENDRE function to evaluate the integral.P2.H.varararginl %GAUSSLEGENDRE GaussLegendre quadrature % % % % % % % % % % % GAUSSLEGENDRE('F1.theta) xlabel('\eta') ylabel('\theta') Ex44>nc.n. QUADR. end theta = 1 . P2. h = e t a ( 2 ) eta(l).A. % Calculation of the temperature profile for k = 1 : length(eta) theta(k) = GaussLegendre(fname.4 Gausskgendre. to pass directly to function F.Example 4.m % Function to be integrated in Example 4.h.eta(k). by a Npoint GaussLegendre quadrature. Constantinides % January 1.M from A to B. SIMPSON % (c) N. 1999 . TRAPZ. GAUSSLEGENDRE('F'.m function Q = GaussLegendre(fnctn.b. .A. Mostoufi & A. %Stepsize fname = input(' Name of mfile containing the function subject to integration = '1. . .O.h).[]. % Plotting the results plot(eta.N) numerically evaluates the integral of the function described by Mfile F. clear clc clf % Input data eta = input(' Vector of independent variable (eta) = ' ) .M see also QUAD.
a) < abs(h) n < 2 % Coefficients of the Legendre polynomial for k = 0 : n/2 c1(2*k+l) = (l)^k * gamma(2*n . denom = 1. % Vector of coefficients end denom = denom * (z(p) . end Ai(n + 1) = 0. end if sign(h) = sign(ba) h = . % Roots of the Legendre polynomial (Zi) % Weight factors forp=l:n B = [l 01.2*k + 11).250 Numerical Differentiation and Integration Chapter 4 if nargin < 4 1 isempty(h1 h = b .k + 1).polyval(Ai .B(r1) * z ( q ) . % Denominator of Li end B = [ A 01. (2^n * gamma(k+ 1) * gamma(n . . A(1) = B(1). . end % Vector of coefficients of integral of Lagrange polynomial f o r k = l :n % Ai are coefficients of the integral polynomial Ai(k1 = A(k) / (n . % Checking input arguments I 1 h == 0 1 abs(b . clear A . if k c n/2 c1(2*k+2) = 0.k + 1) * gamma(n . % Constructing vector of coefficients of the % Lagrange polynomial (coefficients of Li) forq=l:n if q = p k = k + l . k = 0.z ( q ) ) . end if nargin < 5 1 isempty(n) n = 2. 1) . 1 ) ) / denom. end end z = roots(c1). % weight factor Wi w(P1 = (polyval(Ai .2*k + 1) / . for r = 2 : k+l A(r1 = B(r) .a .h : end n = fix(n1.
). varargini:)) * hr /2.2: 21 Name of mfile containing the function subject to integration = 'Ex44func' Discussion of Results: The temperature profile of the liquid near the wall is calculated by the program Example44. E4. Both these situations are specified as boundary conditions of the problem . b. end end Input and Results Initial value of Vector of independent variable (eta) = [O: 0. since passing a longenough distance along the wall. In the liquid next to the wall (at y = 0 and at all values of 2). In the fluid far away from the wall (at y = and at all values of z). The variable q becomes infinity under the following circumstances: a.4: The results represented in Fig.4 also shows that at highenough dimensionless number q the temperature of the liquid is equal to the initial temperature of the liquid. hr = b . % Integration 251 f o r x = a : h : b . the temperature of the liquid is identical to that of the plate (that is. E4.4.9 Spline Integration Q = 0. Fig. At the origin of the flow (at z = 0 and at all values of y). T = T. The variable q attains a value of zero at only two situations: a .xr. x p . After an infinite distance from the origin of flow (at z = and at all values of y). Situation a is consistent with the boundary conditions given in the statement of the problem whereas situation b is an expected result. E4.h forp=l : n xp = x + (z(p)+ 1) * h / 2.4 show that at q = 0. Q = Q + w(p) * feval(fnctn. if hr > 0 f o r p = l :n xp = x r + (z(p) + 1 ) * hr / 2. Q = Q + w(p) * feval(fnctn . xp . that is. varargini:)) * h /2. E4. O = 1. We can verify the solution at the boundaries of y and z from Fig.4. end end % Integral of the remainder interval (if (ba)/his not an integer) xr = x + h.m for 0 I q 2 and is plotted in Fig. all the liquid will be at the same temperature as the wall. therefore. b.
4 Another method of integrating unequally spaced data points is to interpolate the data using a suitable interpolation method.108)] is corrected for the curvature of the function [the second term in the bracket of Eq. It is enough to replace the formula of the interpolation section with the integration formula. .147). (3. the integral of Eq.2) in order to calculate the integral of a function from a series of tabulated data. the second derivatives for the first and the last intervals are equal to zero. A good example of applying such a method can be found in Hanselman and Littlefield [3].143) over the interval [x. (4. (4. Eq.] and summing up these terms for all the intervals: Prior to calculating the integral from Eq. (4.+.m is able to give the piecewise polynomial coefficients from which the integral of the function can be evaluated..1081. Eq. 108)l. Remember that sp1ine.252 Numerical Differentiation and Integration Chapter 4 Figure E4. such as cubic splines. the MATLAB function sp1ine. and then evaluate the integral from the relevant polynomial. Note that if a natural spline interpolation is employed. (4.108). x. (4. Also. Therefore.m (see Example 3.108) is basically an improved trapezoidal formula in which the value of the integral by trapezoidal rule [the first term in the bracket of Eq. The reader can easily modify the MATLAB functionNatura1SPLZNE.66) may be calculated by integrating Eq. (4. the values of the second derivative at the base points should be calculated from Eq. (3.m applies the notaknot algorithm for calculating the polynomial coefficients.
that is. and x is considered to be constant.9.1 11): Here n is the number of divisions and h is the integration step in the xdirection.4. is considered to be constant. (4.109) results in Now we apply the trapezoidal rule to each of the integrals of Eq. Replacing Eq. (4.76)] for simplicity: where m is the number of divisions and k is the integration step in the ydirection. . (4. Evaluation of integrals with more than two dimensions can be obtained in a similar manner. and y.10 Multiple Integrals 253 In this section.110) into Eq. we discuss the evaluation of double integrals. We use the trapezoidal rule [Eq. Let us start with a simple case of double integral with constant limits. 4.74. (4. integration over a rectangle in the xy plane: The inner integral may be calculated by one of the methods described in Secs.
However.111) and (4. we may have to change the step size k slightly to obtain an integer value for the number of divisions. hk 2 I + m .yj) j. In practice.] j. in order to maintain the same order of error throughout the calculation. constant step size) should be employed.. 4 )changes with the value of x. [c.l "1 2 E Ef(x2. we combine Eqs.. the second condition (that is.y. Therefore.yj) j1 1 The method described above may be slightly modified to be applicable to the double integrals with variable inner limits of the form Because the length of the integration interval for the inner integral (that is. in the following form to count for the variable limits: where m. we may either keep the number of divisions constant in the ydirection and let the integration step change with x [k = k(x)] or keep the integration step in the ydirection constant and use different number of divisions at eachx value [m = m(x)]. Although this does not change the order of magnitude of .Ef(a. ml + Cf(b. indicates that the number of divisions in the ydirection is a function of x.112) to calculate the estimated value of the integral (4. at each x value. Eq.254 Numerical Differentiation and Integration Chapter 4 Finally.109): + .) j. (4. (4.110) can be written at each position x.
using forward finite differences. we have to acknowledge this change at each step of outer integration.3.6 Derive the equations for the first. it is not necessary to apply Eqs.4 4. second.114) is calculated from If writing a computer program for evaluation of double integrals.113) and (4.5 4. 4.2 4.1. The MATLAB function dblquad evaluates double integral of a function with fixed inner integral limits. the approximate value of the integral (4. 4. As amatter of fact. PROBLEMS 4.3 Repeat Prob. and third derivatives of y in terms of central finite differences with error of order h6.115) in such aprogram. 4. and third derivatives of y in terms of backward finite differences with error of order h'. Repeat Prob. with (a) Error of order h2 (b) Error of order h4. using forward finite differences. therefore. .1 Derive the equation that expresses the thirdorder derivative of y in terms of backward finite differences. second. with (a) Error of order h (b) Error of order hZ. any ordinary integration function may be applied to evaluate the inner integral at each value of the outer variable: then the same function is applied for the second time to calculate the outer integral. 4. Derive the equation which expresses the thirdorder derivative of y in terms of central finite differences. (4. Derive the equations for the first.Problems 255 the step size. This algorithm can be similarly applied to the multiple integrals of any dimension.
63 1.7 8.laz).25 23.35 11.37 5.25 71.51 34.66 15.11 11.41 27.81 3. Table P4.91 17.65 0.9612 23.22 79.8 In studying the mixing characteristics of chemical reactors.44 18.05 57.45 42.29 9.86 18.77 1.45 33.3 1.08 52.21 0.26 0.4288 41.44 25.49 30.64 64. a sharp pulse of a nonreacting tracer is injected into the reactor at time t = 0.21 1.29 0.26 0.62 61. The residence time distribution (RTD) function for the reactor is defined as E(t) ~ ( t ) = rn 0 and the cumulative distribution function is defined as The mean residence time of the reactor is calculated from .34 128.35 27.42 110. Calculate the axial gradient of velocities (that is.18 18 49.61 82.35 2.07 13.5 25.4938 7.14 15.8962 58.36 116.57 7.49 25 75 125 175 225 275 325 375 425 475 13.26 16.18 3.81 1. The concentration of material in the effluent from the reactor is measured as a function of time c(t).3 20.09 15.43 5.88 2.1 1.07 42.2 17.65 10 15.29 73.17 0.44 37.45 4. JVJJz of 1 d s are given in Tables P 4 .256 4.17 21 71.81 19.06 69.97 2.63 0.88 17.05 i a I P 0 i i O n ' m 4.32 58.7a Radial velocity profile (mmls) Radial position (mm) 4.3 31. Plot the zaveraged gradients versus radial position and compare their order of magnitude.7 Numerical Differentiation and Integration Chapter 4 Velocity profiles of solids in a bed of sand particles fluidized with air at the superficial velocity ~ b.99 1.39 22.21 22.3638 54.2988 37.79 0.98 50.35 76. 7 and and aV.8313 52.7663 A X 14.5 2.09 2.43 19.
66 416.18 279.93 63.78 115. cumulative distribution function.97 1.33 31. Calculate the RTD function.9612 21.21 191.88 53.35 177.52 366.68 103.8 16.33 244.33 23. mean residence time.25 44.8 Time (s) c( t) (mglL) Time (s) c( t) (mglL) 0 1 2 3 4 0 2 4 7 6 5 6 7 8 5 2 1 0 .47 133.8962 18.16 309. Table P4.67 328.26 89.4 271.46 55.91 82.61 203.07 260.86 98.08 184.23 47.73 304.74 52.3 314.24 38.46 65.03 42.2988 74.69 91.09 111.08 63.7b Axial velocity profile (mmls) Radial position (mm) 257 4.8 were obtainedfrom a tracer experiment studying the mixing characteristics of a continuous flow reactor.66 9.92 160.77 32.6 61.Problems Table P4.65 59.25 12.82 133.48 51.95 61.4938 22.81 136.3638 43.81 379.74 108.23 125.68 111.97 76.92 65.76 53.96 157.88 71.58 281.56 74.9 41.09 201.28 118.7663 A X 14.15 209.72 42.04 71.1 9.37 25 75 125 175 225 275 325 375 425 475 93.87 22.8313 69.74 67.57 175.23 51.74 52.43 84.9 165. The variance of the RTD function is defined by The exit concentration data shown in Table P4.4288 6.94 46. and the variance of the RTD function of this reactor.79 118.99 18.25 i a I P 0 i i O n ' m where Vis the volume of the reactor and q is the flow rate.12 217.9 39.34 308.5 33.
k. (b) The timeneeded toproduceaproduct containing 11. in kmol/m3. 4. = concentration of A. = concentration of B. is the rate of the reaction in kmolim3. Assume that the solids move in plug flow at the same velocity of the gas (U). Larachi.01 s1(gmo1L)~' at 500°C C. Chaouki. Evaluate the height of the reactor at which the conversion of A is 60%.s. and k. Larachi. gmol/L Choose a basis of 100 gmol of feed and assume that all gases behave as ideal gases... are constants.. in which both A and B are absorbed on the surface of the catalyst. C. and 20% inert will be processed in a reactor. (eds. 40% B. F. and k. the rate law is rA = I + k. I. F. P. Additional data are as follows: 4. M.. CA k2CA + k3CB where r. P. and Dudukovit. 1997. Calculate the following: (a) The time needed to produce a product containing 11. and C... and Dudukovit. NonInvasive Monitoring o f Mulriphase Flows...10 A gaseous feedstock containing 40% A.258 4.9 Numerical Differentiation a n d Integration Chapter 4 The following catalytic reaction is carried out in an isothermal circulating fluimzed bed reactor:  Bk) For a surfacereaction limited mechanism. Kennedy. Elsevier. respectively. J. "Radioactive Particle Tracking in Multiphase Reactors: Principles and Applications.8% Bin abatch reactoroperating at 500°C and constant volume. REFERENCES I. Amsterdam. .).8% Bin a hatch reactor operating at 500°C and at constant pressure of 10 aun.. gmollL C. are concentrations of A and B. M. where the following chemical reaction takes place: A + 2 B + C The reaction rate is where k = 0. The temperature of the reactor is 500°C and the initial pressure is 10 atm. G." in Chaouki.11 Derive the numerical approximation of double integrals using Simpson's 113 ~ l in e both dimensions.
. S. W. New York.. B.0. Hanselman.. H. N. Wiley. New York. 1998. and Canale. Stewart. New York. McGrawHill. hentice Hall. Mastering MATLAB 5.. D. Upper Saddle River... 1960. E. A Comprehensive Tutorial and Reference. Chapra.. Bird. Luther.. E. NJ.. 3rd ed. 4. 3. B. P. Wiley. Transport Phenomena. and Lightfoot.References 259 2. B.AppliedNumerical Methods. A. R. C. Camahan. 1. 1998. 5.. and Wilkes. and Littlefield. NumericalMethodsforEngineers. R... 1969.
equations arise from the study of the dynamics of physical and chemical systems that have one independent variable. The latter may be either the space variable x or the time variable t depending on the geometry of the system and its boundary conditions. For example. when a chemical reaction of the type .C H A P T E R 5 Numerical Solution of Ordinary Differential Equations 0rmnary . differenbal . .
when a chemical reaction of the type .C H A P T E R 5 Numerical Solution of Ordinary Differential Equations 0rmnary . . equations arise from the study of the dynamics of physical and chemical systems that have one independent variable. differenbal . For example. The latter may be either the space variable x or the time variable t depending on the geometry of the system and its boundary conditions.
This is a set of simultaneous firstorder nonlinear ordinarq.3) Assuming that reaction (5. and C. The volume of the liquid in the fermentor is V.. (5. can be integrated to obtain the time profiles of all the concentrations. differential equations. and the flow rate of products out of the fermentor is F.. Consider the growth of a microorganism.. the material balance can he applied: Input + Generation = Output + Accumulation (5. represent the concentrations of the five chemical components of this reaction.3) written for each component of the reaction will have the form where C. in a continuous fermentor of the type shown in Fig.. say a yeast.. therefore. The material balance for the cells X IS Input + Generation = Output + Accumulation The material balance for the substrate S is given by .I) takes place in the liquid phase with negligible change in volume.2) For a hatch reactor. which describe the dynamic behavior of the chemical reaction..1. the material balance simplifies to Accumulation = Generation (5.. 5. C. C.. with a set of initial conditions. C. The flow rate of nutrients into the fermentor is F..262 Numerical Solution of Ordinary Differential Equations Chapter 5 takes place in a reactor. Eq. these equations. the input and output terms are zero. With the methods to be developed in this chapter.
that is. the concentrations at e v e q point in the fermentor are the same. then and the equations simplify to Further assumptions are made that the flow rates in and out of the fermentor are identical.5.1 Continuous fermentor. and that the rates of cell formation and subsuate utilization are given by Figure 5. .1 Introduction 263 The overall volumetric balance is If we make the assumption that the fermentor is perfectly mixed.
The liquid holdup on the stage is designated as H.264 Numerical Solution of Ordinary Differential Equations Chapter 5 The set of equations becomes This is a set of simultaneous ordinary differential equations.+. Fig.2)] becomes Accumulation =Input .2 Material balance around stage n of a distillation column. which describe the dynamics of a continuous culture fermentation. (5. . .. There is no generation of material in this process. Therefore.. respectively. and out of the stage V. and a vapor flow into the stage V . 5. The mole fractions of each component in the liquid and vapor phases are designated by xiand y . The dynamic behavior of a distillation column may he examined by making material balances around each stage of the column. so the material balance [Eq.Output The liquids and vapors in this operation are multicomponent mixtures of k components.2 shows a typical stage n with a liquid flow into the stage L. the material balance for the ith component is Figure 5. and out of the stage L..
= Vn. The delay in liquid flow is ~.firstorderordinary d~fferential encountered types of problems in the analysis of multicomponent andlor multistage operations.21) Third order: .20) Second order: d2y dx2 + y d y= k x dx (5. then V. In all the ahove examples. The order of a differential equation is the order of the highest derivative present in that equation. Examples of first. their linearity. We first show that higherorder differential equations can be reduced to first order by a series of substitutions.. These are the most commonly of simultaneous. In this chapter. where r is the hydraulic time constant. numerical methods have been thoroughly developed for the solution of sets of simultaneous differential equations.5. Ordinary differential equations are classified according to their order. and thirdorder differential equations are given below: First order: dy dx + y =kx (5. Closedform solutions for such sets of equations are not usually obtainable. The ahove equations applied to each stage in a multistage separation process result in a large set of simultaneous ordinary differential equations.2 Classification of Ordinary Differential Equations 265 The concentrations of liquid and vapor are related by the equilibrium relationship If the assumptions of constant molar overflow and negligible delay in vapor flow are made. second. and their boundary conditions. However. the systems were chosen so that the models resulted in sets equations. we discuss the most useful techniques for the solution of such problems.
the term initialhalue problem refers to hoth cases. For example. the dependent variables andlor their derivatives are known at more than one point of the independent variable. (5. Eqs.(x)d n . n ) are called variable coefficients when they are functions of x and constant coefficients when they are scalars. Therefore. I A problem whose dependent variables andlor their derivatives are all known at the Final value of the independent variahle (rather than the initial value) is identical to the initialvalue problem. then this is a twopoint boundaryvalue problem.23) If R(x) = 0. The general form of a linear differential equation of order n may be written as bo(x)d "Y dx" b.5. .. the equation is called homogeneous. A differential equation is autonomous if the independent variable does not appear explicitly in that equation.21) and (5. (5.20) is linear. whereas Eq. Ordinary differential equations may be classified as initialvalue problems or boundaryvalue problems.266 Numerical Solution of Ordinary Differential Equations Chapter 5 Ordinary differential equations may be categorized as linear and nonlinear equations. it is necessary to specify n values of the dependent variables (or their derivatives) at specific values of the independent variable.(x). For example. The methods of solution of initialvalue problems are developed in Sec. A differential equation is nonlinear if it contains products of the dependent variable or its derivatives or of both. and the methods for houndaryvalue problems are discussed in Sec.dy dxn' dx + + b. respectively.22) are nonlinear because they contain the terms y(dy1dx) and (dyldx)'. If R(x)tO.23) is homogeneous with constant coefficients.' y + . The coefficients (b. . . it is also autonomous.6.. If some of the dependent variables (or their derivatives) are specified at the initial value of the independent variable. In initialvalue problems. (5. 5. if Eq. the values of the dependent variables andlor their derivatives are all known at the initial value of the independent variable..' In boundaryvalue problems. 5. the equation is nonhomogeneous. and the remaining variables (or their derivatives) are specified at the final value of the independent variable. . because only the direction of integration must be reversed. I i = 1.(x)y = R(x) (5. To obtain a unique solution of an nthorder differential equation or of a set of n simultaneous firstorder differential equations. + b". ..
(x. : common point x ?*(x.3 Transformation to Canonical Form 267 Numerical integration of ordinary differential equations is most conveniently performed when the system consists of a set of n simultaneous firstorder ordinary differential equations of the form: This is called the canonical form of the equations.5. Y...(x) FZ(x) The above problem can be condensed into matrix notation. where the system equations are represented by .24) have solutions of the form YI = Y? = F." then the system equations (5. When the initial conditions are given at a .) = Yl.) = Y".
dx" The following transformations dn'z dx"' dx z dx2 = Y..268 Numerical Solution of Ordinary Differential Equations Chapter 5 the vector of initial conditions is and the vector of solutions is Y = F(x) Differential equations of higher order... or systems containing equations of mixed order. can be transformed to the canonical form by a series of substitutions. give the equivalent set of n firstorder equations of canonical form  dy. For example.30). 23% dx" dx when substituted into the nthorder equation (5. dx  Y2 . consider the nthorder differential equation ..
5 and 5. Solution methods for these equations were developed in Chap. then the equations can be written in matrix form: as in Example 5.Example 5.32) to the following ordinaq differential equations: . If the functions f(y) are linear in terms of y. A more restricted form of differential equation is where f (x) are functions of the independent variable only. Solutions for linear sets of ordinary differential equations are developed in Sec.6.1: Transformation of Ordinary Differential Equations into Their Canonical Form: Apply the transformations defined by Eqs. 4. A nonautonomous set may he transformed to an autonomous set by an appropriate substitution.1 (b) and (dl. The methods for solution of nonlinear sets are discussed in Secs.31) and (5. See Example 5. (5. 5.4.1 Transformation to Canonical Form 269 If the righthand side of the differential equations is not a function of the independent variable.1 (a) and (b). The next example demonstrates the technique for converting higherorder linear and nonlinear differential equations to canonical form. then the set is autonomous. that is. Example 5. 5.
= Y4 df3 dt d4z df4 d ~ 4 dy. 5. ( a ) to obtain the following four equations: dy1 dt dY2 dt . dt dt d2z dy2 .31): z = Y.270 . so that the .. Numerical Solution of Ordinary Differential Equations Chapter 5 Solution: (a)Apply the transformation according to Eqs.Y3 dt2 dt d 3 z _ dy. dz = = Y2 df Make these substitutions into Eq. The lefthand side is identical to that of Eq. (a).Y2 Y3 3 ' Y 4 dt dY4 .4. (5.34) where matrix A is given by The method of obtaining the solution of sets of linear ordinary differential equations is discussed in Sec. (b) The presence of the term e~'on the righthand side of this equation makes it a nonhomogeneous equation. + 6y2 + 2y3 dt  5y4 This is a set of linear ordinary differential equations which can be represented in matrix form yl=Ay (5.= 3y.
This transformation is Make the substitutions into Eq.Example 5. . ( a )are applicable. An additional transformation is needed to replace the e' term. with the matrixA given by 0 A = 1 0 0 1 0 0 0 0 1 0 0 0 0 3 6 2 5 0 1 0 0 0 0 ( c ) Apply the following transformations: 1 z = Y.1 Transformation to Canonical Form 271 transformations of Eq. (5.34). ( b ) to obtain the following set of five linear ordinary differential equations: dt which also condenses into the matrix form of Eq.
. ( c ) to obtain the set dx This is a set of nonlinear differential equations which cannot be expressed in matrix form.6. ( d ) Apply the following transformations: Make the substitutions into Eq. The methods of solution of nonlinear differential equations are developed in Secs.272 Numerical Solution of Ordinary Differential Equations Chapter 5 Make the substitutions into Eq. ( d ) to obtain the set dy4 . ( d ) to a set of autonomous equations.5 and 5. 5. Note that the above set of substitutions converted the nonautonomous Eq.1  dt This is a set of autonomous nonlinear differential equations.
(5. without chemical reaction. Sets of linear ordinary differential equations with constant coefficients have closedform solutions that can be readily obtained from the eigenvalues and eigenvectors of the matrix A.37) is essentially the scalar form of the matrix set of Eq. In order to develop this solution. The solution of the scalar equation can be obtained by separating the variables and integrating both sides of the equation In an analogous fashion.5. The unsteadystate material and energy balances of multiunit processes.34). often yield linear differential equations. (5.34) with given initial conditions Such examples abound in chemical engineering.4 Linear Ordinary Differential Equations 273 The analysis of many physicochemical systems yields mathematical models that are sets of linear ordinary differential equations with constant coefficients and can be reduced to the form yl=Ay (5. the matrix set can be integrated to obtain the solution . let us first consider a single linear differential equation of the type with the given initial condition Eq.
(5. Ax.7 of the dependent variables and the initial conditions. In Chap.43) . this solution can be modified by further algebraic manipulation to express it in terms of the eigenvalues and eigenvectors of the matrix A. respectively.83): It can be demonstrated that Eq. this case. which can be obtained from Eq. All the above eigenvectors and eigenvalues can be represented in a more compact form as follows: AX = XA (5.40) is a solution of Eq.274 Numerical Solution of Ordinary Differential Equations Chapter 5 In. However. we showed that a nonsingular matrix A of order n has n eigenvectors and n nonzero eigenvalues.x.40). The term eA' is the matrix exponential function. = = h. are vector. (2. h2x. whose definitions are given by Ax.34) by differentiating it: The solution of the set of linear ordinary differential equations is very cumbersome to evaluate in the form of Eq. y and y . 2. because it requires the evaluation of the infinite series of the exponential term eA'. (5. (5.
46) to any power n we obtain Starting with Eq. (5. we obtain . (5.4 Linear Ordinary Differential Equations 275 where the columns of matrix X are the individual eigenvectors: and A is a diagonal matrix with the eigenvalues ofA on its diagonal: If we postmultiply each side of Eq. we obtain AXX' Squaring Eq. (5. Eq. . (5.46): = A = XAX' A' = = [XAX'][XAX'I XA~X' Similarly.48).5.41) and replacing the matrices A. Therefore. A'. Eqs. .A" with their equivalent from The identity matrix I can be premultiplied by X and postmultiplied by K1 without changing it.49) rearranges to . (5. raising Eq. .46)(5. (5.43) by X'.
2. Develop a general MATLAB function to solve the set of linear differential equations. Solve the problem numerically for the following values of the kinetic rate constants: The value of k. = 1 CB" = 0 C " . = X e ~ t X .2: Solutionof a Chemical Reaction System. . = 0 reveals that the first reaction is irreversible in this special case. o o o e"' . .51): The eigenvalues and eigenvectors of matrix A can be calculated using the techniques developed in Chap. (5.A.40) and (5.276 Numerical Solution of Ordinary Differential Equations Chapter 5 which simplifies to . Apply this function to determine the concentration profiles of all components of the following chemical reaction system: Assume that all steps are firstorder reactions and write the set of linear ordinary differential equations that describe the kinetics of these reactions. = 0 Plot the graph of concentrations versus time. .51) where the exponential matrix eA' is defined as ehl' eA' = o o e"" o . Example 5. o o The solution of the linear differential equations can now be expressed in terms of eigenvalues and eigenvectors by combining Eqs. .. This is demonstrated in Example 5.~ (5. 2 or simply by applying the builtin function eig in MATLAB. The initial concentrations of the three components are c*. .. o .
(5. the set of differential equations that give the rate of formation of each compound is: In matrix form.2 Solution of a Chemical Reaction System 277 Method of Solution: Assuming that all steps are firstorder reactions. where the matrix X consists of the eigenvectors of K and c.53): c = [x~~'x']c. is the vector of initial concentrations: .Example 5. (5.40): c = eK'cO or by Eq. this set reduces to d where = Kc The solution of a set of linear ordinary differential equations can be obtained either by applying Eq.
This action might not be possible to undo. Are you sure you want to continue?
We've moved you to where you read on your other device.
Get the full title to continue reading from where you left off, or restart the preview.