% Discussed the use of random number generators for Monte Carlo calculations
% starting off with the command  hist  for generating/plotting a histogram

>> help hist

 HIST  Histogram.
    N = HIST(Y) bins the elements of Y into 10 equally spaced containers
    and returns the number of elements in each container.  If Y is a
    matrix, HIST works down the columns.
 
    N = HIST(Y,M), where M is a scalar, uses M bins.
 
    N = HIST(Y,X), where X is a vector, returns the distribution of Y
    among bins with centers specified by X.  Note: Use HISTC if it is
    more natural to specify bin edges instead.
 
    [N,X] = HIST(...) also returns the position of the bin centers in X.
 
    HIST(...) without output arguments produces a histogram bar plot of
    the results.
 
    See also HISTC.

>> type Clouds

% Script File: Clouds
% 2-dimensional pictures of the uniform and normal distributions.

close all
Points = rand(1000,2);
subplot(1,2,1)
plot(Points(:,1),Points(:,2),'.')     
title('Uniform Distribution.')
axis([0 1 0 1])
axis square
Points = randn(1000,2);
subplot(1,2,2)
plot(Points(:,1),Points(:,2),'.')    
title('Normal Distribution.')
axis([-3 3 -3 3])
axis square

% discussed the Script File: Darts  for estimating the area of the unit disk 
% but did not bother to run it. 

%Skipped the section on polygon smoothing.

% Started discussion of  error:
%       exact = approx. + error
%
% and quickly looked at details of floating point numbers.

% Matlab provides the built-in number   eps 
% which, strictly speaking, is twice the unit round-off for the
(double-precision) fl.arithmetic used by matlab. Here it is:

>> eps

ans = 2.2204e-016

% It's easier to look at its logarithm to the base 2:

>> log2(eps)
ans = -52

% ... showing  eps  to be  2^{-52} . So, if this is twice the unit roundoff
% then the mantissa length in matlab fl.# must be t = 53 . 
% Yet, the matlab fl.# uses a 64 bit word, with 1 bit for the sign, 11 bits
% for the exponent (that ranges roughly from -1023 to 1023), and that leaves only
% 52 bits for the mantissa. Nevertheless, t=53, since, except for the number 0,
% all fl.# in matlab are NORMALIZED, hence have 1 as their most significant
% digit and, that being so, there's no need to store that 1 explicitly.

% Check this out by looking at eps in hexidecimal format which takes the 
% bits in a machine number four bits at a time and shows each such four-bit as
% the corresponding hexadecimal character 0,1,...,9,a,b,c,d,e,f :

>> format hex
>> eps
ans = 3cb0000000000000

% You can see that the first twelve bits are 0011 1100 1011, and the rest are
% zero, hence, the sign is + (as indicated by the initial 0 bit), the exponent
% reads, literally (recall that (c)_16 = 12, (b)_16 = 11 )
%      (3*16 + 12)*16 + 11 = 971
% and the mantissa (as indicated by those 52 binary zeros) is (.1)_2 = 1/2.

% Since 2^{-52} = (.1)_2 * 2^{-51}
% it must be the case that, in matlab fl.#s, the actual exponent  e  of a
% number is stored as  e+1022. Let's check this by looking at the number 
% 1 = +(.1)_2 2^1  :

>> 1
ans = 3ff0000000000000

% so, the exponent is given here as the number 

>> (3*16+15)*16+15 
ans = 408ff80000000000

% WELL, that didn't help! We need to see it in the ordinary format. To get
% back to the ordinary format, just type

>> format
>> (3*16+15)*16+15 

ans = 1023

%  (of course!! It's (3ff)_{16} = (400)_{16}-1 = 2^{10}-1 ) ,  )
% and this equals  1022 + 1 , as expected.

% to be continued
