% Skript zur Musterlösung
%
% Numerische Mathematik, SS 2013
%
% Interpolationspolynome berechnen und auswerten
%
% Ingolf Busch, Juli 2013

% Stützstellen und Funktionswerte generieren

x = rand(5,1);
y = randn(5,1);

% Auswertungsvektor generieren

t = linspace(0,1);

% Neville-Aitken

yt_na = my_neville_aitken(x,y,t);

% Dividierte Differenzen

f = my_div_diff(x,y);

yt_dd = my_horner_newton(f,x,t);

% Plotten der Interpolationspolynome

figure;
plot(x,y,'*',t,yt_na);

figure;
plot(x,y,'*',t,yt_dd);

% Ilustration Runge-Beispiel

runge = @(x) 1./(1+x.^2);
a=-5;
b=5;
t = linspace(a,b,100*(b-a));

for n=[10 14]
    
    x_equi = linspace(a,b,n+1);
    y_equi = runge(x_equi);
    f_equi = my_div_diff(x_equi,y_equi);
    
    x_t    = (b-a)/2 * cos((2*(n-(0:n))+1)*pi/(2*n+2)) + (a+b)/2;
    y_t    = runge(x_t);
    f_t    = my_div_diff(x_t,y_t);
    
    figure;
    subplot(1,2,1);plot(x_equi,y_equi,'*',t,my_horner_newton(f_equi,x_equi,t));
    subplot(1,2,2);plot(x_t,y_t,'*',t,my_horner_newton(f_t,x_t,t));
end