matlab basic. matlab product family 2 3 entering & quitting matlab to enter matlab double click...

44
Matlab Basic Dr. Imtiaz Hussain Associate Professor email: [email protected] URL :http://imtiazhussainkalwar.weebly.com/

Upload: sharyl-dorsey

Post on 21-Jan-2016

271 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: 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

Matlab Basic

Dr. Imtiaz HussainAssociate Professoremail: [email protected] :http://imtiazhussainkalwar.weebly.com/

Page 2: 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

2

MATLAB Product Family

Page 3: 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

3

Basic Commands

Page 4: 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

4

Entering & Quitting MATLAB

• To enter MATLAB double click on the MATLAB icon.

• To Leave MATLAB Simply type quit and press enter.>> quit

Page 5: 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

5

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

Page 6: 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

6

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’

Page 7: 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

7

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’

Page 8: 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

8

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’

Page 9: 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

9

Working With MATLAB Variables

Page 10: 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

10

Types of MATLAB Variables

• Scalar

– 1x1 array

• Vector

– nx1 (column vector) or 1xn (row vector)

• Matrix

– mxn

• Character Arrays (Strings)

Page 11: 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

11

Defining Scalars

Variables are assigned numerical values by typing the expression directly, for example, typing

>> a = 2

yields:

a =

2

Page 12: 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

12

Variable Definitions

We can also assign numerical values to the variables by typing the expression

>> b = 1+2

yields:

b = 3

Page 13: 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

13

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;

Page 14: 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

14

Arithmetic Operators on Scalars

• MATLAB utilizes the following arithmetic operators:

 

+ ADDITION

 -  SUBTRACTION

 *  MULTIPLICATION

 /  DIVISION

 

^

 POWER

OPERATOR

 

Page 15: 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

15

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

Page 16: 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

16

Variables in Workspace

• Type who to check the stored variables in workspace.

>> who

Your variables are:

a b c d

Page 17: 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

17

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

Page 18: 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

18

Complex numbers

• A complex number 3+2i in Matlab is entered in the following form

>> 3+2*i

Or

>> 3+2*j

Page 19: 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

19

Complex numbers

• An exponential number 3x10-2 in Matlab is entered in the following form

>> 3e-2

Yields:

ans=

0.0300

Page 20: 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

20

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

Page 21: 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

21

Defining Vectors

• Row Vectors

– 1xn

• Column Vectors

– nx1

naaaA ...21

nb

b

b

B

.

.

.2

1

Page 22: 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

22

Defining Row Vectors

To create a row vector A simply type in:

A = [2 0 1 4 7 1 5 6 4]

1x9 vectorA =2 0 4 7 1 5 6 411 2 3 4 5 6 7 8 9

A(5)A(2)

Page 23: 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

23

Defining Row Vectors

v = [2 0 1 4 7 1 5 6 4]

1x9 vectorA =2 0 4 7 1 5 6 411 2 3 4 5 6 7 8 9

A(6:9)A(1:4)

Page 24: 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

24

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

10

0

5

3 1

2

3

4

5

6

7

8

9

B = 9x1 vector

B(5)

B(3)

Page 25: 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

25

Defining Column Vectors

B = [3; 5; 0; 0; 1; 4; 9; -1; 1]

1-1

9

4

10

0

5

3 1

2

3

4

5

6

7

8

9

9x1 vector

B(7:9)

B(2:5)

B =

Page 26: 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

26

Arithmetic Operators (Arrays)

 + ADDITION

 - SUBTRACTION

 .* ARRAY MULTIPLICATION

 ./ ARRAY DIVISION

 .^ ARRAY POWER OPERATOR

 ' TRANSPOSE

Page 27: 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

27

Exercise#2Investigate 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

Page 28: 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

28

Exercise#3

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’

Page 29: 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

29

Defining Matrices

A Matrix is a mxn array

mnmm

n

n

aaa

aaa

aaa

M

....

.

.

.

.

.

.

.

.

.

.

....

...

21

22221

11211

Page 30: 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

30

Defining Matrices

To enter the matrix

43

21M

The most obvious ways are to type

M = [1 2; 3 4]

or

M = [ [1 3]’ [3 4]’ ]

Page 31: 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

31

Defining Matrices

0391

8147

4713

1931

N

1 3

3 1

9 1

7 4

7 4

1 9

1 8

3 0

1

2

3

4

5

8

9

6

7

10

11

12

14

15

16

13

N =

N(1,3) or N(9)

N(4,3) or N(12)

N=[1 3 9 1; 2 1 7 4; 7 4 1 8; 1 9 3 0]

Page 32: 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

32

Defining Matrices

0391

8147

4713

1931

N

1 3

3 1

9 1

7 4

7 4

1 9

1 8

3 0

1

2

3

4

5

8

9

6

7

10

11

12

14

15

16

13

N =

N(1:4)

N(10:12)

N=[1 3 9 1; 2 1 7 4; 7 4 1 8; 1 9 3 0]

Page 33: 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

33

Defining Matrices

0391

8147

4713

1931

N

1 3

3 1

9 1

7 4

7 4

1 9

1 8

3 0

1

2

3

4

5

8

9

6

7

10

11

12

14

15

16

13

N =

N(1:2,1:2)

N(3:4,3:4)

Page 34: 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

34

Defining Matrices

0391

8147

4713

1931

N

1 3

3 1

9 1

7 4

7 4

1 9

1 8

3 0

1

2

3

4

5

8

9

6

7

10

11

12

14

15

16

13

N =

N(:,1:2)

Page 35: 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

35

Defining Matrices

0391

8147

4713

1931

N

1 3

3 1

9 1

7 4

7 4

1 9

1 8

3 0

1

2

3

4

5

8

9

6

7

10

11

12

14

15

16

13

N =

N(3:4,:)

Page 36: 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

36

Exercise#4Investigate the effect of the following commands:

(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,:)

M=[1 2; 3 4] N=[-1 3; 5 2]

Page 37: 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

37

Exercise#5

Investigate the effect of the following commands:

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

43

21MM=[1 2; 3 4]

Page 38: 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

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

Page 39: 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

39

Defining Character Arrays (Strings)

Character arrays are created using single quote delimiter

>> str = ‘MATLAB‘

Yields

str =

MATLAB

1x6 vectorstr = M A L A BT1 2 3 4 5 6

Page 40: 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

40

Defining Character Arrays (Strings)

>> str = ‘MATLAB‘

str = M A L A BT1 2 3 4 5 6

str(3)str(5:6)

Page 41: 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

41

Conversion B/W Numeric & String Arrays

• To convert from numeric to string array

– num2str

• To convert from string array to numeric array

– str2num

Page 42: 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

42

Numeric to string conversion

>> num=120;

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

Page 43: 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

43

String to Numeric conversion

>> str=‘1330’;

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

Page 44: 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

44

QUESTIONSThank you for your concentration