%         This is a sample matlab .m file.
%         Prof. Strikwerda   Fall 1995. Modified Dec. 1999.

%         It shows how to interpolate data with a spline and how
%         to fit polynomials of different degrees to data.

%         Give this file the name spline_poly.m
%         To run it in a matlab session, enter the name
%         of the file, i.e., spline_poly

%         Clear all old variables
clear

%         Record this session
%   diary

%         Get the data
load curve1;
disp('The input file is curve1.')

%         x is column 1 of the data
x = curve1(:, 1 ) ;

%         y is column 2 of the data
y = curve1(:, 2 ) ;

%         Clear unneeded variables
clear curve1

%         Set the x vector to graph the spline
newx = 0: 0.05 : 20. ;

%         Set the y vector to graph the spline
newy = spline(x, y, newx ) ;

%         Set the axis parameters
%            xlo xhi   ylo yhi 
axisvector = [ 0. 20.  0. 3.0 ] ;

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

plot ( newx, newy ) 
axis( axisvector ) ;
legend = sprintf('spline fit' ) ;
title(legend) ;

%         Print the spline plot
disp('The spline figure is output to the file splinefig.ps')
print splinefig

%         Set the counter for the plots
plotcount = 0  ;

first = 9 ;
last  = 11 ;

for k = first : last

   disp(' ')
   disp('Hit any key to continue')
   pause

%         Get the coefficients of the fitting polynomial
   coef = polyfit( x, y, k) ;
 
%         Set the y vector to graph the polynomial
   newy = polyval( coef, newx );

%         Print information
   disp('Plotting the polynomial that fits the data')
   disp( sprintf('degree = %3d', k) )

%         Plot the fit
   plot ( newx, newy, x, y, 'o' )
   axis( axisvector ) ;
   legend = sprintf('degree = %3d', k) ;
   title(legend) ;

%         Inquire of the user if they want to save the plot
   yesno = input(' Do you wish to save this plot? (0 = no, 1 = yes ) :') ;
   if yesno == 1
      if plotcount == 0 
         print  poly_fit
         disp('The plot figure is on the file poly_fit.ps')
	 plotcount = 1 ;
      else
         print -append poly_fit
         disp('The plot figure is appended to the file poly_fit.ps')
	 plotcount = plotcount + 1 ;
      end
   end
end

%   diary off
