USING MATLAB
(A) Use the rand command to generate “X” of size [10000 1] and the command histogram to plot the probability distribution of “X” along with axis labels and a title that includes the mean and variance for “X”. (B) Use a while loop and the rand command to generate “X1”, also of size [10000 1] but with each element of “X1” being the mean of 2 “samples” generated using rand, i.e. a “sample size” of 2 separate numbers generated using rand that are then averaged. This is repeated until “X1” is of size [10000 1].
% Matlab script to create random vectors and plot probability
distribution
% A
% generate X of size [10000 1]
X = rand(10000, 1);
% histogram to plot the probability distribution of "X"
histogram(X);
% specify the X and Y labels
xlabel('X');
ylabel('Y');
% specify the title containing the mean and variance of X
txt = sprintf('Mean: %f Variance: %f',mean(X), var(X));
title(txt);
%B
% create X1 of size [10000 1]
X1 = zeros(10000,1);
i=1; % set i to 1
% loop over X1 vector, generating elements for X1
while(i <= length(X1))
samples = rand(2,1); % generate a sample size of 2 using rand
X1(i) = mean(samples); % set ith element of X1 to average of the
samples
i = i + 1;
end
%end of script
Output:
Get Answers For Free
Most questions answered within 1 hours.