clear ;
clf;

%       13 data points


x= -1.0 : .5 : 5.0 ;
y= exp(-0.3*x).*sin( 2.3*x ) ;

% Runge's function.
% y= 1./( (x-2).*(x-2) + 1. ) ;

disp('Input a small amount to perturb the curve (0. to 0.5)');
pert = input(':');
y = y + pert*rand( size(y) ) ;

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

plot( x, y, 'o' ) ;

%               x-axis   y-axis
%               lo  hi   lo   hi
axisvector =  [ -1. 5.  -1.5  1.5 ] ;
axis( axisvector ) ; 
hold on ;

% Now we do the highest degree polynomial.

s = size(x,2); 
d = s-1 ;       
plot( x, y, 'o' ) ;
axis( axisvector ) ; 
legend = sprintf('degree = %d', d) ;
title(legend) ;
hold on ;

k = 1 ;

disp('The fit through points ...')
coef = polyfit( x, y, s-1) ;
zz   = polyval( coef, tt ) ;
plot( x, y, 'o', tt, zz ) ;


disp('Hit any key to continue' ) 
pause

% Spline fit
ss = spline(x, y, tt ) ;

% Plot the spline
disp('Plotting the spline')
plot ( tt, ss, x, y, 'o' )
axis( axisvector ) ;
title('Spline Fit') ;


