% Script to understand and practice iterations with a for loop, and to see 
% some functions to manipulate dates. 
%
% This script is a silly simulation of the water content dynamics of a bucket exposed
% to outside conditions.
% Pay attention to each cell and look in the Matlab help each function.
% Feel free to modify and play with the values. Run the script and look in
% the workspace variable types, dimensions, size, etc.
%
% To better understand the for loop, eliminate the if statement. I did not
% set any negative value, so the script will run without problems if you
% eliminate (or deactivate by inserting a % symbol) the if statement. The
% only reason I left it is because you will often see a for loop combined
% with if statements.

%% Generate a random set of data.
Evaporation = round (rand(30,1) * 5)'; % Random evaporation dataset
Rainfall = repmat ([0 0 0 0 0 10], 1, 5); % Rainfall dataset (Rainfall every 6 days).
% Instead of milimiters or inches I assume that somebody pours 10 liters every 6 days in the bucket.

%% Generate dates
initialDate = '1-Jun-2013'; % Day 1
endDate = '30-Jun-2013'; % Day 30

dayVec = datenum(initialDate):datenum(endDate); % make a date vector (see that datenum converts the string format into numeric date Matlab format)

bucketWater = zeros (1,numel(dayVec)); % Pre-allocate memory. Type doc numel to see the help for this function.

%% Define initial water content of the bucket.
bucketWater(1) = 50; % Initial water content (Day 1). Try modifying this value!!

%% Iterate line 16 for each day.
for n = 2:length(dayVec) % See I used length as an alternative to numel function
    bucketWater(n) = max (bucketWater(n-1) + Rainfall (n-1) - Evaporation(n-1),0) ; % Calculation of water in the bucket at the beginning of day n.    
end

% Check for some examples in "doc for", and "doc if/elseif/else"
% CHECK THE DEMOS (type "demo" in the command window)

%% Plot
 plot(dayVec,bucketWater);hold on; % Generate a plot and waits for the bar plot in next statement
 bar(dayVec,Rainfall);
 datetick('x'); % Converts the date numeric format back into string.
 xlabel('Date')
 ylabel('Water Content (Liters)')
 hold off;
 



