%% Test Pa7_dividiertenDiff
% x = [0,1,3,2];
% f = [1,3,2,3];
% 
% [a,F] = PA7_dividiertenDifferenzen(x,f);

%%

clear all;
close all;

xxAequi = @(n) -1+(0:(2/n):2);
xxCheb = @(n) cos( (2*(0:n)+1)*pi/(2*(n+1)));

nStuetzstellenVek=2:2:20;
legendLabels = cell(length(nStuetzstellenVek)+1,1);

%% i)
f1 = @(x) 1./(1+25*x.^2);

for l=1:length(nStuetzstellenVek) %2:20
   n = nStuetzstellenVek(l);
   
   % Aequidistante Stuetzstellen
   x = xxAequi(n);
   yAequi = zeros(n*20,1);
   p = zeros(n*20,1);
   a = PA7_dividiertenDifferenzen(x, f1(x));
   for i=1:n
       for j=1:20
        %disp(['(i j)=(', num2str([i,j]),')' ])
        xbar = x(i) + j*(x(i+1)-x(i))/21;
        yAequi(20*(i-1)+j) = xbar;
        p(20*(i-1)+j) = PA7_HornerNewtonSchema(xbar, a, x);
       end
   end
   
   figure(11)
   legendLabels{l} = ['n=', num2str(n)]; 
   plot(yAequi, p, 'x')
   hold on;
   
   errAequi(l)=max(abs(p-f1(yAequi)));
   
   % Chebyshev Stuetzstellen
   x = xxCheb(n);
   yCheb = zeros(n*20,1);
   p = zeros(n*20,1);
   a = PA7_dividiertenDifferenzen(x, f1(x));
   for i=1:n
       for j=1:20
        %disp(['(i j)=(', num2str([i,j]),')' ])
        xbar = x(i) + j*(x(i+1)-x(i))/21;
        yCheb(20*(i-1)+j) = xbar;
        p(20*(i-1)+j) = PA7_HornerNewtonSchema(xbar, a, x);
       end
   end
   
   figure(12)
   plot(yCheb, p, 'x')
   hold on;
   
   errCheb(l)=max(abs(p-f1(yCheb)));
end

figure(11)
legendLabels{end} = 'f_1(x)'; 
plot(yAequi, f1(yAequi), '-g')
legend(legendLabels)
title('Aequidistante Stuetzstellen')
xlabel('x')
ylabel('P(x)')


figure(12)
legendLabels{end} = 'f_1(x)'; 
plot(yCheb, f1(yCheb), '-g')
legend(legendLabels)
title('Chebyshev Stuetzstellen')
xlabel('x')
ylabel('P(x)')


figure(13)
semilogy(nStuetzstellenVek, errAequi, '--x')
hold on
semilogy(nStuetzstellenVek, errCheb, '--x')
xlabel('n')
ylabel('err_{max}')
legend('Aequidistante Stuetzstellen', 'Chebyshev Stuetzstellen')


%% ii)
f2 = @(x) sqrt(abs(x));

for l=1:length(nStuetzstellenVek) %2:20
   n = nStuetzstellenVek(l);
   
   % Aequidistante Stuetzstellen
   x = xxAequi(n);
   yAequi = zeros(n*20,1);
   p = zeros(n*20,1);
   a = PA7_dividiertenDifferenzen(x, f2(x));
   for i=1:n
       for j=1:20
        %disp(['(i j)=(', num2str([i,j]),')' ])
        xbar = x(i) + j*(x(i+1)-x(i))/21;
        yAequi(20*(i-1)+j) = xbar;
        p(20*(i-1)+j) = PA7_HornerNewtonSchema(xbar, a, x);
       end
   end
   
   figure(21)
   legendLabels{l} = ['n=', num2str(n)]; 
   plot(yAequi, p, 'x')
   hold on;
   
   errAequi(l)=max(abs(p-f2(yAequi)));
   
   % Chebyshev Stuetzstellen
   x = xxCheb(n);
   yCheb = zeros(n*20,1);
   p = zeros(n*20,1);
   a = PA7_dividiertenDifferenzen(x, f2(x));
   for i=1:n
       for j=1:20
        %disp(['(i j)=(', num2str([i,j]),')' ])
        xbar = x(i) + j*(x(i+1)-x(i))/21;
        yCheb(20*(i-1)+j) = xbar;
        p(20*(i-1)+j) = PA7_HornerNewtonSchema(xbar, a, x);
       end
   end
   
   figure(22)
   plot(yCheb, p, 'x')
   hold on;
   
   errCheb(l)=max(abs(p-f2(yCheb)));
end

figure(21)
legendLabels{end} = 'f_2(x)'; 
plot(yAequi, f2(yAequi), '-g')
legend(legendLabels)
xlabel('x')
ylabel('P(x)')
title('Aequidistante Stuetzstellen')

figure(22)
legendLabels{end} = 'f_2(x)'; 
plot(yCheb, f2(yCheb), '-g')
legend(legendLabels)
xlabel('x')
ylabel('P(x)')
title('Chebyshev Stuetzstellen')

figure(23)
semilogy(nStuetzstellenVek, errAequi, '--x')
hold on
semilogy(nStuetzstellenVek, errCheb, '--x')
xlabel('n')
ylabel('err_{max}')
legend('Aequidistante Stuetzstellen', 'Chebyshev Stuetzstellen')

