% example mixing problem
% by Gavin LaRose, 6/28/2012
% this demonstration models the number of black balls in a box as
%    a mixing problem.  we use a continuous model and plot the
%    solution to the differential equation.
%
% (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 balls added each time unit
blackInPerFrame = 3;
% the size of the hole through which balls leave the box, in
%     ball units
holeSize = 4;

% how long to calculate for 
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;


%----------------------------------------------------------------------
% generate the plot

% (numerical) solution to the modeling differential equation; this
%    gives the number of black balls in the box
[t,theorySol] = ode45(@(t,y) blackInPerFrame - holeSize*y/volume, ...
    1:simulationLength, numBlack);

% we can then plot this result
figure();
plot(t,100*theorySol/volume, 'b-','LineWidth',2);
axis( [0 simulationLength 0 volume] );
title_handle=title('Balls in a Box: ODE Model');
set( title_handle, 'FontSize', 18 );
legend_handle=legend( '% of black balls in box' );
set( legend_handle, 'FontSize', 14, 'Location', 'SouthEast' );

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