Anda di halaman 1dari 23

CLASS: 04

ME 262: Numerical Analysis Sessional


Department of Mechanical Engineering, BUET
Numerical Integration
15 May 2011
2
ME262 Numerical Analysis Sessional
15 May 2011 ME262 Numerical Analysis Sessional
3
Graphical Representation of Integral
Integral = area under the curve
15 May 2011 ME262 Numerical Analysis Sessional
4
x
0
x
1
x
n
x
n-1
x
f(x)
Graphical Representation of Integral
15 May 2011 ME262 Numerical Analysis Sessional
5
f
n
(x): linear f
n
(x): quadratic f
n
(x): higher-order
polynomial
Numerical Approximation of Integral
Newton-Cotes Closed Formulae -- Use both end points
Trapezoidal Rule : Linear
Simpsons 1/3-Rule : Quadratic
Simpsons 3/8-Rule : Cubic
15 May 2011 ME262 Numerical Analysis Sessional
6
6
Approximate the function by a parabola
| |
0 1 2
( ) ( ) 4 ( ) ( )
3
b
a
h
f x dx f x f x f x = + +
}
x
0
x
1
x
f(x)
x
2
h h
L(x)
Simpsons 1/3-Rule
15 May 2011 ME262 Numerical Analysis Sessional
7
n
a b
h

=
Piecewise Quadratic approximations
x
0
x
2
x
f(x)
x
4
h h x
n-2
h x
n
...
h x
3
x
1
x
n-1
Composite Simpsons Rule
15 May 2011 ME262 Numerical Analysis Sessional
8
8
i = 0 1 2 3 4 5 6 . . n-1 n
Applicable only if
the number of segments is
even
| |
0 1 2
( ) 4 ( ) ( )
3
h
I f x f x f x = + +

`
)

n-1 n-2
0 i i n
i=1,3,5 i=2,4,6
h
I = f(x )+ 4 f(x )+ 2 f(x )+ f(x )
3
Composite Simpsons 1/3 Rule
15 May 2011 ME262 Numerical Analysis Sessional
9
Composite Simpsons 1/3 Rule
Algorithm for Uniform Spacing (Equal Segments)
n
a b
h

=
2
n
m =
( ) ( ) ( )
1
3
m
k=
( +

2k-2 2k-1 2k
h
I = f x + 4f x f x
x
0
x
1
x
2
x
3
x
4
x
5
x
6
. . . x
n-1
x
n
15 May 2011 ME262 Numerical Analysis Sessional
10
Composite Simpsons 1/3 Rule
Evaluate the integral
n = 2
n = 4
dx xe I
4
0
x 2
}
=
| |
| | % 96 . 57 411 . 8240 4 ) 2 ( 4 0
3
2
) 4 ( ) 2 ( 4 ) 0 (
3
8 4
= = + + =
+ + =
c e e
f f f
h
I
| |
| | % 70 . 8 975 . 5670 4 ) 3 ( 4 ) 2 ( 2 ) ( 4 0
3
1
) 4 ( ) 3 ( 4 ) 2 ( 2 ) 1 ( 4 ) 0 (
3
8 6 4 2
= = + + + + =
+ + + + =
c e e e e
f f f f f
h
I
15 May 2011 ME262 Numerical Analysis Sessional
11
MATLAB Code
dx xe I
4
0
x 2
}
=
function s = fvalue(x)
s = x*exp(2*x);
a = 0; b = 4; n = 4;
h = (b - a)/n;
S = 0; m = n/2;
for k = 1 : m
S = S + fvalue(a+h*(2*k-2))+4*fvalue(a+h*(2*k-1))+fvalue(a+h*2*k);
end
I = h*S/3;
disp(I);
fvalue.m
simpsn3.m
15 May 2011 ME262 Numerical Analysis Sessional
12
12
Approximate the function by a cubic polynomial
Simpsons 3/8-Rule
| |
0 1 2 3
3
( ) ( ) 3 ( ) 3 ( ) ( )
8
b
a
h
f x dx f x f x f x f x = + + +
}
( ) ( ) ( ) ( )
1
3
m
k=
( + +

3k-3 3k-2 3k-1 3k
3h
I = f x + 3f x f x f x
8
x
0
x
1
x
f(x)
x
2
h h
L(x)
x
3
h
n must be a multiple of 3
3
n
m =
15 May 2011 ME262 Numerical Analysis Sessional
13
Example: Simpsons Rules
Evaluate the integral
Simpsons 1/3-Rule
Simpsons 3/8-Rule
dx xe
4
0
x 2
}
| |
% .
.
. .
. ) (
) ( ) ( ) (
96 57
926 5216
411 8240 926 5216
411 8240 e 4 e 2 4 0
3
2
4 f 2 f 4 0 f
3
h
dx xe I
8 4
4
0
x 2
=

=
= + + =
+ + ~ =
}
c
| |
% 71 . 30
926 . 5216
209 . 6819 926 . 5216
209 . 6819 832 . 11923 ) 33933 . 552 ( 3 ) 18922 . 19 ( 3 0
8
) 4/3 ( 3
) 4 ( f )
3
8
( f 3 )
3
4
( f 3 ) 0 ( f
8
h 3
dx xe I
4
0
x 2
=

=
= + + + =
(

+ + + ~ =
}
c
15 May 2011 ME262 Numerical Analysis Sessional
14
MATLAB Code
dx xe I
4
0
x 2
}
=
function s = fvalue(x)
s = x*exp(2*x);
a = 0; b = 4; n = 6;
h = (b - a)/n;
S = 0; m = n/3;
for k = 1 : m
S = S + fvalue(a+h*(3*k-3))+(3*fvalue(a+h*(3*k-2)))+(3*fvalue(a+h*(3*k-1)))+fvalue(a+h*3*k);
end
I = (3*h*S)/8;
disp(I);
fvalue.m
simpsn38.m
15 May 2011 ME262 Numerical Analysis Sessional
15
MATLAB Code (Combined Rules)
x=[0.5 2 3 4 6 8 10 11 ]
f=[320 268 233 226 226 212 142 107 ]
n=8;
h =x(2) - x(1);
k = 1;
sum = 0;
for j =2:n
if j~=n
hf = x(j+1) - x(j);
else
hf = abs(h+1); % this is for last value
end
if abs(h-hf)<0.000001
if k == 3
sum = sum + (h/3)*(f(j-3)+(4*f(j-2))+f(j-1));
k = k-1;
else
k = k+1;
end
else
15 May 2011 ME262 Numerical Analysis Sessional
16
MATLAB Code
if k ==1
sum = sum + (h/2)*(f(j-1)+f(j));
else
if k==2
sum = sum + (h/3)*(f(j-2)+(4*f(j-1))+f(j));
else
sum = sum + ((3*h)/8)*(f(j-3)+(3*f(j-2))+(3*f(j-1))+f(j));
end
k=1;
end
end
h = hf;
end
disp(sum);
15 May 2011 ME262 Numerical Analysis Sessional
17
MATLAB Functions
Command Description
quad (function,a,b,tol) Uses an adaptive Simpsons rule to compute the integral of
the function function with a as the lower integration limit
and b as the upper limit. The parameter tol is optional. tol
indicates the specified error tolerance.
trapz (x,y) Uses trapezoidal integration to compute the integral of y
with respect to x, where the array y contains the function
values at the points contained in the array x.
(use of the trapz function)
>> x = linspace(0, pi, 10);
>> y = sin(x);
>> trapz(x, y)
ans =
1.97965081121648
(use of the quad function)
>> quad('sin(x)', 0, pi, 0.001)
ans =
1.99999349653496
Curve Fitting
15 May 2011
18
ME262 Numerical Analysis Sessional
15 May 2011 ME262 Numerical Analysis Sessional
19
MATLAB Functions
Command Description
polyfit(x,y,n) Two vectors x and y fits a polynomial of order n through
the data points (x
i
,y
i
) and returns (n+1) coefficients of the
powers of x in the vector a. The coefficients are arranged
in the decreasing order of the powers of x, i.e.,
a = [a
n
a
n-1
. a
1
a
0
]
polyval(a,x) A data vector x and the coefficients of a polynomial in a
row vector a, the command y = polyval(a,x) evaluates the
polynomial at the data points x
i
.
15 May 2011 ME262 Numerical Analysis Sessional
20
Example
x = 5:5:50;
y = [16 25 32 33 38 36 39 40 42 42];
p = polyfit(x,y,2); % fit a line (2nd order polynomial)
F_fit = polyval(p,x); % evaluate the polynomial at new points
plot(x,y,'o',x,F_fit); % plot the data and fitted curve
xlabel('x');
ylabel('y');
title ('The variation of x with y');
x 5 10 15 20 25 30 35 40 50
y 16 25 32 33 38 36 39 49 42
Example (Contd.)
15 May 2011 ME262 Numerical Analysis Sessional
21
15 May 2011 ME262 Numerical Analysis Sessional
22
Example
% Power curve fit example y = a x^b ---- log(x) = log(a)+ b log(y)
x = 5:5:50;
y = [16 25 32 33 38 36 39 40 42 42];
xbar = log10(x);
ybar = log10(y);
p = polyfit(xbar,ybar,1); % fit a line (1st order polynomial) p=[p1 p0]
a = 10^p(2); % since p(2) is p0
ynew = a.*(x).^p(1);
plot(x,y,'o',x,ynew); % plot the data and fitted curve
xlabel('x');
ylabel('y');
title ('The variation of x with y');
disp([p]);
disp([p(1) a]);
x 5 10 15 20 25 30 35 40 50
y 16 25 32 33 38 36 39 49 42
THANK YOU
5/15/2011
23
Its all about today!!

Anda mungkin juga menyukai