function area = simp( fcn, a, b, n ) % Uses Simpson's rule to approximate the integral of the function % with handle fcn from a to b using n *pairs* of evenly spaced % subintervals. % Author: R Smyth % Version 1.0 8/22/2013 h = (b-a)/(2*n); % Subinterval spacing x = a:h:b; % Partition points y = fcn(x); % Function values at partition points i = 0:2*n; coeffs = 3+(-1).^(i+1); % Kludge to generate pattern of alternating 2s and 4s coeffs(1)=1; coeffs(end)=1; area = h/3 * sum(coeffs.*y); % Simpson's rule formula