0% found this document useful (0 votes)
24 views29 pages

Lec 24 Numerical Integral

Uploaded by

Mazhar Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views29 pages

Lec 24 Numerical Integral

Uploaded by

Mazhar Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Lecture Slides

on
Modeling and Simulation

Lecture: Numerical Integration

Dr. Rubina Nasir


Email: [email protected]
Numerical Integration

• When we have integral from point a to point b, such an


integral is known a definite integral and it is a solution of a
simple ordinary differential equation.
• Numerical integration can used when the function is a
difficult to find or when its data form is given.
• Common numerical integration techniques are:
– Trapezoidal rule,
– Simpson’s method,
– Romberg Integration, and
– Gaussian quadratures.
Matlab functions
• There are three functions available in MATLAB for numerical integration.
• The functions quad and quad8 use adaptive, recursive rules, the low order
Simpson's and the higher order Newton Cotes-8 panel respectively.
• The basic format is quad('function', start, finish) to get the integral of
function from start to finish.
• This function takes a single vector as input (of the length of start and finish)
and return a single vector of output.
• If the integration requires large number of recursion and integral is singular,
the value Inf is returned. If function takes more than one argument, the
others may be specified as constant (for the integration) parameters by
quad('function', start, finish, tolerance, trace, first parameter, second
parameter...).
Single trapezoid
You can fit a straight line and / or a polynomial between two data points.
Single trapezoid
You can just fit a straight line
as approximation to actual
function and say what is area
under this straight line.

This area will have large error


as function and straight line
are not same.
Single trapezoid
• Figure shows the area bounded by a curve
for f(x) in the range from x = a to x = b. y = f(x)
y
• If we have only one interval as shown, on
both ends of which the values of f(x) are
given, then linear interpolation can easily f(a) f(b)
be used i.e., b
• we may construct a single trapezoid in the a
range.
x
For a single trapezoid within the range [a, b], the area or integral can be
approximated as:
1
T (0) = ( f a + f b )(b − a)
2
Many trapezoid

Many subdivisions
can be done
between x=a and
x=b and then a
straight line fit gives
better comparison to
the actual curve.
Many trapezoids
• The same area for two sub-intervals is given by following:
(b − a)
T ( 1) = [ f ( xo ) + 2 f ( x1 ) + f ( x2 ) ]
2×2

where, f(x0) and f(x2) are same as fa and fb respectively.


• For four sub-divisions within same boundaries, the area is:

(b − a)
T (2) = [ f ( x o ) + 2 f ( x 1 ) + 2 f ( x 2 ) + 2 f ( x 3 ) + f ( x 4 )]
2×4

• When there are eight sub-divisions in the same range [a, b], the area is

(b − a)
T ( 3) = [ f ( xo ) + 2 f ( x1 ) + 2 f ( x2 ) + 2 f ( x3 ) + 2 f ( x 4 )
2×8
+ 2 f ( x 5 ) + 2 f ( x6 ) + 2 f ( x7 ) + f ( x8 ) ]
Many trapezoid
Integral : Area under the curve
• When many sufficiently Strips = 18
small strips are added
we can generate the area f(x)
under the curve from x =
a to x = b with very
small error.
b
g b − g a = ∫ f ( x )dx a b x
a

Here with 18 Strips of different heights and same Width we get the
area under the curve.
Integral for N trapezoids
• The integral in general using N compound trapezoids within
the range, is given by
∆x
Integral ≈ { f ( x0 ) + 2 f ( x1 ) + 2 f ( x2 ) +  + 2 f ( x N −1 ) + f ( x N ) }
2

(b − a )
• where, ∆x =
N
• and
xi = a + i∆x, i = 0, 1, 2, 3,  , N

• The error in the computed integral value is given by


(b − a)(∆x) 2 ( 2 )
error = − f ( ξ)
12
Example 1 on integration
• Simulate the area under curve for f(x) = 1/x2 , using
trapezoidal rule with one, two, four, eight and sixteen equal
subdivisions between x = 2.5 and 4.5.
• Solution: Consider the full interval as a single subdivision.
f o = f (2.5) = 0.16

f1 = f (4.5) = 0.0494
• The approximate area under the curve is

(4.5 − 2.5)
Area = (0.16 + 0.0494) = 0.2094
2 ×1
Example 1 on integration
• For two subdivisions, we need to have:

f o = f (2.5) = 0.16
f1 = f (3.5) = 0.0816
f 2 = f (4.5) = 0.0494

• Thus
(b − a)
T ( 1)
= [ f ( xo ) + 2 f ( x1 ) + f ( x2 ) ]
2×2

(4.5 − 2.5)
Integral = (0.16 + 2. × 0.0816 + 0.0494)
2× 2
= 1863
Example 1 on integration
• If we divide the interval into four equal parts, then
∆x = (4.5 − 2.5) / 4 = 0.5
f o = f (2.5) = 0.16

f1 = f (4.5) = 0.0494
f1 = f (3.0) = 0.111
f 2 = f (3.5) = 0.0816

f 3 = f (4.0) = 0.0625

(b − a)
T (2) = [ f ( x o ) + 2 f ( x 1 ) + 2 f ( x 2 ) + 2 f ( x 3 ) + f ( x 4 )]
2×4

(4.5 − 2.5)
Integral = [0.16 + 2. × (0.111 + 0.0816 + 0.0625) + 0.0494]
2× 4
Example 1 on integration

• The exact value of the Number of Area from Percentage


integral is 0.17778. equal Sub- Trapezoidal Error
divisions Rule
• A script file for MATLAB
based on trapezoidal rule
is given below and its use 1 0.2094 17.8%
for several sub-intervals is
2 0.1863 4.8%
given as Table .
• It shows that the error 4 0.1799 1.2%
decreases rapidly as the
8 0.1783 0.3%
number of subdivisions in
the range of integration is 16 0.1779 0.08%
increased.
32 0.1778 0.02%
Example 1 on integration
% Program Name: Integral_1.m
% A MATLAB script file to compute Integral of f(x)
% over an interval [a, b] by trapezoidal rule.
% It calls an external function trapez.m using
% inputs of a, b and N (number of sub-divisions).
N = 8; a = 2.5; b = 4.5;
exact_value = 0.177778;
estimated_value = trapez(a, b, N)
error = abs(100*(exact_value -
estimated_value)/exact_value)
% ------------------------------------------------
Example 1 on integration
• It uses a function trapez.m as a subroutine:
function z = trapez(a, b, N)
% function for integration by trapezoidal rule: trapez.m
% to be used with integral_1.m code.
delta_x = (b - a)/N;
x(1) = a;
if(x(1) == 0), error('A is zero, change it')
end
sum = 0;
f(1) = 1.0/(x(1)^2);
for i=2: N+1
x(i) = x(i-1) + delta_x;
f(i) = 1/(x(i)^2);
end
if(N > 1)
for i=2: N, sum = sum + f(i); end
end
z = double((delta_x/2)*(f(1) + 2*sum + f(N+1)));
Matlab function: trapz
• Another function, trapz, is used for trapezoidal numerical
integration with format: trapz(x, y).
• Here x is a vector and y is a matrix of any number of columns,
but as many rows as the length of x.
• If x is omitted, unit spacing between points is assumed.
• Each column of y is considered to be a function over x and is
integrated with the trapezoidal approximation.
• The result is a vector of the column integrations.
Simpson Rule
• When we formed trapezoids, straight lines between points of a
curve were joined; much better approximation is obtained if we
join points by segments of parabolas.
• Suppose we have two intervals for which are known, the
interpolation formula of the second order polynomial can be
obtained.
• Integrating the polynomial in those two intervals of ∆x, we
obtain the integration formula:

∆x
xk

∫ f ( x)dx = { f ( xo ) + 4 f ( x1 ) + f ( x2 )}
x0
3
Simpson’s Rule
• . If we have 2I intervals and apply Simpson's rule repeatedly I
times, the integration formula is

∆x
b


a
f ( x)dx =
3
[ f ( x o ) + 4 f ( x1 ) + 2 f ( x 2 ) + 4 f ( x3 ) + 2 f ( x 4 ) +

+  + 2 f ( x N − 2 ) + 4 f ( x N −1 ) + f ( x N )]

• where, the step size is


(b − a )
∆x =
N
• and
xi = a + i∆x, for i = 0, 1, 2, 3,  , N
Error in Simpson’s Rule
• The error in the integral estimate is proportional to

(b − a)(∆x) 4 ( 4 )
error = − f ( ξ)
180

• The series of integration formulas represented by above


equations can be extended to a higher order.
• The series thus obtained are called the Newton Cotes formulas.
Newton-Cotes Integration Formulas
• 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 an approximating function that
is easy to integrate:
b b
I = ∫ f ( x)dx ≅ ∫ f n ( x)dx
a a
n −1
f n ( x) = a0 + a1 x +  + an −1 x + an x n
Example on Simpson’s Rule
Simulate the area under curve for f(x) = 1/x2 , using Simpson’s rule
with one, two, four, eight and sixteen equal subdivisions between x
= 2.5 and 4.5.
Solution:
Here the function is 1/x2 (as shown in Fig. ) with range of
,
integration [2.5, 4.5] is given.
Let us consider first, the full interval as a two subdivision.

f o = f (2.5) = 0.16 ∆x = (4.5 − 2.5) / 2 = 1.0

f1 = f (3.5) = 0.0816

f 2 = f (4.5) = 0.0494
Example on Simpson’s Rule

30 30
y y
2
f(x)=1/ x f(x)=1/ x2
20 20

10 10

0
x 0
x
0.2 0.3 0.4 0.5 0.6 0.2 0.3 0.4 0.5 0.6
Example on Simpson’s Rule
• The approximate area under the curve is

(1.0)
Area = (0.16 + 4 × 0.0816 + 0.0494)
3
= 0.1786

• where, error is 0.48% when compared to the exact value.


Example on Simpson’s Rule
• For two subdivisions:
∆x = (4.5 − 2.5) / 4 = 0.5

f o = f (2.5) = 0.16

f1 = f (3.0) = 0.111

f 2 = f (3.5) = 0.0816
f 3 = f (4.0) = 0.0625

f 2 = f (4.5) = 0.0494

(0.5)
Area = [0.16 + 4 × 0.111 + 2. × 0.0816 + 4 × 0.0625 + 0.0494]
3
= 0.17783
The error is 0.04%.
Example on Simpson’s Rule
• Let us now try a sub-division into 8 equally-spaced sections,
then ∆x = (4.5 − 2.5) / 8 = 0.25

f1 = f (2.75) = 0.132 f 2 = f (3.0) = 0.111

f 3 = f (3.25) = 0.095 f 4 = f (3.5) = 0.0816

f 5 = f (3.75) = 0.0711 f 6 = f (4.0) = 0.0625 f 7 = f (4.25) = 0.0554

(0.25)
Area = (0.16 + 4 × 0.132 + 2. × 0.111 + 4 × 0.095 + 2 × 0.0816
3
+ 4 × 0.0711 + 2 × 0.0625 + 4 × 0.0554 + 0.0494)
= 0.1778

and the error is 0.003%.


Example on Simpson’s Rule
Matlab Program

• % % Program Name: Integral_2.m


• % A MATLAB script file to compute Integral of f(x)
• % over an interval [a, b] by Simpson’s rule.
• % It calls an external function simpson.m using
• % inputs of a, b and N (number of sub-divisions).
• % N must be even.
• N = 8; a = 2.5; b = 4.5;
• exact_value = 0.177778;
• estimated_value = simpson(a, b, N)
• error = abs(100*(exact_value -
estimated_value)/exact_value)
• % ------------------------------------------------
Example on Simpson’s Rule
Function for program
% function z = simpson(a, b, N)
% function for integration by Simpson’s rule: simpson.m
M = N + 1
delta_x = (b - a)/N;
x(1) = a;
if(x(1) == 0), error('A is zero, change it')
end
sum1 = 0;
sum2 = 0;
f(1) = 1.0/(x(1)^2);
for i=2: M
x(i) = x(i-1) + delta_x;
f(i) = 1/(x(i)^2);
end
for i = 1: N/2,
sum1 = sum1 + f(2*i);
if(i == N/2) break
else sum2 = sum2 + f(2*i+1);
end
end
z =((delta_x/3)*(f(1) + 4*sum1 + 2*sum2 + f(M)));

You might also like