%Program 3.2.  Polynomial Interpolation Program
%Left click in Matlab figure window to locate data point.
%     Continue, to add more points.
%     Right-click to terminate program.
function clickinterp
xl=-3;xr=3;yb=-3;yt=3;
plot([xl xr],[0 0],'k',[0 0],[yb yt],'k');grid on;
xlist=[];ylist=[];
button=1;k=0;                      % initialize counter k
while(button ~= 3)                 % if right click, terminate
  [xnew,ynew,button] = ginput(1);  % get one mouse click
  if button == 1                   % current click is a left click
    k=k+1;                         % k counts clicks
    xlist(k)=xnew; ylist(k)=ynew;  % add new point to the list
    c=newtdd(xlist,ylist,k);       % get interpolation coeffs
    x=xl:.01:xr;                   % define x coordinates of curve
    y=nest(k-1,c,x,xlist);         % get y coordinates of curve
    plot(xlist,ylist,'o',x,y,[xl xr],[0,0],'k',[0 0],[yb yt],'k');
    axis([xl xr yb yt]);grid on;
  end
end
