Polynomial inflexibility

%      Polynomial inflexibility.
%       See also Schumaker L.L. (1981), Spline
%       Functions, New York: John Wiley & Sons.
%       To obtain hardcopies delete the comment signs, %,
%       preceding the PRINT statements.

x = -5: 0.1: 5;
y = ones(size(x))./(1 + x.^2);
x5 = -5: 2.5: 5;                % interpolate at 5 points
y5 = ones(size(x5))./(1 + x5.^2);
c5 = polyfit(x5, y5, 4);
y5s = polyval(c5, x);
plot(x, y, x, y5s), grid
title(‘y = 1/(1 + x^2), polynomial interpolation at 5 points’)
xlabel(‘x’),ylabel(‘y’)
% print
pause
x15 = -5: 10/14: 5;
y15 = ones(size(x15))./(1 + x15.^2);
c15 = polyfit(x15, y15, 14);
y15s = polyval(c15, x);
plot(x, y, x, y15s), grid
title(‘y = 1/(1 + x^2), polynomial interpolation at 15 points’)
xlabel(‘x’), ylabel(‘y’)
% print
pause
% i1 = find(x == -4);
% i2 = length(x) -i1;
% plot(x(i1:i2), y(i1:i2), x(i1:i2), y15(i1:i2))
% grid
% title(‘y = 1/(1 + x^2), polynomial interpolation at 15 points, zoomed’)
% xlabel(‘x’)
% ylabel(‘y’)
% print
pause
ys = spline(x5, y5, x);
plot(x, y, x, ys), grid
title(‘y = 1/(1 + x^2), spline interpolation at 5 points’)
xlabel(‘x’), ylabel(‘y’)
pause
xi = -5: 1: 5;
yi = ones(size(xi))./(1 + xi.^2);
ys = spline(xi, yi, x);
plot(x, y, x, ys), grid
title(‘y = 1/(1 + x^2), spline interpolation at 11 points’)
xlabel(‘x’), pause
% print