You are on page 1of 20

ME 262: Numerical Analysis Sessional

CLASS: 05
Course Teachers :
Dr. Md. Mamun
MD. JAMIL HOSSAIN
Mohd. Aminul Hoque
Sourav Saha
Satyajit Mojumder
Saif Khan Alen

Department of Mechanical Engineering, BUET

Numerical Integration

ME262 Numerical Analysis Sessional

9 May 2016

Graphical Representation of Integral


3

Integral = area under the curve


ME262 Numerical Analysis Sessional

9 May 2016

Graphical Representation of Integral


4

f(x)

x0

x1
ME262 Numerical Analysis Sessional

xn-1

xn

9 May 2016

Numerical Approximation of Integral


5

fn (x): linear

fn (x): quadratic

fn (x): higher-order
polynomial

Newton-Cotes Closed Formulae -- Use both end points


Trapezoidal Rule : Linear
Simpsons 1/3-Rule : Quadratic
Simpsons 3/8-Rule : Cubic
ME262 Numerical Analysis Sessional

9 May 2016

Simpsons 1/3-Rule
6

Approximate the function by a parabola

h
f ( x)dx f ( x0 ) 4 f ( x1 ) f ( x2 )
3
L(x)
f(x)

x0

x1

ME262 Numerical Analysis Sessional

x2

9 May 2016
6

Composite Simpsons Rule


7

Piecewise Quadratic approximations h

ba
n

f(x)

...
x0

x1 h

x2 h

x3 h

x4

xn-2

ME262 Numerical Analysis Sessional

xn-1

xn

x
9 May 2016

Composite Simpsons 1/3 Rule


8
n-1
n-2

h
h
I f ( x0 ) 4 f ( x1 ) f ( x2 ) I = f(x0 )+ 4 f(xi )+ 2 f(xi )+ f(xn )
3
i=1,3,5
i=2,4,6
3

Applicable only if
the number of segments is
even

i=0

ME262 Numerical Analysis Sessional

. .

n-1 n
9 May 2016
8

Composite Simpsons 1/3 Rule


9

Algorithm for Uniform Spacing (Equal Segments)

h m
I =
f
3 k 1

x2k - 2 + 4f x2k -1

x2k
ba
h
n

x0 x1 x2 x3

x4 x5 x6 . . .

n
m
2

xn-1 xn

ME262 Numerical Analysis Sessional

9 May 2016

MATLAB Code
10

I xe 2 x dx
0

fvalue.m

function s = fvalue(x)
s = x*exp(2*x);

simpsn3.m
a = 0; b = 4;
n=input(Enter the value of n);
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);
ME262 Numerical Analysis Sessional

9 May 2016

Composite Simpsons 1/3 Rule


11

2x
I

xe
dx
Evaluate the integral
0

n=2

n=4

h
I f (0) 4 f (2) f (4)
3
2
0 4(2e4 ) 4e8 8240.411
3

57.96%

h
f (0) 4 f (1) 2 f (2) 4 f (3) f (4)
3
1
0 4(e2 ) 2(2e4 ) 4(3e6 ) 4e8 5670.975 8.70%
3

ME262 Numerical Analysis Sessional

9 May 2016

Simpsons 3/8-Rule
12

Approximate the function by a cubic polynomial


L(x)

f(x)

n must be a multiple of 3

m
x0

x1

x2

x3

n
3

3h
f ( x)dx
f ( x0 ) 3 f ( x1 ) 3 f ( x2 ) f ( x3 )
8

3h m
f x3k -3 + 3f x3k -2 3 f x3k -1 f x3k
I=

8 k 1
ME262 Numerical Analysis Sessional

9 May 2016
12

Example: Simpsons Rules


13

Evaluate the integral

I
Simpsons 1/3-Rule

xe 2 x dx

xe 2 x dx

h
f (0 ) 4 f ( 2 ) f ( 4 )
3

2
0 4 ( 2 e 4 ) 4 e 8 8240.411
3
5216.926 8240.411

57.96%
5216.926

I xe 2 x dx
0

Simpsons 3/8-Rule

3h
4
8

f
(
0
)

3
f
(
)

3
f
(
)

f
(
4
)

8
3
3

3( 4/3 )
0 3( 19.18922 ) 3( 552.33933 ) 11923.832 6819.209
8
5216.926 6819.209

30.71%
5216.926

ME262 Numerical Analysis Sessional

9 May 2016

Combined Simpsons 1/3 & 3/8 Rule


14

I I 1/p3 I 3q/ 8
p

I 1/ 3
q

I 3/8

p
2

h
= f x2j -2 + 4f x2j -1 f x2j
3 j 1
q
3

3h
f x3k -3 + 3f x3k -2 3 f x3k -1 f x3k

8 k 1
p and q must be a multiple of 2 and 3 respectively
ME262 Numerical Analysis Sessional

9 May 2016

Combined Simpsons 1/3 & 3/8 Rule


15

ME262 Numerical Analysis Sessional

9 May 2016

a = 0; b = 4;
p=input('Enter p');
q=input('Enter q');
n=p+q;
h = (b - a)/n;
S1 = 0;
for k = 1 : (p/2)
S1 = S1 + fvalue(a+h*(2*k-2))...
+4*fvalue(a+h*(2*k-1))+fvalue(a+h*2*k);
end
I1 = h*S1/3;
S2 = 0;
%starting point d:
d=a+p*h;
for k = 1 : (q/3)
S2 = S2 + fvalue(d+h*(3*k-3))+3*fvalue(d+h*(3*k-2))...
+3*fvalue(d+h*(3*k-1)) +fvalue(d+h*3*k);
end
I2 = 3*h*S2/8;
16

I=I1+I2

ME262 Numerical Analysis Sessional

9 May 2016

NUMERICAL INTEGRATION in MATLAB

17

integral
integral2
integral3
quadgk

Numerical integration

quad2d
cumtrapz
trapz
polyint

Numerically evaluate double integral, tiled method

Numerically evaluate double integral


Numerically evaluate triple integral
Numerically evaluate integral, adaptive Gauss-Kronrod
quadrature

Cumulative trapezoidal numerical integration


Trapezoidal numerical integration
Polynomial integration

ME262 Numerical Analysis Sessional

9 May 2016

NUMERICAL INTEGRATION in MATLAB


q = integral(fun,xmin,xmax)
q = integral(fun,xmin,xmax) numerically integrates function fun from xmin to xmax
using global adaptive quadrature and default error tolerances.
.

fun = @(x) exp(-x.^2).*log(x).^2;


q = integral(fun,0,Inf)

18

ME262 Numerical Analysis Sessional

9 May 2016

NUMERICAL INTEGRATION in MATLAB


q = integral2(fun,xmin,xmax,ymin,ymax)
q = integral3(fun,xmin,xmax,ymin,ymax,zmin,zmax)

Integrate over the region:


[X]=(0,pi)
[Y]=(0,1)
[Z]=(-1,1)

19

ME262 Numerical Analysis Sessional

9 May 2016

NUMERICAL INTEGRATION in MATLAB

Q = trapz(Y)
Q = trapz(X,Y)

Integrates Y with spacing increment


X. By default, trapz operates on the first dimension
.
of Y whose size does not equal 1. length(X) must be equal to the size of this
dimension. If X is a scalar, then trapz(X,Y) is equivalent to X*trapz(Y).

20

ME262 Numerical Analysis Sessional

9 May 2016

You might also like