Group 5 - Numerical Integration
Group 5 - Numerical Integration
Transforming lives by CSU is committed to transform the lives Productivity Compassion Competent
of people and communities through high Accessibility Accountability
quality instruction and innovative Relevance
Self-disciplined
Excellence
research, development, production and
COLLEGE OF ENGINEERING
(ChE 56)
VENTULA, LEA B.
Program: BSChE
1
Table of Contents
Numerical Integration
I. Introduction ---------------------------------------------------------------------------- 1
a. Trapezoidal Rule
i. Algorithm
ii. Flowchart
iii. Application
iv. M-File
b. Simpson’s Rule
i. Algorithm
ii. Flowchart
2
iii. Application
iv. M-File
C. Gauss Quadrature ------------------------------------------------------------------------------ 18-28
a. Romberg Integration
i. Algorithm
ii. Flowchart
iii. Application
iv. M-File
b. Adaptive Quadrature
i. Algorithm
ii. Flowchart
iii. Application
iv. M-File
c. Three-Point Quadrature
i. Algorithm
ii. Flowchart
iii. M-File
V. References --------------------------------------------------------------------------------- 30
3
I. Introduction
Mathematically, definite integration stands for the integral of the function f (x) with
respect to the independent variable x, evaluated between the limits x =a to x =b. For functions
lying above the x axis, the integral of the function corresponds to the area under the curve of f
(x) between x = a and b. Under this is the numerical integration which is sometimes referred
as quadrature. This is an archaic term that originally meant the construction of a square
having the same area as some curvilinear figure. Today, the term quadrature is generally
taken to be synonymous with numerical definite integration.
Integration has many engineering and scientific applications. Many specific examples of
such applications could be given in all fields of engineering and science. A number of
examples relate directly to the idea of the integral as the area under a curve. For example, a
common application is to determine the mean or average of a continuous function. That mean
of a continuous function has hundreds of engineering and scientific applications. For
example, it is used to calculate the center of gravity of irregular objects in mechanical and
civil engineering and it is also used to determine the root-mean-square current in electrical
engineering. Integrals are also employed by engineers and scientists to evaluate the total
amount or quantity of a given physical variable. The integral may be evaluated over a line, an
area which is called area integral, and for the volume which is called volume integral .When
the functions to be analyzed are simple, you will normally choose to evaluate them
analytically. However, it is often difficult or impossible when the function is complicated, as
is typically the case in more realistic examples. In addition, the underlying function is often
unknown and defined only by measurement at discrete points. For both these cases,
numerical techniques are used to obtain approximate values for integrals.
Another method discussed under this is the relationship between summation and
integration with the formula
4
b
f ( x)dx
Mean a
(2)
ba
This is usually used in getting a variable given a set of values within a set of intervals.
The Newton-Cotes formulas are the most common numerical integration schemes. They are
based on the strategy of replacing a complicated function or tabulated data with a polynomial
that is easy to integrate. The trapezoidal rule is the first of the Newton-Cotes closed
integration formulas. The trapezoidal rule works by approximating the region under the graph
of the function f(x) as a trapezoid and calculating its area or simply
f (b) f (a)
x a dx
b
I f (a)
a
ba (3)
Integrating the function, the equation will be simplified into
f (a) f (b)
I (b a)
2 (4)
One way of improving the accuracy of the trapezoidal rule is to divide the integration interval
into a number of segments and apply the method to each segment arriving at the formula
n 1
f ( x 0 ) 2 f ( x i ) f ( x n )
I (b a) i 1
(5)
2n
which is known to be the composite trapezoidal rule.
Aside from applying the trapezoidal rule with finer segmentation, another way to obtain a
more accurate estimate of an integral is to use higher-order polynomials to connect the points.
If there are two points equally spaced between f (a) and f (b), the four points can be
connected with a third-order polynomial. The formulas that result from taking the integrals
under these polynomials are called Simpson’s rules. The first application of Simpson’s rule
is the Simpson’s 1/3 rule that can be used when the equation to be evaluated is a second-order
with the formula.
X2 ( x x1 )( x x 2 ) ( x x0 )( x x 2 ) ( x x0 )( x x1 )
I [ f ( x0 ) f ( x1 ) f ( x 2 )]dx
X0 ( x0 x1 )( x0 x 2 ) ( x1 x0 )( x1 x 2 ) ( x 2 x0 )( x 2 x1 ) (6)
simplifying this, we get
h
I [ f ( x0 ) 4 f ( x1 ) f ( x2 )] (7)
3
5
The second application of the Simpson’s rule is the Simpson’s 3/8 rule. This is similarly
derived with the 1/3 Simpson’s rule, the only difference is that this equation is used in third-
order equations with the formula
3h
I [ f ( x0 ) 3 f ( x1 ) 3 f ( x2 ) f ( x3 )]
8 (8)
Gauss quadrature is the name for a class of techniques to implement a strategy where
in that the constraint of fixed base points was removed and we were free to evaluate the area
under a straight line joining any two points on the curve. By positioning these points wisely,
we could define a straight line that would balance the positive and negative errors. Hence,
these cases results in a small error. The first method under gauss quadrature is the Romberg
integration with a general formula of
4 1
I I (h2 ) I (h1 )
3 3 (9)
and for computation of the error, we use
I1, k I 2, k 1
a
I1, k
(10)
The second application of the gauss quadrature is the two-point gauss quadrature. Just as the
trapezoidal rule, by assuming that the equation fits the integral of a constant and a linear
function exactly to fit the integral of a parabolic and a cubic function, arriving to the formula
1 1
I f f
3 3 (11)
Next is the three-point gauss quadrature which is a higher version of the two-point gauss
quadrature. Adaptive quadrature is another application of the gauss quadrature which shows
the desired accuracy of the answer by taking small steps in variations and larger intervals are
used when the function changes gradually.
6
General Flowchart: Algorithm:
Step 2: Input 𝑥
Input 𝒙
Step 3: Use equation (1)
Print answer
Stop
The flowchart shows how to evaluate a definite integral given a function f(x). It has input
x, a and b.
Application:
As a specified in the following table, the earth’s density varies as a function of the distance
from its (r = 0):
Use numerical integration to estimate the earth’s mass (in metric tonnes) and average
density (in g/cm3). Develop vertically stacked subplots of (top) density versus radius, and
(bottom) mass versus radius. Assume that the earth is a perfect sphere.
7
Analytical Method:
6380
M 4 r (r
2
)dr
0
4r 4
M
4
M 6.1 10 24 g
a. Trapezoidal Rule
Trapezoidal rule is equivalent to approximating the area of the trapezoid under the
straight line connecting f (a) and f (b). To compute for the area under a curve, used the
formula width multiplied by the average height since the trapezoid is on the side.
Algorithm:
Step 1: Start
Step 3: Input initial boundary value, final boundary value and length of interval
8
Step 4: Calculate number of strips, n = (final boundary value –final boundary
value)/length of interval.
x[i]=x0+i*h
y[i]=f(x[i])
print y[i]
If i %2 = 0
So=s0+y[i]
Otherwise
Se=se+y[i]
9
Flowchart:
Start
Define Function
Declare Variables
n ( xn xo ) / h
i=0
i<=n
xi x0 i h
yi f ( xi )
i++
Print y[i]
se = s0 = 0
i=1
i<=n
i<=n
Stop
10
Trapezoidal method is based on the principle that the area under a curve which is to be
calculated is divided into a number of small segments. The bounding curve in the segment is
considered to be a straight line as a result the small enclosed area becomes a trapezium. The
area of each small trapezium is calculated and summed up. This idea is the working
mechanism of the give flowchart.
Application:
(1 e
x
)dx
0
Analytical Method:
i. Trapezoidal Rule
f (a) f (b)
b
I [ f (a) ( x a)]dx
a
ba
f (a) f (b)
I (b a)
2
f (0) f (4)
I (4 0)
2
0 0.99966
I ( 4 0) x
2
I 1.99932
2n
f (0) 2 f (2) f (4)
I (4 0)
4
I 1.99932
11
0 2 f (1) f (2) f (3) f (4)
I (4 x)
8
I 3.34369
function
I = trap(func,a,b,n,varargin)
% trap: composite trapezoidal rule quadrature
% I = trap(func,a,b,n,pl,p2,...):
% composite trapezoidal rule
% input:
% func = name of function to be integrated
% a, b = integration limits
% n = number of segments (default = 100)
% pl,p2,... = additional parameters used by func
% output:
% I = integral estimate
if nargin<3,error('at least 3 input arguments required'),end
if ~(b>a),error('upper bound must be greater than lower'),end
if nargin<4|isempty(n),n=100;end
x = a; h = (b - a)/n;
s=func(a,varargin{:});
for i = 1 : n-1
x = x + h;
s = s + 2*func(x,varargin{:});
end
s = s + func(b,varargin{:});
I = (b - a) * s/(2*n);
function y=f(x)
y=1-exp(-x);
12
13
Determine the distance traveled from the following velocity data:
Analytical Method:
a.
56 6 5.5 5.5 7 7 8.5
d (2 1) (3.25 2) (4.5 3.25) (6. 4.5)
2 2 2 2
8.5 6 66 67 77 75
(7 6) (8 7) (8.5 8) (9.3 8.5) (10 9.3)
2 2 2 2 2
d 58.425
M-File:
format long
t= [1 2 3.25 4.5 6 7 8 8.5 9.3 10];
v= [5 6 5.5 7 8.5 6 6 7 7 5];
p= polyfit (t,v,3)
tt = linspace(1,10);
vv = polyval(p,tt);
plot(tt,vv,t,v,'o')
xlabel('Time')
ylabel('Velocity')
14
15
b. Simpson’s Rule
Simpson’s rule is a method of numerical integration which is more accurate than the
Trapezoidal rule. It divides the area under the function to be integrated, f(x), into vertical
strips, but instead of joining the point’s f(x) with straight lines, every set of three such
successive points is fitted with a parabola.
Algorithm:
Set x =a + h*i.
If i%3=0
16
Flowchart:
Start
Enter a,b,n
Sum=0
h=(b-a)/n
Do i = 1 to n-
1
x= a +h*i
No
Is
i%2=0
?
sum=sum+3*f(x)
Yes
sum=sum+2*f(x)
I=n-1
Next
Ans = sum*(h/3)
17
The flowchart shows how to implement Simpson’s 1/3 and 3/8 rule. It has inputs a,b and
n which is substituted to equations (7) and (8) to determine the area under a given curve. The
computed values will have a small percent error.
Application:
Analytical Method:
Equation of Simpson’s 1/3 rule
h
I [ f ( x0 ) 4 f ( x1 ) f ( x2 )]
3
n ba
n 60
n6
ba
h
n
60
h
6
h 1
X 0 1 2 3 4 5 6
Y 1 0.5 0.3333 0.2500 0.2 0.1667 0.1429
Therefore:
h
I [ f ( x 0 ) 4 f ( x1 ) f ( x 2 )]
3
h
I [( y1 y 7 ) 2( y 3 y 5 ) 4( y 2 y 4 6 )]
3
I 11.75
18
M-File to implement Simpson’s Rule:
for (i=0;i<n+1;i++)
{ %loop to evaluate x0,...xn and y0,...yn
x[i]=a+i*h; %and store them in arrays
y[i]=f(x[i]);
}
for (i=1;i<n;i++)
{
if (i%3==0)
sum=sum+2*y[i];
else
sum=sum+3*y[i];
}
integral=3*h/8*(y[0]+y[n]+sum);
%3h/8*[y0+yn+3*(y1+y2+y4+...)+2*(y3+y6+y9+...+)]
cout<<\nThe definite integral is "<<integral<<"\n"<<endl;
return 0;
}
19
%Question: Evaluate the integral 1/(1+x) within limits 0 to 6
clc;
clear all;
close all;
f=@(x)1/(1+x);
a = 0; b = 6;
n = b-a;
h=b-a/n;
p=0;
for i=a:b
p=p+1;
x(p)=i;
y(p)=1/(1+i)
end
l=length(x);
x
y
answer=(h/3)*((y(1)+y(7))+2*(y(3)+y(5))+4*(y(2)+y(4)+y(6)))
20
C. Gauss Quadrature
a. Romberg Integration
The first application of the gauss quadrature is the Romberg integration. This technique is
designed to attain efficient numerical integrals. Through this method ad mathematical
manipulation, superior results are attained with less effort.
Flowchart:
Algorithm:
Start
Step 1: Start
Step 2: Define and declare function
Step 3: Input value to the formula
2
2 1
given to determine the I x
1 x
integral
Step 4: Calculate the percent error
Step 5: Determine the number of Determine the integral
iterations
Step 6: End
4 1
I I (h2 ) I (h1) A
3 3
Xi+1
I1, k I 2 , k 1
a
I1, k
Error<0.5% A
?
End
21
Application:
Develop an M-file function to implement Romberg integration based on Fig. 20.2. Test
the function by using it to determine the integral of 0.2+25x-200x2+675x3-900x4+400x5
from a=0 and b=0.8.
Analytical Method:
n 1, h 0.8
f (0.8) f (0.0)
I (0.8 0.0) 0.1728
2
t 89.5%
n 2, h 0.4
f (0.8) 2 f (0.4) f (0)
I (0.8 0.0) 1.0688
4
t 34.9%
n 4, h 0.2
f (0.8) 2 f (0.6) 2 f (0.4) 2 f (0.2) f (0)
I (0.8 0) 1.4848
4
t 9.5%
Using extrapolation
4(1.0688) (0.1728)
I 1.36747
3
t 16.6%
4(1.4848) (1.0688)
I 1.62347
3
t 1.0%
Romberg Integration
4 k I (h / 2) I (h)
I
4k 1
O(h 2 k 2 )
16(1.62347) (1.36747)
I 1.64503
15
t 0.0%
22
First Iteration
O( h 2 ) O( h 4 ) O( h 6 ) O(h 8 )
0.172800 1.367467
1.068800
Second Iteration
O( h 2 ) O( h 4 ) O( h 6 ) O(h 8 )
0.172800 1.367467 1.640533
1.068800 1.623467
1.484800
Third Iteration
O( h 2 ) O( h 4 ) O( h 6 ) O(h 8 )
0.172800 1.367467 1.640533 1.640533
1.068800 1.623467 1.640533
1.484800 1.639467
1.600800
function I = trap(func,a,b,n,varargin)
% composite trapezoidal rule
% input:
% func = name of function to be integrated
% a, b = integration limits
% n = number of segments (default = 100)
% output:
% I = integral estimate
if nargin<3,error('at least 3 input arguments required'),end
if ~(b>a),error('upper bound must be greater than lower'),end
if nargin<4|isempty(n),n=100;end
x = a; h = (b - a)/n;
s=func(a,varargin{:});
for i = 1 : n-1
x = x + h;
s = s + 2*func(x,varargin{:});
end
s = s +func(b,varargin{:});
I = (b - a) * s/(2*n);
23
b. Adaptive Quadrature
Flowchart:
Start
Algorithm:
Step 1: Start
Step 2: Define and declare function 2
2 1
Step 3: Input value to the formula I x
1 x
given to determine the
integral
Determine the integral
Step 4: Calculate the percent error
Step 5: Determine the number of
iterations 4 1
I I (h2 ) I (h1)
Step 6: End 3 3
Xi+1
I1, k I 2 , k 1
a End
I1, k
24
Application:
Develop an M-file function to implement adaptive quadrature based on Fig. 20.6. Test the
function by using it to determine the integral of of 0.2+25x-200x2+675x3-900x4+400x5
from a=0 and b=0.8.
Analytical Method:
n 1, h 0.8
f (0.8) f (0.0)
I (0.8 0.0) 0.1728
2
t 89.5%
n 2, h 0.4
f (0.8) 2 f (0.4) f (0)
I (0.8 0.0) 1.0688
4
t 34.9%
n 4, h 0.2
f (0.8) 2 f (0.6) 2 f (0.4) 2 f (0.2) f (0)
I (0.8 0) 1.4848
4
t 9.5%
Using extrapolation
4(1.0688) (0.1728)
I 1.36747
3
t 16.6%
4(1.4848) (1.0688)
I 1.62347
3
t 1.0%
Romberg Integration
4 k I (h / 2) I (h)
I
4k 1
O(h 2 k 2 )
16(1.62347) (1.36747)
I 1.64503
15
t 0.0%
25
M-File to implement Adaptive Quadrature:
function q = quadadapt(f,a,b,tol,varargin)
%Evaluates definite integral of f(x) from a to b
if nargin < 4 | isempty(tol),tol = 1.e-6;end
c = (a + b)/2;
fa = feval(f,a,varargin{:});
fc = feval(f,c,varargin{:});
fb = feval(f,b,varargin{:});
q = quadstep(f, a, b, tol, fa, fc, fb, varargin{:});
end
function q = quadstep(f,a,b,tol,fa,fc,fb,varargin)
% Recursive subfunction used by quadadapt.
h = b - a; c = (a + b)/2;
fd = feval(f,(a+c)/2,varargin{:});
fe = feval(f,(c+b)/2,varargin{:});
q1 = h/6 * (fa + 4*fc + fb);
q2 = h/12 * (fa + 4*fd + 2*fc + 4*fe + fb);
if abs(q2 - q1) <= tol
q = q2 + (q2 - q1)/15;
else
qa = quadstep(f, a, c,tol, fa, fd, fc, varargin{:});
qb = quadstep(f, c, b, tol, fc, fe, fb, varargin{:});
q = qa + qb;
end
end
26
c. Three Point Gauss Quadrature
Gauss-Legendre quadrature interpolates the integrand by a polynomial and integrates
the polynomial. Instead of uniformly spaced points, Gauss-Legendre uses optimally-
spaced points.
Algorithm
Step I: Start
27
Flowchart:
Start
a,b:integration Interval
N: Number of Points
Ans=0;
2 1 2
f ( x)dx ( x )dx
a 2
Return Ans;
End
28
Application:
Evaluate the following integral with the three-point Gauss Quadrature formula
I xe2 x dx
3
Analytical Method:
True Solution
3
I xe2 x dx
0
I 504.5399
3 1 3 3
I ( x ) dx
2 1 2 2
3 5 3 3 3 8 3 3 5 3 3 3
I f f 0 f
2 9 2 5 2 9 2 2 9 2 5 2
3 5 8 5
I f 0.338 f (1.5) f (2.6619)
2 9 9 9
3
I (0.369 26.78 303.400)
2
I 495.8235
504.5399 495.8235
100
495.8235
1.73%
29
M-File to implement Three Point Quadrature:
30
for n=1:5
N=N*2;
i=i+1;
k=N-1;
x1=linspace(a,b,N);
h=x1(2)-x1(1);
end;
loglog(K,errG2,'-or')
hold on
loglog(K,errG3,'-ob')
grid on
title('Blue = Gauss 3-point Red = Gauss 2-
point','Color','r','FontSize',14)
xlabel('Number of Subintervals','FontSize',14);
ylabel('Error','FontSize',14);
31
IV. Generalizations
Although most mathematical functions can be differentiated analytically, the same cannot
be said for integration. There are no general rules for integrating, as there are for
differentiating. We therefore need numerical methods for evaluating integrals. The common
methods used are the newton cotes formula which includes Trapezoidal rule and Simpson’s
1/3 and 3/8 rule, and Gauss Quadrature which includes Romberg Integration, Adaptive
Quadrature, Two Point Quadrature, and Three Point Quadrature. These methods are used to
estimate the area under a curve. For trapezoidal rule, its error is large compared to Simpson’s
rule. Therefore Simpson’s 1/3 rule is more accurate than trapezoidal rule. Simpson’s 1/3 rule
is third order accurate even though it is based on only three points. In other words, it yields
exact results for cubic polynomials even though it is derived from a parabola. In trapezoidal
rule, if the function being integrated is linear, it will be exact because the second derivative of
a straight line is zero. Otherwise, for functions with second- and higher-order derivatives
(with curvature), some error can occur.
When the function is known and high accuracy is required, a method such as Romberg
integration occurs. For Romberg integration, that technique can be used to generate an
integral estimate within a pre specified error tolerance. It is a method for combining two
numerical integral estimates to obtain a third, more accurate value. Gauss-quadrature
formulas employ x values that are positioned between the integration limits in such a manner
that a much more accurate integral estimate results. Although Romberg integration is more
efficient than the composite Simpson’s 1/3 rule, both use equally spaced points. This
constraint does not take into account that some functions have regions of relatively abrupt
changes where more refined spacing might be required. Hence, to achieve a desired accuracy,
fine spacing must be applied everywhere even though it is only needed for the regions of
sharp change. Adaptive quadrature methods remedy this situation by automatically adjusting
the step size so that small steps are taken in regions of sharp variation.
32
V. References
Chapra, S. C. (2005). Applied Numerical Methods with MATLAB for Engineers and
Scientist. New York: McGraw-Hill Icn.
Hahn, B. D., & Valentine, D. T. (2001). Essential MATLAB for Engineers and Scientist.
Oxford: Elsvier Ltd.
33