clear
clc
clf

x=linspace(-2.*pi,2.*pi,1000);
nodi=linspace(-2.*pi,2.*pi,11)
f=@(x) sin(1./(1+x.^2));
X=nodi;
Y=f(nodi);
P=polyfit(X,Y,10)
figure(1)
plot(x,f(x),x,polyval(P,x),nodi,f(nodi),'o')
grid on
err=max(abs(f(x)-polyval(P,x)))

n=[4,8,10];
for k=1:length(n)
	distcheb=1:pi./n(k):pi;
	nodicheb=cos(distcheb);
	X=nodicheb;
	Y=f(nodicheb);
	P=polyfit(X,Y,n(k)-1);
	figure(k+1)
	plot(x,f(x),x,polyval(P,x),nodicheb,f(nodicheb),'o')
	grid on
	err(k)=max(abs(f(x)-polyval(P,x)));
end
err