Documente Academic
Documente Profesional
Documente Cultură
LAB # 1 HANDOUT
1. INTRODUCTION TO MATLAB
A= 1 2 matrix b= [ 1 2 3 ] vector
3 4
MATLAB is also known as Matrix Laboratory since vectors are also type of
matrix.
+ addition conjugate
- subtraction \ left division ( a\b means ba)
* multiplication / right division (a/b means ab)
^ power
These operations apply to scalars as well. Because scalars are 1-by-1 matrices.
Ex:
>> 6+8
ans =
14
>> a=7/10
a =
0.7000
>> 3^4
ans =
81
Ex:
When you use % in matlab the things that you write after this sign are comments.
>> a=4;
>> b=2^a;
>> c=a*b/8
c = 8
>> clear a %clear a
>> clear all %clear all variables & functions from memory
>> clc %clear screen
1
Lab Assistant: Neyre Tekbyk EEE420 Lab Handout
3. CREATING A MATRIX
% A matrix
>> A =[ 1 2 3 % press enter
4 5 6
7 8 9]
>> A =[ 1 2 3 ; 4 5 6 ; 7 8 9]
% Row vector
>> b =[ 5 20 12 7 ];
% Column vector
>> c =[ 3
8
6
9 ]
>> c =[ 3;8;6;9 ]
4. TRANSPOSE OPERATOR
2
Lab Assistant: Neyre Tekbyk EEE420 Lab Handout
I.Way:
Vectors
>> [ 5 4 3 ].* [ 2/5 3/4 5/3 ]
ans=
[ 2 3 5 ]
Matrices
>>k= [ 3 3
1 2 ]
>> m= [ 1 2
3 1 ]
>>k.^m
ans= [ 3 9
1 2 ]
And similarly ./ will divide each element of the first vector (or matrix) to the
respective element of the second vector (or matrix).
II.Way:
Vectors
% inner product
>> [ 5 4 3 2 1 ].*[ 0; 1; 2; 3; 4 ]
ans= 20
% outer product
>> [ 1; 5; 6; 11 ]*[ 2 0 4 1 ]
ans=
[ 2 0 4 1
10 0 20 5
12 0 24 6
22 0 44 11 ]
Matrices
k & m we had before
>> n=k*m
n=[ 12 9
7 4 ]
6. SPECIAL MATRICES
3
Lab Assistant: Neyre Tekbyk EEE420 Lab Handout
Ex:
>>zeros(4)
ans= 0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
>>d=zeros(2,3)
d= 0 0 0
0 0 0
>>b=ones(3)
b= 1 1 1
1 1 1
1 1 1
>>2*ones(3,2)
ans= 2 2
2 2
2 2
>>I=eye(4)
I= 1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
% and we can change it by,
>> I(2,3)=5
I= 1 0 0 0
0 1 5 0
0 0 0 1
0 0 1 0
>> I(3,3)
ans= 1
>> A=rand(3,4)
% The result can be seen in command window.
>> c=[ 8 6 1 ; 7 3 5 ; 2 9 4 ]
>> m=max(c)
m= 8 9 5
>> max(max(c))
ans= 9
>> n=min(c)
n= 2 3 1
>> s=sum(c)
s= 17 18 10
>> diag(c)
4
Lab Assistant: Neyre Tekbyk EEE420 Lab Handout
>> B(1:2,3)
ans= 6
1
>> B(:,3)
ans= 6
1
5
>> B(:,[2 4])
Ex:
>> A=eye(5)
>> A([ 3 4 5 ],[ 2 3 4 ])
ans= 0 1 0
0 0 1
0 0 0
Special Constants:
In matlab
pi
j i where j= (-1)^1/2
Ex:
>> sin(pi/6)
ans= 0.5
>> cos([ pi 2*pi pi/3 ])
ans= -1 1 0.5
>>abs(-2)
ans=2
>> sqrt(256)
ans=16
>> x=[ 0.0:0.1:2.0];
y=sin(x);
[ x y ]
5
Lab Assistant: Neyre Tekbyk EEE420 Lab Handout
10.1 For
Ex:
>> x=[ 3 4 5 6];
for i=1:3,
x=i*x
end
10.2 While
Ex:
n=0;
while 2^n < 9
n=n+1
end
10.3 If
IF statement condition.
The general form of the IF statement is
IF expression
statements
ELSEIF expression
statements
ELSE
statements
END
Ex:
6
Lab Assistant: Neyre Tekbyk EEE420 Lab Handout
>> x=6;
if (x<7)
c=1
else
c=0
end
Ex:
>> x=-1.5:0.5:1.5;
>> y=exp(-x.^2);
>> plot(x,y)
Ex:
>> x=-4:0.01:4;
>> y=sin(10.*x);
>> plot(x,y)
Ex:
>> t=[-pi:0.05:pi];
>> x=sin(2*pi/8*t);
>> plot(t,x)
>> stem(t,x)
% titles & labels for the graphs
>> xlabel(time)
>> ylabel(Amplitude)
>> title(sin wave)