0% found this document useful (0 votes)
51 views3 pages

Numerical Integration Methods Code

This document contains MATLAB code implementing different numerical integration methods including Simpson's 3/8 rule, Simpson's 1/3 rule, and the trapezoidal rule. For each method, both simple and composite implementations are shown using sample functions. The code demonstrates how to calculate the integral approximation using each method and compares the results to the exact integral value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views3 pages

Numerical Integration Methods Code

This document contains MATLAB code implementing different numerical integration methods including Simpson's 3/8 rule, Simpson's 1/3 rule, and the trapezoidal rule. For each method, both simple and composite implementations are shown using sample functions. The code demonstrates how to calculate the integral approximation using each method and compares the results to the exact integral value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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=[Link]n
fx1 = fx1 + f(x(i)) ;
end
for j=[Link]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

You might also like