Spline interpolation over sine values

%     Spline interpolation over sine values
%     This M-file exemplifies the advantages of spline interpolation
%     for a sinusoidal function. To obtain hardcopies delete the
%     comment signs preceding the PRINT statements.

x = 0: pi/5: pi;
y = sin(x);
disp(‘    x     y’)
[ x’ y’ ]
disp(‘c2 is the array of coefficients of the second-degree’)
disp(‘interpolating polynomial’)
c2 = polyfit(x, y, 2)
pause
xx = 0: pi/10: pi;
y2 = polyval(c2, xx);
ys = spline(x, y, xx);
plot(x, y, xx, y2, ‘+’, xx, ys, ‘o’)
title(‘sin(x)’)
xlabel(‘x, radians’)
text(0.5, 0.2, ‘+ — 2nd degree polynomial’)
text(0.5, 0.3, ‘o — spline’)
pause
% print
c3 = polyfit(x, y, 3);
y3 = polyval(c3, xx);
plot(xx, y2-y3)
title(‘sin(x)’)
xlabel(‘x, radians’)
ylabel(‘Differences between 2nd and 3d degree regressions’)
% print