Professional Documents
Culture Documents
X = ones(3,5); % Matrix of ones Y = eye(4) % Identity matrix Z = zeros(6,7); % Matrix of zeros y1 y2 Z1 Z2 = = = = [1:5] [6:10] [y1 ; y2] % a 2x5 matrix [y1' y2'] % a 5x2 matrix
How to define random numbers, random vectors, and random matrices in MATLAB:
% Random number uniformly distributed U~[0,1] rand(1) % Random column vector with coefficients uniformly distributed U~[0,1] rand(3,1) % Random matrix with coefficients uniformly distributed U~[0,1] rand(3,2) % Random matrix with coefficients Normally distributed N~[0,1] randn(3,2) % Random numbers from other PDFs random('exp',10,1) % Random vectors from other PDFs random('beta',10,5,[2,1]) % Random matrices from other PDFs random('wbl',5,10,[2,1]) Use the matlab help to better understand all these commands!
How to perform simple operations with vectors (e.g., dot products, component-bycomponent products)
x = [-10:1:10]; y = randn(1,21); z1 = x.*y; % component-by-component product z2 = x.^y; % component-by-component power z3 = x./y; % component-by-component division z4 = x*y' % sends out a scalar value z5 = x'*y % sends out a matrix % x y E Expected value (E) of a discrete RV = [0:5]; = [0.1 0.2 0.3 0.15 0.15 0.1]; = sum(x.*y)
% Second-order moments of a discrete RV x = [0:5]; y = [0.1 0.2 0.3 0.15 0.15 0.1]; E2 = sum(x.^2.*y) E2c= E2-sum(x.*y)^2 % this represents the variance
How to you use MATLAB built-in functions to generate PDFs and CDFs
% Normal Distribution x = [-10:0.1:20]; y = normpdf(x,5,5); z = 0.1*normcdf(x,5,5); % scaled CDF function plot(x,y,'b',x,z,'r') legend('PDF', '0.1*CDF') set(legend,'Location','East') grid on
0.1 0.09 0.08 0.07 0.06 0.05 0.04 0.03 0.02 0.01 0 -10 PDF 0.1*CDF
-5
10
15
20
% Lognormal Distribution x = [0:0.01:20]; y = lognpdf(x,1,1); Y = 0.25*logncdf(x,1,1); % scaled CDF function plot(x,y,'b',x,Y,'r') legend('PDF', '0.25*CDF') set(legend,'Location','East') grid on
0.25
0.2
0.05
10
12
14
16
18
20
% Binomial Distribution p = 0.3; % Probability of success for each trial n = 10; % Number of trials k = [0:n]; % Outcomes m = binopdf(k,n,p); % Probability mass vector figure(1) bar(k,m,0.5) % Visualize the probability distribution ylabel('Binomial PMF') grid on figure(2) stem(k,m) % Visualize the probability distribution ylabel('Binomial PMF') grid on M = binocdf(k,n,p) figure(3) stairs(k,M) ylabel('Binomial CDF') grid on
0.35
0.3
0.25
0.2
0.15
0.1
0.05
10
0.35
0.3
0.25
0.2
0.15
0.1
0.05
10
10
How to plot joint PDFs of 2 random variables (the PDF is in this case a surface)
x = y = Z = S = for [-2:0.1:10]; [-5:0.1:15]; []; [3 1.5 ; 1.5 6]; i=1:length(x) for j=1:length(y) Z(i,j) = mvnpdf([x(i) y(j)],[2.5 5],S); end
end surf(x,y,Z')
How to find the roots of a polynomial The polynomial x3 6 x 2 72 x 27 is represented in MATLAB software as
p = [1 -6 -72 -27]