% Example 4.1.9:
% approximate f(x)=cos(x) by interpolating polynomials P1:P6 
% on data (xi,cos(xi)), 
% where
% x_nodes = [0.0 0.2 0.4 0.6 0.8 1.0 1.2]
% hence 
% y_values = cos(x_nodes), 
% and compare values of Pi at 0.1, 0.3, 0.5

clear all

x_nodes = [0.0 0.2 0.4 0.6 0.8 1.0 1.2] ;
y_values = cos(x_nodes) ;

x_eval = [0.1 0.3 0.5] ; 
%p_eval = zeros(length(x_nodes),length(x_eval));

for i=2:length(x_nodes)
    divdif_y = divdif(x_nodes(1:i),y_values(1:i));
    p_eval(i-1,:) = interp(x_nodes(1:i),divdif_y,x_eval);
end
p_eval;
index=1:1:(length(x_nodes)-1);
s=index';
table = [s';p_eval(:,1)';p_eval(:,2)';p_eval(:,3)'];
disp('n  Pn(0.1)      Pn(0.3)      Pn(0.5)')
format long
fprintf('%1.f  %.8f  %.8f  %.8f\n', table)
%Tabular(1:length(x_eval),:)  ;  


