function traptest
% Trapezoidal Rule Test
clc;
% Input:
n = 100; a = -2.; b = 2.;
fprintf('\nTrapezoidal Rule Test Input: n=%i; [a,b]=[%9.6f,%9.6f];',n,a,b)
h = (b-a)/n;
x = a:h:b;
y = f(x);
trapans = trap(y,n,h);
fprintf('\n\n Trap Test Answer = %9.6f',trapans)
% end traptest
%
function trapf = trap(y,n,h)
% trapezoidal rule in efficient form:
trapf = (0.5*(y(1)+y(n+1))+sum(y(2:n)))*h;
% end trap
%
function y = f(x)
% trap rule test function
y = exp(-(x.^2)/2)/sqrt(2*pi);
% end f(x)