function polygui(y)
%POLYGUI  Interpolation by a spline, a shape preserving Hermite cubic
%   and a single polynomial.  See SPLINE, PCHIP, POLYINTERP.

if nargin == 0
   % Default initial y is all zeros.
   y = zeros(1,10);
elseif length(y) == 1
   y = zeros(1,y);
end
if ~isempty(y)
   if isequal(y,'reset')
      % Restore original data
      y = get(0,'userdata');
   else
      % Save input data.
      y = double(y);
      set(0,'userdata',y);

      % Initialize figure.
      clf reset
      set(gcf, ...
        'doublebuffer','on', ...
        'windowbuttondownfcn', ...
        'polygui([]); set(gcf,''windowbuttonmotionfcn'',''polygui([])'')', ...
        'windowbuttonupfcn', ...
        'set(gcf,''windowbuttonmotionfcn'','''')')
      uicontrol('string','reset','callback', 'polygui(''reset'')');
   end
   
   % Initialize plot
   n = length(y)-1;
   x = 0:1/n:1;
   t = 0:1/(16*n):1;
   p = polyinterp(x,y,t); 
   s = spline(x,y,t);
   h = pchip(x,y,t);
 plot(x,y,'o',t,p,'-',t,s,'-',t,h,'-');
legend('(x_n,y_n)','Lagrange polynomial','Cubic spline','Hermite cubic spline'); 
   ymin = min(y);
   ymax = max(y);
   del = 0.5*(ymax-ymin);  if del == 0; del = 1; end
   axis([0 1 ymin-del ymax+del])
   set(gcf,'userdata',[x; y]);
 
else

   % Respond to mouse motion.
   xy = get(gcf,'userdata');
   x = xy(1,:);
   y = xy(2,:);
   n = length(y)-1;
   u = get(gca,'currentpoint');
   u = u(1,:);
   e = abs(x-u(1));
   k = min(find(e == min(e)));
   x(k) = u(1);
   y(k) = u(2);
   t = 0:1/(16*n):1;
   p = polyinterp(x,y,t);
   s = spline(x,y,t);
   h = pchip(x,y,t);
   c = get(gca,'children');
   ax = axis;
   set(c(4),'xdata',x,'ydata',y)
   set(c(3),'ydata',p)
   set(c(2),'ydata',s)
   set(c(1),'ydata',h)
   axis(ax);
   set(gcf,'userdata',[x; y])
end
