You are on page 1of 9

# CONTROL ENGINEERING-I

Lab-1 Dated: 24-10-2007
1. What is MATLAB:
The name Matlab is short for the MATrix LABoratory. It is commercial software
package whose main function is to perform calculations on matrices, row vectors, and
column vectors. It is widely used in both industry and academic institutions. It
possesses many of the features of the High Level, numerically oriented programming
language, but in addition has a large collection of built-in functions for performing
matrix and vector operations in a way that is very simple for the user.
In addition to functions for performing the usual computations from linear algebra
(e.g., inverse, transpose, determinants etc.) Matlab has collection of toolboxes of
more specialized functions for computations in specific areas of science and
engineering including Control system Toolbox.
2. Entering and Quitting MATLAB.
Start windows and double click on the MATLAB icon (if it is installed!). A command
window will appear. You can write your programs directly on the window or you
may open a new file from file menu, which appears on top of the command window.
3. Creating and Manipulating Matrices and Vectors.
Matlab works on matrices and vectors that may be real or complex. Variables do not
have to be declared (as in C-language). Computations are performed using double-
precision arithmetic, and the results can be displayed in a variety of different formats.
Let us enter a row matrix into the Matlab workspace. Type in:
» v=[2 4 7 5]
and pressing enter key will result in echo back to you:
v =
2 4 7 5
this creates a variable v whose current value is a row vector with four elements as
shown. To suppress the echo one uses semicolon following the command line. Thus
» W=[1 3 8 9];
creates another row vector W, but does not echo. To check that W has appeared in the
Matlab workspace, type
» who
which will display all the variables in the Matlab workspace as:
W v
and to check the value of W simply type:
» W
and press enter key,
W =
1 3 8 9
Note that Matlab is case sensitive.
EXERCISE-1: investigate the effect of the following commands:
(i) v(2)
(ii) W(4)
(iii) sum=v+W.
(iv) diff=v-W
(v) vW=[v W]
(vi) vW(2:6)
(vi) v’
column vectors can be typed directly in one of two ways, for example, to enter the
column vector
1 
1 
Z = 
0 
 
0 
you can type
» Z=[1;1;0;0]or
» Z=[1
1
0
0]
both will result in:
Z =
1
1
0
0
EXERCISE-2: investigate the effects of following commands:
(i) Z’
(ii) Z*v
(iii) Z’*v’
(iv) [v;W]
(v) [Z;v’]
it creates a new matrix of single column combining the column vector Z and
transpose of row matrix v.
(vi) Z+v’
it simply adds the column vector Z with transpose of row vector v.
These are various ways of entering matrices. For example, to enter the matrix
1 2
M = 
3 4
the most obvious ways are to type
» M=[1 2;3 4] or
» M=[1 2 3 4]
but there are more perverse ways such as:
» M=[[1 3]'[2 4]']
EXERCISE-3: investigate the effect of the following commands:
(i) N=inv(M)
this creates a new matrix N whivh is a inverse of matrix M.
(ii) M*N
it simply multiply M with N.
(iii) det(M)
it finds the determinant of matrix M.
(iv) I=eye(2)
this display the identity matrix of order 2-by-2.
(v) M+I
this add the Identity matrix with matrix M.
(vi) M*Z
it will multiply matrix M to first and second row of matrix Z.
(vii) v(3:4)*M
(viii) M(I,I)
It will display first element (1x1) of the matrix.
(ix) M (1:2,1:2)
will display elements in(2x2) matrix.
(x) M(:,1)
will only display first column’s elements.
(xi) M(2,:)
it will only display the second row of the M matrix.

4.Built-in functions and Matlab HELP.
You have already encountered some built-in functions. In addition to standard arithmetic
operations such as “+” and “*”, you have met inv (M) that generates the inverse of matrix
M, det(M) that generates determinant of M and eye(2) that generates the 2-by-2 Identity
matrix. Matlab has a very large number of built-in functions. To see the list of those
» help

To get information a specific function (“det” for example) type
» help det
DET Determinant.
DET(X) is the determinant of the square matrix X.
Use COND instead of DET to test for matrix singularity.
The help command is very useful. Most of the Matlab built-in functions have very
descriptive names and are easily remembered or guessed.
EXERCISE-4: use the help command to find out about the following built-in functions
and make your own sample examples
(i) ones
Solution:
ONES Ones array.
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(M,N,P,...) or ONES([M N P ...]) is an M-by-N-by-P-by-
...
array of ones.
ONES(SIZE(A)) is the same size as A and all ones.
Example program:
» ones(3)

ans =
1 1 1
1 1 1
1 1 1
(ii) zeros
Solution:
ZEROS Zeros array.
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(M,N,P,...) or ZEROS([M N P ...]) is an M-by-N-by-P-
by-...
array of zeros.
ZEROS(SIZE(A)) is the same size as A and all zeros.
Example program:
» zeros(3)

ans =
0 0 0
0 0 0
0 0 0
(iii) linspace
Solution:
» help linspace

LINSPACE Linearly spaced vector.
LINSPACE(x1, x2) generates a row vector of 100 linearly
equally spaced points between x1 and x2.
LINSPACE(x1, x2, N) generates N points between x1 and x2.
Example program:
» linspace(0,1)

ans =
Columns 1 through 7
0 0.0101 0.0202 0.0303 0.0404 0.0505
0.0606

(iv) logspace
Solution:
LOGSPACE Logarithmically spaced vector.
LOGSPACE(d1, d2) generates a row vector of 50
logarithmically
equally spaced points between decades 10^d1 and 10^d2. If
d2
is pi, then the points are between 10^d1 and pi.
LOGSPACE(d1, d2, N) generates N points.
Example program:
ans =
Columns 1 through 7
1.0000 1.0481 1.0985 1.1514 1.2068 1.2649
1.3257
5.graphs
The results of many control computations in Matlab are huge matrices containing, for
example, frequency responses, that one would like to display in graphical form.
We will plot graph of
ys=sin(t)
for t going from 0 to 5 seconds. First we need to generate a vector t containing the values
of time that we want to use. Type:
» t=linspace(0,5,51)
which makes t into a row vector of 51 values from0 to 5 inclusive. Another way of doing
same thing would be to type
» t=(0:0.1:5)
both ways will result in:
t =
Columns 1 through 7
0 0.1000 0.2000 0.3000 0.4000 0.5000
0.6000
» plot(t,ys)
1

0 .8

0 .6

0 .4

0 .2

0

-0 . 2

-0 . 4

-0 . 6

-0 . 8

-1
0 0.5 1 1 .5 2 2 .5 3 3 .5 4 4.5 5

the graph can be prettied using thee following self explanatory sequence of commands:
» xlabel('time[in seconds]');
» ylabel('graph of sin(t)');
» title('2KES23');
2K E S 23
1

0.8

0.6

0.4

0.2
graph of sin(t)

0

-0 . 2

-0 . 4

-0 . 6

-0 . 8

-1
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
t im e [ in s e c o n d s ]

more than one set of points can be plotted on the same axes, and more than one graph can
be displayed on the screen at once.
EXERCISE-5
(i) Generate a vector containing the values of cos (t) for the same time
range before.
Solution:
» yc=cos(t)
yc =
Columns 1 through 7
1.0000 0.9950 0.9801 0.9553 0.9211
0.8776 0.8253
» plot(t,yc)

1

0.8

0.6

0.4

0.2

0

-0 . 2

-0 . 4

-0 . 6

-0 . 8

-1
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

(ii) From “help” entry for “plot” find out how to get both ys an yc plotted
against t on the same axes.
Solution:
» t=(0:0.1:5);
» ys=sin(t);
» plot(t,ys)
» hold on
» plot(t,yc)
» hold off
1

0 .8

0 .6

0 .4

0 .2

0

-0 . 2

-0 . 4

-0 . 6

-0 . 8

-1
0 0 .5 1 1 .5 2 2 .5 3 3 .5 4 4 .5 5

(iii) From the “help” entry for “subplot” find out how to plot ys and yc
plotted on two separate graphs, one above the other.
Solution:
» subplot(2,1,1),plot(t,ys)
» subplot(2,1,2),plot(t,yc)

1

0.5

0

-0.5

-1
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

1

0.5

0

-0.5

-1
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5