Вы находитесь на странице: 1из 12

DEPARTMENT OF ENGINEERING

ELECTRICAL & ELECTRONICS ENGINEERING SECTION


Higher College of Technology,
P O BOX 74, AL-KHUWAIR, CODE 133

( 24473600 fax 24485364

EEPW3200P
CONTROL SYSTEMS PRACTICAL

Name
ID
Specializatio
n

Group

Digital Control Systems Practical

Advanced Diploma

EECP 3275P

Chapter 1
INTRODUCTION TO MATLAB
1.1 Introduction to MATLAB
The name MATLAB stands for matrix laboratory. It is a high-performance language for technical computing
MATLAB is an interactive system whose basic data element is an array that does not require dimensioning.
This allows one to solve many technical computing problems easily. Typical uses include:

Math and computation


Algorithm development
Modeling, simulation and prototyping
Data analysis, exploration and visualization
Scientific and engineering graphics
Application development, including Graphical User Interface building

1.2

Starting a MATLAB Session

On Microsoft Windows platforms, start the MATLAB program by double-clicking the MATLAB R2009a
shortcut

on your Windows desktop.

1.3 MATLAB ENVIRONMENT


The major components of the MATLAB environment are as follows :

Command Window
Command History
Workspace
Current Directory
Figure window
Edit window

Prepared by Mr. Ashkar Mohammed

Department of Engineering EEE Section

Digital Control Systems Practical

Advanced Diploma

EECP 3275P

1.3.1 Command Window


Whenever MATLAB is invoked , the main window called command window is activated. The command
window displays the command prompt >> and a cursor where commands are entered and executed
instantaneously on pressing the Enter key of the keyboard.
For example , if one wants to evaluate the expression (20*3-5) , it is entered at the command prompt ,
as shown below :
20*3-5
Command window :
>> 20*3-5
ans =
55
>> x=20*3-5
x=
55

Prepared by Mr. Ashkar Mohammed

Department of Engineering EEE Section

Digital Control Systems Practical

Advanced Diploma

EECP 3275P

The result obtained on pressing Enter key is displayed in the command window as shown below :
ans =
55
The output display indicates that the result is now stored in the variable x . If the output of a statement is not
needed to be displayed , ; (semicolon) is added at the end of the statement.
For example ,
>> x=20*3-5;
On execution of this statement , no output is displayed in the command window and the result is stored in
the variable x
Example 1
Find the value of z for the expression z=x+y , if x=5 and y=7
Solution:
The following statements are entered in the Command window ;
>> x=5;
>> y=7;
>> z=x+y;
To display the result , type the name of variable z in the command window and press Enter key .
1.3.2 Command History Window :
Command History window consists of a list of all the commands that are entered at the command
window . It consists of commands of previous sessions also. These commands remain in the list until they
are deleted.
1.3.3 Workspace Window :
A workspace is a collection of all the variables that have been generated so far in the current MATLAB
session and shows their data type and size. All the commands executed from the command window share
common workspace.
For example , the variables x & y can be declared as shown below :
x=[ 1 2 3 4];
y= 8;
compute z as shown below:
z=x*y;
The workspace window will contain the variables x,y & z and their details as shown in figure

Prepared by Mr. Ashkar Mohammed

Department of Engineering EEE Section

Digital Control Systems Practical

Workspace window :
Name
value
x

[1 2 3 4]

[8 16 24 32]

Advanced Diploma

EECP 3275P

Min Max
1 4

8 32

1.3.4 Current Directory


In the current Directory window , all the files and folders present in the current Directory are listed as shown
in figure. To run any file, it must either be in the current directory or on the search path.
1.3.5 Edit window
An Edit window is used to create a new program file , or to modify existing files. In this window , programs
can be written , edited and saved. The programs written using the MATLAB editor are automatically
assigned an extension (.m) by the editor and are known as M-files.
A new M-file can be created by selecting an appropriate option from the desktop file menu , i.e.,
File/New/M.file or by clicking the toolbar icon
An existing M-file can be opened by selecting file/open option from the desktop menu or by clicking the
toolbar icon
as shown in figure. The edit window is opened automatically when a new M-file is created
or an existing M-file is opened.
1.3.6 Figure window
A figure window is a separate window with default white background and is used to displayed MATLAB
graphics. The results of all the grapic commands executed are displayed in the figure window.There can be
any number of figure windows depending upon the system memory.
For example , the following commands are given in the command window for plotting a sine function.
The output of plot function is displayed in the figure window and is shown in figure.
>> x= 0:0.05:20;
>> y= sin(x);
>> plot(x,y);
2.1 CONSTANTS ,VARIABLES AND EXPRESSIONS
2.1.1 Character Set :
The character set is a set of characters that form a part of the statements written using the programming
language. Characters can be broadly classified into four categories:
1. Alphabets
2. Numerals
3. Special characters
4. White Space characters
MATLAB recognizes all 26 alphabets of the English language. Further , it is case sensitive , i.e., A (uppercase)
and a (lowercase) alphabets are recognized as different characters. Numerals 0-9 also form part of its character set.
Tab, blank, newline are white space characters.

Prepared by Mr. Ashkar Mohammed

Department of Engineering EEE Section

Digital Control Systems Practical

Advanced Diploma

EECP 3275P

2.2 Constants & Variables :


Constants refer to fixed values which do not change during the execution of a program. Constants are of
two types :
i)Numeric constants
ii)Character constants
2.2.1 Numeric constants :
Numeric constants are classified into three types :
i) Integer constants
ii) Real constants
iii) Complex constants
However, MATLAB does not differentiate between integer and real constants during execution
2.2.2 Integer constants/Numbers
Integer constants/Numbers consist of a set of digits , 0 through 9 , with an optional - or + sign. Some
examples of integer constants are :
456 -120 +34 23456
Spaces ,commas and non-digit characters are not allowed between digits.
2.2.3 Real constants/Numbers
Numbers that contain fractional parts are called real/floating point constants. Some examples of real
constants are :
0.0065
-45.67
102.345
Real constant/Number can also be expressed in exponential (or scientific) notation . For example , the
value 435.25 can be written as 4.3525e+2 or 43525e-2 , in exponential notation, where e 2 means
multiplication by 10 2 .
2.2.4 Complex constants/Numbers
Complex numbers are used frequently by scientists and engineers.complex numbers consists of two
parts , i.e., a real part and an imaginary part.Imaginary parts use either i or j as a suffix
MATLAB predefines i=j= 1
Therefore , a complex constant can be easily created using Cartesian coordinates. For example, the
statement x=-6+j*8 will assign the complex constant -6+j*8 to the variable x, that is
x=-6.0000+8.0000i
where -6.0 is the real part and 8.0 is the imaginary part of the complex number.
The real and imaginary components of x can be separated by using the real and imag functions.
For example , the statements
>>x_real = real(x);
>>x_imag = imag(x);
will give the following result :
>>x_real = -6
>>x_imag = 8
The command conj(x) will produce the complex conjugate of x ,i.e.,
ans=
-6.0000-8.0000i
Prepared by Mr. Ashkar Mohammed

Department of Engineering EEE Section

Digital Control Systems Practical

Advanced Diploma

EECP 3275P

For example , if you give the following MATLAB statements


>>z=1+j*3;
>> y=conj(z)
The result is given as follows:
>> y=1.0000-3.0000i
2.3 Magnitude and Angle of complex Number
To abtain the magnitude and angle of a complex number, x=6+j*8
x_mag=abs(x);
x_rad=angle(x)
2.4 Conversion of Coordinate system :
In MATLAB , the coordinates of a point in space can be converted into another coordinate system using
simple commands
The command [theta,r]=cart2pol(x,y) converts the Cartesian coordinates x and y into Polar coordinates,
ie angle theta and radius r.
For example, let the Cartesian coordinates of a complex nuber be x=1 and y=1 ; then the command
[theta,r]=cart2pol(1,1) will produce the following result :
theta =
0.7854
r

=
1.4142

Likewise , the command


[x,y]=pol2cart(theta,r) will convert the Polar coordinates into Cartesian coordinates.
For example let theta=1 and r=5 , then convert polar to Cartesian coordinates , the following commands are
used ;
[x,y]=pol2cart(1,5)
The result is obtained as
x=
2.7015
y=
4.2074
2.5 Character constants
Character constants can be single character constants , string constants
Single character constants
A single character constant contains a single character enclosed within a pair of single quote marks
Example : Y ,G , 1
String constants
A string constant is a sequence of characters enclosed in a pair of single quotes.The characters may be
alphabets, numbers, special characters or blank space.
Example : good , 1947 best of luck
Special constants and variables
Some special constants & variables defined in MATLAB are listed as follows :
pi
3.14159
Prepared by Mr. Ashkar Mohammed

Department of Engineering EEE Section

Digital Control Systems Practical

i
j
inf
NaN
eps
ans
realma
x

Advanced Diploma

EECP 3275P

1
1

Not a number
Number small enough
equivalent to zero
default output variable
Largest real number

2.6 Operators
MATLAB operators are classified into three categories :
1) Arithmetic operators that perform arithmetic computations like addition, multiplication etc.,
2) Relational operators that compare (less than ,not equal to)
3) Logical operators that perform logical operations like AND, OR, NOT etc.,
The evaluation of expressions is mainly achieved with arithmetic operators, shown below.
Arithmetic Operators
Operation
Addition
Subtraction
Multiplicatio
n
Division
Power

Matlab symbol
+
*
/
^

Relational Operators
Operation
Less than
Less than or equal to
Greater than
Greater than or equal
to
Equal to
Not equal to

Matlab symbol
<
<=
>
>=
~=

2.7 Hierarchy of operations


This is the order in which operators are executed by the computer. Higher precedence operations are
executed before lower precedence operations. If two operators have the same precedence then the
expressions are executed from left to right.
1. Parentheses, innermost first
2. Power operation (^), left to right
3. Multiplication (*) and division (/) with equal precedence, left to right
4. Addition (+) and subtraction () with equal precedence, left to right
Example: If x = 4/2 * 6 + (3-2)
3-2 is evaluated first, then 4/2, then 4/2 *6 and finally 4/2*6 + (3-2)

Prepared by Mr. Ashkar Mohammed

Department of Engineering EEE Section

Digital Control Systems Practical

Advanced Diploma

EECP 3275P

2.8 Assignment statements


MATLAB statements are of the form
variable _name = expression ; or simply
expression
Example:
i) >> A = 9
ii) >> B = 2*3/4 + (5-2)
iii) >> 2*3/4 + (5-2)
When a command of this form is executed, the expression is evaluated, producing a number that is
assigned to the variable. The variable name and its value are displayed. If a variable name is not specified,
Matlab will assign the result to the default variable, ans as in example (iii).
Commands involving variables

who: lists the names of defined variables


whos: lists the names and sizes of defined variables
clear: clears all variables, resets default values of special variables
clear var: clears variable var
clc: clears the command window, homes the cursor (moves the prompt to the top line), but does not affect
variables.
clf: clears the current figure and thus clears the graph window.
Input and Output Statements
Matlab Programmed input
Information from the user can be requested by using the input function
Eg: x = input (enter the value of x)
will display enter the value of x in the command window.
Give a value for x and it will be placed in variable x
Matlab Programmed output
fprintf : To generate a text line
Eg: fprintf (this is a test file)
will display
=> this is a test file in the command window
\n can be used anywhere in the text string to generate a new line.
Eg: fprintf (this is a test file \n)
%g format character can be used in the text string to display the value of a variable. The value of the
variable will replace the %g when the line is printed.
We can also use a string variable to replace the character string
Eg: a = 25;
x = the value of a is %g;
fprintf (x , a)
will display
=> the value of a is 25

Prepared by Mr. Ashkar Mohammed

Department of Engineering EEE Section

Digital Control Systems Practical

Advanced Diploma

EECP 3275P

Elementary Math Functions Commands


Sl.N
o
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Comman
d
abs(x)
acos(x)
acosh(x)
angle(x)
asin(x)
asinh(x)
atan(x)
atan2(x,y)
atanh(x)
ceil(x)
conj(x)
cos(x)
cosh(x)
exp(x)
fix(x)
floor(x)
imag(x)
log(x)
log10(x)
real(x)
rem(x,y)

22
23

round(x)
sign(x)

24
25
26
27
28

sin(x)
sinh(x)
sqrt(x)
tan(x)
tanh(x)

Meaning of the command


Absolute value or magnitude of complex number
Inverse cosine
Inverse hyperbolic cosine
Angle of complex
Inverse sine
Inverse hyperbolic sine
Inverse tangent
Four quadrant inverse tangent
Inverse hyperbolic tangent
Round towards plus infinity
Complex conjugate
Cosine
Hyperbolic cosine
Exponential:e^x
Round towards zero
Round towards minus infinity
Complex imaginary part
Natural logarithm
Common logarithm
Complex real part
Remainder after division
rem(x,y) gives the remainder of x/y
Round towards nearest integer
Signum function:return sign of argument,
e.g.,sign(1.2)=1,sign(-23.4)=-1,sign(0)=0
Sine
Hyperbolic sine
Square root
Tangent
Hyperbolic tangent

Matlab help facilities


The Matlab help facility can be used to find information on any function or operation that it provides.
Type help in the command window to get the general help screen or go to help menu / Matlab help.
help elfun (Elementary math functions) displays trigonometric, exponential, complex and numeric
mathematical functions.
Lookfor command searches for the keyword specified in the look for command in detail.
Example: lookfor log
Interrupting and Terminating Matlab

Ctrl-C (pressing the Ctrl and C keys simultaneously): Interrupts processing, but does not terminate Matlab.
You may want to interrupt Matlab if you mistakenly command it to display thousands of results and you wish
to stop the time-consuming display.
quit: Terminates Matlab
exit: Terminates Matlab
Select Exit under File menu terminates Matlab (MS Windows).
Prepared by Mr. Ashkar Mohammed

10

Department of Engineering EEE Section

Digital Control Systems Practical

Advanced Diploma

EECP 3275P

Laboratory Grading System


(Total Laboratory Mark will be scaled to 30 marks)

To be filled-up and signed by the Lab Instructor as proof of students performance and completion of experiment.

Date Performed:

________________________________________

Date Completed:

________________________________________

Date Submitted:

________________________________________

Notes:
Experiment should be accomplished on a 2-hour session only. Failure to complete will reflect
deductions on above Promptness Outcome Basis.

Prepared by Mr. Ashkar Mohammed

11

Department of Engineering EEE Section

Digital Control Systems Practical

Advanced Diploma

EECP 3275P

Laboratory papers are accepted until the day before next class. Failure to submit will reflect 0 on
above Promptness Outcome Basis.
Use black or blue pen only. Deduction will be imposed for not following instructions.
In case of absence, only those with valid reasons are allowed to repeat the experiment.

Prepared by Mr. Ashkar Mohammed

12

Department of Engineering EEE Section