function T = compTrap(f,a,b,n)
% Composite trapezoid rule for approximating the definite integral of a
% function.
%
% INPUT: f (function to be integrated)
%        a (left endpoint)
%        b (right endpoint)
%        n (number of subintervals)
%
% OUTPUT: T (approximate integral value)
%

h = (b-a)/n; % Computing stepsize
x = linspace(a,b,n+1); % Vector of nodes
y = f(x); 

y([1,end]) = 0.5*y([1,end]);
T = h*sum(y);