% Approximates the area under the function f over a given interval
% Make sure the file f.m is contained in the same folder as this file

% Input: xmin = left end of interval
%        xmax = right end of interval
%        n = how many trapezoids to use

% Output: sum = approximation of the area under f over a given interval

function sum = Trap(xmin,xmax,n)

% Initially set sum to zero
sum = 0;

% Calculate the width of the rectangles
deltax = (xmax-xmin)/n;

% One pass thru the for loop for each trapezoid
for i = 0:n-1      
    % Add onto the current sum the area of the current trapezoid
    sum = sum + (f(xmin + i*deltax) + f(xmin + (i+1)*deltax))/2 * deltax;
end
