Lecture 13 Integration

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

Numerical integration

NEWTON-COTES FORMULA
TRAPEZOİD RULE
SİMPSON RULE
Integration and differentiation

 Derivative: the rate of change of a • Integration: summing


dependent variable with respect to instantaneous information to give a
an independent variable. total result over an interval.

slope of a function Distance traveled

d t
v(t )  y(t ) y (t )   v(t )dt
0
dt
Why do we need numerical integration?

 There are functions that cannot be integrated analytically.


 If discrete values are available for a certain situation, it is
possible to integrate the discrete data using numerical
integration methods.

Numerical integration: sometimes referred to as


quadrature.
•Construction of a square having the same
area as some curvilinear figure.
•Numerical definite integration.
Integration vs summation

A common application: determining the mean of a continuous


function:
•Calculate the center of gravity of irregular objects
•Determine the average velocity in a fluid stream.

mean or average of the continuous


mean of n discrete data points function y = f (x) for the interval from
a to b.
Newton-Cotes Formulas

 Most common numerical integration schemes:


 Replacing a complicated function or tabulated data with a polynomial that
is easy to integrate.
b b
I   f ( x)dx   f n ( x)dx
a a

f n ( x)  a0  a1 x  ...  an1 x n1  an x n nth order polynomial

Line: 1st order Series of lines or


Polynomial
polynomials
Closed and open forms of the Newton-Cotes formulas

 The closed forms: where the data points at the


beginning and end of the limits of integration are
known.

The open forms: integration limits that extend


beyond the range of the data
1-The Trapezoidal Rule

b b
I   f ( x)dx   f n ( x)dx
a a

 f (b)  f (a) 
b
I    f (a)  ( x  a) dx First order polynomial
a 
ba 

Result of integration:

f (a)  f (b)
I  (b  a) “Trapezoidal rule”
2
Error of the Trapezoidal Rule
An estimate for the local truncation error:

1 a<ξ<b
Et   f ' ' ( )(b  a ) 3
12

If the function being integrated is linear, the trapezoidal rule will be exact
because the second derivative of a straight line is zero.
Otherwise, for functions with second- and higher-order derivatives (i.e., with
curvature), some error can occur.
Example: Numerically integrate from a=0 to b=0.8:
f ( x)  0.2  25x  200x 2  675x3  900 x 4  400x5
Exact value of the integral = 1.640533

Solution:

f (a)  f (b)
I  (b  a)
2
0.2  0.232
I  (0.8  0)  0.1728
2

Truncation error:
Et  1.640533  0.1728  1.467733
 t  89.5% percent relative error
Actually, we would not know the true value.
 error estimate
1
Et   f ' ' ( )(b  a ) 3
12

f ' ' ( x)  400  4050x  10800x 2  8000x3

Average value of the second derivative:


0.8

    
2 3
( 400 4050 x 10800 x 8000 x )dx
f ' ' ( x)  0
 60
0.8  0

1 Note the same order of magnitude and sign as the true


Ea   (60)(0.8)3  2.56
12 error.
A discrepancy does exist: for an interval of this size, the
True error is 1.467733 average second gives not an accurate approximation of f
‘’(ξ ). Thus, we use the notation Ea, (the error is
approximate) rather than by using Et .
Example:
The vertical distance covered by a rocket from t=8 to t=30 seconds
is given by:

a) Use single segment Trapezoidal rule to find the distance


covered.
b) Find the true error, Et for part (a).
c) Find the absolute relative true error, for part (a).
a)
b)

c)
The Composite Trapezoidal Rule

 Improve the accuracy of the trapezoidal rule by dividing the integration


interval from a to b into a number of segments.
 The resulting equations: composite, or multiple-segment, integration
formulas.

n + 1 equally spaced base points


(x0, x1, x2, . . . , xn).  n segments of equal width:
ba
h
n
Total integral:
x1 x2 xn

I  f ( x)dx   f ( x)dx  ...   f ( x)dx


x0 x1 xn1
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
h n 1

I   f ( x0 )  2 f ( xi )  f ( xn )
2 i 1 
n 1
f ( x0 )  2 f ( xi )  f ( xn )
I  (b  a) i 1
2n
width Average height

Error for the composite trapezoidal rule


n

(b  a) 3 n  f ' ' ( )
i n
Et  
12n 3
 f ' ' ( )
i 1
i f '' i 1
n
  f ' ' ( i )  nf ' '
i 1

(b  a) 3
Approximate error Ea   2
f ''
12n
Example: Use the two-segment trapezoidal rule to estimate the integral of
f ( x)  0.2  25x  200x 2  675x3  900 x 4  400x5

from a = 0 to b = 0.8. Estimate the error. Recall that the exact value of the
integral is 1.640533

Solution. For n = 2 (h = (0.8-0)/2=0.4):


f (0)  0.2
0.2  2(2.456)  0.232
f (0.4)  2.4564 I  0.8  1.0688
2
f (0.8)  0.232
Et  1.640533  1.0688  0.57173   t  34.9%
0.8

 (400  4050 x  10800x  8000 x 3 )dx


2

f ' ' ( x)   60


−60 is the average
0
0.8  0

(0.8) 3 second derivative


Ea   2
(60)  0.64
12(2)
the error decreases as the number of segments increases
2-Simpson’s Rules

 Simpson’s 1/3 Rule:


 ( x  x1 )( x  x2 ) ( x  x0 )( x  x2 ) ( x  x0 )( x  x1 ) 
x1

I   f ( x0 )  f ( x1 )  f ( x2 ) dx
x0 
( x0  x1 )( x0  x2 ) ( x1  x0 )( x1  x2 ) ( x2  x0 )( x2  x1 ) 

a= x0 and b= x2

The result of the integration:

I   f ( x0 )  4 f ( x1 )  f ( x2 )
h
3
h  (b  a) / 2
“Simpson’s 1/3 rule” a= x0 b= x2
The label .1/3: h is divided by 3
(b  a)
I  f ( x0 )  4 f ( x1 )  f ( x2 )
6
a  x0
b  x2
(a  b) / 2  x1

 Truncation error: a (a+b)/2 b


1 5 ( 4)
Et   h f ( ) h  (b  a) / 2
90

(b  a ) 5 ( 4 )
Et   f ( ) Where a< ξ < b
2880

Simpson’s 1/3 rule is more accurate than the trapezoidal rule


EXAMPLE: Integrate
f ( x)  0.2  25x  200x 2  675x3  900 x 4  400x5
from a = 0 to b = 0.8. Estimate the error. Recall that the exact integral
is 1.640533.

Solution: n = 2 (h = 0.4):
f (0)  0.2
0.2  4(2.456)  0.232
f (0.4)  2.4564 I  0.8  1.367467
6
f (0.8)  0.232
Et  1.640533  1.367467  0.2730667   t  16.6%
(0.8) 5
Approximate error: Ea   (2400)  0.2730667
2880

−2400 is the average fourth derivative for the


interval
 The Composite Simpson’s 1/3 Rule:
Simpson’s rule can be improved by dividing the integration
interval into a number of segments of equal width

an even number of
segments must be
utilized
to implement the
method

x2 x4 xn

I  f ( x)dx   f ( x)dx  ...   f ( x)dx


x0 x2 xn2
Substituting Simpson’s 1/3 rule for each integral yields
f ( x0 )  4 f ( x1 )  f ( x2 ) f ( x2 )  4 f ( x3 )  f ( x4 )
I  2h  2h
6 6
f ( xn  2 )  4 f ( xn 1 )  f ( xn )
 ...  2h
6
Rearrange,
n 1 n2
f ( x0 )  4  f ( x ) 2  f ( x )  f ( x )
i 1, 3, 5
i
j  2, 4, 6
j n

I  (b  a )
3n

Error estimate
(b  a ) 5 ( 4 ) where f (4) is the average fourth
Ea   f
180n 4
derivative for the interval
EXAMPLE: Use composite Simson’s rule with n = 4 to estimate the integral of
f ( x)  0.2  25x  200x 2  675x3  900 x 4  400x5
from a = 0 to b = 0.8. Estimate the error. Recall that the exact integral is
1.640533.

Solution. n = 4 (h = 0.2):

f (0)  0.2 0.2  4(1.288  3.464)  2(2.456)  0.232


I  0.8  1.623467
f (0.2)  1.288 12
f (0.4)  2.4564
f (0.6)  3.464
f (0.8)  0.232 Et  1.640533  1.623467  0.017067   t  1.04%
5
Approximate error: Ea  
( 0.8)
4
(2400)  0.017067
180(4)
 Simpson’s 3/8 Rule:
A third order Lagrange polynomial can be fit to four points and
integrated to yield

I
3h
 f ( x0 )  3 f ( x1 )  3 f ( x2 )  f ( x3 ) h  (b  a) / 3
8
“Simpson’s 3/8 rule”

The 3/8 rule can also be expressed in the form of


(b  a)
I  f ( x0 )  3 f ( x1 )  3 f ( x2 )  f ( x3 )
8

Error:

3h 5 ( 4 ) (b  a)5 ( 4 )
Et   f ( )   f ( )
80 6480
•The 3/8 rule is somewhat more accurate than the 1/3 rule.
•The 3/8 rule is useful when the number of segments is odd
•The combination of 1/3 rule and 3/8 rule can be used as well.

Example: 1/3 rule to the first two segments and 3/8 rule to the last.
 Obtain an estimate with third-order accuracy across the entire
interval
EXAMPLE
(a) Use Simpson’s 3/8 rule to integrate
f ( x)  0.2  25x  200x 2  675x3  900 x 4  400x5
from a = 0 to b = 0.8.

(b) Use it in conjunction with Simpson’s 1/3 rule to integrate the same
function for five segments.

Solution.
(a) A single application of Simpson’s 3/8 rule requires four equally spaced
points:

f (0)  0.2
f (0.2667)  1.432724
f (0.5333)  3.487177
I
0.8
0.2  3(1.432724  3.487177)  0.232  1.51970
8
f (0.8)  0.232
(b) The data needed for a five-segment application (h = 0.16) are
f (0)  0.2 f (0.48)  3.186015
f (0.16)  1.296919 f (0.64)  3.181929
f (0.32)  1.743393 f (0.8)  0.232
The integral for the first two segments is obtained using Simpson’s 1/3 rule:

I
0.32
0.2  4(1.296919)  1.743393  0.3803237
6

For the last three segments, the 3/8 rule can be used to obtain

I
0.48
1.743393  3(3.186015  3.181929)  0.232  1.264754
8

The total integral is computed by summing the two results:

I  0.3803237  1.264754  1.645077


Newton-Cotes closed integration formulas. step size is given by h = (b − a)/n.
clc
clear
%composite trapezoidal
segment=50;
nokta=segment+1;
a=1;
b=5;
h=(b-a)/segment;
x=a:h:b;
% the function
y=x.^2-sin(x);

% calculate integral
I=(h/2)*(y(1)+2*sum(y(2:nokta-1))+y(nokta))

I=

41.0811
clc
clear
%simpson 1/3 rule
segment=4;
nokta=2*segment+1;
a=1;
b=5;
h=(b-a)/(2*segment);
x=a:h:b;
% the function
y=x.^2-sin(x);
% calculate the integral
I=(h/3)*(y(1)+4*sum(y(2:2:nokta-1))+2*sum(y(3:2:nokta-
2))+y(nokta))

I=

41.0766
trapz Trapezoidal numerical integration.
Z = trapz(X,Y) computes the integral of Y with respect to X using
the trapezoidal method.

quad Numerically evaluate integral, adaptive Simpson quadrature.


Q = quad(FUN,A,B) tries to approximate the integral of scalar-valued
function FUN from A to B to within an error of 1.e-6 using recursive
adaptive Simpson quadrature. FUN is a function handle. The function
Y=FUN(X) should accept a vector argument X and return a vector result
Y, the integrand evaluated at each element of X.

>> t=[0 10 15 20 22 30];


>> v=[0 227 363 517 603 902];
>> I1=trapz(t,v)
I1 =
11950

>> I2=quad('x.^2-sin(x)',1,5)
I2 =
41.0767
Integration With Unequal Segments

 All formulas for numerical integration have been based


on equispaced data points.
 In practice, there are many situations where we must
deal with unequal-sized segments.
 Example: experimentally derived data
 One method is to apply the trapezoidal rule to each
segment and sum the results:

hi = the width of segment i.

Similar to composite trapezoidal rule


EXAMPLE. The information in Table is generated using
f ( x)  0.2  25x  200x 2  675x3  900 x 4  400x5
Determine the integral for these data. Recall that the correct answer is
1.640533.

Solution.

Absolute percent relative error of εt = 2.8%.


Multiple Integrals

 Multiple integrals are widely used in engineering and science


 Example: a general equation to compute the average of a two-
dimensional function:
d
 b f ( x, y )dx dy
c  a 
double integral.
f 
(d  c)(b  a )

d
 b f ( x, y )dx dy  b  d f ( x, y )dy dx

c  a  a  c 

The integral in one of the dimensions is evaluated first. The


result of this first integration is integrated in the second
dimension.
A numerical double integral:
 First, methods such as the composite trapezoidal or Simpson’s
rule would be applied in the first dimension with each value of
the second dimension held constant.
 Then the method would be applied to integrate the second
dimension.
EXAMPLE: Using Double Integral to Determine Average Temperature

Suppose that the temperature of a rectangular heated plate is described by the


following function:

T ( x, y)  2 xy  2 x  x 2  2 y 2  72

If the plate is 8 m long (x dimension) and 6 m wide (y dimension), compute the


average temperature.
Solution.
T ( x, y)  2 xy  2 x  x 2  2 y 2  72

6 A simple average of these values is 47.33


(analytical result= 58.66667)
3

0
0 4 8m
1- Apply the two-segment trapezoidal rule along the x dimension for each
y value.
These values are integrated along the y-dir to give the final result of 2544.
Dividing this by the area yields the average temperature as 2544/(6 × 8) =
53.
n 1
f ( x0 )  2 f ( xi )  f ( xn )
Two-segment (n=2) trapozoidal rule: I  (b  a) i 1
2n

2- Apply a single-segment Simpson’s 1/3 rule in the same fashion.


This results in an integral of 2816 and an average of 58.66667
(b  a)
Simpson’s 1/3 rule: I  f ( x0 )  4 f ( x1 )  f ( x2 )
6

You might also like