Вы находитесь на странице: 1из 28

Numerical Analysis

Integration
Topic: Trapizoidal Rule
Integration
• The inverse process of differentiation
• Dictionary definition of integrate - “to bring together,
as parts, into a whole; to unite; to indicate the total
amount”
• Mathematically, it is the total value or summation of
f(x)dx over a range of x. In fact the integration
symbol is actually a stylized capital S intended to
signify the connection between integration and
summation.
What is Integration
Integration:
The process of measuring the f(x)
area under a function plotted on
a graph.
b
I   f ( x )dx
a

Where:
f(x) is the integrand
a= lower limit of integration
a x b
b= upper limit of integration
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:
f(x)
Trapezoidal rule,
Simpson’s method,
Romberg Integration, and
Gaussian quadratures.

x
Newton-Cotes Integration
• It is a Common numerical integration scheme
• Based on the strategy of replacing a complicated function or
tabulated data with some approximating function:

b b
I   f  x  dx   f  x  dx
n
a a fn(x) is an nth order
f n  x   a0  a1 x  ....  an x n polynomial

Trapezoidal Rule is based on the Newton-Cotes Formula that states


we can approximate the integrand as an nth order polynomial and
integrate.
Newton-Cotes Integration
The approximation of an integral by the area under
- a first order polynomial
- a second order polynomial

f(x) f(x)

x x
a b a b
We can also approximate the integral by using a series of polynomials
applied piece-wise.
Single trapezoid
You can fit a straight line and / or a polynomial between two data points.

f(x) f(x)

a b x a b x
Single trapezoid
You can just fit a straight
line as approximation to f(x)
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.
a b x
Single trapezoid
• Figure shows the area bounded by a
curve for f(x) in the range from x = y = f(x)
a to x = b. y
• If we have only one interval as
shown, on both ends of which the
values of f(x) are given, then f(a) f(b)
b
straight line can easily be used i.e.,
a
• we may construct a single
trapezoid in the 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
Single Segment Trapezoidal rule
In this case we fit a straight line as approximate function between a
and b and then find area under the curve. This area will be full of
errors. However, let us find the area.

(b  a )
area  f (b)(b  a )   f (a)  f (b)  f(x)
2

(b  a ) f(a)
area   2 f (b)   f (a)  f (b) 
2
f(b)

(b  a )
area   f (a)  f (b)
2
a b x
Error in the area from the Trapezoidal Rule

1
error   f ' '    b  a 
3

12
where a    b
When the function being integrated is linear, the area under curve
from the trapezoidal rule will give exact results as its error will be
zero.

When the function has the second and higher order derivatives (that
is with curvature) then the error can occur.

A reasonable estimate of ξ is the average value of b and a


Example 1
The vertical distance covered by a rocket from t=8 to
t=30 seconds is given by:
30
x   2000 ln 14000 /(140000  2100t )  9.8t  dt
8

a) Use single segment Trapezoidal rule to find the


distance covered.
b) Find the true error for part (a).
c) Find the absolute relative true error for part (a).
Example 1, continued
 f ( a )  f ( b ) a 8 b  30
a) I  ( b  a ) 
 2
 140000 
f ( t )  2000 ln    9.8t
140000  2100t 

 140000 
f ( 8 )  2000 ln    9.8( 8 )  177.27 m / s
140000  2100( 8 )

 140000 
f ( 30 )  2000 ln    9.8( 30 )  901.67 m / s
140000  2100( 30 )

177.27  901.67 
I  (30  8)    11868 m
 2
Example 1 (continued)
b) The exact value of the above integral is


30
 140000  
x    2000 ln    9.8t dt  11061 m
8 140000  2100t  
Et  True Value  Approximate Value
 11061  11868
 807 m
c)
The absolute relative true error,t , would be
11061  11868
t   100  7.2959%
11061
Multiple Application of the Trapezoidal Rule

• We can Improve the


accuracy by dividing
f(x) Strips = 11

the interval into a


number of smaller
segments;
• Then Apply the
method to each
segment;
• Resulting equations x
a b
are called multiple-
application or
composite integration
formulas.
Area under the curve: Many trapezoid
When many sufficiently
small strips are added f(x) Strips = 11
we can generate the
area under the curve
from x = a to x = b with
very small error.

b
gb  g a   f ( x )dx
a a x b

Here with 18 Strips of different heights and same Width


we get the area under the curve.
Many trapezoids
The same area for two sub-intervals is given by following:
(b  a )
area   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 )
area   f ( xo )  2 f ( x1 )  2 f ( x2 )  2 f ( x3 )  f ( x4 )
2 4

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

(b  a )
area  [ f ( xo )  2 f ( x1 )  2 f ( x2 )  2 f ( x3 )  2 f ( x4 )
28
 2 f ( x5 )  2 f ( x6 )  2 f ( x7 )  f ( x8 ) ]
The Multiple Application: Trapezoidal Rule
One way to improve the accuracy of the trapezoidal rule is to
divide the integration interval from a to b into a number of
segments and apply the method to each segment.

ba
h a  x0 b  xn
n
x1 x2 xn

I  
x0
f ( x ) dx   f ( x ) dx   
x1

xn 1
f ( x ) dx

Substituting the trapezoidal rule for each integral yields:

f ( x0 )  f ( x1 ) f ( x1 )  f ( x2 ) f ( xn 1 )  f ( xn )
I h h  h
2 2 2

18
Integral for N trapezoids
The integral in general using N compound trapezoids within the
range, is given by
h
Integral   f ( x0 )  2 f ( x1 )  2 f ( x2 )    2 f ( xN 1 )  f ( xN ) 
2
(b  a)
where, h
N
xi  a  nh, n  0, 1, 2, 3, , N

The error in the computed integral value is given by

(b  a )(h) 2 ( 2 )
error   f ( )
12
Example 2
The vertical distance covered by a rocket from to seconds is
given by:
30
  140000  
x    2000 ln    9.8t dt
8 140000  2100t  

a) Use two-segment Trapezoidal rule to find the distance covered.


b) Find the true error for part (a).
c) Find the absolute relative true error for part (a).
Example 2, continued
a) The solution using 2-segment Trapezoidal rule is
ba
I  f (a)  2 f (a  ih)  f (b)
2n

For two segments: n = 2; a= 8 ; b = 30; h = (b – a)/n = 11


Then: 30  8
I  f (8)  2 f (8  11)  f (8  2 11)
2( 2 )

22
  f ( 8 )  2 f ( 19 )  f ( 30 )
4
22
 177.27  2( 484.75 )  901.67
4
 11266 m
Solution (cont)
b) The exact value of the above integral is
30
  140000  
x    2000 ln    9.8t dt  11061 m
8 140000  2100t  

so the true error is E t  True Value  Approximat e Value


 11061  11266

c) The absolute relative true error, t , would be

True Error 11061  11266


t   100   100
True Value 11061

 1.8534%
Solution (cont)
30
  140000  
x  2000 ln 140000  2100t   9 .8t dt

8

Table 1 gives the values obtained using multiple segment Trapezoidal


rule:
n Value Et t % a %
1 11868 -807 7.296 ---
2 11266 -205 1.853 5.343
3 11153 -91.4 0.8265 1.019
4 11113 -51.5 0.4655 0.3594
5 11094 -33.0 0.2981 0.1669
6 11084 -22.9 0.2070 0.09082
7 11078 -16.8 0.1521 0.05482
8 11074 -12.9 0.1165 0.03560
Example 3: on integration
Simulate the area under curve for f(x) =
1/x2 , using trapezoidal rule with one, two,
four, eight and sixteen equal subdivisions f(x)
between x = 2.5 and 4.5.
Solution: Consider the full interval as a
single subdivision.

f o  f (2.5)  0.16
f 1  f (4.5)  0.0494
a b x
The approximate area under the curve is

(4.5  2.5)
Area  (0.16  0.0494)  0.2094
2 1
Example 3
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

(b  a )
area   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 3
If we divide the interval into four segments, then
h  (4.5  2.5) / 4  0.5 f o  f ( 2.5)  0.16

f 1  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 )
area   f ( xo )  2 f ( x1 )  2 f ( x2 )  2 f ( x3 )  f ( x4 )
2 4
(4.5  2.5)
area  [0.16  2.  (0.111  0.0816  0.0625)  0.0494]
2 4
area  0.1799
Example 3
c integral over an interval [a, b] by trapezoidal rule.
c It calls an external function trapez.m using inputs of a, b
and N (number of sub-divisions).
• Initialize N = 8; a = 2.5; b = 4.5; exact_value =
0.177778;
• Compute delta_x = (b - a)/N;
• Initialize x(1) = a;
• decide : if(x(1) == 0), error('A is zero, change it')
• Initialize values: sum = 0; f(1) = 1.0/(x(1)^2);
• Start a loop1 : from i=2 to N+1
• (a) compute x(i) = x(i-1) + delta_x;
• (b) compute f(i) = 1/(x(i)^2);
• End of loop1
• Start another loop2: from i=2 to N,
• compute sum = sum + f(i);
• End of the loop2
• Compute integral = (delta_x/2)*(f(1) + 2*sum + f(N+1));
• error = abs(100*(exact_value - estimated_value)/exact_value)
Example 3
• The exact value of the Number Area from Percentage
integral is 0.17778. of equal Trapezoidal Error
• A file for algorithm on Sub- Rule
trapezoidal rule is given divisions
and its use for several 1 0.2094 17.8%
sub-intervals is shown as
Table. 2 0.1863 4.8%
• It shows that the error
decreases rapidly as the 4 0.1799 1.2%
number of subdivisions in
the range of integration is 8 0.1783 0.3%
increased.
16 0.1779 0.08%

32 0.1778 0.02%