
% structure for flipping a coin 20 times using a "for loop"        
for k=1:20
            % flip a coin: generate a uniformly distributed random number
            % between 0 and 1
            num=rand(1)
end
%  This first little exercise was simply to show how to use the "rand"
%  function
% Please learn to use the "Help" feature if there is syntax
% or logic you don't understand.
%********************************************************************

            
% In this second construction, we show how to let a mother cell "divide"
% 20 times and to count how many copies of the protein each daughter
% gets as a result of the cell division process.   You could use this as
% the basis of your own code by adding one more "for" loop that will sum
% over the number of "cells" you watch divide. 
d1=0;
d2=0; % initialize the counters for daughter 1 (d1) and daughter 2 (d2).
% Note that if you are going to insert another loop over number of cells,
% you will need to reinitialize your counter for each new mother cell 
% outside of this for loop (but inside the new one you add).
for k=1:20
            % flip a coin: generate a uniformly distributed random number
            num=rand(1);
            % if rand num bigger than or equal to 0.5, the daughter cell 1 gains 1 molecule
            if num>=0.5
                d1=d1+1;
            end
            % if rand num less than 0.5, daughter cell 2 gains 1 molecule
            if num<0.5
                d2=d2+1;
            end
end
d1
d2
%  *******************************************************************

% our next tutorial has to do with finding the average of  a list of
% numbers.  In particular, let's use rand(10) to generate 100 random
% numbers between 0 and 1 to work with.  If you look up rand(n) you will
% see that it generates a 10x10 matrix of random numbers.

y=rand(10) % this generates a 10x10 matrix called y whose elements are all 
% numbers between 0 and 1

mean(y) % Here is what the Matlab help says: If A is a matrix, mean(A) 
% treats the columns of A as vectors, returning a row vector 
% of mean values.

mean(mean(y)) % this reports the mean of the 100 numbers in our matrix..
% Some version of this function could come in handy for your results.  
% Probably you will be taking the mean of a vector of values which means
% you will not need the nested construction using 
% "mean" twice  I have used here.

% *********************************************************

% The last little hint that I think might be useful is to figure out how
% to fit the function y=sqrt(alpha*N) through your discrete set of values for
% y and N.     Let's use (3,10), (7,50) and (9.5,100) as our three values
% sqrt(<(I_1-I_2)^2>) and I_tot.  

% create fitting function to my 3 "data" points
Itot=[10,50,100] % input the total intensity of the mother cell
Ivar=[3,7,9.5]   % input the difference in intensity from the daughters
figure,
% create the fitting function using fittype, see more in help
func=fittype('sqrt(alpha*x)','indep','x','coeff','alpha');
% fit input data
% the output of "fitfunc" is parameters involved in the function,
% particularly in this case "alpha" along with a 95% confidence interval
fitfunc=fit(Itot',Ivar',func)
% Create a new set of Itot-values with which to plot our fit results
% between 0 to 120 by incrementing by 1
newItot=[0:1:120];
% This is the new set of Ivar values evaluated using fitted function
% corresponding to each element in newItot
fitIvar=feval(fitfunc,newItot);

% plot the original data points
plot(Itot,Ivar,'k.','MarkerSize',20);
% on the same graph plot the fitting result
hold on;
plot(newItot,fitIvar,'r-','MarkerSize',20);
% set x, y axes limit
xlim([0 120]);
ylim([0 10]);
% label x, y axes, where au here is "arbitrary unit"
xlabel('I_t_o_t_a_l [au]');
ylabel('I_v_a_r [au]');
% add title
% num2str function is a way to change numbers to
% characters
title(['Y = (',num2str(fitfunc.alpha),'*N)^1^/^2']);
hold off;



