% Example: Composite Simpson's Rule 

clear; 
format long e 

% Defintion of integration 
f = inline('1/x') 
a = 1;
b = 2; 
exactval = log(2);  

% number of subintervals. 
n = 4         % for simplicity, n is chosen to be even. 
               % n = 2, the standard Simpson's rule
h = (b-a)/n;   % the subinterval width 

% initialization 
simpson = f(a) + 4*f(a+h) + f(b);

% loop 
for i = 2:2:n-2,
    simpson = simpson + 2*f(a+i*h) + 4*f(a+(i+1)*h);
end
simpson = h*simpson/3

exactval 
relative_error = abs(exactval - simpson)/abs(exactval) 
fcount = 3 + ((n-2)/2)*2 
