Presentation is loading. Please wait.

Presentation is loading. Please wait.

Matlab Basic Dr. Imtiaz Hussain

Similar presentations


Presentation on theme: "Matlab Basic Dr. Imtiaz Hussain"— Presentation transcript:

1 Matlab Basic Dr. Imtiaz Hussain
Assistant Professor URL :

2 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

3 Introduction to MATLAB

4 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

5 The MATLAB System MATLAB System Handle graphics
MATLAB language MATLAB working Environment Handle graphics MATLAB mathematical function library MATLAB application program interface (API)

6 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

7 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

8 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

9 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

10 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.

11 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

12 Basic Commands

13 Entering & Quitting MATLAB
To enter MATLAB double click on the MATLAB icon. To Leave MATLAB Simply type quit and press enter. >> quit

14 MATLAB Desktop

15 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

16 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’

17 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’

18 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’

19 Working With MATLAB Variables

20 Types of MATLAB Variables
Scalar 1x1 array Vector nx1 (column vector) or 1xn (row vector) Matrix mxn Character Arrays (Strings)

21 Defining Scalars >> a = 2 a = 2
Variables are assigned numerical values by typing the expression directly, for example, typing >> a = 2 yields: a = 2

22 Variable Definitions We can also assign numerical values to the variables by typing the expression >> b = 1+2 yields: b = 3

23 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;

24 Arithmetic Operators on Scalars
MATLAB utilizes the following arithmetic operators:  +  Addition  -  Subtraction  *  Multiplication  /  Division  ^  Power Operator

25 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

26 Variables in Workspace
Type who to check the stored variables in workspace. >> who Your variables are: a b c d

27 Variables in Workspace
Type whos to check the stored variables in long form. >> whos Name Size Bytes Class a x double array b x double array c x double array d x double array Grand total is 4 elements using 32 bytes

28 Complex numbers A complex number 3+2i in Matlab is entered in the following form >> 3+2*i Or >> 3+2*j

29 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

30 Exercise#1 Investigate the effect of following commands (i) k= (ii) f= 2*c/ (iii) g=c*d^ (iv) h=c-d+k (v) who (vi) whos (vii) clear (viii) who (ix) whos (x) 3x10-5+5j

31 Defining Vectors Row Vectors 1xn Column Vectors nx1

32 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)

33 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)

34 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)

35 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)

36 Arithmetic Operators (Arrays)
 + Addition  - Subtraction  .* Array multiplication  ./ Array division  .^ Array power operator  ' transpose

37 Exercise#2 Investigate the effect of the following commands: V=[ ] and w=[ ] (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

38 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’

39 Defining Matrices A Matrix is a mxn array

40 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]’ ]

41 Defining Matrices N=[ ; ; ; ] 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)

42 Defining Matrices N=[ ; ; ; ] N(1:4) 1 3 9 7 4 8 2 5 6 10 11 12 14 15 16 13 N = N(10:12)

43 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)

44 Defining Matrices N(:,1:2) 1 3 9 7 4 8 2 5 6 10 11 12 14 15 16 13 N =

45 Defining Matrices 1 3 9 7 4 8 2 5 6 10 11 12 14 15 16 13 N = N(3:4,:)

46 Exercise#4 (i) N’ (ii) M*N (iii) M/N
Investigate the effect of the following commands: M=[1 2; ] N=[ ; ] (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,:)

47 Exercise#5 (i) K = inv(M) (ii) I = eye(2) (iii) rank(M)
Investigate the effect of the following commands: M=[1 2; ] (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)

48 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

49 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

50 Defining Character Arrays (Strings)
>> str = ‘MATLAB‘ str = M A L B T 1 2 3 4 5 6 str(5:6) str(3)

51 Conversion B/W Numeric & String Arrays
To convert from numeric to string array num2str To convert from string array to numeric array str2num

52 Numeric to string conversion
>> strnum=num2str(num); >> whos Name Size Bytes Class strnum x char array num x double array Grand total is 4 elements using 14 bytes

53 String to Numeric conversion
>> num=str2num(str); >> whos Name Size Bytes Class num x double array str x char array Grand total is 5 elements using 16 bytes

54 Thank you for your concentration
Questions


Download ppt "Matlab Basic Dr. Imtiaz Hussain"

Similar presentations


Ads by Google