INTRODUCTION TO MATRIX CALCULATION USING MATLAB Learning objectives Getting started with MATLAB and it s user interface Learn some of MATLAB s commands and syntaxes Get a simple introduction to use of MATLAB to collect data in matrix or array form Learn how to manipulate data in matrix form 1. Introduction This section introduces MATLAB through a step-by-step procedure of simple matrix operations. The name MATLAB stands for MATrix LABoratory. The program is specially developed for numerical calculation and visualization or plotting of data that is mostly represented in matrix form. Matrices that are also symbolized by arrays represent the most important data elements in MATLAB. A list of elementary matrices and matrix operations is available by writing the following command in the command window. >> Help elmat Starting MATLAB: Start Matlab R2014 (server) MATLAB starts with the following standard windows. 1. Command Window this window contains command lines where we write codes and execute MATLAB expressions. 2. Workspace shows the variables that are generated since the current session started. Prepared by Hirpa Lemu./UiS - 31.07.2017 1
3. Current Directory shows which directory MATLAB is currently using as a working directory and the files that are saved in the directory. 4. Command History shows all executed commands both in the current and previous sessions. Note that new sessions start at every start up of MATLAB. Before starting the step-by-step tutorial, please notice the use of the following three symbols: >>: Command line(s) coming after this symbol are executable. Write the commands after this symbol in the command window and press the enter-key, and observe how MATLAB reacts. : Exercise questions appear right after this symbol. Users are expected to write appropriate MATLAB codes/commands to answer these questions. %: Comment line - everything that comes to the right of the percent symbol % on a line is comment and cannot be executed by MATLAB. As a user, you do not need to write these comments as part of solving the exercise in this tutorial. The comments are for your information. 2 Prepared by Hirpa Lemu./UiS - 31.07.2017
2. Constructing matrices A matrix has the dimensions mxn where m = number of rows (lines) and n = number of columns. An element in matrix A located at row i and column j is denoted by A(i, j). Rows are separated by using either a semicolon (;) or by the enter-key. Columns or elements in a row are separated by a space or a comma (,). For example: to setup the 2 x 2 matrix >>A=[1 2;3 4] é1 2ù A = ê ú in MATLAB ë3 4 û % Here the two columns are separated by spaces and the two rows % are separated by a semicolon. >> A=[1,2;3,4] % Here the columns are separated by a comma. >> A=[1 2 3 4] %Here the columns are separated by spaces and rows by the enter-key. Notice that MATLAB will always produce screen outputs for each input. One can suppress the display of outputs on the screen on any command using a semicolon (;) at the end of the command (i.e., before pressing the enter-key). This is particularly important for large matrices. For example, compare how MATLAB reacts for the following two commands: >> A1=[1 2;3 4] >> A2=[1 2;3 4]; An element of a matrix can be a real number, a complex number or an expression. For example, the matrix >> x = sqrt (10) >> B=[1 2i;3 4+x] é1 2i ù B = ê ú is valid and can be given in MATLAB as follows ë3 4 + x û A row vector is a 1-by-n matrix. That means it contains only one row. A row vector can be constructed, for example, using the following command line: or >>L1 = [2 3 4 5 6] >>L2 = [2:6] % A vector with element increment by 1. A column vector is an m-by-1 matrix. That means it contains only one column. A column vector can be constructed, for example, using the following command line: >>L3 = [2; 3; 4; 5; 6] Prepared by Hirpa Lemu./UiS - 31.07.2017 3
Concatenation of a matrix: A larger matrix can be constructed by concatenating existing matrices or vectors. For example, >> x=[1;3;6 ]; y=[2;4;6]; % Constructs two column vectors, x and y >> M =[x,y] % Matrix M is constructed from matrix x in column 1 and y in column 2 >> L =[x;y] % Constructs a 6x1 matrix (a column vector) with elements from x and y. Construct a row vector from the above two vectors x and y. Dimension of a matrix the size of a matrix, for example, that of matrices M and L constructed above can be found using the command: >>[m,n] = size(m) >>[m,n] = size(l) Addressing elements of a matrix an element of a matrix can be accessed as follows >>w = M(3,1) % This gives the element in row 3 and column 1 of matrix M and % assigns it to w. The value of a given element of a matrix is given as follows: >>M(5,1)=7 % This allocates the value of element x 51 in M to 7. 3. Simple matrix operations MATLAB uses the following simple arithmetic operations on matrices. The precedence rules of the operations follow the given sequence. Operator Matrix operations Array operations + Addition - Subtraction * Multiplication.* Element for element multiplication / Division./ Element for element division \ Left division ^ Power Transpose 4 Prepared by Hirpa Lemu./UiS - 31.07.2017
a) Addition/subtraction Addition and subtraction of matrices is defined element-by-element. These operations require that both matrices must have the same dimension, or one of them must be a scalar. Let s first construct two matrices A and B, having the same dimension. >>A=[2 3 5;0 3 4], B=[1 2 3;3 2 1] >>S1=A+B >>S2=B+A >>S3=A-B Show that matrix addition is both commutative and associative. b) Multiplication by a constant or a scalar >>s=10 % This assigns the value 10 to the variable s. >>Smul1 = s*a >>Smul2=10*B c) Matrix multiplication and element-by-element multiplication Let us construct two 3-by-3 mtrices using the built-in functions Pascal and Magic; P= pascal(3) % Defines a Pascal matrix of dimension 3-by-3. M = magic(3) Mmul = (M*P) Emul = (M.*P) % Defines a Magic matrix of dimension 3-by-3. What is the difference between the two multiplication operations M*P and M.*P? What conditions should be fulfilled to execute the two operations? d) Transpose of a matrix MATLAB uses the apostrophe operator ( ) as the transpose operator. The transpose operation flips the matrix about its main diagonal. For example, observe how MATLAB reacts for these two commands. >>T1 = P >>T2 = M Prepared by Hirpa Lemu./UiS - 31.07.2017 5
Construct two matrices A and B with arbitrary and compatible sizes and show that (A+B) T = A T +B T (A*B) T = B T *A T and Let s assume that the following two matrices are given: >>A = [2-1;0 3; -4 1]; B = [4-3;1 2; -2 5]; Determine the products: AB T, BA T, AA T and A T A. What do you observe from the results? 4. Special, square matrices MATLAB has many built-in functions and matrices that can be easily picked and used. Below are some of commonly used square matrices. a) Null matrix: >>NULL = zeros(n) % where n = size of a square matrix with all elements equal to zero (0). b) Identity matrix: >>ID = eye(n) % where n = size of a square matrix with all elements equal to one (1). c) Diagonal matrix: >>D = diag([3, 4, 6, 8]) % gives a diagonal matrix with 3, 4, 6 and 8 along the main diagonal. d) Scalar matrix: >>S = 5*ID e) Triangular matrices: We have two forms of triangular matrices 1. Lower trinagular matrix: A matrix with all null entries over the main diagonal. Let s assume a random matrix A. The operation tril(a) gives a lower triangular matrix. >>A=magic(4); L = tril(a) 2. Upper triangular matrix: A matrix with all null entries below the main diagonal. The operation triu(a) gives an upper triangular matrix. >> U = triu(a) f) Submatrices: Let s construct the following 4-by-4 matrix >>B=[10 15 20 25;30 35 40 45;50 55 60 65;70 75 80 90] We can create submatices from this matrix, for example >>B1= B(1:4,1:2) >>B1= B(3:4,3:4) % Constructs a submatrix from the two columns % Constructs a 2-by-2 submatrix from the lower right hand entries. 6 Prepared by Hirpa Lemu./UiS - 31.07.2017
Construct a 3-by-3 submatrix from matrix B. 5. Computation of determinants The determinant of a square matrix A can be found using this very simple command in MATLAB: >>A = [1 2 3;4 5 6;7 8 9]; >>d = det(a) % Notice that a determinant is a scalar value. >>B=A ; % This copies the entries of the transpose of matrix A to matrix B. >>db=det(b) % Evaluates the determinant of matrix B and saves the scalar value in db. What do you conclude from the results of the two determinants, det(a) and det(b)? 6. Lineære ligningssystemer A linear equation of the form Ax = b can be solved for the unknown variable x using the MATLAB command: >> x = A\b We have seen the following system of linear equations in the lecture: x 3x 2x 1 1 1 + 2x + 8x + 7x 2 2 2 + x + 7x + 9x 3 3 3 = 4 = 20 = 23 Formulate the equations in a matrix multiplication form (Ax = b) and find x values using MATLAB. 7. Inverting matrices As shown below, matrix inversion is a very simple operation in MATLAB. >> x = inv(a)*b Observe that x = A\b = inv(a)*b Prepared by Hirpa Lemu./UiS - 31.07.2017 7
8 Prepared by Hirpa Lemu./UiS - 31.07.2017