% Interpolation and extrapolation of data 
% from census of the US population.
% Evaluate Newton polynomial via shifted Horner's method
% Polynomial is assumed to have Newton interpolant form
% Coefficents computed using divided differences. 
% K. Ming Leung, 02/26/03
% Example here is from Heath

clear all; hold off;
% Input data
ti=[1940; 1950; 1960; 1970; 1980; 1990];
yi=[132165; 151326; 179323; 203302; 226542; 249633];
% Compute coefficients:
x = dividedDifference1(ti,yi);

t=1929:1:2001; % Points to compute
% Evaluate interpolant via shifted Horner's method
N=length(x);    % N=n+1
p = x(N);
for k=N-1:-1:1
    p = x(k)+(t-ti(k)).*p;
end

% Plot the resulting fit:
plot(t,p,'-b',ti,yi,'r*'); hold on;
xlabel('t');
ylabel('y');
title('Newton Interpolation');

tout=1930;
pout = x(N);
for k=N-1:-1:1
    pout = x(k)+(tout-ti(k)).*pout;
end
pout,
plot(tout,pout,'or');