% mbounce_survey_template Instructor supplied script for PHYS210 2014 HW 3.4
% 
% Uses your implementation of mbounce_fcn to perform a parameter
% space survey with
%
%    deltat = 0.01
%
% and for all values of the coefficient of restitution defined by
%
%    kappa = linspace(0.1, 1.0, 19)
%
% For each element of the kappa array, the number of times the 
% particle bounces off the floor is computed by calling your 
% version of mbounce_fcn, and storing the vertical bounce count
% in the corresponding element of the array nb.
% 
% The script then makes a plot of the bounce count as a function
% of the coefficient of restitution and saves a copy as the JPEG
% image file 'hw34_survey_template.jpg'
% 
% You are to copy this script to a new file, mbounce_survey.m, and 
% then modify it so that it performs 4 separate surveys identical
% in nature to the one done here, but with kappa vectors having
% 251 elements, rather than 19.  Your script will produce 5 plots:
% 4 of the exact type as the one generated by this script, and 
% the 5th a "scatter plot" which shows the correlation (or lack
% of it) between the data from experiments 2 and 4. Those two 
% surveys use the same physical parameters, but different values 
% of the fundamental numerical parameter, deltat.
% 
% The 4 surveys are organized as follows:
%
% (A) The first 2 are done with delta t = 0.01 and two
%     different sets of kappa values given by
%
%     Survey 1:  kappa = linspace(0.940, 0.995, 251)
%     Survey 2:  kappa = linspace(0.990, 0.995, 251)
%
% (B) The second 2 ar done with delta t = 0.001 and the 
%     same two sets of kappa.
%
%     Survey 3:  kappa = linspace(0.940, 0.995, 251)
%     Survey 4:  kappa = linspace(0.990, 0.995, 251)
%
%  Approximate run times for my implementation on one of the 
%  lab machines
%
%     Experiment 1:   15 sec
%     Experiment 2:   30 sec
%     Experiment 3:   60 sec
%     Experiment 4:  120 sec
% 
% Each survey MUST store the vertical bounce counts that are 
% computed in a SEPARATE 1D array (row vector), each having 251 
% elements.
% 
% I suggest using the variable names:
% 
%       nb1
%       nb2
%       nb3
%       nb4
% 
% and I will assume that you have so named them in the remainder
% of the discussion.
% 
% PLOTTING
%
% After each survey, <i>, with <i> = 1, 2, 3, 4, plot 
%
%       nb<i> vs kappa
%
% being sure to use the correct kappa vector in each case.
% The plots should appear in separate figure windows, so use
% figure(1), figure(2), etc. before you start each one.
% 
% Label the x and y axes and give the plots titles precisely
% as below, and save a JPEG version of each plot in the 
% file h34_s<i>.jpg
%
% Finally, plot nb4 vs nb2 (i.e. nb2 on the x-axis, nb4
% on the y-axis) and save a JPEG version using the 
% following code:
%
% figure(5);
% clf;
% hold on;
% axis square;
% box on;
% plot(nb2, nb4, 'ro', 'MarkerSize', 2);
% titlestr = sprintf('%s -- Scatter plot of bounce counts', getenv('LOGNAME'));
% title(titlestr);
% xlabel('\Delta t = 0.01');
% ylabel('\Delta t = 0.001');
% print hw34_s5.jpg -djpeg;
%
% AND YOU'LL BE DONE WITH THE CODING OF THIS HOMEWORK!!!!
%
% One straightforward way to proceed is to first modify the 
% code below so that it uses the right call to linspace to
% run the first experiment, and that the name for the JPEG
% file is set correctly.  Run the script until you're satisfied
% that things are working OK, then replicate and modify the
% code for the other three experiments.
%
% IMPORTANT!!
%
% (1) It is extremely important that your implementation of mbounce_fcn
%     exit the main time step loop, and hence, apart from the possible
%     finalizing of an AVI recording, the function, WHEN THE PARTICLE 
%     LEAVES THE SOLUTION DOMAIN.  Otherwise, with tmax set to 1000 as 
%     it is here, it will take much too long for the parameter survey
%     to run.  Ask for help if you're having difficulty with that 
%     part of the programming, but remember that you should have already
%     solved this problem in Problem 2.
%
% (2) It is extremely important that your implementation of mbounce_fcn
%     return the correct output argument, namely a two-element row vector
%     whose first and second elements are the numbers of horizontal
%     and vertical bounces respectively.  The script records and 
%     plots only  the vertical count as a function of kappa, but 
%     it ASSUMES that mbounce_fcn returns a two-element vector, and 
%     the the number of vertical bounces is the second element of 
%     that vector.

% I first clear all variables in the workspace as a guard against possible
% conflicts with values you've defined, and close all the plot windows.
clear;
close all;

% Set tmax to a LARGE value so that no matter what the value of 
% kappa there is a very good chance that it will escape before that 
% time.  Again, note that if your mbounce_fcn does not exit when
% the ball leaves the plotting domain, this script could run for 
% a VERY long time!
tmax = 1000;
plotenable = 0;
% Disable plotting
pausesecs = 0.0;

% Set up the parameters for the survey
deltat = 0.01;
nkappa = 19;
kappa = linspace(0.9, 1.00, nkappa);

% We store only the vertical bounces, so nb can be a column vector (1D
% array).
nb1 = zeros(1, nkappa);

% Start the timer.
tic;
fprintf('Running for %d values of kappa:', nkappa);
for ii = 1 : nkappa
   % Perform a single simulation with the ii-th value of kappa.
   nb = mbounce_fcn(tmax, deltat, kappa(ii), plotenable, pausesecs); 
   % Assign the second element of the return value (the # of vertical
   % bounces), to the ii-th element of nb1.
   nb1(ii) = nb(2);
   % Print progress.
   if mod(ii,10) == 0
      fprintf(' %d', ii);
   end
end
fprintf('\n');
% Stop the timer and report the elapsed time.
toc;

% Make a plot of number of vertical bounces vs coefficient of
% restitution, and save a copy as a JPEG image.
figure(1);
clf;
hold on;
axis square;
box on;
% Set limits on x-axis to coincide with those of kappa itself.
xlim([min(kappa), max(kappa)]);

plot(kappa, nb1);

% Generate a plot title that includes the login name.
titlestr = sprintf(['%s -- Number of vertical bounces vs coefficient ',...
  'of restitution: \\Delta t = %.2e\n'], getenv('LOGNAME'), deltat);
title(titlestr);
xlabel('Coefficient of restitution');
ylabel('Number of bounces');
print hw34_survey_template.jpg -djpeg;
print hw34_survey_template.eps -depsc;
