% p9.m - polynomial interpolation in equispaced and Chebyshev pts
N = 16;
xx = -1.01:.005:1.01; clf
for i = 1:2
if i==1, s = 'equispaced points'; x = -1 + 2*(0:N)/N; end
if i==2, s = 'Chebyshev points'; x = cos(pi*(0:N)/N); end
subplot(2,2,i)
u = 1./(1+16*x.^2);
uu = 1./(1+16*xx.^2);
p = polyfit(x,u,N); % interpolation
pp = polyval(p,xx); % evaluation of interpolant
plot(x,u,'.','markersize',13)
line(xx,pp)
axis([-1.1 1.1 -1 1.5]), title(s)
error = norm(uu-pp,inf);
text(-.5,-.5,['max error = ' num2str(error)])
end
Also fiddle with the plotting.
NOTE: You will not see improvement in the Chebyshev approximation if you make $N$ much larger due to the instability of polyfit
and polyval
.
N = 32;
xx = -1.01:.005:1.01; clf
for i = 1:2
if i==1, s = 'equispaced points'; x = -1 + 2*(0:N)/N; end
if i==2, s = 'Chebyshev points'; x = cos(pi*(0:N)/N); end
figure
u = 1./(1+16*x.^2);
uu = 1./(1+16*xx.^2);
p = polyfit(x,u,N); % interpolation
pp = polyval(p,xx); % evaluation of interpolant
plot(x,u,'.','markersize',13)
line(xx,pp)
axis([-1.1 1.1 -1 1.5]), title(s)
error = norm(uu-pp,inf);
text(-.5,-.5,['max error = ' num2str(error)])
end
[Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering and scaling as described in HELP POLYFIT.] [> In polyfit at 76 In pymat_eval at 31 In matlabserver at 24] [Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering and scaling as described in HELP POLYFIT.] [> In polyfit at 76 In pymat_eval at 31 In matlabserver at 24]