Matlab Basic MATLAB Product Family 2 3 Entering & Quitting MATLAB • To enter MATLAB double click on the MATLAB icon. • To Leave MATLAB Simply type quit and press enter. 4 Some Basic Commands • To check the list of installed toolboxes type • To clear the screen type • To move the cursor to upper left corner of the command window type 5 Some Basic Commands (contd…) • To list the current variables type • To list the current variables in long form type • To clear the workspace type • To remove particular variable from the workspace type 6 Some Basic Commands (contd…) • To get list of Help topics type • To get help for any topic type • To get help for any command type 7 Some Basic Commands (contd…) • To search command type • To list the files in a directory type • To list the Matlab files only type 8 9 Types of MATLAB Variables • Scalar array • Vector (column vector) or • Matrix • Character Arrays (Strings) (row vector) 10 Defining Scalars Variables are assigned numerical values by typing the expression directly, for example, typing yields: 11 Variable Definitions We can also assign numerical values to the variables by typing the expression yields: 12 Variable Definitions • After typing the expressions the answers are echoed back. • To suppress the echo put semicolon at the end of the expression. 13 Arithmetic Operators on Scalars • MATLAB utilizes the following arithmetic operators: 14 Variable Definition (Contd…….) A variable can be assigned using a formula. For example, since a was defined previously, the following expression is valid yields: 15 Variables in Workspace • Type who to check the stored variables in workspace. 16 Variables in Workspace • Type whos to check the stored variables in long form. 17 Complex numbers • A complex number 3+2i in Matlab is entered in the following form 18 Complex numbers • An exponential number 3x10-2 in Matlab is entered in the following form 19 Exercise#1 Investigate the effect of following commands 20 Defining Vectors • Row Vectors A a1 a2 ... an • Column Vectors b1 b 2 . B . . bn 21 Defining Row Vectors To create a row vector A simply type in: A = [2 1 A(2) 2 0 1 3 4 4 5 7 6 1 5 7 8 6 4] 9 A(5) 22 Defining Row Vectors v = [2 1 0 1 2 A(1:4) 3 4 7 4 5 1 6 7 5 6 8 9 4] A(6:9) 23 Defining Column Vectors To create a column vector B simply type in: B = [3; 5; 0; 0; 1; 4; 9; -1; 1] 31 52 B = B(3) 03 04 15 46 9x1 vector 97 -1 8 19 B(5) 24 Defining Column Vectors B = [3; 5; 0; 0; 1; 4; 9; -1; 1] 31 52 B = 03 04 15 46 B(2:5) 9x1 vector 97 -1 19 B(7:9) 8 25 Arithmetic Operators (Arrays) 26 Exercise#2 Investigate the effect of the following commands: V=[2 4 7 5] and w=[1 3 8 9] 27 Exercise#3 Investigate the effect of the following commands. z=[1; 1; 0; 0] 28 Defining Matrices A Matrix is a mxn array a11 a 21 . M . . am1 a12 a22 . . . am 2 ... a1n ... a2 n . . . . . . ... amn 29 Defining Matrices To enter the matrix 1 2 M 3 4 The most obvious ways are to type or 30 Defining Matrices N(1,3) or N(9) N=[1 3 9 1; 2 1 7 4; 7 4 1 8; 1 9 3 0] 1 3 N 7 1 3 9 1 1 7 4 4 1 8 9 3 0 1 1 3 5 9 9 1 13 N = 32 1 7 6 4 10 14 7 3 4 7 1 11 8 15 1 4 9 8 3 12 0 16 N(4,3) or N(12) 31 Defining Matrices N=[1 3 9 1; 2 1 7 4; 7 4 1 8; 1 9 3 0] 1 3 N 7 1 3 9 1 1 7 4 4 1 8 9 3 0 N(1:4) 1 1 3 5 9 9 1 13 N = 32 1 7 6 4 10 14 7 3 4 7 1 11 8 15 1 4 9 8 3 12 0 16 N(10:12) 32 Defining Matrices N(1:2,1:2) 1 3 N 7 1 3 9 1 1 7 4 4 1 8 9 3 0 1 1 3 5 9 9 1 13 N = 32 1 7 6 4 10 14 7 3 4 7 1 11 8 15 1 4 9 8 3 12 0 16 N(3:4,3:4) 33 Defining Matrices N(:,1:2) 1 3 N 7 1 3 9 1 1 7 4 4 1 8 9 3 0 1 1 3 5 9 9 1 13 N = 32 1 7 6 4 10 14 7 3 4 7 1 11 8 15 1 4 9 8 3 12 0 16 34 Defining Matrices 1 3 N 7 1 3 9 1 1 7 4 4 1 8 9 3 0 1 1 3 5 9 9 1 13 N = 32 1 7 6 4 10 14 7 3 4 7 1 11 8 15 1 4 9 8 3 12 0 16 N(3:4,:) 35 Exercise#4 Investigate the effect of the following commands: M=[1 2; 3 4] N=[-1 3; 5 2] 36 Exercise#5 Investigate the effect of the following commands: M=[1 2; 3 4] 1 2 M 3 4 37 Exercise#6 1) Define a matrix A of dimension 2 x 4 whose (i,j) entry is A(i,j)=i+j 2) Extract two 2 x 2 matrices A1 and A2 out of the matrix A. A1 contains the first two columns of A, A2 contains the last two columns of A 3) Compute the matrix B to be the sum of A1 and A2 4) Compute the eigen values and eigen vectors of B 5) Compute the determinant of B 6) Compute the inverse of B 7) Compute the rank of B Defining Character Arrays (Strings) Character arrays are created using single quote delimiter 1 2 3 4 5 6 39 Defining Character Arrays (Strings) 1 2 3 4 5 6 40 Conversion B/W Numeric & String Arrays • To convert from numeric to string array – num2str • To convert from string array to numeric array – str2num 41 Numeric to string conversion 42 String to Numeric conversion 43 Thank you for your concentration QUESTIONS 44