%MATLAB version of the script for example 7.5 on page 324 of conte/deboor
% example 7.5  Corrected Trapezoid Rule
%
%  Reference:
%
%    Samuel Conte, Carl de Boor,
%    Elementary Numerical Analysis,
%    Third Edition,
%    SIAM, 2017,
%    ISBN: 978-1-611975-19-2.
%
F = @(x) exp(-x.*x); FPRIME = @(x) -2*x.*F(x);
disp('Example 7.5: Corrected Trapezoidal Rule')

A = 0; 
for cc=1:2
   if cc==1, B = single(1); disp('Single precision output')
   else      B = 1;         disp('Double precision output')
   end
   disp('   N      TRAPEZOID SUM    CORR.TRAP.SUM')

   for N=10:15
      value = TRAP(F,A,B,N); 
      corval = value + 1/N^2*(FPRIME(A)-FPRIME(B))/12;
      fprintf('  %3d   %15.7e  %15.7e\n', [N,value,  corval])
   end
end
