MATLAB_basics_PMSS
MATLAB_basics_PMSS
GIRDHAR GOPAL
Research Scholar
ECE department, MNIT JAIPUR
What Is MATLAB?
ENVIRONMENT
Command History
CALCULATIONS ON
THE COMMAND LINE
>>
>> -5/(4.8+5.32)^2
-5/(4.8+5.32)^2 >>
>> aa == 2;
2; Semicolon suppresses
ans
ans == >>
>> AA == 5;
5; screen output
-0.048821
-0.048821 >>
>> a^A
a^A
ans Variables are case
>> ans ==
>> (3+4i)*(3-4i)
(3+4i)*(3-4i) sensitive
ans 32
ans == 32
25
25 >>
>> xx == 5/2*pi;
5/2*pi;
>>
>> yy == sin(x)
sin(x) Results assigned to
>>
>> cos(pi/2)
cos(pi/2) yy == “ans” if name not given
ans
ans == 11
6.1232e-017
6.1232e-017
>>
>> zz == asin(y)
asin(y) Use parentheses ( )
>> zz ==
>> exp(acos(0.3))
exp(acos(0.3)) for function inputs
ans
ans == 1.5708
1.5708
3.547
3.547
Assigning Variables
MATLAB as a calculator
What does Matlab code look like?
t = 0:pi/100:2*pi;
y = sin(t);
plot(t,y)
What does Matlab code look like?
A = [2 7 4] 2 7 4
A = [2; 7; 4] 2
7
4
A = [2 7 4; 3 8 9] 2 7 4
3 8 9
B=[AA] ?
Matrices
A = [2 7 4] 2 7 4
A = [2; 7; 4] 2
7
4
A = [2 7 4; 3 8 9] 2 7 4
3 8 9
B=[AA] 2 7 4 2 7 4
3 8 9 3 8 9
Matrices
Matrices
A = [1 2 ; 4 5]
B=A*A prints 9 12
24 33
B = A .* A prints 1 4
16 25
c = a(b):
a(2:5)
prints
ans = 200 300 400 500
b=0
for i = [ 3 9 17]
b = b + i;
end
Result: 29
Note:
The MATLAB way to write that program would have been:
b = sum([ 3 9 17]);
for i = 1:6
…
end
for i = [1 2 3 4 5 6]
…
end
ENTERING NUMERIC ARRAYS
Row separator: >>
>> a=[1
a=[1 2;3
2;3 4]
4]
Semicolon (;) or aa == Use square
newline 11 22 brackets [ ]
Column separator: 33 44
space or comma (,)
>>
>> bb == [2:-0.5:0]
[2:-0.5:0]
bb ==
Creating sequences 22 1.5 11 0.5 00
1.5 0.5
using the colon
operator (:) >>
>> cc == rand(2,4)
rand(2,4)
cc ==
Utility function for 0.9501
0.9501 0.6068
0.6068 0.8913
0.8913 0.4565
0.4565
creating matrices. 0.2311
0.2311 0.4860
0.4860 0.7621
0.7621 0.0185
0.0185
Matrices must
be rectangular. (Undefined
elements set to zero)
ENTERING NUMERIC ARRAYS
(CONTINUED)
Using other MATLAB >>
>> ww == [-2.8,
[-2.8, sqrt(-7),
sqrt(-7), (3+5+6)*3/4]
(3+5+6)*3/4]
expressions ww ==
-2.8
-2.8 00 ++ 2.6458i
2.6458i 10.5
10.5
2 8 2
1.2 7 9 12
4 17
25 22
A(1:5,5) A(1:end,end)
3 7.2 3
5 8
7 13
1 18
11 23 A(:,5) A(:,end)
A(21:25) A(21:end)’
4 0 4
0.5 9 4 14
5 19
56 24
A(3,1)
A(3) 5 5 10 15 20 25
23 83 13 0 10
A(4:5,2:3)
A([9 14;10 15])
• Use () parentheses to specify index
• colon operator (:) specifies range / ALL
• [ ] to create matrix of index subscripts
• 'end' specifies maximum index value
THE COLON OPERATOR
>> N = 5:10:35
N =
5 15operator
Colon 25 35 occurs in several forms
>> P To= indicate
[1:3;a range (as above)
30:-10:10]
To indicate a range with non-unit increment
P =
1 2 3
30 20 10
To extract ALL the elements of an array (extracts
everything to a single column vector)
NUMERICAL ARRAY
CONCATENATION [ ]
MATRIX AND ARRAY OPERATORS
Matrix Operators Array operators
+ addition
- subtraction
>> a.*b
??? Error using
Matrix dimensions must agree.
Size and shape must match
>> a.*c
ans =
3 8
MATRIX CALCULATION-
SCALAR EXPANSION
>>
>> w=[1
w=[1 2;3
2;3 4]
4] ++ 55
ww ==
66 77
88 99
>>
>> w=[1
w=[1 2;3
2;3 4]
4] ++ 55
11 22
== ++ 55
33 44
Scalar expansion
11 22 55 55
== ++
33 44 55 55
66 77
==
88 99
Inner dimensions must be equal.
Dimension of resulting matrix = outermost
dimensions of multiplied matrices.
Resulting elements = dot product of the rows
of the 1st matrix with the columns of the 2nd
matrix.
MATRIX
MULTIPLICATION
>>
>> aa == [1
[1 22 3;4
3;4 55 6];
6]; [2x3]
>>
>> bb == [3,1;2,4;-1,2];
[3,1;2,4;-1,2]; [3x2]
>>
>> cc == a*b
a*b [2x3]*[3x2] [2x2]
cc ==
44 15
15
16
16 36
36 a(2nd row).b(2nd column)
Matrices must have the same dimensions (size and shape)
Dimensions of resulting matrix = dimensions of multiplied
matrices
Resulting elements = product of corresponding elements
from the original matrices
>>
>> aa == [1
[1 22 33 4;
4; 55 66 77 8];
8];
>>
>> bb == [1:4;
[1:4; 1:4];
1:4];
>>
>> cc == a.*b
a.*b
cc ==
11 44 99 16
16
55 12
12 21
21 32
32
ARRAY (ELEMENT-BY-
ELEMENT)
MULTIPLICATION
>> A = [1:3;4:6;7:9]
A =
1 2 3
Many common functions
4 5 6
operate on columns by
7 8 9 default
Mean of each column in A
>> mean(A)
ans =
4 5 6
>> sum(A)
ans =
12 15 18 Mean of all elements in A
>> mean(A(:))
ans =
5
>> clear clear all workspace
>> clear VARNAME clear named variable
>> clear all clear everything
(see help clear)
>> close all close all figures
>> clc clears command
window display only
CLEARING UP
Any inbuilt MATLAB function
documentation can be found out by
typing:
>> help function_name
Or
>> lookfor function_name