x=linspace(0,2*pi,1000);
y=sin(2*x); yy=y+(10^(-1))*rand(size(x)); 
for n=2:8
    coeff=polyfit(x,yy,n); % COEFFS. BEST APPROX (B.A.) 
    z=polyval(coeff,x);    % VALORE B.A. NEI NODI "x".
    plot(x,yy,'r-',x,z,'k-');
    err2=norm(z-y,2); err2p=norm(z-yy,2); % ERRS.
    errinf=norm(z-y,inf); errinfp=norm(z-yy,inf); % ERRS.
    fprintf('\n\t[DEG]:%2.0f',n); 
    fprintf(' [2]:%2.2e %2.2e',err2,err2p); 
    fprintf(' [INF]:%2.2e %2.2e',errinf,errinfp); 
    pause(2);
end
fprintf('\n \n');
