You are on page 1of 14

- - Basic Ideas Basic Ideas

- - Symbolic Symbolic vs vs. . Numerical Integration Numerical Integration


- - Trapezoid Rule Trapezoid Rule
- - Simpson Simpson s Rule s Rule
- - MATLAB quad and quad MATLAB quad and quad8 8 Functions Functions
Numerical Methods for Numerical Methods for Civil Civil Engineers Engineers
Mongkol J I RAVACHARADET
S U R A N A R E E INSTITUTE OF ENGINEERING
UNIVERSITY OF TECHNOLOGY SCHOOL OF CIVIL ENGINEERING
Lecture
Lecture
9
9
Numerical I ntegration
Numerical I ntegration
BASIC BASIC IDEAS IDEAS
) ( curve under Area ) ( x f dx x f I
b
a
= =

Approximated by piecewise-linear:
Area = Sum of trapezoidal regions
f (x)
a b
Trapezoidal
region
Node
x
Symbolic Integration with MATLAB
>> I = int(f) % Indefinite integral
>> I = int(f, v) % Designating integration variable
>> I = int(f, a, b) % Definite integral on close interval
>> I = int(f, v, a, b)
where f = symbolic expression
Variables must be define as symbolic by sym or syms
>> x = sym(x), y = sym(y), z = sym(z)
or >> syms x y z
Symbolic Math Toolbox Symbolic Math Toolbox
Symbolic Math Toolboxes incorporate symbolic computation into the numeric
environment of MATLAB.
dx c x I
b
a

= ) ( : Example
3
>> syms x a b c
>> I = int(x^3-c,x,a,b)
I =
1/4*b^4-c*b-1/4*a^4+c*a
What is Integration? What is Integration?
dx x f I
b
a

= ) (
The integral is equivalent to the area under the curve.
f(x)
x
a b
I
Cross-sectional area of a river
Examples of how integration is used to evaluate areas Examples of how integration is used to evaluate areas
Net force due to
wind blowing
against the building
Newton Newton- -Cotes Formulas Cotes Formulas
Replace a complicated function with a polynomial that is easy to integrate
dx x f I
b
a

= ) ( dx x f
b
a
n

) (
n
n
n
n n
x a x a x a a x f
1
1
1 0
) ( where
+

+ + + + = L
f(x)
x
a b
Approximate area by a straight line
f(x)
x
a b
Approximate area by a parabola
Trapezoidal Rule Trapezoidal Rule
x
b a
f(a)
f(b)
f(x)
f
1
(x)
1
st
degree polynomial = Linear line
) (
) ( ) (
) ( ) (
1
a x
a b
a f b f
a f x f

+ =
2
) ( ) (
) ( ) (
1
b f a f
a b dx x f I
b
a
+
= =

Area of trapezoid = width x average height


width = b a = h
average height =
2
) ( ) ( b f a f +
f
1
f
2
x
1
x
2
f(x)
x
3
x
4
x
5
f
3
f
4
f
5

=
5
1
) (
x
x
dx x f

+
2
1
) (
x
x
dx x f

+
3
2
) (
x
x
dx x f

+
4
3
) (
x
x
dx x f

5
4
) (
x
x
dx x f
Composite Trapezoidal Rule Composite Trapezoidal Rule
Improve accuracy by dividing interval into subintervals
f(x)
x
1
= a
x
1
x
2
x
3
x
4
x
5
x
6
x
n
= b
h =
b a
n
There are n segment with equal width:
h =
b a
n
dx x f dx x f dx x f I
n
n
x
x
x
x
x
x

+ + + =
1
3
2
2
1
) ( ) ( ) ( L
2
) ( ) (
2
) ( ) (
2
) ( ) (
1
3 2 2 1
n n
x f x f
h
x f x f
h
x f x f
h I
+
+
+
+
+
+
=

L
2 2 2
1 3 2 2 1 n n
f f
h
f f
h
f f
h I
+
+ +
+
+
+
=

L

+ + =

=
n
n
i
i
f f f
h
I
1
2
1
2
2
Substitute h =
b a
n
MEAN
f(x)
x
1
x
2
x
3
x
4
x
5
x
6
f
1
f
2
f
3
f
4 f
5
f
6
width
average height
n
f f f
a b I
n
n
i
i
2
2
) (
1
2
1

+ +
=

=
Example 9-1 Use Trapezoidal rule to numerically integrate
5 4 3 2
400 900 675 200 25 2 . 0 ) ( x x x x x x f + + + =
from a = 0 to b = 0.8. Note that the exact value of the integral can be determined
analytically to be 1.640533.
or using MATLABs Symbolic Math Toolbox :
>> x = sym(x)
>> I=int(0.2+25*x-200*x^2+675*x^3-900*x^4+400*x^5,x,0,0.8)
I = 3076/1875 = 1.6405
Single Trapeziod: f(0) = 0.2 and f(0.8) = 0.232
1728 . 0
2
232 . 0 2 . 0
) 0 8 . 0 (
2
) ( ) (
) (
=
+
=
+
=
b f a f
a b I
% 5 . 89 % 100
6405 . 1
1728 . 0 6405 . 1
=

=
t

f(x)
0 0.8
Error
Integral estimate
2 Trapeziods: n = 2 (h = 0.4) :
f(0) = 0.2 f(0.4) = 2.456 f(0.8) = 0.232
n
f f f
a b I
n
n
i
i
2
2
) (
1
2
1

+ +
=

=
0688 . 1
4
232 . 0 ) 456 . 2 ( 2 2 . 0
) 8 . 0 ( =
+ +
= I
% 9 . 34 % 100
6405 . 1
0688 . 1 6405 . 1
=

=
t

Trapezoidal numerical integration


>> z = trapz(y) % Integral of y with unit spacing
>> z = trapz(x, y) % Integral of y with respect to x
o
o
90
0
Example: sin x dx

>> angle = 0:15:90;


>> x = (pi*angle/180);
>> y = sin(x);
>> z = trapz(x,y)
>> z =
0.9943
MATLAB MATLAB s s trapz trapz function function
Example 9-2 Use MATLABs trapz function to numerically integrate
5 4 3 2
400 900 675 200 25 2 . 0 ) ( x x x x x x f + + + =
from a = 0 to b = 0.8. Note that the exact value of the integral can be determined
analytically to be 1.640533.
For n = 2 : >> x = linspace(0,0.8,3);
>> fx = 0.2+25*x-200*x.^2+675*x.^3-900*x.^4+400*x.^5;
>> I = trapz(x,fx)
I =
1.0688
n
2
3
4
5
6
I
1.0688
1.3639
1.4848
1.5399
1.5703

t
34.9
16.5
9.49
6.13
4.28
Using higher-order polynomials to connect the points
f(x)
x
Simpsons 1/3 rule
Quadratic connecting 3 points
f(x)
x
Simpsons 3/8 rule
Cubic connecting 4 points
Simpson Simpson s Rules s Rules
f(x)
x
0
Quadratic connecting 3 points
x
2
x
1
f(x
0
)
f(x
2
)
f(x
1
)
f
2
(x)
2 2
0 0
2
( ) ( )
x x
x x
I f x dx f x dx =

1 2
2 0
0 1 0 2
0 2
1
1 0 1 2
0 1
2
2 0 2 1
( )( )
( ) ( )
( )( )
( )( )
( )
( )( )
( )( )
( )
( )( )
x x x x
f x f x
x x x x
x x x x
f x
x x x x
x x x x
f x
x x x x

=


+


+

[ ]
2 0
0 1 2
( ) 4 ( ) ( ) ,
3 2
x x h
I f x f x f x h

+ + =
Simpson Simpson s 1/3 Rules s 1/3 Rules
Example 9-3 Use Simpsons 1/3 rule to numerically integrate
5 4 3 2
400 900 675 200 25 2 . 0 ) ( x x x x x x f + + + =
from a = 0 to b = 0.8. Note that the exact value of the integral can be determined
analytically to be 1.640533.
Solution: n = 2 (h = 0.4) :
f(0) = 0.2 f(0.4) = 2.456 f(0.8) = 0.232
3675 . 1 ) 232 . 0 ) 456 . 2 ( 4 2 . 0 (
3
4 . 0
= + + = I
% 6 . 16 % 100
6405 . 1
3675 . 1 6405 . 1
=

=
t

Composite Simpson Composite Simpson s 1/3 Rules s 1/3 Rules


number even , where ) ( ) ( ) (
2
4
2
2
0
=

= + + + =

n
n
a b
h dx x f dx x f dx x f I
n
n
x
x
x
x
x
x
L
[ ] [ ] [ ]
n n n
f f f
h
f f f
h
f f f
h
I + + + + + + + + + =
1 2 4 3 2 2 1 0
4
3
4
3
4
3
L

+ + +

=
) ( ) ( 2 ) ( 4 ) (
3
) (
1
5 , 3 , 1
2
6 , 4 , 2
0 n
n
i
n
j
j i
x f x f x f x f
n
a b
I
1
4
2
4
2
4
2
4
2
4
1
1 4 1
1 4 1
1 4 1
4
1 4 1
1 1
a b
Example 9-4 Use composite Simpsons 1/3 rule with n = 4 to numerically integrate
5 4 3 2
400 900 675 200 25 2 . 0 ) ( x x x x x x f + + + =
from a = 0 to b = 0.8. Note that the exact value of the integral can be determined
analytically to be 1.640533.
Solution: n = 4 (h = 0.2) :
f(0) = 0.2 f(0.2) = 1.288 f(0.4) = 2.456 f(0.6) = 3.464 f(0.8) = 0.232
624 . 1 ) 232 . 0 ) 456 . 2 ( 2 ) 464 . 3 288 . 1 ( 4 2 . 0 (
) 4 ( 3
8 . 0
= + + + + = I
% 04 . 1 % 100
6405 . 1
624 . 1 6405 . 1
=

=
t

+ + +

=
) ( ) ( 2 ) ( 4 ) (
3
) (
1
5 , 3 , 1
2
6 , 4 , 2
0 n
n
i
n
j
j i
x f x f x f x f
n
a b
I
2
( ) ( )
b b
a a
I f x dx f x dx =

f(x)
x
Cubic connecting 4 points
[ ]
( )
0 1 2 3
3 0
3
( ) 3 ( ) 3 ( ) ( )
8
/ 3
h
I f x f x f x f x
h x x
+ + +
=
x
0
x
1
x
2
x
3
a b
Simpson Simpson s 3/8 Rules s 3/8 Rules
[ ] ) ( ) ( 3 ) ( 3 ) (
8
) (
3 2 1 0
x f x f x f x f
a b
I + + +

= or
3
1
( )
12
h f

5 (4)
1
( )
90
h f
5 (4)
3
( )
80
h f
Trapezoid
Simpsons 1/3
Simpsons 3/8
Simpsons 3/8 rule is used when number of segments is odd.
h b a =
2
b a
h

=
3
b a
h

=
Method E
t
E
t
Interval
width
3
( )
( )
12
b a
f

5
(4)
( )
( )
2880
b a
f

5
(4)
( )
( )
6480
b a
f

Truncation Errors Truncation Errors


Apply Simpsons 1/3 and 3/8 Rules
To handle multiple application with odd number of intervals
f(x)
x
1/3 rule 3/8 rule
Example 9-5 Use Simpsons 1/3 + 3/8 rule with n = 5 to numerically integrate
5 4 3 2
400 900 675 200 25 2 . 0 ) ( x x x x x x f + + + =
from a = 0 to b = 0.8. Note that the exact value of the integral can be determined
analytically to be 1.640533.
f(x)
x
1/3 rule 3/8 rule
0 0.16 0.32 0.48 0.64 0.8
Solution: n = 5 (h = 0.16) :
f(0) = 0.2 f(0.16) = 1.297
f(0.32) = 1.743 f(0.48) = 3.186
f(0.64) = 3.182 f(0.8) = 0.232
% 274 . 0 % 100
6405 . 1
645 . 1 6405 . 1
=

=
t

For the first two segments use Simpsons 1/3 :


380 . 0 ) 743 . 1 ) 297 . 1 ( 4 2 . 0 (
3
16 . 0
= + + = I
For the last three segments use Simpsons 3/8 :
265 . 1
) 232 . 0 ) 182 . 3 186 . 3 ( 3 743 . 1 (
8
48 . 0
=
+ + + = I
Total integral : I = 0.380 + 1.265 = 1.645
>> q = quad(f',a,b)
Approximates the integral of f(x) from a to b within a relative
error of 1e-3 using an adaptive recursive Simpson's rule.
quad : low order method, quad8: high order method
f' is a string containing the name of the function.
>> quad(sin, 0, pi/2)
Function f must return a vector of output values if given
a vector of input values.
MATLAB MATLAB s s quad quad and and quad8 quad8 Functions Functions
0.8
2 3 4 5
0
Example: 0.2 25 200 675 900 400 x x x x x dx + + +

function p = poly5(x)
p=0.2+25*x-200*x.^2+675*x.^3-900*x.^4+400*x.^5;
poly5.m
>> quad(poly5, 0, 0.8)
ans =
1.6405
-1
- 0.5
0
0.5
1
-1
-0. 5
0
0.5
1
0
2
4
6
8
10
Example: Computing the Length of a Curve
[ ]
( ) sin(2 ), ( ) cos( ), ( )
where 0, 3
x t t y t t z t t
t
= = =

>> t = 0:0.1:3*pi;
>> plot3(sin(2*t), cos(t), t)
Length of the curve:
Norm of derivative
3
2 2
0
4cos(2 ) sin( ) 1 t t dt

+ +

hcurve.m
function f = hcurve(t)
f = sqrt(4*cos(2*t).^2 + sin(t).^2 + 1);
>> len = quad(hcurve,0,3*pi)
len =
17.2220
Example: Double Integration
max max
min min
( , )
y x
y x
f x y dx dy

>> xmin = pi; xmax = 2*pi;
>> ymin = 0; ymax = pi;
>> result = dblquad(integrnd,xmin,xmax,ymin,ymax);
result =
-9.8698
For example: f (x, y) = y sin(x) + x cos(y)
integrnd.m
function out = integrnd(x,y)
out = y*sin(x) + x*cos(y);

You might also like