% Exercise 1

% Generating a sample from Rayleigh distribution with variance sigma2 = 1
N = 500;        % Size of the sample
X = randn(2,N); % Sample of vectors in the plane whose components are independen Gaussian
R = sqrt(sum(X.*X));  % Sample of the radii

% Plotting

figure(1)
plot(X(1,:),X(2,:),'k.','MarkerSize',10)
axis('square')

figure(2)
hist(R,20);
hold on

% Comparing the histogram with the analytic Rayleigh distribution

t = linspace(0,6,100)
Rayleigh = t.*exp(-0.5*t.*t);
% Scaling so that the distribution looks 'nice' on the histogram
scaling = 100;
figure(2)
plot(t,scaling*Rayleigh,'r-','LineWidth',2)
legend('Sample histogram','True (scaled)')
hold off
