MATLAB Lecture 1
MATLAB Lecture 1
Introduction to MatLab
• MATrix LABoratory
• >> varname = 12
varname =
12
Tot_Num =
60
• The following are valid matlab variable
assignments
A=1
Speed=1500
B_t= v*Q
Name =‘john Smith’
>> prompt
... continue statement on next line
, separate statements and data
% start comment which ends at end of line
; (1) suppress output
(2) used as a row separator in a matrix
: specify range
Entering Matrices (1)
>> A = [1 2 3; 4 5 6; 7 8 9]
OR
>> A = [
123
456
789]
Entering Matrices (2)
» who
Your variables are:
a b c
» whos
Name Size Bytes Class
a 8x8 512 double array
b 9x9 648 double array
c 9x9 648 double array
Grand total is 226 elements using 1808 bytes
Matrix Addition
» A = [ 1 1 1 ; 2 2 2 ; 3 3 3]
» B = [3 3 3 ; 4 4 4 ; 5 5 5 ]
»A+B
ans =
4 4 4
6 6 6
8 8 8
Matrix Multiplication
» A = [ 1 1 1 ; 2 2 2 ; 3 3 3];
» B = [3 3 3 ; 4 4 4 ; 5 5 5 ];
»A*B
ans =
12 12 12
24 24 24
36 36 36
Matrix - Power
»A^2
ans =
6 6 6
12 12 12
18 18 18
»A^3
ans =
36 36 36
72 72 72
108 108 108
Matrix Transpose
A=
1 1 1
2 2 2
3 3 3
» A'
ans =
1 2 3
1 2 3
1 2 3
Identity Function
» triu(a)
ans =
1 1 1 1 1
0 1 1 1 1
0 0 1 1 1
0 0 0 1 1
0 0 0 0 1
Lower Triangle Matrix
» a = ones(5)
a=
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
» tril(a)
ans =
1 0 0 0 0
1 1 0 0 0
1 1 1 0 0
1 1 1 1 0
1 1 1 1 1
Dot Operator
•A*B
• A.*B
• 100:-7:50
– 100 93 86 79 72 65 58 51
Some built-in functions
• a = 7; b=cos(a), cosh(a)
• b=0.6570
• C=548.3170
• >> sqrt([1,4;9,16])
• Ans= 1 2
• 3 4
• abs(x)=|x|
• abs(-7)
• Ans = 7
• round(x)- nearest integer
• X=[-5.5 5.5];
• >>round(x)
• Ans = -6 6
• >>factorial(5)
• Ans = 120
• Log(4)
• Ans =1.3863
• Sin(30)
• Ans= -0.9880
Vector Functions
• Other MATLAB functions operate essentially
on vectors returning a scalar value.
- max largest component
- min smallest component
- length length of a vector
- sort sort in ascending order
- sum sum of elements
- mean mean value
End