Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB: For this assignment we are asked to produce a function that has no param

ID: 3856908 • Letter: M

Question

MATLAB: For this assignment we are asked to produce a function that has no parameters and no return, yet creates a random 2d array (usind randi with integers between 0 and 100) and also has random dimensions. After doing this, our teacher wants us to put the dimensions and the matrix itself (IN THAT ORDER) into a text file.

Here is my code thus far....need help putting this into a text file. I seem to be able to put the matrix in it but not the dimensions:

function populate_matrix

% create random matrix between 0 and 100.

N = randi ([1,10], 1);

M = randi ([1,10], 1);

R = randi ([0,100],N,M)

row_size = length(N);

column_size = length(M);

fid = fopen('My_Matrix.txt','wt');

fprintf(fid,'row_size','column_size')

for ii = 1:size(R,1)

fprintf(fid,'%g ',R(ii,:));

fprintf(fid,' ');

end

fclose(fid)

Explanation / Answer

for m=1:M
if rand(1,1)<0.5
y(m,1)=PMFdata(1,[1 2 3 4 5 6]’,[1/6 1/6 1/6 1/6 1/6 1/6]’);
else
y(m,1)=PMFdata(1,[2 3]’,[1/2 1/2]’);
end
end
7
for m=1:M
ux=rand(1,1);
uy=rand(1,1);
if ux<=1/4; % Refer to px[i]
x(m,1)=0;
if uy<=1/2 % Refer to py|x[j|0]
y(m,1)=0;
else
y(m,1)=1;
end
else
x(m,1)=1; % Refer to px[i]
if uy<=1/3 % Refer to py|x[j|1]
y(m,1)=0;
else
y(m,1)=1;
end
end
end
Chapter 9
% covexample.m
clear all % clears out all previous variables from workspace
rand(’state’,0); % sets random number generator to initial value
M=1000;
for m=1:M % generate realizations of X (see Section 7.11)
u=rand(1,1);
if u<=0.25
x(1,m)=-8;x(2,m)=0;
elseif u>0.25&u<=0.5
x(1,m)=0;x(2,m)=-8;
elseif u>0.5&u<=0.75
x(1,m)=2;x(2,m)=6;
else
x(1,m)=6;x(2,m)=2;
end
end
meanx=[0 0]’; % estimate mean vector of X
for m=1:M
8 CHAPTER 23. MATLAB CODE
meanx=meanx+x(:,m)/M;
end
meanx
CX=zeros(2,2);
for m=1:M % estimate covariance matrix of X
xbar(:,m)=x(:,m)-meanx;
CX=CX+xbar(:,m)*xbar(:,m)’/M;
end
CX
A=[1/sqrt(2) -1/sqrt(2);1/sqrt(2) 1/sqrt(2)];
for m=1:M % transform random vector X
y(:,m)=A*x(:,m);
end
meany=[0 0]’; %estimate mean vector or Y
for m=1:M
meany=meany+y(:,m)/M;
end
meany
CY=zeros(2,2);
for m=1:M % estimate covariance matrix of Y
ybar(:,m)=y(:,m)-meany;
CY=CY+ybar(:,m)*ybar(:,m)’/M;
end
CY

for i=1:M
u=rand(1,1);
if u<=0.75
x(i,1)=1; % head mapped into 1
else
x(i,1)=0; % tail mapped into 0
end
end
% Assume outcomes are in x, which is M x 1 vector
M=1000;
bincenters=[-4:0.5:4]’; % set binwidth = 0.5
bins=length(bincenters);
h=zeros(bins,1);
9
for i=1:length(x) % count outcomes in each bin
for k=1:bins
if x(i)>bincenters(k)-0.5/2. ...
& x(i)<bincenters(k)+0.5/2
h(k,1)=h(k,1)+1;
end
end
end
pxest=h/(M*0.5); % see (10.38)
% Q.m
%
% This program computes the right-tail probability
% (complementary cumulative distribution function) for
% a N(0,1) random variable.
%
% Input Parameters:
%
% x - Real column vector of x values
%
% Output Parameters:
%
% y - Real column vector of right-tail probabilities
%
% Verification Test Case:
%
% The input x=[0 1 2]’; should produce y=[0.5 0.1587 0.0228]’.
%
function y=Q(x)
y=0.5*erfc(x/sqrt(2)); % complementary error function
% Qinv.m
%
% This program computes the inverse Q function or the value
% which is exceeded by a N(0,1) random variable with a
% probability of x.
%
% Input Parameters:
%
10 CHAPTER 23. MATLAB CODE
% x - Real column vector of right-tail probabilities
% (in interval [0,1])
%
% Output Parameters:
%
% y - Real column vector of values of random variable
%
% Verification Test Case:
%
% The input x=[0.5 0.1587 0.0228]’; should produce
% y=[0 0.9998 1.9991]’.
%
function y=Qinv(x)
y=sqrt(2)*erfinv(1-2*x); % inverse error function
Chapter 11
rand(’state’,0) % sets random number generator to
% initial value
M=10000;gamma=5;% change M for different estimates
u=rand(M,1); % generates M U(0,1) realizations
x=-log(1-u); % generates M exp(1) realizations
k=0;
for i=1:M % computes estimate of P[X>gamma]
if x(i)>gamma
k=k+1;
y(k,1)=(1/sqrt(2*pi))*exp(-0.5*x(i)^2+x(i)); % computes weights
% for estimate
end
end
Qest=sum(y)/M % final estimate of P[X>gamma]

randn(’state’,0) % set random number generator to initial value
G=[1 0;0.9 sqrt(1-0.9^2)]; % define G matrix
M=2000; % set number of realizations
for m=1:M
x=randn(1,1);y=randn(1,1); % generate realizations of two independent
% N(0,1) random variables
wz=G*[x y]’+[1 1]’; % transform to desired mean and covariance
WZ(:,m)=wz; % save realizations in 2 x M array
11
end
Wmeanest=mean(WZ(1,:)); % estimate mean of W
Zmeanest=mean(WZ(2,:)); % estimate mean of Z
WZbar(1,:)=WZ(1,:)-Wmeanest; % subtract out mean of W
WZbar(2,:)=WZ(2,:)-Zmeanest; % subtract out mean of Z
Cest=[0 0;0 0];
for m=1:M
Cest=Cest+(WZbar(:,m)*WZbar(:,m)’)/M; % compute estimate of
% covariance matrix
end
Wmeanest % write out estimate of mean of W
Zmeanest % write out estimate of mean of Z
Cest % write out estimate of covariance matrix

randn(’state’,0) % set random number generator to initial value
rho=0.9;
M=500; % set number of realizations to generate
for m=1:M
x(m,1)=randn(1,1); % generate realization of N(0,1) random
% variable (Step1)
ygx(m,1)=rho*x(m)+sqrt(1-rho^2)*randn(1,1); % generate
% Y|(X=x) (Step 2)
end

C=[1 2/3 1/3;2/3 1 2/3;1/3 2/3 1];
G=chol(C)’; % perform Cholesky decomposition
% MATLAB produces C=A’*A so G=A’
M=200;
for m=1:M % generate realizations of x
u=[randn(1,1) randn(1,1) randn(1,1)]’;
x(:,m)=G*u; % realizations stored as columns of 3 x 200 matrix
end

%
% clt_demo.m
clear all
delu=0.005;
u=[0:delu:1-delu]’; % p_X defined on interval [0,1)
p_X=ones(length(u),1); % try p_X=abs(2-4*u) for really strange PDF
x=[u;u+1]; % increase abcissa values since repeated
% convolution increases nonzero width of output
p_S=zeros(length(x),1);
N=12; % number of random variables summed
for j=1:length(x) % start discrete convolution approximation
% to continuous convolution
for i=1:length(u)
if j-i>0&j-i<=length(p_X)
p_S(j)=p_S(j)+p_X(i)*p_X(j-i)*delu;
end
end
end
plot(x,p_S) % plot results for N=2
grid
axis([0 N 0 1]) % set axes lengths for plotting
xlabel(’x’)
ylabel(’p_S’)
title(’PDF for S_N’)
text(0.75*N,0.85,’N = 2’) % label plot with the
% number of convolutions
for n=3:N
pause
x=[x;u+n-1]; % increase abcissa values since
% repeated convolution increases
% nonzero width of output
p_S=[p_S;zeros(length(u),1)];
g=zeros(length(p_S),1);
for j=1:length(x) % start discrete convolution
for i=1:length(u)
if j-i>0
g(j,1)=g(j,1)+p_X(i)*p_S(j-i)*delu;
end
end
end

=======================================================================================

% PMFdata.m
%
% This program generates the outcomes for N trials
% of an experiment for a discrete random variable.
% Uses the method of Section 5.9.
% It is a function subprogram.
%
% Input parameters:
%
% N - number of trials desired
% xi - values of x_i’s of discrete random variable (M x 1 vector)
% pX - PMF of discrete random variable (M x 1 vector)
%
% Output parameters:
%
% x - outcomes of N trials (N x 1 vector)
%
function x=PMFdata(N,xi,pX)
M=length(xi);M2=length(pX);
if M~=M2
message=’xi and pX must have the same dimension’
end
for k=1:M ; % see Section 5.9 and Figure 5.14 for approach used here
if k==1
bin(k,1)=pX(k); % set up first interval of CDF as [0,pX(1)]
else