Sunteți pe pagina 1din 3

Simpson 3/8 simple

clc;
clear;
x=linspace(0,4,4); h=x(2)-x(1);
f=@(x) (1-exp(-x));
integ = (3*h/8)*(f(x(1)) + 3*f(x(2)) +3*f(x(3)) +f(x(4)))
integ_exact= (4+exp(-4)) - (0 + exp(-0))

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Simpson 3/8 composite
n=12;
x=linspace(1,9,n+1); h=x(2)-x(1);
f=@(x) (1-exp(-x));
s=0;
for i=1:n/3
shift=(i-1)*3;
s=s+(3*h/8)*(f(x(shift+1)) + 3*f(x(shift+2)) +
3*f(x(shift+3))+f(x(shift+4)));
end
s

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Simpson 1/3 simple
clc;
clear;
x=linspace(0,4,3); h=x(2)-x(1);
f=@(x) (1-exp(-x));
integ = (h/3)*(f(x(1)) + 4*f(x(2)) + f(x(3)))
integ_exact= (4+exp(-4)) - (0 + exp(-0))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Simpson 1/3 Composite
n=6;
x=linspace(0,2,n+1); h=x(2)-x(1);
f=@(x) (1-exp(-x));

fx1=0;
fx2=0;
for i=2:2:n
fx1 = fx1 + f(x(i)) ;
end
for j=3:2:n
fx2 = fx2 + f(x(j));
end;
integ = (h/3)*(f(x(1)) + 4*fx1 + 2*fx2 + f(x(n+1)))

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Trapezoidal
clc;
clear;
n=4;
x=linspace(0,5,n+1); h=x(2)-x(1);
f=@(x) (1-exp(-x));
integ=0;
for i=2:n
integ = integ + f(x(i)) ;
end
integ = (h/2)*(f(x(1)) + 2*integ +f(x(n+1)))
integ_exact= (4+exp(-4)) - (0 + exp(-0))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Professor Simpson 3/8 composite
clc;
clear;
n=30; x=linspace(1,9,n+1); h=x(2)-x(1);
f=@(x) (1-exp(-x));
s=0;
for i=1:n/3
shift=(i-1)*3;
s = s + (3*h/8)*(f(x(shift+1)) + 3*f(x(shift+2)) + 3*f(x(shift+3)) +
f(x(shift+4)));
end
s

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Professor 1/3 Simpson composite
clc;
clear;

n=30; x=linspace(1,9,n+1); h=x(2)-x(1);


f=@(x) (1-exp(-x));
s=0;
for i=1:n/2
shift=(i-1)*2;
s = s + (h/3)*(f(x(shift+1)) + 4*f(x(shift+2)) + f(x(shift+3)));
end
s

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Professor trapezoidal composite
clc;
clear;
n=30; x=linspace(1,9,n+1); h=x(2)-x(1);
f=@(x) (1-exp(-x));
s=0;
for i=1:n
shift=(i-1);
s = s + (h/2)*(f(x(shift+1)) + f(x(shift+2)));
end
s

S-ar putea să vă placă și