clear
close all
more off

n=6

% equidistant nodes
t=zeros(n+1,1);
y=zeros(n+1,1);

for j=0:n
 t(j+1) = j/3-1;
 y(j+1) = fun(t(j+1));
end  

A=zeros(n+1,n+1);
for i=1:n+1
 for j=1:n+1
  A(i,j) = t(i) ^ (j-1);  
 end
end

c = A \ y

% create plot data of the given function
tp=-1:0.01:1;
for i=1:length(tp)
  yf(i) = fun( tp(i)  );
end

% create plot data for the interpolation with equidistant nodes
for i=1:length(tp)
  yp(i) = c(1) + c(2)*tp(i) + c(3)*tp(i)^2 + c(4)*tp(i)^3 ...
         +c(5)*tp(i)^4+c(6)*tp(i)^5+c(7)*tp(i)^6;
end


% Chebyshev nodes 

tc=zeros(n+1,1);
yc=zeros(n+1,1);

for j=0:n
 tc(j+1) = cos(  (2*j+1)*pi / 14 );
 yc(j+1) = fun( tc(j+1) );
end  

A=zeros(n+1,n+1);
for i=1:n+1
 for j=1:n+1
  A(i,j) = tc(i) ^ (j-1);  
 end
end

c = A \ yc

% create plot data for the interpolation with chebyshev nodes
for i=1:length(tp)
  ypc(i) = c(1) + c(2)*tp(i) + c(3)*tp(i)^2 + c(4)*tp(i)^3 ...
         +c(5)*tp(i)^4+c(6)*tp(i)^5+c(7)*tp(i)^6;
end


% plot all together

plot (tp,yf,'LineWidth',3,tp,yp,'LineWidth',3,tp,ypc,'LineWidth',3)
xlabel('t')
hold on
plot (t,y,'r*',tc,yc,'b*')
legend('function','equidistant nodes','chebyshev nodes')

print -dpng 'cheby1.png'



