function Composite_Integration( int_type, n )

% int_type - composite integration type;
% n - number of subintervals in [a,b]
%
% Example:  Running
%          Composite_Integration( 's', 100 )
% in Matlab prompt would result in running Simpson's rule 
% with 100 subintervals.

a = 0;
b = 2;

n = n
h = (b-a)/n

x = a:h:b;
plotf(x);

approx = 0;

for i=1:n
    
     if( int_type == 'l' )          % Left end point rule
         approx = approx + h* f(x(i));     
     
     elseif( int_type == 'r' )      % Right end point rule
         approx = approx + h* f(x(i+1));
         
     elseif( int_type == 'm' )      % Midpoint rule
         approx = approx + h* f(0.5*(x(i) + x(i+1)));
         
     elseif( int_type == 't' )      % Trapezoidal rule 
         approx = approx + 0.5*h* (f(x(i)) + f(x(i+1)));   
    
     elseif( int_type == 's' )      % Simpson's rule
         approx = approx + (h/6)*( f(x(i)) + 4*f(0.5*(x(i) + x(i+1))) + f(x(i+1)) );
         
     else
         error( 'The integration type is undefined!' )
     end
end

integration_error = abs(approx - getExact())

clear all

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function fvalue = f(x)

fvalue = x^2 * sin(-x);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function exact_answer = getExact()

exact_answer = 2*(1+cos(2)-2*sin(2))

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function plotf(x)

fx = (x.^2).*sin(-x);
plot(x,fx)
