%MATLAB version of the script for example 7.6 on page 324 of conte/deboor
% example 7.6  Simpson's 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);
disp('Example 7.6: Simpson''s Rule')
disp('   N    I(SP)           ERROR(SP)   I(DP)             ERROR(DP)')

for N=25*2.^(0:2)
   value = SIMPSON(F,single(0),1,N); error =  0.746824132812427  - value;
   valued = SIMPSON(F,0,1,N); errord =  0.746824132812427 - valued;
   fprintf('  %3d  %14.7e  %10.3e  %14.7e    %10.3e\n', ...
             [N,    value,    error, valued,  errord])
end

function value = SIMPSON( F , A , B , N )
H = (B - A)/N; 
value = H/6*(F(A)+2*sum(F((1:N-1)*H)) + 4*sum(F(((0:N-1)+1/2)*H))+F(B));
return, end
