%
% Test the accuracy of polynomial interpolation using Newton Divided Differences
%
   
clf
N = 5;
x = (-N:N)'/N;
f = exp(x);
df= exp(x);
F = NewtonDividedDifference2(x,f,df);
xnew=sort(2*rand(2*N+1,1)-1);
fnew = EvaluateNDD2(xnew,x,F);
ftrue = exp(xnew);
figure(1);
clf;
subplot(2,1,1);
plot(xnew,ftrue,'bd-');
hold on
plot(xnew,fnew,'r.-');
title('Hermite Interpolation (red) vs. exact function (blue) for e^x on [-1, 1], 11 points');
dff = abs(ftrue-fnew);
subplot(2,1,2);
semilogy(xnew,dff,'b.-');
title('Interpolation Error, semilog scale');


print('-dpsc',['NDD2exp.ps'])
cmd = cat(2,'ps2pdf ', [' NDD2exp.ps'], [' NDD2exp.pdf']);
[r,s]= system(cmd);

close all
semilogy(abs(F),'b.-');
title('Divided Difference Coefficients, semilog scale');

print('-dpsc',['NDD2expCoe.ps'])
cmd = cat(2,'ps2pdf ', [' NDD2expCoe.ps'], [' NDD2expCoe.pdf']);
[r,s]= system(cmd);

N = 10;
x = (-N:N)'/N;
xnew=sort(2*rand(2*N+1,1)-1);
f = 1./(1/5+x.^2);           
df = -(2*x)./((1/5+x.^2).^2);           
F = NewtonDividedDifference2(x,f,df);
fnew = EvaluateNDD2(xnew,x,F);
ftrue = 1./(1/5+(xnew).^2);
figure(2);
clf;
subplot(2,1,1);
plot(xnew,ftrue,'bd-');
hold on
plot(xnew,fnew,'r.-');
title('Hermite Interpolation (red) vs. exact function (blue) for 1/(0.2+x^2) on [-1, 1], 21 points');
dff = abs(ftrue-fnew);
subplot(2,1,2);
semilogy(xnew,dff,'b.-');
title('Interpolation Error, semilog scale');


print('-dpsc',['NDD2Gibbs10Pt.ps'])
cmd = cat(2,'ps2pdf ', [' NDD2Gibbs10Pt.ps'], [' NDD2Gibbs10Pt.pdf']);
[r,s]= system(cmd);

close all
semilogy(abs(F),'b.-');
title('Divided Difference Coefficients, semilog scale');

print('-dpsc',['NDD2GibbsCoe.ps'])
cmd = cat(2,'ps2pdf ', [' NDD2GibbsCoe.ps'], [' NDD2GibbsCoe.pdf']);
[r,s]= system(cmd);

