Sunteți pe pagina 1din 7

Lab Assistant: Neyre Tekbyk EEE420 Lab Handout

LAB # 1 HANDOUT
1. INTRODUCTION TO MATLAB

Matlab is an interactive, matrix-based system for scientific and engineering


numeric computation and visualization.
The basic element in MATLAB is matrix. A matrix is just a rectangular table of
numbers. If a matrix has only one row or one column, we call it a vector.

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.

2. VECTOR AND MATRIX OPERATIONS

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

We must remember that,


>> x =[ 3 5 7 ]
>> y =[ 4;-1;-3]
>> z = x+y is not valid.

4. TRANSPOSE OPERATOR

% conjugate and transpose


>> A = A
A= [ 1 4 7
2 5 8
3 6 9 ]
>> A =[ 1 1+j 1-j 2 ]
ans=[1
1-j
1+j
2 ]

Also, in matlab small matrices can be used to build large matrices.


>> x =[ 3 5 7 9];
>> y =[ 2 4 6 8];
>> z =[ x;y ]
z= [ 3 5 7 9
2 4 6 8 ]

Example for addition and subtraction :


>> [ 24 21 5 16 ] + [ 6 9 5 4 ]
ans= 30 30 10 20
>> [ 24 21 5 16 ] - [ 6 9 5 4 ]
ans= 18 12 0 12

2
Lab Assistant: Neyre Tekbyk EEE420 Lab Handout

5. MULTIPLICATION AND DIVISION OF VECTORS & MATRICES

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

We can build special matrices with these functions

eye -- identity matrix


zeros -- matrix of zeros
ones -- matrix of ones
rand -- randomly generated matrix (between 0 - 1)

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.

7. SOME USEFUL COMMANDS

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

8. SUBMATRICES AND COLON NOTATION


Lets assume
>> B= [ 1 2 6 8 ; 3 0 1 7; 8 -2 5 -6 ]
>> size(B)

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

9. SCALAR FUNCTIONS (Sin, cos, exp, abs, sqrt, )

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 ]

Note: You can use help to get information about functions


>>help size
>>help sin
10. FOR, WHILE AND IF RELATIONS

5
Lab Assistant: Neyre Tekbyk EEE420 Lab Handout

You can create loops by using for, while and if relations.

10.1 For

For is used to repeat statements for a specific number of times.


The general form of a FOR statement is:

FOR variable = expr, statement, ..., statement END

Ex:
>> x=[ 3 4 5 6];
for i=1:3,
x=i*x
end

10.2 While

While is used to repeat statements for an indefinite number of times.


The general form of a WHILE statement is:
WHILE expression
statements
END

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

11. PLANAR PLOTS

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)

M-files : We can write new functions in m-files.

S-ar putea să vă placă și