
a=0; b=2*pi;
h=0.01;
x=a:h:b;  
y=sin(2*x)+(10^(-1))*rand(size(x));

for n=2:8
    coeff=polyfit(x,y,n); % VALUTAZIONE COEFF. 
                          % MIGLIORE APPROX. ("M.A.").
                          
    z=polyval(coeff,x);   % VALORE M.A. NEI NODI "x".
    err2=norm(z-y,2);
    errinf=norm(z-y,inf);
    
    xx=0:(h/4):2*pi;      % NODI TEST.
    yy=polyval(coeff,xx); % VALORE M.A. IN NODI TEST.
    
    plot(x,y,'r-',xx,yy,'k:');
    fprintf('\n \t [GRADO]: %2.0f [ERR. 2]: %2.2e [ERR. INF]: %2.2e',n,err2,errinf);
    pause(5);
end

