% 1D integration
clear  % Clears command window
clc    % Clears command history 
tic

rng('default')      % reset RNG
rng('shuffle')      % use seed as current time

Nmax = 100000;
a = 0.0;
b = pi;
Sint = 0.0;
fav1 = 0.0;
fav2 = 0.0;
for it=1:Nmax
    x = a+(b-a)*rand;
    Sint = Sint + fint(x);
    
    fav1 = fav1 + fint(x);
    fav2 = fav2 + (fint(x))^2;
end
Sint = Sint*(b-a)/double(Nmax);
fprintf('Nmax = %9i \n', Nmax)
%fprintf('Sint = %9.6f \n',Sint)

fav1 = fav1/double(Nmax);
fav2 = fav2/double(Nmax);
Serr = (b-a)*sqrt((fav2 - fav1^2)/Nmax);
fprintf('Sint = %9.6f ± %8.6f \n',Sint,Serr)
%fprintf('Serr = %9.6f \n', Serr)

toc
function F = fint(x)
    %F = sin(x);
    F = x*sin(10*x)*cos(12*x);
end





