Control Systems Lab Manual in Sci Lab
Control Systems Lab Manual in Sci Lab
Control Systems Lab Manual in Sci Lab
Objectives:
1) Familiarisation with Matrix operations
2) Familiarisation with Polynomial Operations
Software:
For this course we need a software package that can handle technical computation, such as
matrix manipulation, polynomials and Control System tools. Although traditionally Matlab is used
in such courses, but this Lab manuals aims to use a free alternative to Matlab, known as Scilab.
Scilab is in no way the only free and open source contender to Matlab, Octave is another number
one alternative, but it relative ease of use and the provision of built in Simulink1 like alternative
makes it a good choice. It has a nice user interface, although it may not have the perfect aesthetics
like that of Matlab.
To do quick overview of what Matlab and Scilab are lets see what does wikipedia has to say about
them():
Matlab:
MATLAB (matrix laboratory) is a multi-paradigm numerical computing environment
and fourth-generation programming language. Developed by MathWorks, MATLAB allows matrix
manipulations, plotting of functions and data, implementation of algorithms, creation of user
interfaces, and interfacing with programs written in other languages, including C, C++, Java,
Fortran and Python.
Although MATLAB is intended primarily for numerical computing, an optional toolbox uses the
MuPAD symbolic engine, allowing access to symbolic computing capabilities. An additional
package, Simulink, adds graphical multi-domain simulation and model-based design for dynamic
and embedded systems.
Scilab:
Scilab is an open source, cross-platform numerical computational package and a
high-level, numerically oriented programming language. It can be used for signal processing,
statistical analysis, image enhancement, fluid dynamics simulations, numerical optimization, and
modelling, simulation of explicit and implicit dynamical systems and (if the corresponding toolbox
is installed) symbolic manipulations.
Scilab is one of the two major open-source alternatives to MATLAB, the other one being GNU
Octave. Scilab is similar enough to MATLAB that some book authors (who use it) argue that it is
easy to transfer skills between the two systems. Scilab however puts less emphasis on
(bidirectional) syntactic compatibility with MATLAB than Octave does.
Note: So this just an overview of what these software are, and essentially we are going to be
mainly focussing on their capability to handle Matrices, Polynomials and later on we will get into
Control System Tool box.
1
Simulink is another software package that we will later talk about.
(note: from now on forward there will be less comparison (between Matlab and Scilab))
Matrix Operations:
Matrices are the basic elements of the Scilab environment. A matrix is a two-
dimensional array consisting of m rows and n columns. In this section of lab we will illustrate how
to apply different operations on matrices.
Entering a vector:
A vector is a special case of a matrix. The elements of vectors in MATLAB are enclosed by square
brackets and are separated by spaces or by commas. For example, to enter a row vector, v, type
-->v = [1 4 7 10 13]
and when you press enter, the following is displayed:
v =
1. 4. 7. 10. 13.
Column vectors are created in a similar way; however, semicolon (;) must separate the components
of a column vector,
Entering a matrix
A matrix is an array of numbers. To type a matrix into MATLAB
- begin with a square bracket, [
- separate elements in a row with spaces or commas (,)
- use a semicolon (;) to separate rows
- end the matrix with another square bracket, ].
type,
-->A = [1 2 3; 4 5 6; 7 8 9]
press enter:
A =
1. 2. 3.
4. 5. 6.
7. 8. 9.
Matrix indexing:
The element of row i and column j of the matrix A is denoted by A(i, j). Thus, A(i, j) in MATLAB
refers to the element Aij of matrix A. Single elements of a matrix are accessed as A(i, j).
The colon operator can also be used to extract a sub-matrix from an existing matrix. e.g.:
-->A(:, 2:3)
ans =
2. 3.
5. 6.
8. 9.
Transpose Operator:
The transpose operation is denoted by an apostrophe or a single quote ('). e.g.:
-->z = v'
z =
1.
4.
7.
10.
13.
-->b = ones(3, 1)
b =
1.
1.
1.
-->l = eye(3, 3)
l =
1. 0. 0.
0. 1. 0.
0. 0. 1.
-->zeros(2, 3)
ans =
0. 0. 0.
0. 0. 0.
Concatenation of matrices:
In addition, matrices can be constructed in a block form. With C defined as
C = [12; 3 4], we may create a matrix D as follows
Matrix inverse:
Let's consider the same matrix A.
A=
123
456
780
In Scilab, we’ve a handy command to find inverse of a matrix
Example:
-->A = [1 2 3; 4 5 6; 7 8 0];
-->inv(A)
ans =
column 1 to 2
- 1.7777778 0.8888889
1.5555556 - 0.7777778
- 0.1111111 0.2222222
column 3
- 0.1111111
0.2222222
- 0.1111111
-->det(A)
ans =
27.
Polynomial Operations
Scilab displays polynomials as row vectors containing the coefficients ordered by descending
powers. For example, the polynomial x 4 +3 x3 +5 x 2+7 x +2 can be entered in Scilab by typing
myPoly = [1 3 5 7 2]
MATLAB provides functions for standard polynomial operations, such as polynomial roots, and
differentiation.
-->r = [1 2 3];
4. Explore commands (all commands require polynomial as type, use poly method to create them)
– derivat
– k = derivat(p)
– k = derivat(a*b)
– k = derivat(a/b)
– kNumerator = numer(k)
– kDenomenator = denom(k)
Objectives:
- Familiarisation with basic plotting
- Familiarisation with control flow
1. Basic Plotting
Mathematical functions:
Scilab offers many predefined mathematical functions for technical computing which
contains a large set of mathematical functions. There is a long list of mathematical functions that are
built into Scilab. These functions are called built-ins, such as sin(x), cos(x), tan(x), exp(x), ln(x) and
more.
-->sin(%pi/4)
ans =
0.7071068
-->exp(10)
ans =
22026.466
Plotting:
Scilab has an excellent set of graphic tools. Plotting a given data set or the results of
computation is possible with very few commands.
Creating a plot:
The basic Scilab graphing procedure, for example in 2D, is to take a vector of x-coordinates,
x = (x1; : : : ; xN), and a vector of y-coordinates, y = (y1; : : : ; yN), locate the points (xi; yi), with
i=1;2; : : : ; n and then join them by straight lines. You need to prepare x and y in an identical array
form; namely, x and y are both row arrays or column arrays of the same length.
The Scilab command to plot a graph is plot(x, y). The vectors x = (1; 2; 3; 4; 5; 6) and y = (3; -1; 2;
4; 5; 1) procedure the picture shown in Figure.
For example, to plot the function sin (x) on the interval [0; 2π], we first create a vector of x values
ranging from 0 to 2π, then compute the sine of these values, and finally plot the result:
-->xlabel('x = 0 : 2*pi')
-->ylabel('Sine of x')
The colour of a single curve is, by default, blue, but other colours are possible. The desired colour is
indicated by a third argument. For example, red is selected by plot(x, y, 'r').
(Note the single quotes, ' ', around r)
Multiple for loops can be nested, in which case indentation helps to improve the readability.
It is important to note that if the condition inside the looping is not well defined, the looping will
continue indefinitely. If this happens, we can stop the execution by pressing Ctrl-C.
Lab Tasks:
Task 1: Linearly independent columns of a matrix
Steps:
• Define A (4x4 Matrix)
• Define B (4x3 Matrix)
• Define W = con_mat(A,B)
• Find dimension of W
• Find linearly independent columns of W and store them in matrix M.
Hints:
• Use for loop to access each column of W
• Use rank command to check independence of columns
Algorithm:
For loop
Take first/next column of W
Check the rank
If (rank increases)
Store in M
Else
Discard it
Check on for loop iterations:
Number of iterations= length (W)
Objectives:
- Familiarisation with system representation forms in Scilab.
- Creating Transfer Function model of system
System representation:
A system is interconnection of elements and devices for a desired purpose. Mathematical
models of systems can be created to have quantitative and qualitative insight of them. The Control
System tools provide algorithms that implements common control system design, analysis and
modelling techniques. Convenient graphical user interfaces (GUI’s) simplify typical control
engineering tasks.
Control systems can be modelled as transfer functions, in zero-pole gain, or state-space form. The
functions syslin(dom, H) and syslin(dom, A, B, C, D) create transfer function models and state-
space models respectively. These functions take the model data as input and produce respective
system response. To model zero-pole-gain control systems the approach is a little meticulous; poles
and zeros have to be defined as polynomials and then a product with the gain is taken, though the
final result will be a simplified transfer function, any system can be viewed as zero-pole-gain molel
when needed using the trfmod() function. This lab introduces transfer function based models of
system.
Example:
-->s = poly(0, 's');
-->num = (s^2) + (2*s);
-->den = (2*s^2) + (2*s) + 1;
-->sys_tf = syslin('c', num, den)
sys_tf =
2
2s + s
----------
2
1 + 2s + 2s
example:
t = linspace(0, 25, 500);
step_resp = csim('step', t, sys_tf);
plot(t, step_res); // use plot() to plot the response
xgrid(); // xgrid() turns the grid on
xtitle('step response', 'time', 'response'); // this adds the title, and axis labels
example:
t = linspace(0, 25, 500);
imp_resp = csim('impulse', t, sys_tf);
plot(t, imp_res);
xgrid();
xtitle('Impulse response', 'time', 'response');
example:
t = linspace(0, 25, 500);
ramp_resp = csim(t, t, sys_tf);
plot(t, ramp_resp);
xgrid();
xtitle('Ramp response', 'time', 'response');
Objectives:
1) Creating zero-pole-gain(zpk) model of system
2) Creating state-space model of system
3) Creating hybrid model of system
Zero-pole-gain models:
Scilab, at the moment, does not have a zpk type; rather transfer functions (and state-space)
models can be created using the zeros, poles and gains of a system. An existing system can also be
viewed as a Zero-pole-gain model.
To create a transfer function using zeros, poles and gains see the example below:
z = poly([1 2 3], 's');
p = poly([-2 -1 -4], 's');
k = 5;
sys_zpk = (z*k)/p; // creates a TF using the systems zeros, poles and gain
trfmod(sys_zpk) // only displays a system in zero-pole-gain form
State-space model:
State-space models are created using:
sys_ss = syslin(dom, A, B, C, D);
// where dom can be 'c' for continuous or 'd' for discrete. another method is to convert
// a transfer function to state-space form
sys_ss = tf2ss(sys_tf);
// there are more functions for converting other forms to state-space form too.
Output:
Dd =
0.
Cc =
1. 2.
Bb =
1.
2.
Aa =
1. 2.
5. 6.
Feedback:
the /. is the operator for feedback. It gives the same effect as:
sys = sys1 / (1 + sys1*sys2)
Note: Use the /. operator instead of calculating it manually with the feedback equation, this is
because Scilab has functions that do calculations efficiently and more precisely. Developing your
own functions is a noble cause and a good learning experience, but to do it properly require
knowledge of how the computer works. Although mathematically your function would be correct but
it may produce results that are unexpected, this can happen if the calculations asked by the your
code exceed the computers ability. Good code takes into account how does the computer behaves to
the given commands, thus efficiency and precision can increased.
(This note is by no means discouraging your own foray into the world of programming, but
rather all it is but a heads-up to those who want to explore how programmes are made and it is
good practice to reuse and understand others code (in our case the built-in Scilab functions); and
when writing your own code make sure you understand what variables and their types are, and how
each variable type has a critical relation to size (in terms of computer memory). By all means learn,
make, teach and share)
1) Transfer function
2) State-space
3) Zero-pole-gain
Lab Task 2:
- Define A, B, C, D matrices of appropriate orders, connect all sub-systems as shown in the
figure. Examine system model:
- Tf, ss and zpk
- Plot impulse response of the system.
- Perform stability analysis of the system.
Objectives:
1) Modelling f transfer function system
2) Modelling of zero-pole-gain system
3) Modelling of state-space system
Xcos:
The Scilab website describes Xcos as:
“Xcos is a graphical editor to design hybrid dynamical
systems models. Models can be designed, loaded, saved, compiled and simulated.”
Although, Xcos is not that polished as Simulink is, and that Simulink has superior documentation
and community support; given the fact that Xcos is free and open source, this makes the effort put
into learning Xcos yourself worth while. You can use all of the Scilab/Xcos tools for free, and then
freely share your own work or reuse code from the Scilab/Xcos community.
Now that we are through with the definitions, lets see what Xcos can do; but first we need open up
Xcos. To do that, just type xcos in the console, and press enter. Two windows should appear; one
being the Pallet browser (essentially the model blocks library), and an Untitled windows, actually
called the diagram window, where the modelling and simulation happens; user can create more of
diagram windows and save them.
1) Though already open, if the need be; to create a new diagram window, click the
New diagram button on the, already open, diagram window.
4) To copy the GENSIN_f block from the Palette Browser, click the GENSIN_f node
to select the GENSIN_f block and drag the it from the Sources
window to your diagram window.
5) Copy the rest of the blocks in a similar manner from their respective palettes into
the diagram window.
6) If you examine the blocks, you see an arrow on the right of the GENSIN_f
block and two on both sides of INTEGRAL_f. The > symbol pointing out of a
block is an output port; if the arrow points to a block, it is an input port. A signal
travels out of an output port and into an input port of another block through a
Simulation:
1. Set up Xcos to run the simulation for 10 seconds. First, open the Setup dialogue box by
choosing Setup from the Simulation menu. On the dialogue box that appears, notice that
the Final integration time is set to 1.0E05 (its default value); change this to 10.
2. Run simulation, by clicking on play button.
3. A graph should appear that is the plot of the CSCOPE. As can be seen, time is on the
independent x-axis (inferred from the fact the plot of the values start from 0 and end at 10,
which is the time domain we set earlier), and then of course the y-axis has the sine wave
generator values; y-axis limits can be set in the CSCOPE settings, accessed when
CSCOPE is double clicked, or for more control, when the plot is generated go to the Edit
Menu and select the Axes properties to open the a dialogue box will appear, where Data
Bounds for x-axis and y-axis can be found, in addition to other properties.
Lab Task 2
• Define A, B, C, D matrices of appropriate orders, connect all sub-systems as shown
in the figure. Model system in Simulink
Objectives:
1. Develop a second-order model of a motor and arm.
2. Study closed-loop response of system to (impulse, step, ramp) inputs.
3. Study the effect of changing system gain.
System Model:
Scilab Script:
Input type:
1. Impulse input
2. Step input
3. Ramp input
Gain:
1. Ka=30;
2. Ka=60;
Plot system output for time 0 ≤ t ≤ 1 sec
Xcos:
Simulate the motor model for above mentioned specifications in Simulink
Observations:
Effect on system response by varying gain
Effect on system response by changing input types
Objectives:
1. Use Scilab to draw the root locus of given system for parameter K.
2. Determination of parameter values associated with poles in the target region.
Root Locus:
The root locus is a curve of the location of the poles of a transfer function as some parameter
(generally gain K) is varied. This is a very powerful graphical technique for investigating the effects
of variation of a system parameter on the locations of closed-loop poles. The root loci are
completed to select the best parameter value for stability. A normal interpretation of improving
stability is when the real part of a pole is further left to the imaginary axis.
The characteristic equation of the closed-loop system is 1+ GH(s) = 0, Substituting the transfer
function from the block diagram gives:
The Scilab commands that produce the root locus diagram are:
s = poly(0, 's');
sys = syslin('c', ((s^2) + (6*s) + 8), (s^3 - s));
evans(sys, 21);
xgrid();
Scilab does not show the direction of the movement of the poles. It is understood that the
movement is from poles to zeros.
Lab Task:
Repeat this lab using GUI based SISO tool available in Scilab, it goes by the package name 'rltool'.
It can be found in the Module manager – ATOMS, under the Modelling and Control tools option.