function I = tintegrate(f, a, b, npanels)
%TINTEGRATE Numerical integration using trapezoid rule.
%   I = TINTEGRATE(F, A, B, NPANELS) computes the integral of F on the
%   domain [A, B] using the composite trapezoid rule with NPANELS.

% panel width
h = (b - a) / npanels;

% where do we need to evaluate f?
x = linspace(a, b, npanels + 1);

% evaluate f
y = f(x);

% sum area of panels
I = 0.5 * h * sum(y(1:(end - 1)) + y(2:end));
%I = 0.5 * h * (y(1) + y(end) + 2 * sum(y(2:(end - 1))));