function runge_spline_demo
%
% This demonstrates the behavior of cubic spline interpolants 
% on equally spaced interpolating points for the Runge function. 
%
xleft = -1.1; xright = 1.1;
ylow = -1; yhigh = 2;
h = 1/100;
xx = [xleft:h:xright];
Rvals = 1./(1+25*xx.^2);
figure(1); clf; hold on; 
plot([xleft xright],[0 0],'k',...
     [0 0],[ylow yhigh],'k','LineWidth',1);
plot(xx,Rvals,'b','LineWidth',1); 
axis([xleft xright ylow yhigh]); 
npts = input('Type the number of interpolating points (or 0 to quit):'); 
while npts > 0 

  h = 2/(npts - 1); 
  x = [-1:h:1]; 
  y = 1./(1+25*x.^2);

  hh = 2/99;
  xx = -1:hh:1;
  yy = spline(x,y,xx);
  rfun_vals = 1./(1+25*xx.^2);
  max_err = max(abs(yy - rfun_vals)); 

  delete(findobj('Type','line','Color','r'));
  delete(findobj('Type','line','MarkerFaceColor','r'));
  plot(xx,yy,'r');
  plot(x,y,'o',...
         'MarkerSize',4,'MarkerFaceColor','r',...
         'MarkerEdgeColor','k','LineWidth',1.8);
  title(['Cubic spline interpolation at ',num2str(npts),' points.',...
         '   Max. error = ',num2str(max_err)]); 
  npts = input('Type the number of interpolating points (or 0 to quit):'); 
end
