Sie sind auf Seite 1von 14

Institute of Information and Communication Technology

University of Sindh, Jamshoro


BS (Telecommunication) Part-III, Second Semester

PRACTICAL HANDOUTS
TELE – 531 CONTROL SYSTEMS LAB

LAB # 01: Introduction to MATLAB environment, Implementation of different functions, plotting

and polynomials.

Name of Student: __________________________________________________________________

Roll No: _________________________________Group: ___________________________________

Date of Experiment : ____________________________________________

Report Submitted on : ____________________________________________

Marks Obtained : ____________________________________________

Remarks if any : ____________________________________________

Signature : ____________________________________________

CONTROL SYSTEMS LABORATORY WORKBOOK GM SHORO


OBJECTIVE: Introduction to MATLAB environment, implementation of different functions,
plotting and polynomials.

MATLAB:
MATLAB is an interactive program for numerical computation and data visualization; it is
extensively used by control system engineers for analysis and design. There are many different
toolboxes available which extend the basic functions of MATLAB into different application areas.
MATLAB (matrix laboratory) is a fourth-generation programming language. Developed by Math
Works, MATLAB allows Matrix manipulations, plotting of functions and data, implementation of
algorithms, creation of user interfaces, and interfacing with programs written in other languages.

Using M-files in MATLAB


There is a built-in editor for m-files; choose "New M-file" from the File menu. You can also use
any other editor you like (but be sure to save the files in text format and load them when you
start MATLAB). However, you need to know that your m-file will be saved in the clipboard.
Therefore, you must make sure that it is saved as file name’s.
You can either type commands directly into MATLAB, or put all of the commands that you will
need together in an m-file, and just run the file. If you put all of your m-files in the same directory
that you run MATLAB from, then MATLAB will always find them.

Getting help in MATLAB


MATLAB has a fairly good on-line help; type help command name

VECTORS:
Let's start off by creating something simple, like a vector. Various commands are used in MATLAB
for vector analysis including:

 Entering a Vector: Enter each element of the vector (separated by a space) between
brackets, and set it equal to a variable. For example, to create the vector “A”, enter into
the MATLAB command window:

>> A = [1 2 3 4 5 6 9 8 7]

MATLAB should return


A=

1 2 3 4 5 6 9 8 7

Let's say you want to create a vector with elements between 0 and 20 evenly spaced in
increments of 2 (this is frequently used to create a time vector, start: increment: end):

CONTROL SYSTEMS LABORATORY WORKBOOK GM SHORO


>> T = 0:2:20

T=

0 2 4 6 8 10 12 14 16 18 20

 Operations performed on vectors: Manipulating vectors is almost as easy as creating


them. Various operations can be performed on the Command window including Addition,
multiplication, division etc.
 First, suppose you would like to add 2 to each of the elements in vector 'A'. The
equation for that looks like:

>> B = A+2

B=

3 4 5 6 7 8 11 10 9

 Now suppose, you would like to add two vectors together. If the two vectors are
the same length, it is easy. Simply add the two as shown below:
>> C = A+B
C=
4 6 8 10 12 14 20 18 16
 Subtraction of vectors of the same length works exactly the same way.
C = A-B
 Multiplication of vectors of the same length works exactly the same way.
D = A*B
MATRICES:
 Entering matrices into MATLAB is the same as entering a vector, except each row of
elements is separated by a semicolon (;):
>> B = [1 2 3 4;5 6 7 8;9 10 11 12]
B=
1 2 3 4
5 6 7 8
9 10 11 12

CONTROL SYSTEMS LABORATORY WORKBOOK GM SHORO


 Matrices in MATLAB can be manipulated in many ways. For one, you can find the
transpose of a matrix using the apostrophe key:
>> C = B'
C=
1 5 9
2 6 10
3 7 11
4 8 12
 Now you can multiply the two matrices B and C together. Remember that order matters
when multiplying matrices.
>> D = B * C
D=
30 70 110
70 174 278
110 278 446
>> D = C * B
D=
107 122 137 152
122 140 158 176
137 158 179 200
152 176 200 224
 Another option for matrix manipulation is that you can multiply the corresponding
elements of two matrices using the. * (point operator) (the matrices must be the same
size to do this).
 C=A .*A……….. it will multiply corresponding elements of A with A
>> A = [1 2 3 4 5 6 9 8 7]
A=
1 2 3 4 5 6 9 8 7
>> C = A.*A

CONTROL SYSTEMS LABORATORY WORKBOOK GM SHORO


C=
1 4 9 16 25 36 81 64 49
 D=A ./A……….. it will divide corresponding elements of A with A
>> D = A./A
D=
1 1 1 1 1 1 1 1 1
 Example
E = [1 2;3 4]
F = [2 3;4 5]
G = E.*F
>> E = [1 2;3 4]
E=
1 2
3 4
>> F = [2 3;4 5]
F=
2 3
4 5
>> G = E.*F
G=
2 6
12 20
 If wanted to cube each element in the matrix, just use the element-by-element
cubing.
>> E.^3
ans =
1 8

CONTROL SYSTEMS LABORATORY WORKBOOK GM SHORO


27 64
 Inverse of a Matrix: You can also find the inverse of a matrix:
>> X = inv(E)
X=
-2.0000 1.0000
1.5000 -0.5000
 or its eigen values:
>> eig(E)
ans =
-0.3723
5.3723
 There is even a function to find the coefficients of the characteristic polynomial of a
matrix. The "poly" function creates a vector that includes the coefficients of the
characteristic polynomial.
>> P = poly(E)
P=
1.0000 -5.0000 -2.0000
 Remember that the eigenvalues of a matrix are the same as the roots of its
characteristic polynomial:
>> roots(P)
ans =
5.3723
-0.3723

 Use of Semicolon and Parenthesis: Semicolon is used to terminate the corresponding line
operation and shift the cursor to the next line without executing it while ( ) are used to
execute ay function you specified.

 Clear and Clc command: Clear command clear all the variables which are used from the
memory location preventing any unpredictable result. Clc command just clear the screen
or the command window.

CONTROL SYSTEMS LABORATORY WORKBOOK GM SHORO


 Trigonometric functions: You have variety of trigonometric functions on MATLAB
including sine, cosine, tangent, inverse and hyperbolic of these function, logarithmic etc.

Let x = 20;
>> cos(x) --- Cosine of 20 >> acos(x) ------- Inverse of cosine
ans = ans =
0.4081 0.0000 + 3.6883i
>> sin(x) --- Sine of 20 >> asin(x) ------ Inverse of sine
ans = ans =
0.9129 1.5708 - 3.6883i
>> csc(x) --- Cosecant of 20 >> sinh(x) ----- Hyperbolic of sine
ans = ans =
1.0954 2.4258e+08

 Zeros and Ones:


 zeros(N) is an N-by-N matrix of zeros.
 zeros (M, N) or zeros ([M, N]) is an M-by-N matrix of zeros.
 zeros(SIZE(A)) is the same size as A and all zeros.
 zeros with no arguments is the scalar 0.
 ones(N) is an N-by-N matrix of ones.
 ones (M, N) or ones ([M, N]) is an M-by-N matrix of ones.
 ones(SIZE(A)) is the same size as A and all ones.
 ones with no arguments is the scalar 1.

 WHO command: who command is used to display all the variables which are declared in
the command window before using CLEAR command. who lists the variables in the
current workspace
>> who
Your variables are:
A B C D E F G P T X ans t x
 Linspace: Linearly spaced vector. It generates a row vector of 100 linearly equally
spaced points between X1 and X2.
logspace(X1, X2)

Logspace: Logarithmically spaced vector. It generates a row vector of 50 logarithmically


equally spaced points between decades 10^X1 and 10^X2. If X2 is pi, then the points are
between 10^X1 and pi.
logspace(X1, X2)

 Identity Matrix: eye Identity matrix. eye(N) is the N-by-N identity matrix.

CONTROL SYSTEMS LABORATORY WORKBOOK GM SHORO


>> eye(3)
ans =
1 0 0
0 1 0
0 0 1
PLOTTING
It is also easy to create plots in MATLAB. Suppose you want to plot a sine wave as a function of
time. First make a time vector (the semicolon after each statement tells MATLAB we don't want
to see all the values) and then compute the sin value at each time.

>> t=0:0.25:7;
>> y=sin(t);
>> plot(t,y)

The plot contains approximately one period of a sine wave. Basic plotting is very easy in
MATLAB, and the plot command has extensive add-on capabilities.
Continuous Time Plots:

 Plot command: PLOT command is used to plot continuous time signal defined in time
domain as shown in figure.
 plot(X,Y): plots vector Y versus vector X. If X or Y is a matrix, then the vector is
plotted versus the rows or columns of the matrix, whichever line up. If X is a
scalar and Y is a vector, disconnected line objects are created and plotted as
discrete points vertically at X.
 plot(Y) plots the columns of Y versus their index. If Y is complex, plot(Y) is
equivalent to plot(real(Y),imag(Y)). In all other uses of plot, the imaginary part is
ignored.
 Various line types, plot symbols and colors may be obtained with plot(X,Y,S)
where S is a character string made from one element from any or all the
following 3 columns:

CONTROL SYSTEMS LABORATORY WORKBOOK GM SHORO


b blue . point - solid
g green o circle : dotted
r red x x-mark -. dashdot
c cyan + plus -- dashed
m magenta * star (none) no line
y yellow s square
k black d diamond
w white v triangle (down)
^ triangle (up)
< triangle (left)
> triangle (right)
p pentagram
h hexagram

 Subplot: It is used to display 2 or more graphical function on the same graph with two
different plots as shown in the figure.
subplot(2,1,1)
2 shows number of graphs, 1 shows one is plotted and last one shows out of 2 functions
1st function is plotted first. (Note: Use plot command first)
subplot(2,1,2)

 Multiple functions on same plot: to plot 2 or more function simultaneously on one graph use the
following code
Plot (axis1,function1,axis2,function2)

Plot(t,y1,t,y2)

Plot(t,y1,’o’,t,y2,’*’)

CONTROL SYSTEMS LABORATORY WORKBOOK GM SHORO


 Plotting with symbols: To plot certain distinct values on the continuous graph use the following
code.
Plot(t,’*’).. just points in form of * are plotted

Plot(t,y,’*’)… Graph along function and distinct points are plotted

 Bar charts:
Bar(x)

Discrete Plots: Use

 Stem Command: STEM command to plot discrete time signals. Define n vector rather t.
Modifying Your Graph:
 Axis Command: Define x and y axis and give range for x and y axis
Axis ([x1 x2 y1 y2])
 X-Label Command: It gives x-axis appropriate label
Xlabel(‘time in seconds’)
 Y-Label Command: It gives x-axis appropriate label
Ylabel(‘particular function’)
 Title: It gives suitable title to your graph.
Title(‘Electronics’)
 Text Command: It adds text on the desired position (row and column) on the graph.
Text (a, b,’Electronics Engineering’)
Text (1.3,2.1,’Electronics Engineering’)
Where a and b are row and column positions

POLYNOMIALS
In MATLAB, a polynomial is represented by a vector. To create a polynomial in MATLAB, simply
enter each coefficient of the polynomial into the vector in descending order. For instance, let's
say you have the following polynomial:

CONTROL SYSTEMS LABORATORY WORKBOOK GM SHORO


𝑺𝟒 + 𝟑𝑺𝟑 − 𝟏𝟓𝑺𝟐 − 𝟐𝑺 + 𝟗
To enter this into MATLAB, just enter it as a vector in the following manner
>> x = [1 3 -15 -2 9]
x=
1 3 -15 -2 9
MATLAB can interpret a vector of length n+1 as an nth order polynomial. Thus, if your
polynomial is missing any coefficients, you must enter zeros in the appropriate place in the
vector. For example,

𝒔𝟒 + 𝟏
Would be represented in MATLAB as:
>> y = [1 0 0 0 1]
y=
1 0 0 0 1
You can find the value of a polynomial using the polyval function. For example, to find the value
of the above polynomial at s=2,
>> z = polyval([1 0 0 0 1],2)
z=
17
You can also extract the roots of a polynomial. This is useful when you have a high-order
polynomial such as

𝑺𝟒 + 𝟑𝑺𝟑 − 𝟏𝟓𝑺𝟐 − 𝟐𝑺 + 𝟗
Finding the roots would be as easy as entering the following command;
>> roots([1 3 -15 -2 9])
ans =
-5.5745
2.5836
-0.7951
0.7860

CONTROL SYSTEMS LABORATORY WORKBOOK GM SHORO


Let's say you want to multiply two polynomials together. The product of two polynomials is
found by taking the convolution of their coefficients. MATLAB's function conv that will do this
for you.
>>x = [1 2];
>> y = [1 4 8];
>> z = conv(x,y)
z=
1 6 16 16
Dividing two polynomials is just as easy. The deconv function will return the remainder as well
as the result. Let's divide z by y and see if we get x.
>> [xx, R] = deconv(z,y)
xx =
1 2
R=
0 0 0 0
As you can see, this is just the polynomial/vector x from before. If y had not gone into z evenly,
the remainder vector would have been something other than zero.

Programs to Explore (observe the result):


1. Use the following codes to observe the use of linespace and logspace functions. Give comments.
linspace(1,0.01,10)

Linspace(1,10)

2. Write the following code in M file or command window to plot sine of 3. Xlabel,ylabel and text
command is also used.
t=0:0.005:10;
q=sin(3);
plot(t,q);
xlabel('sine function');
ylabel('graph');
text(1.2,0.6,'sine function');
3. Write the following code to observe the use of subplot.
t=0:0.01:7;
p=cos(t);

CONTROL SYSTEMS LABORATORY WORKBOOK GM SHORO


q=sin(p);
plot(t,p);
subplot(2,1,1);
plot(t,q);
subplot(2,1,2);
xlabel('time in seconds');
text(0.5,0.5,'graphs');
title('using subplot');

4. Write the following code to observe multiple plot.


t=0:0.01:100;
z=t*2*0;
v=t*5;
p=cos(z);
q=sin(v);
plot(p,t,q,t);
5. Write the following code to understand the use of Axis command.
x=3;
z=sin(x);
v=cos(x);
p=cos(z);
q=sin(v);
plot(t,x,t,v,t,p,t,q);
axis on
axis ([-5,5,-10,10]);
title ('my roll number is 2k---');

Explore:
Help, Matlab Environment, Square root function (sqrt), hold on and hold off command, exp function and
M-file. (attach their answers in the handouts)

Lab Activity: (To be Submitted)


You are supposed to perform them on MATLAB and copy them to word along attached printouts.

1. Write any program of your choice and use at least 3 commands mentioned above.
2. Find Square root of 3, 4, and 9.
3. Write a matlab code to plot following functions (attach graphs) Using t=1:0.01:10
(i) cos(𝜋𝑡) (ii) sin(𝑡) (iii) cos(𝑡) (iv) cosec(2𝜋𝑡)
4. List few features of MATLAB. Why it is called Integrated development environment?
5. When the graph is plotted, go to its menu bar and explore the following.
(file, edit, view, insert, tools)

CONTROL SYSTEMS LABORATORY WORKBOOK GM SHORO


Following Tasks Should be Performed and Submitted
Question # 1  Define a vector ‘a’ with values from 0 to 100 that are multiples of 10
(table of 10), vector should not be displayed,
 Define another vector ‘b’ with values 0 to 50 that are multiples of 5
(table of 5),
 Add both these vectors so that result will be ‘c’ that will be displayed
on the screen.
Question # 2  Define x as 0 to 20 with difference step of 0.005,
 Define y as exp (Matlab function exp(x)).
 a) Plot the function y (on vertical axis) against x (on horizontal axis).
 b) Similarly draw a plot for negative exponential functions.
Question # 3 Explore the following on Matlab plot commands:
 Title (‘______’)
 Xlabel (‘_____’)
 Ylabel (‘______’)
 Text (x, y, ’text’)
 Subplot
 Grid
Use above command on your plotted curve in Question#02
Question # 4 Use Matlab to find the roots of;
𝑥 3 + 3𝑥 2 + 2𝑥
Also verify that the roots are correct by putting the roots in the given
polynomial (using polyval).
Question # 5 Define a matrix ‘A’ as [1 2 3; 2 3 4; -1 -4 -2],
Find 𝐴 ∗ 𝐴; 𝐴.∗ 𝐴; 𝐴3 ; 𝐴−1 Transpose of A (use A’ command for transpose).
(all results to be displayed)
Note:  The results should be displayed on the screen whereas the intermediate
variable declarations and value assignments should not be bounced on the
screen.
 You have to submit the hardcopy and softcopy of the lab report. It should
have purpose (what you are doing?), all the programming commands and
the results, Conclusion (your understanding).
 Print your name and roll number at every individual page.
 You should expect viva questioning from your solution. So, try your solution
by yourself.
Good Luck

CONTROL SYSTEMS LABORATORY WORKBOOK GM SHORO

Das könnte Ihnen auch gefallen