% ------------------------------
% int_vec.m
% integration using Simpson rule 
% vectorized version 
% ------------------------------
clear all;close all;
format long;
tic
N=3001;
h=2*pi/(N-1);
% points - column vector
x=(0:h:2*pi)';
% weights - row vector
w=ones(1,N);
w(2:2:N-1)=4;w(3:2:N-2)=2;
w=w*h/3;
%
% integrate
%
% version 1
% ---------
%f=@(x) exp(-x.^2).*sin(x);
%int=w*f(x);
% versione 2
% ----------
int=w*(exp(-x.^2).*sin(x));
toc
diff=int-0.42443638350202229589; 
disp(['diff to exact ',num2str(diff)])

% without the sin(x) term version 2 is much faster than version 1