Académique Documents
Professionnel Documents
Culture Documents
: 7
NUMERICAL INTEGRATION
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
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
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;
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
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);
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;
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
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
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;
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)
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.