% example mixing problem
% by Justin Dunmyre, 6/2012
% modified by Gavin LaRose, 6/28/2012
% this demonstration models the number of black balls in a box as
%    a mixing problem.  this file runs a simulation and plots the
%    resulting percent of black balls.
%
% (c)2012 Regents of the University of Michigan
% This work is licensed under the
% Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
% To view a copy of this license, visit
% http://creativecommons.org/licenses/by-nc-sa/3.0/
% or send a letter to
% Creative Commons,
% 444 Castro Street, Suite 900,
% Mountain View, CA, 94041, USA.
%----------------------------------------------------------------------
% configuration variables

% number of black and white balls added each time units
blackInPerFrame=3;
% the size of the hole through which balls leave the box, in
%     ball units
holeSize = 4;
% then we put in invisible white balls to fill out the volume
whiteInPerFrame = holeSize - blackInPerFrame;
% length of simulation, in time units
simulationLength=200;

% initial number of black balls in the box
numBlack=0;
% "volume" of the box, in number of balls; should be divisible
%    by the boxWidth, below.  good values are 50, 100 and 200
volume=100;
% initial number of white balls (that is, empty spaces) in box
initWhite=volume-numBlack;

%----------------------------------------------------------------------
% generate the demo
   
% contColor is a cell array giving the colors of each dot
%    (unit volume) in the box
contColor={};
% initially we have initWhite white balls and numBlack black
for j=1:initWhite
    contColor{end+1}='w';
end
for j=1:numBlack
    contColor{end+1}='k';
end
    
% simulation loop
for j=2:simulationLength
    % shake the box (randomly relocate all of the balls)
    contColor=contColor(randperm(length(contColor)));
    % then initially we have the same number of black balls
    %    as we did at the last time interval
    numBlack(j)=numBlack(j-1);
    % the loop below adds blackInPerFrame black balls, bumping
    %    a dot off the end of the array for each one we add and
    %    keeping track of the number of black balls in the
    %    box
    for k=1:blackInPerFrame
        contColor=cat(2,'k',contColor);
        cout=contColor{end};
        if(cout~='k')
            numBlack(j)=numBlack(j)+1;
        end
        contColor=contColor(1:end-1);
    end
    % similar for the number of white balls added
    for k=1:whiteInPerFrame
        contColor=cat(2,'w',contColor);
        cout=contColor{end};
        if(cout=='k')
            numBlack(j)=numBlack(j)-1;
        end
        contColor=contColor(1:end-1);
    end
end

plot(1:length(numBlack),100*numBlack/length(contColor), 'k-',...
    'LineWidth', 2);

title_handle=title('Balls in a Box: Simulation');
set( title_handle, 'FontSize', 18 );
legend_handle = legend( '% of black balls', 'Location', 'SouthEast');
set( legend_handle, 'FontSize', 14 );
axis([1 simulationLength 0 volume]);

% all done
%----------------------------------------------------------------------
