ELEN90055 Control Systems Workshop 0: Prepared by Alejandro Maass and Michael Cantoni Email: Cantoni@unimelb - Edu.au
ELEN90055 Control Systems Workshop 0: Prepared by Alejandro Maass and Michael Cantoni Email: Cantoni@unimelb - Edu.au
Workshop 0
1 Introduction
The aim in Workshop 0 is to work through a very basic tutorial on Matlab and Simulink.
This introduction is tailored to control systems in particular. It includes use of the key
Matlab functions and Simulink blocks required for the forthcoming workshops. Specif-
ically, the focus is to build and simulate models; the context is not so important. The
interpretation of models and simulations will be the focus of subsequent workshops.
https://au.mathworks.com/support/learn-with-matlab-tutorials.html
1
2 3 Matlab implementation of the model
Consider the water tank in Figure 1, where qin is the inflow and qout is the outflow. The
volume V of water in the tank behaves according to the following model:
dv(t)
= qin (t) − qout (t). (1)
dt
For the purpose of this workshop, it is assumed that the inflow and outflow are constant.
Therefore, (1) can be written as
Z t
v(t) = v(0) + (qin − qout )dt = V (0) + (qin − qout )t, (2)
0
The aim of this workshop is to implement (2) in both Matlab and Simulink. In par-
ticular, the required Matlab functions and Simulink blocks are presented and explained.
Matlab is an interpreter. The user can enter commands one at a time directly. Each
command, which may define and assign a value to a data object, for example, perhaps
by function call, is executed as it is entered. There are many built-in data object types
(scalars, vectors, systems, etc.) and toolbox extensions of these. A collection of commands
can be grouped into a script. The script is a file with a .m extension. A script is run by
entering its name at the command line, once in the same working directory, or by pushing
the play button in the in-built script editor. Each command in the script is executed
in the line order specified. A script can be created in the editor tab that is part of the
Matlab environment, or using any other text editor. Alternatively, it can be created in
the command window by typing edit scriptfilename.
• Create a Matlab script, in which you will record your working for Workshop 0.
3 Matlab implementation of the model 3
There are several ways to create this transfer function. Some examples are provided below.
% example 1
sys = tf(1,[1 0]);
% example 2
s = tf(’s’);
sys = 1/s;
% example 3
s = tf([1 0],1);
sys = 1/s;
Please type help tf in the command window for a better understanding of this command.
Note, ending a Matlab command with ; stops the result from being displayed. To see
result of that line when the script is run, remove the ;. The character % marks the start
of a comment.
Given the system object sys, we can use the command step to plot the water volume
over time, given a constant inflow and outflow. The command step computes the step
response of the integrator and generates a plot of this, assuming zero initial conditions in
line with the transfer function specification of the system; i.e., v(0) = 0 in (2). A non-zero
4 3 Matlab implementation of the model
initial condition can be added manually as explained further below. Let us assume qin = 5
and qout = 1, i.e. the tank is filling up with water. Note that the step magnitude is
q = qin − qout = 4.
• Compute the step response, with amplitude 4, of the transfer function 1/s.
To do this, we first need to set the amplitude of the step to 4. This is done by entering
opt = stepDataOptions(’StepAmplitude’,4);
Then, type
step(sys,opt);
Note that sys is the system object with integrator transfer function created previously.
The result of this is a plot that is displayed when the script is run even with the ; in
place. This plot is shown in Figure 2. Sometimes it is useful to access to record the data
Step Response
180
160
140
120
Amplitude
100
80
60
40
20
0
0 5 10 15 20 25 30 35 40
Time (seconds)
generated by the step (and other) command(s) in new data objects. This would allows us
to manually add the initial condition v(0), for example. To do this, use
3 Matlab implementation of the model 5
[y,t]=step(sys,opt);
We can plot the contents of the data vector y versus the time vector t using the plot
command, which includes options for specifying labels, title, etc. Specifically, to add an
initial condition of v(0) = 5, type
plot(t,y+5);
Note that Figure 2 represents the tank getting filled with water as the inflow is greater
than the outflow. Furthermore, it is consistent with the ramp function described alge-
braically in (2).
• Compute the tank step response for different constant values of qin , qout and v(0).
We now show how to simulate in a Matlab script the step response of the integrator
using for loops. To do this, we need to discretise (1), and we pick the following simple
discretisation for the derivative
where T is the discrete time step. For the sake of argument, we take T = 1 below. To
implement (3) in our Matlab script use the following code:
qin = 5; % Inflow
qout = 1; % Outflow
v(1) = 0; % Initial condition (n.b. vectors are indexed starting from 1)
for i=1:1:40 % This means i ranges from 1 to 40 in steps of 1
v(i+1) = v(i) + qin - qout; % Discretization (3) of (1) with T=1
end
stem([0:1:40],v); % use of plot instead would join the dots
160
140
120
100
Amplitude
80
60
40
20
0
0 5 10 15 20 25 30 35 40
Time(seconds)
Note that (3) is a simple (Euler) discretization of the continuous-time equation (1).
There are many other more sophisticated methods available as well, which can also be
easily implemented with for loops.
• Run the above code for different values of inflow, outflow and initial condition.
4 Simulink implementation
Simulink is a graphical extension to Matlab for the modelling and simulation of dynam-
ical systems. In Simulink, system models are captured schematically as block diagrams.
Many libraries of elemental building blocks are available, such as transfer function blocks,
summing junctions, etc., as well as virtual input and output blocks such as function gen-
erators and scopes. These virtual devices allow you to create simulations of the models
you will build by configuring elemental building blocks selected into the model from the
available libraries, and graphically linking the inputs and outputs of these blocks. Linking
is achieved by drawing lines with the mouse between ports. Simulink is integrated with
Matlab. Data from the Matlab workspace is available in the Simulink model. This is
often used to set configurable parameters in blocks. Data generated in Simulink during
4 Simulink implementation 7
simulation can also be exported for use at the Matlab command line.
There are two major types of item in a Simulink model: blocks and lines. Blocks are
used to generate, modify, combine, output, and display signals. Lines are used to transfer
signals from one block to another. There are several general classes of blocks, and we list
the ones most relevant to our needs below:
The tank model expressed mathematically in (2) can be constructed in Simulink as shown
in Figure 5. We need two step blocks, one integrator block, and one scope block. The step
block can be found in the Library Browser under the ‘Sources’ tab. The integrator can be
found in the ‘Continuous’ tab, and the scope can be found in the ‘Sinks’ tab. By double-
clicking a block, you can access to its different options. These block parameters can be
set to fixed values specified by you in the available fields, or they can be linked to data
objects in the Matlab workspace. To start the simulation you should click the ‘Play’
(run) button at the top of the model window. To set the initial condition of the integrator,
you can double-click the integrator block and modify it accordingly.
• Create the block diagram in Figure 5, and simulate the response for different values
of inflow, outflow and initial volume. Consider linking the source block parameters to
appropriate variables in the Matlab workspace set up by running the script devel-
oped above. The value of a parameter can be changed by changing the corresponding
assignment command in the script and re-running the script, or just that command.
4 Simulink implementation 9
In this section we feed the volume signal back to the inflow with a constant gain as shown
in Figure 6. To do this we need to add a ‘Gain’ block and a ’summing junction’ block, both
Figure 6: Tank model in Simulink under a constant gain feedback control of inflow to
compensate for outflow disturbance.
found under the ‘Commonly Used Blocks’ tab. We have also replaced the constant outflow
by a sinusoid with offset, and we included a reference volume level. The ‘Sine Wave’ can
be found under the ‘Sources’ tab, and the ‘Constant’ can be found under ‘Commonly Used
Blocks’ tab.
• Construct the block diagram in Figure 6 and simulate the response for different
values of the feedback gain, integrator initial condition, sine wave frequency, offset,
and reference volume level.
Sometimes, we would like to use the results of a Simulink simulation in the Matlab
command window for further calculations and plotting. In fact, doing this is helpful for
your reports as screenshots of a Simulink scope are hard to analyse. Exporting the signal
data to Matlab will help you get improved plots which can be easily analysed. We can
achieve this by using the ‘To Workspace’ block, found in the ‘Sinks’ tab (see Figure 7).
You can double-click this block in order to edit the signal name and save format.
10 5 Summary of main points
• Include a ‘To Workspace’ block in your open-loop tank model to export the output
of the integrator to Matlab. Note that you need to run the simulation for this data
to be saved.
• Plot the data, which is now in your workspace, using Matlab and include appropri-
ate labels to it. (This should look exactly as Figure 2, which was previously plotted
using a Matlab commands instead of Simulink models).
Sometimes it is useful to save the plot directly from the Scope block. To do this, double-
click the scope after the simulation has stopped, and click File->Save to Figure. A
Matlab figure will open, and this can be saved in a large range of formats including
.eps, .jpg, .png, etc. This can also be done by using the Matlab command print,
which you are encouraged to explore due to its multiple printing options (type help print
in the command window).
By the end of Workshop 0, you should have gained an improved understanding of Matlab
and Simulink, within the context of analysing a simple but insightful example. The tools
learned in this workshop are key for the forthcoming workshops, in which more complicated
systems are modelled, simulated and analysed.