clear ;
clf ;

% 7 data points

disp('This is the data to be interpolated ')
x = [ -1.0  -0.7  0.2  1.1  1.9  2.5  3.0 ]
y = [  0.3   1.1  2.2  2.4  2.1  1.5  1.5 ]

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

plot( x, y, 'o' ) ;

%               x-axis  y-axis
%               lo  hi  lo  hi
axisvector =  [ -1. 3.  0. 3.0 ] ;

axis( axisvector ) ;
hold on ;

disp('We begin by looking at the linear interpolants using successive points.');

for k = 1:(size(x,2) - 1)
   xx= x(k:k+1) ;
   yy= y(k:k+1) ;
   
   zz = y(k) + (tt -x(k)) *( y(k+1) - y(k) )/( x(k+1) - x(k) );
   plot( tt, zz ) ;

   disp('Hit any key to continue' ) 
   pause
end
hold off
disp('This is the straight-line interpolating function.')
plot( x, y, 'o', x, y ) ;
axis( axisvector ) ;

disp('Hit any key to continue' ) 
pause

% Now we do the higher degree polynomials.  d = degree

for d = 2: size(x,2) -1
   plot( x, y, 'o' ) ;
   axis( axisvector ) ;
   hold on ;

   disp(' ') ;
   disp(sprintf('Now we plot polynomials of degree %d using successive points', d )) ;

   for k = 1:(size(x,2) - d)

      comment = sprintf('The fit through points %d through %d', k, k+d) ;
      disp( comment )

      % Get the coefficients for the interpolating polynomial.
      coef = polyfit( x(k:k+d), y(k:k+d), d) ;

      % Evaluate the polynomial at tt.
      zz   = polyval( coef, tt ) ;

      plot( x, y, 'o', tt, zz ) ;

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

comment= sprintf('\nThis last plot shows the UNIQUE polynomial of degree %d',... 
size(x,2) -1 ); 
disp( comment ) 
disp('that interpolates this set of data.')
