Matlab Basic Dr. Imtiaz Hussain Assistant Professor email: imtiaz.hussain@faculty.muet.edu.pk URL :http://imtiazhussainkalwar.weebly.com/
Contents What is MATLAB? Basic commands Variable definition Introduction to Matlab Matlab system MATLAB Product Family Toolboxes SIMULINK Basic commands Entering and quitting Matlab MATLAB Desktop Other basic commands Variable definition Defining Scalars in MATLAB Defining Vectors in MATLAB Defining Matrices in MATLAB Defining character arrays
Introduction to MATLAB
Introduction to MATLAB Integrated software environment Math and Computation Algorithm Development Modeling Simulation Visualization Analysis Scientific and engineering graphics Application Development GUI development Easy-to-use environment
The MATLAB System MATLAB System Handle graphics MATLAB language MATLAB working Environment Handle graphics MATLAB mathematical function library MATLAB application program interface (API)
MATLAB Language This is a high level matrix/array language with following features. Control flow statements Functions Data structures Input/output Object oriented programming features
MATLAB working Environment This is the set of tools and facilities that you work with as MATLAB user or programmer. It Includes the tools and facilities of Managing the variables Importing and exporting data Developing, managing and debugging M-files
Handle Graphics This is the MATLAB graphic system. It includes high level commands for: 2-D data visualization 3-D data visualization Image processing Animations Presentation graphics GUI tools
MATLAB mathematical function Library This is vast collection of computational algorithms like: Sum Sine cosine Complex arithmetic Matrices and inverses Matrix Eigen values Bessel functions FFT And many others
Application Program Interface (API) This is the library that allows you to write C and Fortran programs that interact with MATLAB. It includes facilities for: Calling routines from MATLAB Calling MATLAB as a computational engine Reading and writing MAT files.
MATLAB Product Family MATLAB MATLAB Extensions MATLAB Compiler SIMULINK Extensions Real Time Workshop Blocksets DSP Control System Communications e.t.c MATLAB SIMULINK MATLAB Extensions MATLAB Compiler MATLAB C Math Library Toolboxes Communication Financial Fuzzy Logic Image Processing Neural Networks Optimization Signal Processing
Basic Commands
Entering & Quitting MATLAB To enter MATLAB double click on the MATLAB icon. To Leave MATLAB Simply type quit and press enter. >> quit
MATLAB Desktop
Some Basic Commands >>ver >>clc >>home 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
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’
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’
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’
Working With MATLAB Variables
Types of MATLAB Variables Scalar 1x1 array Vector nx1 (column vector) or 1xn (row vector) Matrix mxn Character Arrays (Strings)
Defining Scalars >> a = 2 a = 2 Variables are assigned numerical values by typing the expression directly, for example, typing >> a = 2 yields: a = 2
Variable Definitions We can also assign numerical values to the variables by typing the expression >> b = 1+2 yields: b = 3
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;
Arithmetic Operators on Scalars MATLAB utilizes the following arithmetic operators: + Addition - Subtraction * Multiplication / Division ^ Power Operator
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
Variables in Workspace Type who to check the stored variables in workspace. >> who Your variables are: a b c d
Variables in Workspace Type whos to check the stored variables in long form. >> whos Name Size Bytes Class a 1x1 8 double array b 1x1 8 double array c 1x1 8 double array d 1x1 8 double array Grand total is 4 elements using 32 bytes
Complex numbers A complex number 3+2i in Matlab is entered in the following form >> 3+2*i Or >> 3+2*j
Complex numbers >> 3e-2 Yields: ans= 0.0300 An exponential number 3x10-2 in Matlab is entered in the following form >> 3e-2 Yields: ans= 0.0300
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) 3x10-5+5j
Defining Vectors Row Vectors 1xn Column Vectors nx1
Defining Row Vectors To create a row vector A simply type in: 1x9 vector A = 2 4 7 1 5 6 1 2 3 4 5 6 7 8 9 A(2) A(5)
Defining Row Vectors v = [2 0 1 4 7 1 5 6 4] A = A(1:4) A(6:9) 2 4 7 1 1x9 vector A = 2 4 7 1 5 6 1 2 3 4 5 6 7 8 9 A(1:4) A(6:9)
Defining Column Vectors To create a column vector B simply type in: B = [3; 5; 0; 0; 1; 4; 9; -1; 1] 1 -1 9 4 5 3 2 6 7 8 B(3) B = 9x1 vector B(5)
Defining Column Vectors B = [3; 5; 0; 0; 1; 4; 9; -1; 1] 1 -1 9 4 5 3 2 6 7 8 B(2:5) B = 9x1 vector B(7:9)
Arithmetic Operators (Arrays) + Addition - Subtraction .* Array multiplication ./ Array division .^ Array power operator ' transpose
Exercise#2 Investigate the effect of the following commands: V=[2 4 7 5] and w=[1 3 8 9] (i) v(2) (ii) sum = v + w (iii) diff = v – w (iv) vw = [v w] (v) vw(2: 6) (vi) v’ (vii) v./w (viii) v.*w (ix) whos
Exercise#3 (i) z’ (ii) z*v (iii) [v; w] (iv) v*z Investigate the effect of the following commands. z=[1; 1; 0; 0] (i) z’ (ii) z*v (iii) [v; w] (iv) v*z (v) [z; v’] (vi) z + v’
Defining Matrices A Matrix is a mxn array
Defining Matrices The most obvious ways are to type M = [1 2; 3 4] or To enter the matrix The most obvious ways are to type M = [1 2; 3 4] or M = [ [1 3]’ [3 4]’ ]
Defining Matrices N=[1 3 9 1; 2 1 7 4; 7 4 1 8; 1 9 3 0] N(1,3) or N(9) 1 3 9 7 4 8 2 5 6 10 11 12 14 15 16 13 N = N(4,3) or N(12)
Defining Matrices N=[1 3 9 1; 2 1 7 4; 7 4 1 8; 1 9 3 0] N(1:4) 1 3 9 7 4 8 2 5 6 10 11 12 14 15 16 13 N = N(10:12)
Defining Matrices 1 3 9 7 4 8 N = N(1:2,1:2) N(3:4,3:4) 2 5 6 10 11 12 2 5 6 10 11 12 14 15 16 13 N = N(3:4,3:4)
Defining Matrices N(:,1:2) 1 3 9 7 4 8 2 5 6 10 11 12 14 15 16 13 N =
Defining Matrices 1 3 9 7 4 8 2 5 6 10 11 12 14 15 16 13 N = N(3:4,:)
Exercise#4 (i) N’ (ii) M*N (iii) M/N Investigate the effect of the following commands: M=[1 2; 3 4] N=[-1 3; 5 2] (i) N’ (ii) M*N (iii) M/N (iv) M + N (v) M*z(1:2) (vi) v(3:4)*M (vii) M(1,1) (viii) M(1:2,1:2) (ix) M(:,1) (x) M(2,:)
Exercise#5 (i) K = inv(M) (ii) I = eye(2) (iii) rank(M) Investigate the effect of the following commands: M=[1 2; 3 4] (i) K = inv(M) (ii) I = eye(2) (iii) rank(M) (iv) Zeros(3) (v) Zeros(3,2) (vi) ones(4) (vii) ones(4,5) (viii) tril(M) (ix) triu(M) (x) diag(M) (xi) size(M) (xii) det(M) (xiii) eig(M) (xiv) magic(3)
Exercise#6 Define a matrix A of dimension 2 x 4 whose (i,j) entry is A(i,j)=i+j 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 Compute the matrix B to be the sum of A1 and A2 Compute the eigen values and eigen vectors of B Compute the determinant of B Compute the inverse of B Compute the rank of B
Defining Character Arrays (Strings) Character arrays are created using single quote delimiter >> str = ‘MATLAB‘ Yields str = MATLAB 1x6 vector str = M A L B T 1 2 3 4 5 6
Defining Character Arrays (Strings) >> str = ‘MATLAB‘ str = M A L B T 1 2 3 4 5 6 str(5:6) str(3)
Conversion B/W Numeric & String Arrays To convert from numeric to string array num2str To convert from string array to numeric array str2num
Numeric to string conversion >> strnum=num2str(num); >> whos Name Size Bytes Class strnum 1x3 6 char array num 1x1 8 double array Grand total is 4 elements using 14 bytes
String to Numeric conversion >> num=str2num(str); >> whos Name Size Bytes Class num 1x1 8 double array str 1x4 8 char array Grand total is 5 elements using 16 bytes
Thank you for your concentration Questions