MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY
What is MATLAB? MATLAB (MATrix LABoratory) developed by The Mathworks, Inc. (http://www.mathworks.com) Key Features: High-level language for numerical computation, visualization, and application development Interactive environment for iterative exploration, design, and problem solving Mathematical functions for linear algebra, statistics, Fourier analysis, filtering, optimization, numerical integration, and solving ordinary differential equations Built-in graphics for visualizing data and tools for creating custom plots 2
MATLAB s Workspace No need to pre-allocate or declare variables o but sometimes it will speed up the code variables will be automatically stored in the workspace o who : to list all the variables existing in your workspace o whos : similar to who, but with all details such as size and class o clear all : to clear all the variables in the workspace o clear name-of-variable : to clear a specific variable o save FileName : save workspace variables to FileName.mat file o load FileName : load variables from FileName.mat file 3
Rules on Variables and File Names Case sensitive, Var and var are 2 distinct variables. Variable begins with a letter, i.e., A2z or a2z Can be a mix of letters, digits, and underscores ( vector_a) Reserved characters: % = + ~ ; :! [ ] ( ), @ # $ & ^ Script Files: Ordinary ASCII (text) files that contain Matlab commands. Have an extension.m (e.g., myfile.m) and, for this reason, they are known as m-files. The commands in this file may then be executed using >> myfile 4
MATLAB s Syntax % is used for comments >> expression o >> a=2+3 >> function(par1, par2) o >>sum(b,1) % sums the elements of B along the first dimension. >> [out1,out2]=function(par1, par2); o [B, Z]=sort(A, 1); % sorts the elements of A in ascending order along the first dimension. o % Z is the same size as A and describes the arrangement of the elements % of A into B along the sorted dimension. We can add ; at the end of a syntax to suppress (hide) printing the results. 5
MATLAB s Syntax We can add ; to enable multiple commands on the same line. o >> a=6; b=7; We can also use comma, to enable multiple commands on the same line without suppressing printing. o >> a=6, b=7 a = b = 6 7 6
Vectors and Matrices >> a = [1 2 3; 4 5 6; 7 8 9] a = >> a(2,1) ans = 4 1 2 3 4 5 6 7 8 9 >> v1=a(1,:) v1 = 1 2 3 % use space or comma to separate elements on the same % row, semicolon to next row % Matrix indexing (row index, column index) % calls the first row vector 7
Vectors and Matrices >> a = [1 2 3; 4 5 6; 7 8 9] a = >> v2=a(:,2) v2 = 2 5 8 1 2 3 4 5 6 7 8 9 % use space or comma to separate elements on the same % row, semicolon to next row % calls the second column vector 8
Vectors and Matrices >> a = [1 2 3; 4 5 6; 7 8 9] a = 1 2 3 4 5 6 7 8 9 >> a(:,1)=v2 a = 2 2 3 5 5 6 8 8 9 % use space or comma to separate elements on the same % row, semicolon to next row % replaces the first column by the second column vector 9
Vectors and Matrices >> a = [1 2 3; 4 5 6; 7 8 9] a = 1 2 3 4 5 6 7 8 9 >> a(:,2)=[] a = 2 3 5 6 8 9 % use space or comma to separate elements on the same % row, semicolon to next row % removes the second column vector 10
Vectors and Matrices >> a = [1 2 3;... 4 5 6;... 7 8 9] a = 1 2 3 4 5 6 7 8 9 % use "..." to continue on the next line. >> v = 0:2:10 % equidistant elements (start_value : interval : end_value) v = 0 2 4 6 8 10 >> v = 2:8 % default interval is 1 v = 2 3 4 5 6 7 8 11
Special Matrices eye(3) ans = 1 0 0 0 1 0 0 0 1 zeros(3, 3) ans = 0 0 0 0 0 0 0 0 0 % to create a 3 x 3 identity matrix % to create a 3 x 3 zero matrix 12
Special Matrices ones(2, 3) % create a 2 x 3 matrix with all elements = 1 ans = 1 1 1 1 1 1 rand(3, 2) % create a 3 x 2 matrix with all elements randomized between 0 to 1 ans = 0.1 0.9 0.2 0.31 0.15 0.4 13
Operators Basic Operators o + Array/Matrix addition o - Array/Matrix subtraction o * Array/Matrix product o / Array/Matrix division o ^ Matrix power o Array/Matrix conjugate For elementwise operation, place a dot before the operator o.*./.^ Relational Operators: <, <=, >, >=, ==, ~= Logical Operators: &,, ~, xor 14
Operators Example ---- find( conditional statement ) o A=[2 8 7; 1 3 2], I=find(A>=2 & A<=3) A = 2 8 7 1 3 2 I = 1 4 6 15
Some Pre-defined Constant Values >> a = pi a = 3.14159 % the π number >> b= 2+3i % i, j are the imaginary unit i = 1 b = 2+3i >> c = Inf; % The infinity >> d = NaN; % Not a Number, i.e., 0 0 or 16
Some Built-in Functions Trigonometric functions: o sin(x), cos(x), tan(x), cot(x), asin(x), acos(x), atan(x), Exponentials and Logarithms: o exp(x), log(x), log10(x) Other elementary functions: o ceil (x), floor (x), round (x), sign (x) o size(x) % gets the size of the matrix X o abs(x) % gets the absolute value of all the elements in matrix X o max(x) or min(x) % gets the max/min values of the matrix X along the first dimension o mean(x) % gets the mean of the elements of the matrix X along the first dimension o sum(x) % gets the sum of the elements of the matrix X along the first dimension 17
How to Build a Function? Functions are Script Files (m-files) with the first line in the following form function [out1, out2]=funname(par1, par2) The function name should be the same as that of the file, o i.e., the m-file should be save as funname.m Functions are executed using local workspaces o There is no risk of conflicts with the variables in the main workspace. At the end of a function execution only the output arguments will be visible in the main workspace. 18
How to Build a Function? 19
Flow Control Statements If Condition: 20
Flow Control Statements switch Condition: Example 21
Flow Control Statements for Loop: Examples while Loop: 22
Plot Function Example 1 >> x=[0 1 2 3 4]; >> plot(x) % plots x versus its index values >> plot(x, x.^2) % plots x 2 versus x Example 2 >> figure % open new figure >> x=pi*[-1:.05:1]; % π x π >> plot(x, sin(x)); % plots sin(x) versus x >> xlabel('radians'); % Assign label for x-axis >> ylabel('sin value'); % Assign label for y-axis >> title('dummy'); % Assign plot title 23
Plot Function Example 2: with multiple plots on the same figure >> figure % open new figure >> subplot(1, 2, 1); % breaks the figure window into 1-by-2 matrix of small axes % and prints on the first column >> plot(x, sin(x)); % plots sin(x) versus x >> legend('sin(x)') % adds the legend sin x to the plot >> subplot(1, 2, 2); % prints on the second column >> plot(x, cos(x)); % plots cos(x) versus x >> legend( cos(x)') % adds the legend cos x to the plot 24
Thank you 25