You are on page 1of 6

MATLAB TUTORIAL

How to define vectors and matrices in MATLAB:


x_min = 0; x_max = 10; d_x = 0.1; % x = [x_min:d_x:x_max]; % other possible commands x1 = [0:d_x:10] x2 = [10:d_x:20] x = [x1 x2] %sequence of the two vectors x1 and x2 % flip of a vector y = [-fliplr(x) x]; z = [-fliplr(x(2:end)) x];

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.15 PDF 0.25*CDF 0.1

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

1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0

10

How to plot multiple pairs of vectors (x1,y1), (x2,y2),


x1 = [0:0.01:5]; x2 = [5:0.01:10]; y1 = sqrt(x1); y2 = x2.^1.2; plot(x1,y1,'b',x2,y2,'r') grid on xlabel('x') ylabel('y_1 , y_2') legend('y_1','y_2') set(legend,'Location','SouthEast','FontSize',12) x1 = [0:0.01:5]; x2 = [5:0.01:10]; y1 = sqrt(x1); y2 = x2.^1.2; plot([x1 x2],[y1 y2]) grid on xlabel('x') ylabel('y_1 , y_2') legend('y_1','y_2') set(legend,'Location','SouthEast','FontSize',12)

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]

The roots can be found using the following line of command


r = roots(p)

How to find the zeros of a generic nonlinear function


x = [0:pi/30:6*pi]; y = x.*sin(x); plot(x,y) grid on z1 = fzero(@(x) x*sin(x),4) z2 = fzero(@(x) x*sin(x),5) Use the matlab help to better understand all these commands!

You might also like