clear; clc; clf

n=10;
m=100;

z=linspace(-1,1,100);
x=linspace(-1,1,100);
x2=linspace(-1,1,100);
f=zeros(100,1);
f2=zeros(100,1);


for i=1:n+1
    
    a(i)=-1+2*(i-1)/n;
    a2(i)=cos(pi*((i-1)+1/2)/(n+1));
    y(i)=(a(i)^2)/(1+20*a(i)^2);
   y2(i)=(a2(i)^2)/(1+20*a2(i)^2);
    
end

for k=1:100
Ln=ones(n+1,1);
Ln2=ones(n+1,1);
for i=1:n+1
for j=1:n+1
    
    if j~=i
        
        Ln(i)=Ln(i)*(x(k)-a(j))/(a(i)-a(j));
        Ln2(i)=Ln2(i)*(x2(k)-a2(j))/(a2(i)-a2(j));
    end
end

end
        f(k)=f(k)+y*Ln;
        f2(k)=f2(k)+y2*Ln2;
end

z2=linspace(-1,1,100);
for i=1:100
    
    ty(i)=(z(i)^2)/(1+20*(z(i)^2));
    
end

x=linspace(-1,1,100);

for j=1:10
    
    
    V(:,j)=x.^(j-1);
    
end

c=(V'*V)\(V'*ty');
f3=V*c;

subplot(2,1,1)
plot(z,f,'r',z,f2,'b',x,f3,'g',z2,ty,'black');
title 'Interpolationen von f(x)=x^2/(1+20*x^2)'
legend('Gleichmäßig', 'Tchebychev', 'Ausgleich', 'plot')
subplot(2,1,2)
plot(z,abs(f-ty'),'r',z,abs(f2-ty'),'b')
title 'Fehlerabschätzungen'
legend('Gleichmäßig', 'Tchebychev')

n=10;
m=100;



    
    [P,S,Q]=svd(V);
    s=1./diag(S);
    Sd=[diag(s), zeros(n,m-n)];
    size(Q)
    size(Sd)
    size(P)
    size(ty')
    c=Q*Sd*P'*ty';
    
    f4=V*c;
figure(2)
    clf;
    plot(z,f3,'r:',z,f4,'b.');
    legend('VtV','svd');
