% eda03_04
% Illustrate the calculatioon of the variance of a
% distribution p(d)

% Illustrative distribution p(d)
Dd = 1.0;  % sampling interval
d = Dd*[0:14]' + 0.5*Dd; % vector of evenly spaced d's
p = [5.00, 15.00, 10.00, 10.00, 10.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00]';

% normalize distribution so its integral is unity
% note element-wise division
p = p./(Dd*sum(p));

% first calculate the mean
dbar = Dd*sum(d.*p);

% now the variance
q = (d-dbar).^2;
sigma2 = Dd*sum(q.*p);
sigma = sqrt(sigma2);

% output to command window
disp(sprintf('sigma2 = %f   sigma = %f', sigma2, sigma));