Vous êtes sur la page 1sur 9

Experiment No.

: 7

Lab Group No.: 3


Student No.: 0906071

Course Number: EEE 212


Name of the Experiment:

NUMERICAL INTEGRATION

Date of Performance: 25/6/2012


Date of Submission: 2/7/2012

Name: Md. Ehsanul Haque Nirjhar


Department: EEE
Section: B
Partners Student No.: 0906070

Problem 1 : Perform numerical integration of the following functions:


i.
ii.

f (x) =xex+x7sin x27x4, from x=1 to 2


f (x)=x3ln x+3sin2 x, from x=/3 to 4/3

Use the following methods to compute the integration:


1. Composite trapezoidal rule
2. Composite Simpsons 1/3 rule
3. Composite Simpsons 3/8 rule
4. Adaptive integration techniques
Also, perform cumulative integration to create a plot of integration of f(x) vs. x in the range 3
to 3 for both functions.
No built in MATLAB functions related directly to numerical integration can be used to solve
the problem.

Code (i) :
clear all
close all
clc
clf
h=10^-4;
n=(3/h)+1;
x=-1;
for i=1:n
f1(1,i)=x*exp(x)+(x^7)*sin(x^2)-7*(x^4);
x=x+h;
end
c1(1,1)=1;
c1(n,1)=1;
for i=2:n-1
c1(i,1)=2;
end
I1=f1*c1;
I1=(h/2)*I1;
I1

%trapezoidal rule Complete

x=-1;
for i=1:n
f2(1,i)=x*exp(x)+(x^7)*sin(x^2)-7*(x^4);
x=x+h;
end

c2(1,1)=1;
c2(n,1)=1;
for i=2:n-1
if(mod(i,2)==0)
c2(i,1)=4;
else
c2(i,1)=2;
end
end
I2=f2*c2;
I2=(h/3)*I2;
I2

% Simpson's 1/3 Rule Complete

x=-1;
for i=1:n
f3(1,i)=x*exp(x)+(x^7)*sin(x^2)-7*(x^4);
x=x+h;
end
c3(1,1)=1;
c3(n,1)=1;
for i=2:n-1
if(mod(i,3)==1)
c3(i,1)=2;
else
c3(i,1)=3;
end
end
I3=f3*c3;
I3=(3/8)*h*I3;
% Simpson's 3/8 Rule Complete
I3
x=-1;
xf=2;
h=(xf-x)/2;
n=(xf-x)/h+1;
n=floor(n);
tol=1;
I=0;
I4=0;

%for avoiding any float value in n


%initially,tollerence is set high
%previous value of integration
%final value of integration

while tol>0.000001
x=-1;
n=(xf-x)/h+1;
n=floor(n);
for i=1:n
f4(1,i)=x*exp(x)+(x^7)*sin(x^2)-7*(x^4);
x=x+h;
end
c4(1,1)=1;
c4(n,1)=1;
for i=2:n-1

c4(i,1)=2;
end
I4=f4*c4;
I4=(h/2)*I4;
tol=abs(I4-I);
I=I4;
h=h/2;
end
I4

%adaptive method(trapezoidal) Complete

h=10^-3;
j=-3:0.1:3;
for k=1:length(j)
xi=0;
xf=j(k);
n=(abs((xf-xi)))/h+1;
n=floor(n);

%calculation is done with reference to 0


%abs() is used to avoid negative values

for i=1:n
f5(1,i)=xi*exp(xi)+(xi^7)*sin(xi^2)-7*(xi^4);
xi=xi+h;
end
c5(1,1)=1;
c5(n,1)=1;
for i=2:n-1
c5(i,1)=2;
end
I5=f5*c5;
I(1,k)=(h/2)*I5;
end
plot(j,I)

%cumulative integration

Output:
I1 =
40.983715049089938

I2 =
40.983714290325935

Complete

I3 =
40.983714290325942

I4 =
40.983714449376379

Graph:

57.4

57.2

57

56.8

56.6

56.4

56.2

56
-3

-2

-1

This is a f(x) dx vs. x graph. The integration is done with reference to x=0.

Code (i) :
clear all
close all
clc
clf
h=10^-4;
n=pi/h+1;
n=floor(n);
x=pi/3;

% floor is used to avoid floating values in point numbers

for i=1:n
f1(1,i)=-(x^3)-log(x)+3*(sin(x))^2;
x=x+h;
end
c1(1,1)=1;
c1(n,1)=1;
for i=2:n-1
c1(i,1)=2;
end
I1=f1*c1;
I1=(h/2)*I1;
I1

%trapezoidal rule Complete

x=pi/3;
for i=1:n
f2(1,i)=-(x^3)-log(x)+3*(sin(x))^2;
x=x+h;
end
c2(1,1)=1;
c2(n,1)=1;
for i=2:n-1
if(mod(i,2)==0)
c2(i,1)=4;
else
c2(i,1)=2;
end
end
I2=f2*c2;
I2=(h/3)*I2;
I2

% Simpson's 1/3 Rule Complete

x=pi/3;
for i=1:n
f3(1,i)=-(x^3)-log(x)+3*(sin(x))^2;
x=x+h;
end
c3(1,1)=1;
c3(n,1)=1;
for i=2:n-1
if(mod(i,3)==1)
c3(i,1)=2;
else
c3(i,1)=3;
end
end
I3=f3*c3;
I3=(3/8)*h*I3;

I3
% Simpson's 3/8 Rule Complete
x=pi/3;
xf=(4*pi)/3;
h=(xf-x)/2;
n=(xf-x)/h+1;
n=floor(n);
tol=1;
%initially,tollerence is set high
I=0;
%previous value of integration
I4=0;
%final value of integration
while tol>0.000001
x=pi/3;
n=(xf-x)/h+1;
n=floor(n);
for i=1:n
f4(1,i)=-(x^3)-log(x)+3*(sin(x))^2;
x=x+h;
end
c4(1,1)=1;
c4(n,1)=1;
for i=2:n-1
c4(i,1)=2;
end
I4=f4*c4;
I4=(h/2)*I4;
tol=abs(I4-I);
I=I4;
h=h/2;
end
I4
h=10^-3;
j=-3:0.1:3;

%adaptive method(trapezoidal) Complete

for k=1:length(j)
xi=0.0001;
%calculation is done wrt 0.0001 as in case
xf=j(k);
n=(abs((xf-xi)))/h+1;
n=floor(n);
for i=1:n
f5(1,i)=-(xi^3)-log(xi)+3*(sin(xi))^2;
xi=xi+h;
end
c5(1,1)=1;
c5(n,1)=1;
for i=2:n-1
c5(i,1)=2;
end
I5=f5*c5;
I(1,k)=(h/2)*I5;

of log(x),x0

end
plot(j,I)

%cumulative integration Complete

Output :
I1 =
-74.755626006635453

I2 =
-74.753203579466685

I3 =
-74.754717602541305

I4 =
-74.762359848242284

Graph:
-15.75
-15.76
-15.77
-15.78
-15.79
-15.8
-15.81
-15.82
-15.83
-15.84
-3

-2

-1

This is a f(x) dx vs. x graph. The integration is done with reference to x=0.0001.

Vous aimerez peut-être aussi