
function esercizio1

esempio=5;

% Problema
switch esempio
    case 1
        f=@(x) exp(x).*cos(4*x);
        a=0; b=pi;
        I=(exp(pi)-1)/17;
    case 2
        f=@(x) x.^(5/2);
        a=0; b=1;
        I=2/7;
    case 3
        f=@(x) exp(cos(x));
        a=-pi; b=pi;
        I=7.95492652101284;
    case 4
        f=@(x) exp(cos(x));
        a=0; b=pi/4;
        I=1.93973485062365;
    case 5
        f=@(x) x.^(1/2);
        a=0; b=1;
        I=2/3;
end

for k=1:9
    
    N(k)=2^k;
    
    % Formula dei trapezi composta
    [x_tr,w_tr]=trapezi_composta(N(k),a,b);
    fx_tr=feval(f,x_tr);
    I_tr=w_tr'*fx_tr;
    E_tr(k)=abs(I_tr-I);
    
    % Formula dei c.s. composta
    [x_cs,w_cs]=simpson_composta(N(k),a,b);
    fx_cs=feval(f,x_cs);
    I_cs=w_cs'*fx_cs;
    E_cs(k)=abs(I_cs-I);
    
end

% Plot errori
semilogy(N,E_tr,'r-','LineWidth',4);
hold on;
semilogy(N,E_cs,'k-','LineWidth',4);
legend('tr','cs');
hold off;

% andamento rapporti
rat_tr=E_tr(1:end-1)./E_tr(2:end);
rat_cs=E_cs(1:end-1)./E_cs(2:end);

for k=1:8
    fprintf('\n \t k: %2.0f rat.tr.: %4.4e',...
        k,rat_tr(k));
end

fprintf('\n \n');

for k=1:8
    fprintf('\n \t k: %2.0f rat.cs.: %4.4e',...
        k,rat_cs(k));
end

fprintf('\n \n');










