clear ;
clf;

% 11 data points

disp(' ');
disp('This is the data:') ;

x = [ -1.0 -0.7 0.2  1.1 1.9 2.5 3.0  3.2 3.9 4.2 4.7 5.0 ]
y = [  0.3  1.1 2.2  2.4 2.1 1.5 1.5  1.7 1.9 2.3 2.6 2.1 ]

plot( x, y, 'o' ) ;
%               x-axis  y-axis
%               lo  hi  lo  hi
axisvector =  [ -1. 5.  0. 3.0 ] ;
axis( axisvector ) ; 
hold on ;

% Set up the vector of x values to use in the plots.
tt = -1 :.1 :5 ;

% Now we do the higher degree polynomials.
for d = 1: size(x,2) -1
   plot( x, y, 'o' ) ;
   axis( axisvector ) ; 
   legend = sprintf('degree = %3d', d) ;
   title(legend) ;
   hold on ;

   if d==1
      fprintf('First, we''ll look at linear interpolants using successive points');
   else
      fprintf('\nNow we plot polynomials of degree %d using successive points', d ) ;
   end

   comment = sprintf('See the polynomials of degree %d? (1=yes,0=no) ',d) ;
   yesno = input(comment);
   if yesno == 1
      for k = 1:(size(x,2) - d)

         fprintf('The fit through points %d through %d\n', k, k+d) ;
         coef = polyfit( x(k:k+d), y(k:k+d), d) ;
         zz   = polyval( coef, tt ) ;
         plot( x, y, 'o', tt, zz ) ;

         disp('Hit any key to continue...' ) 
         pause
      end
      hold off
   end
end

% Spline

disp(' ');
disp('Cubic splines are a useful way to interpolate data.');
disp('A cubic spline is formed from cubic polynomials to get a smooth');
disp('curve that interpolates the data.');

% ss is the values of the cubic spline at the points tt.
ss = spline(x, y, tt ) ;

% Plot the spline
disp(' ') ;
disp('Plotting the spline')

plot ( tt, ss, x, y, 'o' )
axis( axisvector ) ;
legend = sprintf('spline fit' ) ;
title(legend) ;


