


%  Basic operations.
x = 1;

disp(sin(x));


for i = 10:1 
    disp(i)
end

%%
% quadrature example
n = 2;

% This is our integrand
f = @(x) (x .^ n) .* exp(x);



% Matlab's built-in integrator.
% We will treat this as the 
I = integral(f, 0, 1);   %older versions of Matlab, use 'quad'.

% Convergence of the left-hand rule.

N = 10;  % Number of refinements.

hsv = zeros(N,1);
Ih = zeros(N,1);
for i = 1:N
    h = 2^-i;  
    x = (0:h:1-h)+h/2;   
    
    hsv(i) = h;
    Ih(i) = sum(f(x)*h);
    
end



disp('    h        Ih        error')
disp([hsv, Ih, Ih-I])

%%
% Plot the outcome:
figure; 
set(gca,'FontSize',16);
loglog(hsv,abs(Ih-I),'-k','LineWidth',2);
grid on;
xlabel('$h$','Interpreter','latex');
ylabel('$\textrm{error}$','Interpreter','latex');
title('Quadrature Error for Left-Hand Rule')
%%
% Estimate the truncation error by fitting:
q = polyfit(log(hsv),log(abs(Ih-I)),1);

    

