%
% Test the accuracy of polynomial using Newton Divided Differences
%
   
clf
N = 5;
x = (-N:N)'/N;
f = exp(x);
F = NewtonDividedDifference(x,f);
xnew=sort(2*rand(2*N+1,1)-1);
fnew = EvaluateNDD(xnew,x,F);
ftrue = exp(xnew);
figure(1);
clf;
subplot(2,1,1);
plot(xnew,ftrue,'bd-');
hold on
plot(xnew,fnew,'r.-');
title('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');

N = 10;
x = (-N:N)'/N;
xnew=sort(2*rand(2*N+1,1)-1);
f = 1./(1/5+x.^2);           
F = NewtonDividedDifference(x,f);
fnew = EvaluateNDD(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('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');

N = 40;
x = (-N:N)'/N;
f = 1./(1/5+x.^2);           
F = NewtonDividedDifference(x,f);
fnew = EvaluateNDD(xnew,x,F);
ftrue = 1./(1/5+(xnew).^2);
figure(3);
clf;
subplot(2,1,1);
plot(xnew,ftrue,'bd-');
hold on
plot(xnew,fnew,'r.-');
title('Interpolation (red) vs. exact function (blue) for 1/(0.2+x^2) on [-1, 1], 81 points ');
dff = abs(ftrue-fnew);
subplot(2,1,2);
semilogy(xnew,dff,'b.-');
title('Interpolation Error, semilog scale');

