% bounce_survey  Runs parameter space survey using bounce_fcn [PHYS210-HW3]
%
%-----------------------------------------------------------------------
% Uses bounce_fcn to computes number of bounces vs x-component of 
% ball's velocity, vx0, (which should be constant throughout any given
% simulation), for 
% 
%   vx0 = 0.05, 0.06, 0.07, .... 0,98, 0.99, 1.00
% 
% Note the use of the Matlab commands
%
%    tic
%    toc
% 
% which when used as a "delimiter pair" around any block of code, 
% will return the elapsed time (wall clock time, not necessarily
% CPU time) spent executing the block:  tic resets and starts the 
% timer, toc stops it and reports the time elapsed since tic was called.
%-----------------------------------------------------------------------

%-----------------------------------------------------------------------
% Assign the fixed parameters (plotting is disabled).
%-----------------------------------------------------------------------
deltat = 0.01;
kappa = 0.98;
plotenable = 0;
pausesecs = 0;
avienable = 0;
avifname = '';

%-----------------------------------------------------------------------
% Define the limits and step size for the varying parameter.
%-----------------------------------------------------------------------
vx0min = 0.05;
vx0inc = 0.01;
vx0max = 1.00;

%-----------------------------------------------------------------------
% Since vx is constant during any simulation, and the particle starts 
% at (0,1), it it should bounce off the end of the floor (x = 1) at 
% about  t = 1 / vx, so set  ...
%-----------------------------------------------------------------------
tmax = (1 / vx0min) + 3;
%-----------------------------------------------------------------------
% ... where the 3 is a "safety margin", which should yield
% a tmax sufficiently long for all of the runs.
%-----------------------------------------------------------------------

%-----------------------------------------------------------------------
% Define vector of (initial) x velocity components.
%-----------------------------------------------------------------------
vx0 = vx0min : vx0inc : vx0max;
%-----------------------------------------------------------------------
% Create and initialize vector of bounce counts.
%-----------------------------------------------------------------------
nb = zeros(1, length(vx0));

%-----------------------------------------------------------------------
% Start the timer.
%-----------------------------------------------------------------------
tic;
for ii = 1 : length(vx0)
   %--------------------------------------------------------------------
   % Output the value of the loop variable, as a progress
   % indicator.
   %--------------------------------------------------------------------
   ii
   %--------------------------------------------------------------------
   % Run the simulation with the ii-th value of vx0, and 
   % store the resulting number of bounces in nb(ii).
   %--------------------------------------------------------------------
   nb(ii) = bounce_fcn(tmax, deltat, kappa, vx0(ii), plotenable, ...
      pausesecs, avienable, avifname);
end
%-----------------------------------------------------------------------
% Stop the timer and report the elapsed time---on my home office
% machine it's about 0.5 seconds.
%-----------------------------------------------------------------------
toc;

%-----------------------------------------------------------------------
% Make a plot of nb vs vx0 and save it in JPEG and EPS format (the 
% latter for inclusion in the homework handout.
%-----------------------------------------------------------------------
clf;
hold on;
plot(vx0, nb, 'bo');
axis square;
box;
%-----------------------------------------------------------------------
% Note that with the following syntax, the x-label is rendered as 
% "v-subscript-x0" ... 
%-----------------------------------------------------------------------
xlabel('v_{x0}');
ylabel('Number of bounces');
%-----------------------------------------------------------------------
% ... which is why I need to "escape" the underscore with a backslash
% in the title string:
%-----------------------------------------------------------------------
title('Results from simple parameter-space survey using bounce\_survey.m');
print -djpeg bounce_survey.jpg;
print -depsc bounce_survey.eps -loose;
