Control System Lab
Control System Lab
The >> symbol indicates that MATLAB is ready to accept input and it is called a prompt.
Type in the following and press enter:
>> x = 3
You will see two changes: First, MATLAB will output the value now stored in x. Second,
on the right, x will appear in the list of variables. When there is a long process taking place,
the prompt will disappear and appear again when the calculation has completed. Usually
when you add a semicolon after a statement, it means that the output won’t be shown on
the screen.
1.2.1.2 The MATLAB Editor
When writing long programs, or writing programs you may want to edit or run over and
over, it is better to create a script in the editor. Click “New Script” near the top left to open
the editor.
The editor is where you will write and save all your programs. For example, type the
following code in the editor:
clc; x =
inv(x);
disp(x);
And press “Run”. Once you do that, it will ask you where to save your script. Save it
somewhere. You should see something similar to the following:
1.2.1.3 Using MATLAB as a Calculator Try
entering the following in MATLAB:
>> 3 +
5 ans =
8 >> 3
- 5 ans
= -
2
Experiment with MATLAB. Do the same as we have done above with different numbers
and also*, / and ^.
Apart from normal division in MATLAB, there is also the left-division operator, \. It
divides the second number (on its right) by the first and gives the result.
You can use parentheses in MATLAB as you would in normal mathematics, or on your
calculator. For example,
>> (3 + 5) *
8 ans =
64 >> 3 + 5 *
8 ans =
43
Note that MATLAB does not simply evaluate expressions from left to right, but it respects
the order of operations, which means that it does multiplication and division first, then
addition and subtraction.
1.2.2 Variables
In MATLAB, you can store the results of any calculation in a variable. Try the following
in MATLAB:
>> x = 1 / 2
x =
0.5000
>>x = 3;
If you look on the right side of MATLAB in the Workspace, you will see at least two
variables defined. You will see x with the value of 3 that you just assigned, and you will
see ans, which stores the result of the last calculation you did. Just keep in mind a few
rules, MATLAB variables always start with a letter, and can have letters, numbers or
underscores in them.
1.2.3 Built-In Functions and Variables
MATLAB has a big library of built-in functions that can do special jobs. For example,
>> theta = pi;
>> x = cos(theta);
>> y = sin(theta);
Check the workspace and you will see that it has three new variables: theta, x, and
y. In the table below you will find some common functions used in calculations and
their explanation. Note also that the semicolon after a statement means it won’t show
the output, but it will store it.
MATLAB Expression Meaning
sqrt(x) √𝑥
𝑛
nthroot(x, n)
√𝑥
log(x) ln 𝑥 = log𝑒 𝑥
log10(x) lg 𝑥 = log10 𝑥
log(x)/log(b) log𝑏 𝑥
exp(x) 𝑒𝑥
pi 𝜋
sin(x) sin 𝑥
cos(x) cos 𝑥
tan(x) tan 𝑥
csc(x) csc 𝑥 = 1
sin𝑥
sec(x) sec 𝑥 = 1
cos𝑥
cot(x) cot 𝑥
tan𝑥
asin(x) sin−1 𝑥
acos(x) cos−1 𝑥
atan(x) tan−1 𝑥
atan2(y,x) tan (𝑦, 𝑥) (takes care of the quadrant)
−1
MATLAB has a lot of operators and functions you can use to check relationships between
two numbers. Each of them returns 0 or 1, for false and true, respectively.
MATLAB Expression Explanation
a == b Is a equal to b?
a > b Is a greater than b?
a >= b Is a greater than or equal to b?
a < b Is a less than b?
a <= b Is a less than or equal to b?
a ~= b Opposite of a == b.
a && b Returns true if both a and b are true.
a || b Returns true if one or both of a and b are true.
xor(a, b) Returns true if exactly one of a and b are true.
~a Returns the opposite of a.
true Always true.
false Always false.
We can use these as follows:
>> 5 >
3 ans =
1 >> 5
< 3 ans
=
0
>> 5 > 3 && 3 <
5 ans = 1
Are you stuck somewhere? There’s always the MATLAB help. Please attempt to solve
your own problems before asking the instructor; that’s how you learn. There are two main
ways to access the help. The first is by pressing F1 on your keyboard. This should bring
up a window like the following in which you can search for in-built functions or help, or
look up the syntax of a function:
The second way is to type in help<function name>. This way, you can quickly look
up the syntax or what a function does without opening the Help Centre. Type in help inv.
You will see something like the following:
You can also jump directly to the help section of a function by typing doc <function
name>.
1.2.6 Vectors/Arrays
Row vectors, also known as 1-D arrays, are defined in MATLAB like this:
>> x = [2 5 4 3 1]
x =
2 5 4 3 1
>> x = [2, 5, 4, 3, 1] x =
2 5 4 3 1
To access one element of a row vector, you can do this:
>>
x(3)
ans =
4
Note that the numbering of an array starts at 1 in MATLAB. Many of the functions we
studied above can be applied to arrays as well. For example,
>> sqrt(x)
ans =
1.4142 2.2361 2.0000 1.7321 1.0000
If arrays are of the same length, addition and subtraction works as usual. You can also
multiply/divide an array by a number (also known as a scalar) with * and /. However, if
you want to multiply two arrays or divide them element by element, you need to use the
operators.*and . /, along with .\, for example:
>> x .* x
ans =
4 25 16 9 1
You can do something similar for exponentiation:
>> x .^ 3
ans =
8 125 64 27 1
A similar thing applies for binary operators. You need to use & instead of && and | instead
of || when working with arrays.
Here are some special commands for generating arrays, along with their explanation. Note
that commands that have a different behavior as compared to scalars are repeated below
for clarity.
MATLAB Expression Explanation
1.2.7 Matrices
To start using Simulink, type in simulink at the MATLAB prompt and press enter.
Some time will pass, and you will be presented with a window similar to this one:
Click on the (new model) icon, near the top center of the window. A new window will
pop up in which you can view and edit your model.
In the Simulink Library Browser, click on Sources under Simulink, and drag the Sine wave
generator onto your model. Your model and window will look something like this:
The outwards pointing arrow from the sine-wave generator is an output of the Sine Wave
block. You can take the input of this block and feed it to another block. Double-clicking
the block brings up different properties related to it you can modify.
Drag out a Scope block from Sinks and connect it to the Sine Wave block by selecting the
Sine Wave block, holding down the Ctrl key, and clicking the Scope block. Then click the
button and double-click on the scope. Observe the output.
Question 1
Create a vector 𝑎 = [0, 0.1, 0.2, … , 10] and a vector 𝑏 = [cos0 , cos0.2 , … , cos 20].
Compute the following:
a. 𝑐 where 𝑐𝑖 = 𝑎𝑖/𝑏𝑖.
b. 𝑑 where 𝑑𝑖 = 𝑎𝑖4
c. The element-wise product of 𝑎 and 𝑏.
d. The dot product of 𝑎 and 𝑏.
Question 2
Create a 3 × 3 matrix with all its elements randomly distributed in [0, 1]. Compute:
Question 3
Use the built-in function eig to find the eigenvalues and eigenvectors of the matrix 𝑥,
where
1⁄4 1⁄4 5⁄4
𝑥 ≜ [ −1 2 1 ]
− 5⁄4 − 1⁄4 15⁄4
Hint: If you are stuck, use help eig to find out all the different things eig can do.
Question 4
a. Create a function that plots the function sinc 𝑥 = sin .
𝜋𝑥(𝜋𝑥)
b. Plot, using your function, the graph of sinc 𝑥 in the interval[−2𝜋, 2𝜋].
Question 5
a. Find out, how functions are written in MATLAB? Write a function that performs the
convolution of two input signals without using any built-in functions, other than
those we have covered in this lab manual.
b. Compare the output of your function with the built-in conv function for multiple
inputs to see that it produces the same output. Hint: Use all (A == B) to compare
two arrays.
Question 6
The inputs given below are used for what characterization of the system. Explain briefly.
1) Impulse
2) step
3) ramp
4) sinusoidal
Question 7
Implement the following transfer function in Simulink.
1
𝐻(𝑠) = 𝑠 2+ 2𝑠 + 1
Observe its
• Impulse response
• Step Response
You may use any blocks or functions available in MATLAB or Simulink.
Hint: You will need the Step, Scope, Derivative and Transfer Function blocks.