
close all;
clear all;


%% Homework 8 Chapter 4 Problem 5

N = 10000;
%Generates N outcomes from the uniform distribution from -1 to 1
X = random('uniform', 0, 1, N, 1);
Y = random('uniform', 0, 1, N, 1);

Z = abs(X-Y);

figure(1);
%Plot the field generated
subplot(3, 1, 1);
plot(X(:), Y(:), 'xb'); 
title('Field Generated');
xlim([-.1,1.1]);
ylim([-.1,1.1]);

%CDF of Z
cdf_z = [];
ind = 1; 
for i = -1: .01:3
    xaxis(ind) = i;
    cdf_z(ind) = length(find(Z<i));
    ind = ind + 1; 
end

subplot(3, 1, 2);
plot(xaxis, cdf_z/max(cdf_z));
title('CDF of Z');


subplot(3, 1, 3);
[n, xout] = hist(Z, 10);
bar(xout, n/N);
title('PDF of Z');





