matlab basic. matlab product family 2 3 entering & quitting matlab to enter matlab double click...
TRANSCRIPT
Matlab Basic
Dr. Imtiaz HussainAssociate Professoremail: [email protected] :http://imtiazhussainkalwar.weebly.com/
2
MATLAB Product Family
3
Basic Commands
4
Entering & Quitting MATLAB
• To enter MATLAB double click on the MATLAB icon.
• To Leave MATLAB Simply type quit and press enter.>> 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
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’
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’
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’
9
Working With MATLAB Variables
10
Types of MATLAB Variables
• Scalar
– 1x1 array
• Vector
– nx1 (column vector) or 1xn (row vector)
• Matrix
– mxn
• Character Arrays (Strings)
11
Defining Scalars
Variables are assigned numerical values by typing the expression directly, for example, typing
>> a = 2
yields:
a =
2
12
Variable Definitions
We can also assign numerical values to the variables by typing the expression
>> b = 1+2
yields:
b = 3
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;
14
Arithmetic Operators on Scalars
• MATLAB utilizes the following arithmetic operators:
+ ADDITION
- SUBTRACTION
* MULTIPLICATION
/ DIVISION
^
POWER
OPERATOR
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
16
Variables in Workspace
• Type who to check the stored variables in workspace.
>> who
Your variables are:
a b c d
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
18
Complex numbers
• A complex number 3+2i in Matlab is entered in the following form
>> 3+2*i
Or
>> 3+2*j
19
Complex numbers
• An exponential number 3x10-2 in Matlab is entered in the following form
>> 3e-2
Yields:
ans=
0.0300
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
21
Defining Vectors
• Row Vectors
– 1xn
• Column Vectors
– nx1
naaaA ...21
nb
b
b
B
.
.
.2
1
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)
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)
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)
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 =
26
Arithmetic Operators (Arrays)
+ ADDITION
- SUBTRACTION
.* ARRAY MULTIPLICATION
./ ARRAY DIVISION
.^ ARRAY POWER OPERATOR
' TRANSPOSE
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
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’
29
Defining Matrices
A Matrix is a mxn array
mnmm
n
n
aaa
aaa
aaa
M
....
.
.
.
.
.
.
.
.
.
.
....
...
21
22221
11211
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]’ ]
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]
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]
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)
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)
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,:)
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]
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]
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
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
40
Defining Character Arrays (Strings)
>> str = ‘MATLAB‘
str = M A L A BT1 2 3 4 5 6
str(3)str(5:6)
41
Conversion B/W Numeric & String Arrays
• To convert from numeric to string array
– num2str
• To convert from string array to numeric array
– str2num
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
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
44
QUESTIONSThank you for your concentration