function area = trapezoid2( f, a, b, n )
%TRAPEZOID2 Integrate f from a to b via the trapezoid rule on n intervals.


% Set up x and f(x) values, assuming that f doesn't vectorize.

x = linspace( a, b, n+1 );

y = [];
for i = 1 : n+1
    y(i) = f( x(i) );
end


% Calculate the area under the curve

deltaX = ( b - a ) / n;
area = sum( deltaX * ( y(1:n) + y(2:n+1) ) / 2 );

end

