clear

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Tschebyscheff-Approximation (Funktionsapproximation durch lineare Programmierung) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


n=5;  % n = Grad des Approximationspolynoms
m=50;  % Anzahl der Berechnungsdatenpunkte

t=linspace(0,1,m)';
f=zeros(m,1);

delta=0.2; % Datenstörung


% Diskrete Funktionswerte
f=2*t.*sin(6*pi*t)+ delta*( 2*rand(m,1)-ones(m,1) );





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% Aufstellen der Größen für das lineare Programm %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
    alpha=zeros(n+1,1); % gesuchter Koeffizientenvektor

    c=zeros(n+1,1); c(n+1)=1;

    b=zeros(2*m,1);
    b(1:m)=-f;
    b(m+1:2*m)=f;

    A=zeros(2*m,n+1);

   
   for k=1:m
        for j=1:n
            A(k,j) = -t(k)^(j);
        end
   end
    
    
    A(m+1:2*m,:)=-A(1:m,:);
    A(:,n+1)=-ones(2*m,1);

    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


options = optimset('LargeScale','off','Simplex','on','Display','iter');  %nutze Simplex



% Bestimmung des Koeffizientenvektors
alpha = linprog(c,A,b,[],[],[],[],[],options);


% Auswertung des Approximanten in (neuen) Datenpunkten 
M=100;  
T=linspace(0,1,M);  

p=zeros(M,1); % Approximationspolynom
for k=1:M
    for j=1:n   % (n+1)-te Komponente entspricht "s"
        p(k) = p(k) + alpha(j)*T(k)^(j);
    end
end



% Darstellung der Daten und der Approximation
figure
plot(t,f,'r-*'); hold on;
plot(T,p,'-+');