Matlab Basic Dr Imtiaz Hussain Associate Professor email
Matlab Basic Dr. Imtiaz Hussain Associate Professor email: imtiaz. hussain@faculty. muet. edu. pk URL : http: //imtiazhussainkalwar. weebly. com/
MATLAB Product Family MATLAB SIMULINK Extensions • Real Time Workshop Blocksets • DSP • Control System • Communications • e. t. c MATLAB Extensions • MATLAB Compiler • MATLAB C Math Library Toolboxes • Control System • Communication • Financial • Fuzzy Logic • Image Processing • Neural Networks • Optimization • Signal Processing • e. t. c 2
Basic Commands 3
Entering & Quitting MATLAB • To enter MATLAB double click on the MATLAB icon. • To Leave MATLAB Simply type quit and press enter. >> quit 4
Some Basic Commands • To check the list of installed toolboxes type >>ver • To clear the screen type >>clc • To move the cursor to upper left corner of the command window type >>home 5
Some Basic Commands (contd…) • To list the current variables type >>who • To list the current variables in long form type >>whos • To clear the workspace type >>clear • To remove particular variable from the workspace type >> clear ‘name of the variable’ 6
Some Basic Commands (contd…) • To get list of Help topics type >>help • To get help for any topic type >>help ‘topic’ • To get help for any command type >>help ‘command/syntax’ 7
Some Basic Commands (contd…) • To search command type >>lookfor ‘keyword’ • To list the files in a directory type >>dir ‘directory name’ • To list the Matlab files only type >>what ‘directory name’ 8
Working With MATLAB Variables 9
Types of MATLAB Variables • Scalar – 1 x 1 array • Vector – nx 1 (column vector) or 1 xn (row vector) • Matrix – mxn • Character Arrays (Strings) 10
Defining Scalars Variables are assigned numerical values by typing the expression directly, for example, typing >> a = 2 yields: a= 2 11
Variable Definitions We can also assign numerical values to the variables by typing the expression >> b = 1+2 yields: b= 3 12
Variable Definitions • After typing the expressions the answers are echoed back. • To suppress the echo put semicolon at the end of the expression. >> c = 5; 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 >> d = 2*a yields: d= 4 15
Variables in Workspace • Type who to check the stored variables in workspace. >> who Your variables are: a b c d 16
Variables in Workspace • Type whos to check the stored variables in long form. >> whos Name Size Bytes Class a b c d 1 x 1 8 8 double array Grand total is 4 elements using 32 bytes 17
Complex numbers • A complex number 3+2 i in Matlab is entered in the following form >> 3+2*i Or >> 3+2*j 18
Complex numbers • An exponential number 3 x 10 -2 in Matlab is entered in the following form >> 3 e-2 Yields: ans= 0. 0300 19
Exercise#1 Investigate the effect of following commands (i) k=3 (ii) f= 2*c/3 (iii) g=c*d^2 (iv) h=c-d+k (v) who (vi) whos (vii) clear (viii) who (ix) whos (x) 3 x 10 -5 +5 j 20
Defining Vectors • Row Vectors – 1 xn • Column Vectors – nx 1 21
Defining Row Vectors To create a row vector A simply type in: A = [2 0 1 4 7 1 5 6 4 1 2 3 4 5 6 7 8 9 A(2) 4] A(5) 22
Defining Row Vectors v = [2 A= 0 1 4 7 1 5 6 2 0 1 4 7 1 5 6 4 1 2 3 4 5 6 7 8 9 A(1: 4) 4] 1 x 9 vector A(6: 9) 23
Defining Column Vectors To create a column vector B simply type in: B = [3; 5; 0; 0; B = 31 52 03 04 15 46 1; 4; 9; -1; 1] B(3) 9 x 1 vector 97 -1 8 19 B(5) 24
Defining Column Vectors B = [3; 5; 0; 0; B = 1; 31 52 03 04 15 46 4; 9; -1; 1] B(2: 5) 9 x 1 vector 97 -1 19 8 B(7: 9) 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 29
Defining Matrices To enter the matrix The most obvious ways are to type M = [1 2; 3 4] or M = [ [1 3]’ [3 4]’ ] 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 N = 3 7 1 1 2 3 5 9 9 1 13 1 6 7 10 4 14 3 4 7 1 11 8 15 4 9 8 3 12 0 16 N(4, 3) or N(12) 31
Defining Matrices N(1: 4) N=[1 3 9 1; 2 1 7 4; 7 4 1 8; 1 9 3 0] 1 N = 3 7 1 1 2 3 5 9 9 1 13 1 6 7 10 4 14 3 4 7 1 11 8 15 4 9 8 3 12 0 16 N(10: 12) 32
Defining Matrices N(1: 2, 1: 2) 1 N = 3 7 1 1 2 3 5 9 9 1 13 1 6 7 10 4 14 3 4 7 1 11 8 15 4 9 8 3 12 0 16 N(3: 4, 3: 4) 33
Defining Matrices N(: , 1: 2) 1 N = 3 7 1 1 2 3 5 9 9 1 13 1 6 7 10 4 14 3 4 7 1 11 8 15 4 9 8 3 12 0 16 34
Defining Matrices 1 N = 3 7 1 N(3: 4, : ) 1 2 3 5 9 9 1 13 1 6 7 10 4 14 3 4 7 1 11 8 15 4 9 8 3 12 0 16 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] 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 A 1 and A 2 out of the matrix A. A 1 contains the first two columns of A, A 2 contains the last two columns of A 3) Compute the matrix B to be the sum of A 1 and A 2 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 usingle quote delimiter >> str = ‘MATLAB‘ Yields str = MATLAB str = M A T L 1 2 3 4 A 5 B 1 x 6 vector 6 39
Defining Character Arrays (Strings) >> str = ‘MATLAB‘ str = M A T L 1 2 3 4 A 5 B 6 str(5: 6) str(3) 40
Conversion B/W Numeric & String Arrays • To convert from numeric to string array – num 2 str • To convert from string array to numeric array – str 2 num 41
Numeric to string conversion >> num=120; >> strnum=num 2 str(num); >> whos Name Size Bytes Class strnum 1 x 3 1 x 1 6 char array 8 double array Grand total is 4 elements using 14 bytes 42
String to Numeric conversion >> str=‘ 1330’; >> num=str 2 num(str); >> whos Name Size num str 1 x 1 1 x 4 Bytes Class 8 double array 8 char array Grand total is 5 elements using 16 bytes 43
Thank you for your concentration QUESTIONS 44
- Slides: 44