
function demo_integrazione3(example)

N=1:100;


if nargin < 1
    example=1;
end

switch example
    case 1
        a=-5; b=5;
        f=@(x) 1./(1+x.^2);
    case 2
        a=0; b=1;
        f=@(x) exp(x.^2);
     case 3
        a=0; b=1;
        f=@(x) x.^(5/2);
   case 4
        a=0; b=1;
        f=@(x) x.^(9/2);
end
% f=@(x) x.^3+2*x.^2+0.2*x+1;
ff=chebfun(f,[a,b]);
sol=sum(ff,[a,b]);


% --------------------- code starts here ---------------------


% simpson composte
fprintf(' \n \t');

for ii=1:length(N)
    I(ii,1)=compute_nc(ff,N(ii),a,b);
    I(ii,2)=compute_ch(ff,N(ii),a,b);
end

for ii=1:length(N)
    fprintf('$%2.0f$ & ',N(ii))
    if isnan(I(ii,1)) == 1
        fprintf('$$ & $$ &');
    else
        fprintf('$%1.1e$ & $%1.1e$ &',abs(I(ii,1)-sol),abs(I(ii,2)-sol));
    end
    fprintf(' \\\\\n \t');
end

fprintf('\n \n');





function I=compute_nc(ff,n,a,b)

x = linspace (a,b , n+1) ;
p = chebfun.interp1 (x, ff(x));
I=sum(p,[a,b]);


function I=compute_ch(ff,n,a,b)

t=cos((2*(0:n)+1)*pi./(2*n+2));
x=((a+b)/2)+((b-a)/2)*t;
p = chebfun.interp1 (x, ff(x));

pp=chebfun(ff,n);

I=sum(pp);
