Unleash Your Potential - Namagunga Girls Coding Club
sol43.pdf
1. The Islamic University of Gaza
Faculty of Engineering
Electrical Engineering Department
SIGNALS AND LINEAR SYSTEMS LABORATORY
EELE 3110
Experiment (1)
Introduction to MATLAB - Part (1)
Prepared by:
Eng. Mohammed S. Abuwarda
Eng. Heba M. Ghurab
Fall Semester - 2017
2. Signals And Systems Lab EELE (3110)
Page 1 of 13
Experiments #1
Introduction To Matlab I
1.1) Introduction:
MATLAB, which stands for Matrix Laboratory, is an interactive program for
numerical computation and data visualization; it is used extensively by control
engineers for analysis and design. There are many different toolboxes available
which extend the basic functions of MATLAB into different application areas; in
addition we will make extensive use of the Control Systems Toolbox in our lab.
1.2) Objectives and Expectations:
This lab is designed to give you a quick way to become familiar with the
MATLAB software by introducing you the basic features, commands, and
functions.
In this lab, you will discover that entering and solving complex numbers in
MATLAB is as easy as entering and solving real numbers, especially with the
help of MATLAB built‐in complex functions.
The lab is intended to be very interactive. You should have the required
software running while you are reading the pages, and you should perform
along with the examples.
Upon completion, you should know how to start MATLAB, how to get HELP,
how to assign variables in MATLAB and to perform the typical complex
numbers operations (i.e., complex conjugate, addition, subtraction,
multiplication, division, expression simplification) and the conversions of
complex numbers in both rectangular and polar forms with and without using
MATLAB built‐in functions.
3. Signals And Systems Lab EELE (3110)
Page 2 of 13
1.3) The MATLAB Environment:
1.4) The program interface components:
- Command Window:
To execute commands in the MATLAB environment.
- Current Directory Window:
To quickly access files on the MATLAB path.
- Workspace Window
To view variable definitions and variable memory allocations.
- Command History Window
Displays all commands were used in MATLAB since the last session.
- Figure Window:
To display graphical output from MATLAB code.
1.5) Making Folders:
- Use folders to keep your programs organized.
- To make a new folder, click the ‘Action’ button next to ‘Current Directory’.
- Click the ‘New Folder’ button, and change the name of the folder.
- Highlight the folder you just made and click ‘OK’.
- The current directory is now the folder you just created.
Current Folder
Command Window
Workspace
Command
History
4. Signals And Systems Lab EELE (3110)
Page 3 of 13
1.6) Help/Docs:
Help:
The most important function for learning MATLAB on your own
To get info on how to use a function:
»help sin
Help lists related functions at the bottom and links to the doc
To get a nicer version of help with examples and easy‐to‐read descriptions:
»doc sin
1.7) Basic Operations:
Computations:
In this document, boxes are used to indicate items directly copied from MATLAB.
When MATLAB starts the MATLAB prompt >> appears, all MATLAB commands
are executed from this prompt.
Unless you assign the result to a variable name, MATLAB assigns the result to
variable name ‘ans’.
A percent (%) sign is a comment and is ignored.
Suppress Display of Results:
A semi‐colon (;) after an expression suppresses the output.
>> % This is an example of mathematical operation
>> 2*3
ans = 6
>> % This is an example of mathematical operation
>> 2*3;
5. Signals And Systems Lab EELE (3110)
Page 4 of 13
The Workspace:
Current values are stored in the MATLAB workspace.
COMMAND DESCRIPTION
CLC Delete commands from the command window
CLEAR Delete variables from the workspace
WHO Tells you what variables are currently defined
WHOS Tells you more about the variables which currently defined
• Command Line Editing and Recall:
o MATLAB does not work like a spreadsheet. It will not go back and repeat
calculations unless you tell it to.
o A comma (,) separates statements on a line.
>> clc;
>> clear
>> x=2;
>> y=2.5;
>> z='a';
>>
>> who
Your variables are:
x y z
>>
>> whos
Name Size Bytes Class Attributes
x 1x1 8 double
y 1x1 8 double
z 1x1 2 char
6. Signals And Systems Lab EELE (3110)
Page 5 of 13
1.8) Matrices:
MATLAB works essentially with only one kind of object – a rectangular numerical
matrix with possibly complex entries. All variables represent matrices. A scalar
is a 1x1 matrix and a vector is a matrix with only one row or column.
Generating Vectors and the Colon Notation:
We can create a vector (matrix with only one row) by directly inputting the
elements,
We could also form a vector by using the colon (:) operator. “Colon notation” is
used to generate vectors and to reference sub matrices. The colon notation and
subscripting by integral vectors are keys to efficient manipulation in MATLAB.
Creative use of these features to factorize operations lets you minimize the use
of loops (which slows MATLAB down) and makes code simple and readable.
We could also use any increment to construct matrices.
Generating Matrices:
Column matrices can be created with the semicolon (;).
>> a=[1, 2, 3, 4]
a =
1 2 3 4
>> b=[1:4]
b =
1 2 3 4
>> c=[2: 2: 10]
c =
2 4 6 8 10
Z=[1 2 3; 4 5 6; 7 8 9]
Z =
1 2 3
4 5 6
7 8 9
7. Signals And Systems Lab EELE (3110)
Referencing Individual Entries:
Round parentheses are used to reference individual elements of a matrix.
Accessing Sub matrices:
The colon notation can be used to access sub matrices of a matrix.
Z(1:3,3) is the column vector consisting of the first 3 entries of the 3rd
column of
matrix Z.
Deleting Rows and Columns:
You can delete rows and columns from a matrix using just a pair of square
brackets. Start with
1.9) Matrix Operations:
The following matrix operations are available.
OPERATION DESCRIPTION
+ Addition
‐ Subtraction
* Multiplication
^ Power
‘ Transpose (real) or conjugate transpose (complex)
, Transpose (real or complex)
Left division
/ Right division
Page 6 of 13
» Z(3,2)
ans =
8
X = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
Then, to delete the second column of X, use
X(:,2) = [ ]
This changes X to
X =
16 2 13
5 11 8
9 7 12
4 14 1
8. Signals And Systems Lab EELE (3110)
Page 7 of 13
Matrix Division:
If A is an invertible square matrix and b is a compatible column vector, or
respectively a compatible row vector, then
x = Ab is the solution of A * x = b
x = b/A is the solution of x * A = b
Entry‐Wise Operations:
The matrix operations addition and subtraction are already entry‐wise but the
other operations are not; they are matrix operations. The other operations, *, ^
, , / can be made to operate entry‐wise by preceding them with a period.
1.10) Matrix Building Functions:
Some matrix building functions.
COMMAND DESCRIPTION
EYE Identity matrix
ZEROS Matrix of zeros
ONES Matrix of ones
TRIU Upper triangular part of a matrix
TRIL Lower triangular part of a matrix
RAND Matrix with random elements
» B=[1 2 ; 3 4]
B =
1 2
3 4
» B*B
ans =
7 10
15 22
» B.*B
ans =
1 4
9 16
9. Signals And Systems Lab EELE (3110)
Page 8 of 13
For example,
Matrices can be built from blocks, for instance try
1.11) Complex number:
Entering complex numbers from the keyboard has to be done carefully. The
symbol "i" identifies the imaginary part and has to be typed immediately after
the numerical value of the imaginary part: for example, 2 + 3i. If you insert a
space ‐ for instance, 2 + 3 i ‐ it looks like the same expression but it will be
processed as a number (2 + 3) and a string (i), and not as the complex number
(2 + 3i).
It is also important to point out that termination with the character i only works
with simple numbers, not expressions. For example, the expression (1 ‐ 2i)i has
no meaning to MATLAB. If you want to multiply a complex expression by i, you
have to use the multiplication operation symbol (*). In the example above, you
must write (1 ‐ 2i)*i. Similarly, the number 1 ‐ sin(2)i has no meaning for
MATLAB. It has to be written as 1 ‐ sin(2)*i to make sense to the program.
» zeros(2,3)
ans =
0 0 0
0 0 0
A=rand(3)
A =
0.9355 0.8936 0.8132
0.9169 0.0579 0.0099
0.4103 0.3529 0.1389
» triu(A)
ans =
0.9355 0.8936 0.8132
0 0.0579 0.0099
0 0 0.1389
A=rand(3)
B=[A,zeros(3,2) ; zeros(2,3) eye(2)]
10. Signals And Systems Lab EELE (3110)
Page 9 of 13
Note: you can use j instead of i.
Functions for Complex Numbers:
Some complex building function.
COMMAND DESCRIPTION
COMPLEX(X,Y) Return a complex number x+yi
REAL(X) real part of a complex number
IMAG(X) imaginary part of a complex number
ABS(X) magnitude of the complex number
ANGLE(X) angle of a complex number x
CONJ(X) complex conjugate of the complex number x
CART2POL Convert Cartesian to Polar form of complex number
POL2CART Convert Polar to Cartesian form of complex number
1.12) Control Flow Statements:
Relations:
The relational operators in MATLAB are:
COMMAND DESCRIPTION
< Less than
> Greater than
<= Less than or equal
>= Greater than or equal
== Equal
~= Not equal
& And
| Or
~ Not
Note that ‘=’ is a direct assignment while ‘= =’ is the logical equal. Relations may
be connected or quantified by the logical operators.
11. Signals And Systems Lab EELE (3110)
Page 10 of 13
Variable Controlled Loops (for):
The general form is
If the increment ‘inc’ is not specified, a default value of 1 is used. For example
Result:
Relational Controlled Loops (while):
The general form is
for variable = first: inc: last
statements
end
x=[ ];
for i=1:4
x=[x,i^2]
end
x =
1
x =
1 4
x =
1 4 9
x =
1 4 9 16
while relation
statements
end
12. Signals And Systems Lab EELE (3110)
Page 11 of 13
For instance,
Result:
A ‘while’ loop is useful when, in contrast to a ‘for’ loop, the calculations are to
be repeated an undetermined number of times.
Branching (if):
The general form is,
i=0
while i<3
i=i+1
end
i =
1
i =
2
i =
3
if relation
true alternative
else
false alternative
end
if 1>2
a=1
else
a=2
end
a =
2
13. Signals And Systems Lab EELE (3110)
Page 12 of 13
Switch:
MATLAB includes a switch structure. The general form is,
For example, try the following script M‐file
switch variable
case value 1
statement group 1
case value 2
statement group 2
…
otherwise
last statement group
end
angle = 75;
switch angle
case 0
disp('East')
case 90
disp('North')
case 180
disp('West')
case 270
disp('South')
otherwise
disp('Your lost')
end
>> switchangle
Your lost
14. Signals And Systems Lab EELE (3110)
Page 13 of 13
Break and Return:
The ‘break’ command causes the enclosed loop – either a ‘for’ or ‘while’ loop
to terminate.
The ‘return’ command causes the currently executing function M‐file to
terminate.