Professional Documents
Culture Documents
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
+
= =
=
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
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
= + + + =
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
=
t
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);