n=20;
f=inline('atan(x)','x');
a=-5; b=5;

x=linspace(a,b,n+1); y=f(x); % equally spaced

len=b-a; j=0:n; 
xj=(a+b)/2+(len/2)*cos(j*pi/n); yj=f(xj); % Chebyshev spaced

xtest=linspace(a,b,1000); ytest=f(xtest);

figure(1); clf

p=polyfit(x,y,n); 
pxtest=polyval(p,xtest);
err=max(abs(pxtest-ytest));
subplot(2,1,1)
plot(x,y,'o',xtest,ytest,'r',xtest,pxtest,'g')
title(['equally--spaced max error ' num2str(err)])

pj=polyfit(xj,yj,n); 
pjxtest=polyval(pj,xtest);
errj=max(abs(pjxtest-ytest));
subplot(2,1,2)
plot(xj,yj,'o',xtest,ytest,'r',xtest,pjxtest,'g')
title(['Chebyshev max error ' num2str(errj)])
