function bouncer ( )

%*****************************************************************************80
%
%% bouncer() is a simple illustration of gravity with a bouncing ball.
%
%  Licensing:
%
%    Copyright 2014 The MathWorks, Inc.
%
%  Author:
%
%    Cleve Moler
%
%  Reference:
%
%    Cleve Moler,
%    Experiments with MATLAB
%    https://www.mathworks.com/moler
%
  [z0,h] = initialize_bouncer ( );
%
%  Set gravity, elasticity, time step, and initial velocity.
%
  g = 9.8;
  c = 0.75;
  delta = 0.005;
  v0 = 21.0;

  while ( 1.0 <= v0 )
    v = v0;
    z = z0;
    while ( all(z >= 0) )
      set(h,'zdata',z)
      drawnow
      v = v - delta*g;
      z = z + delta*v;
    end
    v0 = c*v0;
  end
  finalize_bouncer ( );

  return
end

%-----------------------------------------------

function [z,h] = initialize_bouncer ( )
%
%% initialize_bouncer() sets z-coordinates and handle for a surf plot of a sphere.
%
   clf ( );
   shg
   set(gcf,'menubar','none','numbertitle','off','name','Bouncer', ...
       'color','white')

   axes('position',[0 0 1 1])
   [x,y,z] = sphere(20);
   z = z + 1;
   h = surf(x,y,z);
   colormap ( 'copper' );
   shading ( 'interp' );
   axis([-12.5 12.5 -12.5 12.5 0 25.0])
   axis ( 'square' );
   axis ( 'off' );
   view(90,0)
   uicontrol('string','TOSS','style','pushbutton', ...
      'units','normal','position',[.10 .05 .12 .05], ...
      'background','white','fontweight','normal', ...
      'enable','off','callback','bouncer')
   drawnow
end

%-----------------------------------------------

function finalize_bouncer ( )

  set(findobj('string','TOSS'),'fontweight','bold','enable','on')

  return
end
