ECPD-2023-2024 Lab Guide
ECPD-2023-2024 Lab Guide
ECPD-2023-2024 Lab Guide
Prepared by
Afonso Botelho
Instituto Superior Técnico
Objective
Design a model predictive controller and Kalman filter for a real thermal plant with one
input and one output. Perform system identification with real data, design controller
and observer in simulation, and then apply them to the real plant. The first two parts
of this laboratory work provide exercises on basic issues on function optimization and
receding horizon and predictive control.
Pedagogical concept
This laboratory work has been produced in the framework of the pedagogical project
MobilLab, financed by IST. According to the pedagogical concept in this project, the
students receive a lab kit to perform the experiments according to their own schedule.
The kit is then given back to IST at the end of the term. Laboratory classes are used to
present short progress reports by the student and to clarify doubts.
Underlying concepts
1. Function optimization
2. Receding horizon and unconstrained MPC
3. Model identification
4. MPC and Kalman filter design
5. Application to a real system
The kit is physically connected to the computer by a USB cable. The kit consists of two
connected boards, one with the plant to control and the other an ARDUINO that is
used just for communication between the plant board and the personal computer.
The TCLab kit must also be connected to a power supply. See the manual TCLab
installation and setup instructions for instructions.
The plant consists of two power transistors that receive a command signal each
between 0 and 100% in order to dissipate heat on metallic plates, and two
temperature sensors (thermistors) that provide a temperature reading at each metallic
plate. In the present work, we will only use one heater and one sensor.
After interconnecting the TCLab kit with the personal computer, and installing the
interface software, it is possible to read each of the temperatures with a single
MATLAB command (in degree Celsius), as well as to send commands to each of the
transistor heaters (a number between 0 and 100, with 0 corresponding to no heating
and 100 corresponding to the maximum possible heating).
1. Read the temperature from the plant (just one command line in MATLAB);
2. Compute the control variable using the predictive control algorithm (this task is
the core of the work to be done);
3. Write the value of the control variable to the plant (just one command line in
MATLAB);
4. Wait for the sampling time to end and go to step 1.
Safety precautions
Never touch the heating elements (see picture). They can be
very hot and injure your skin.
Grades
The final grade of the laboratory is computed as
Appendix 1 lists the good practices that must be followed when writing the report.
Failure to comply with these practices will imply a penalty on the grade. Appendix 1
can be used as a check list to verify the report and improve it.
It is not mandatory for the students to attend the full class, but only during the period
required to present the progress report.
Not all the students must be present. At least 1 student must be present in each class.
During the whole laboratory classes, all the group members must have been present in
the lab at least once. Failure to comply with these guidelines imply a penalty of 1 point
in the lab grade.
Software to deliver
Besides the report, you must deliver the following developed code and data:
Motivation
The key issue in MPC is that it is based on on-line minimization of a cost function to
obtain the value of the control variable to apply to the plant. In this way, MPC allows
to enforce constraints on several variables, a feature that is unique among on-line
design techniques (optimal control based on Pontryagin’s Principle may also embed
constraints, but this design technique is an off-line one).
Hence, it is important to get acquainted with algorithms and software for function
minimization. This work provides an exercise on the use of popular function
minimization Matlab tools. Although basic, this knowledge is very useful in writing our
own tools for MPC, as well as in using MPC toolboxes.
Objectives
Both surf and contour plot the function in a grid of points that can be defined using the
function
• meshgrid
A useful example
This example illustrates how to use the minimization software described above to
compute unconstrained and constrained minima for a function. This problem is a trivial
one, the aim being just to illustrate the use of the Matlab software.
It is obvious that this function has an unconstrained minimum for 𝑥 = (1, 1).
min 𝑓(𝑥)
𝑥
To use the Matlab solver fminunc to solve this problem, start by creating a Matlab
function script with the name of the function to minimize. For instance, calling the
function BasicFunction, you must create the script:
function f = BasicFunction(x)
f = (x(1)-1)^2 + (x(2)-1)^2;
end
To solve numerically the unconstrained optimization problem, define the initial guess
of the optimal value of 𝑥 (called x0 below) and run the following code
options = optimoptions('fminunc','Algorithm','quasi-newton');
xopt=fminunc(@BasicFunction,x0,options)
The first line defines the options of the solver (see Matlab help) and the second line
calls fminunc to actually solve the problem. The name of the script that contains the
definition of the function to minimize is given as the first argument of fminunc by the
name of the script preceded by the symbol @.
Forbidden
1 region
Allowed
region
1 x2
𝑥2 ≤ 1 − 𝑥1 ,
that corresponds to the allowed values below the straight line in figure P1-1. This
constraint can be written in the form
𝐴𝑥 ≤ 𝐵, with 𝐴 = [1 1] and 𝐵 = 1.
This form of the constraint is the standard one to be specified in the Matlab solver
software function fmincon.
min 𝑓(𝑥)
𝑥
𝑠𝑢𝑏𝑗𝑒𝑐𝑡 𝑡𝑜 𝐴𝑥 ≤ 𝐵
The numerical solution of this constrained problem using fmincon is coded in Matlab
by
Ac=[1 1];
Bc=1;
xoptconstr=fmincon(@BasicFunction,x0,Ac,Bc)
Work to perform
Motivation
The receding horizon strategy is the key component of model predictive control (MPC).
It consists of designing the control action such as to optimize it over an extended
horizon, apply to the plant only the first control move of the optimized control
sequence, and then repeated the process at the next discrete time instant, starting
from the new state. It is the receding horizon strategy that allows to transform an
open-loop optimal control action into a feedback control action.
When enlarging the optimization horizon, the initial control move approximates the
one that would be obtained with an infinite horizon, which is stabilizing.
Objectives
The objective of the work to be done here is to compute the state feedback gain yield
by the receding horizon strategy in a problem with linear dynamics, quadratic cost and
without constraints, and to compare it with the infinite horizon gain computed using
the solution of an equivalent linear quadratic problem with infinite horizon, that
involves the solution of an algebraic Riccati equation.
1 Cost optimization
Consider the infinite horizon linear quadratic (LQ) optimal control problem that
consists of minimizing the infinite horizon unconstrained quadratic cost
subject to
𝐴𝑇 𝑆𝐴 − 𝑆 − 𝐴𝑇 𝑆𝐵(𝐵 𝑇 𝑆𝐵 + 𝑅)−1 𝐵𝑇 𝑆𝐴 + 𝑄 = 0,
1. (𝐴, 𝐵) is stabilizable
2. 𝑅 ≻ 0 (read ≻ positive definite) and 𝑄 ≽ 0 (read ≽ positive semi-definite)
3. (𝑄, 𝐴) has no unobservable mode on the unit circle. If 𝑄 = 𝐶 𝑇 𝐶, with 𝐶 a
vector, this condition is verified if (𝐶, 𝐴) is observable.
Numerically, the discrete time LQ optimal gain can be computed using the MATLAB
Control Systems Toolbox function dlqr. The syntax to call this function is
[KLQ,S,lambda] = dlqr(A,B,Q,R)
where KLQ is the optimal gain, S is the positive definite solution of the Riccati equation
and lambda is the vector of eigenvalues of the closed-loop system dynamics 𝐴 − 𝐵𝐾𝐿𝑄 .
Consider now the finite horizon unconstrained quadratic cost, defined over a horizon
with 𝐻 steps, given by
It is assumed that the plant input is scalar, and hence 𝑅 is a positive scalar weight.
Figure P2-1. The control sequence to optimize in the RH cost and the corresponding
sequence of states that is influenced by it.
𝑦(𝑡) = 𝐶𝑥(𝑡),
Figure P2-1 shows the time synchronization of the variables in this cost function. “We”
are at the present time 𝑡 and want to compute the control sample 𝑢(𝑡) to apply to the
plant. For that sake, we consider the outputs (or the states) between 𝑡 + 1 and 𝑡 + 𝐻.
Since the plant has a unit delay between applying a control sample and its influence on
the state and the output, this set of future outputs is influenced by the control inputs
between 𝑡 and 𝑡 + 𝐻 − 1.
The outcome of the optimization is, therefore, the sequence of future control inputs
between 𝑡 and 𝑡 + 𝐻 − 1. According to the receding horizon strategy, of this whole
control sequence, only the first element, 𝑢(𝑡) is applied to the plant, the whole
procedure being repeated at time 𝑡 + 1 (where the present time will be 𝑡 + 1, the
control sequence with respect to which the optimization is performed is 𝑢(𝑡 +
1), … , 𝑢(𝑡 + 𝐻), and the output sequence will be 𝑦(𝑡 + 2), … , 𝑢(𝑡 + 𝐻 + 1)) and so
on.
𝑦(1) 𝑢(0)
𝑌=[ ⋮ ] and 𝑈=[ ⋮ ].
𝑦(𝐻) 𝑢(𝐻 − 1)
𝐽𝑅𝐻 = 𝑌 𝑇 𝑌 + 𝑅𝑈 𝑇 𝑈.
Using the state model, the vector of future output samples, 𝑌, can be expressed as a
function of the vector of future control samples, 𝑈, and of the initial state, thereby
resulting in a function of 𝑈. In order to follow this procedure, iterate the state model
to compute future values of the state, yielding (remember that time 0 actually
corresponds to present time 𝑡; 𝑏 is used instead of 𝐵 to emphasize that 𝑢 is scalar) the
following predictive models for the state
or, in general,
𝑦(1) 𝐶𝑏 0 0 ⋯ 0 𝑢(0) 𝐶𝐴
𝑦(2) 𝐶𝐴𝑏 𝐶𝑏 0 ⋯ 0 𝑢(1) 𝐶𝐴2
𝑦(3) = 𝐶𝐴2 𝑏 𝐶𝐴𝑏 𝐶𝑏 ⋱ ⋮ 𝑢(2) + 𝐶𝐴3 𝑥(0).
⋮ ⋮ ⋱ ⋱ ⋱ 0 ⋮ ⋮
[𝑦(𝐻)] [ 𝐶𝐴𝐻−1
𝑏 𝐶𝐴𝐻−2
𝑏 ⋯ 𝐶𝐴𝑏 ]
𝐶𝑏 [𝑢(𝐻 − 1)] [ 𝐶𝐴 𝐻]
With these definitions, the pencil of output predictive models can be written as
𝑌 = 𝑊𝑈 + Πx(0).
𝐽𝑅𝐻 = 𝑌 𝑇 𝑌 + 𝑅𝑈 𝑇 𝑈,
or
𝑀 ≔ 𝑊 𝑇 𝑊 + 𝑅𝐼.
∇𝑈 𝐽𝑅𝐻 = 2𝑈 𝑇 𝑀 + 2𝑥 𝑇 (0)Π𝑇 𝑊.
Equating the gradient to zero, yields the following equation, satisfied by the optimal
control sequence
𝑈 𝑇 𝑀 + 𝑥 𝑇 (0)Π𝑇 𝑊 = 0.
Transposing, and solving this equation with respect to 𝑈, yields the optimal control
sequence
According to the receding horizon strategy, only the first entry of this sequence is
applied to the plant. Recalling that 𝑥(0), actually, corresponds to 𝑥(𝑡), and that 𝑢(0)
corresponds to 𝑢(𝑡), the optimal receding horizon control is given by the state
feedback
𝐾𝑅𝐻 = 𝑒1 𝑀−1 𝑊 𝑇 Π,
where
𝑒1 = [1 0 ⋯ 0]
Work to perform
For this plant, develop a Matlab script to answer the following questions:
In the above study, take 𝑄 = 1 and select values of 𝑅 that you consider that yield
representative results. Try with small and large values of 𝑅 (you must decide the actual
values. The maximum range of 𝐻 must also be decided by you.
P3 – Model identification
Motivation
MPC requires a dynamic model of the plant to control, in order to perform state
predictions. Although it is possible to build a very good model of the TCLab system
from first principles, via the laws of heat transfer and radiation, this may not be the
case for other systems, due to the high complexity of the system. Therefore, we
instead perform system identification, where a plant model is estimated from
experimental data.
Furthermore, in order to simplify the resulting MPC optimization problem, the model
to identify will be linear. This model will also be used later for the design of a Kalman
filter for state estimation, and for building a simulation environment that supports the
design of the controller and observer. Since we will only consider a single heater, the
identified model will be single-input/single-output (SISO).
Objectives
The objective for this part is the identification of a linear model for the TCLab system
from experimental data, considering only one heater and one temperature sensor.
Furthermore, we will validate the model using data obtained from a separate
experiment.
We assume that the system can be described by a nonlinear difference equation such
as
𝑦(𝑘) = 𝑔(𝑥(𝑘)),
For the TCLab system, the input variables are the two heater controls, and the output
variables are the measured temperatures of the two sensors. For this work, however,
we will only consider the control of one heater, in which case 𝑚 = 𝑝 = 1. We assume
that we do not know anything about the number of states 𝑛, their dynamics 𝑓, or
output function 𝑔. The system is essentially a “black box”, where we only have access
to its inputs and outputs. We must therefore obtain a model by exciting the system
sufficiently and appropriately.
Assume we want to control the output of the system around a constant equilibrium
value 𝑦̅, to which corresponds the equilibrium state 𝑥̅ , i.e.
𝑦̅ = 𝑔(𝑥̅ ),
and for which there exists a constant control 𝑢̅ such that the system is kept in
equilibrium, i.e.
𝑥̅ = 𝑓(𝑥̅ , 𝑢̅).
We want to find a linearised model of 𝑓 that holds well in the neighbourhood of 𝑥̅ and
𝑢̅, in order to design a controller that can regulate the plant output at 𝑦̅.
To this end, we can perform an experiment where the system is operated in this
neighbourhood, and decompose the commanded inputs 𝑢 and the measured outputs
𝑦 into the two components
𝑦 = 𝑦̅ + Δ𝑦,
where Δ𝑦 and Δ𝑢 are the deviations/increments from the equilibrium. We can then
estimate the 𝐴, 𝐵, 𝐶 and 𝐾𝑒 matrices that best explain the observed experimental
observations of the Δ𝑦 and Δ𝑢 dynamics with the discrete linear time-invariant model
𝑥 = 𝑥̅ + ∆𝑥,
The controller will therefore consider this linear model for the incremental variables,
where we can achieve the desired output 𝑦̅ by driving Δ𝑦 to the origin, via a control
increment ∆𝑢 about 𝑢̅.
Work to perform
The work to perform in this task consists of a series of experiments in which the plant
is operated in open-loop, with the control consisting of increments around the
equilibrium point.
0) Follow the TCLab installation and setup instructions from the guide available in
the Fénix course page. In the present work we will not use Simulink, so you may
skip the instructions related to that.
1) The MATLAB script TCLab_openloop.m allows for performing experiments in
open-loop, with a pre-defined control applied to the heaters. Modify this script
to use a sampling time of 5 seconds; this time interval will be the sampling time
of the discrete-time model to identify and of the MPC. Modify the input profile
of the first heater such that:
For the best results, try to minimize energy changes with the environment, e.g.
touching and moving the board, opening/closing windows, changing the room
air conditioning, etc.
2) Perform a second experiment that is shorter in duration (< 1000 seconds), but
which excites the system significantly more, with input changes of higher
amplitude and without necessarily waiting for the system to reach equilibrium
Motivation
Given the model previously identified, we will now design a model predictive
controller for the system and test it in simulation with the same model, albeit with
some disturbances. We will initially consider an unconstrained MPC regulator, and
then incrementally complexify the controller to track a non-zero reference and include
control and output constraints. Given that the MPC requires knowledge of the state,
we will also design a state observer, namely a Kalman filter.
Objectives
The objectives of this part are to design a model predictive controller and a Kalman
filter, supported by a simulation environment, before applying them to the real system
in the next part.
Given that the optimization problem must be solved in real-time, the computation
time of the optimizer is the main drawback of this control method. In this case, since
the optimization problems considered are quadratic programs, we will use the
MATLAB quadprog function, which implements an optimization algorithm specific for
that class of problem and is therefore more efficient at solving it than the previously
used fmincon (that must be used, for example, when the cost is not quadratic or the
dynamics nonlinear). Naturally, the bigger the problem – number of states, controls,
length of the prediction horizon, constraints – the higher the optimizer computational
load.
The quadprog function allows to minimize a quadratic program with the form
1 𝑇
minimize 𝑧 𝐹𝑧 + 𝑓 𝑇 𝑧
𝑧 2
subject to 𝐴𝑖𝑛𝑒𝑞 𝑥 ≤ 𝑏𝑖𝑛𝑒𝑞
𝐴𝑒𝑞 𝑧 = 𝑏𝑒𝑞
𝑙𝑏 ≤ 𝑧 ≤ 𝑢𝑏
z = quadprog(F,f,Aineq,bineq,Aeq,beq,lb,ub),
where any unnecessary inputs may be replaced with an empty variable. To use this
function, we must therefore first write our problem in matrix form and determine the
parameters 𝐹, 𝑓, 𝐴𝑖𝑛𝑒𝑞 , 𝑏𝑖𝑛𝑒𝑞 , 𝐴𝑒𝑞 , 𝑏𝑒𝑞 , 𝑙𝑏 and 𝑢𝑏 that correspond to the specific
problem at hand.
Without loss of generality, we assume that we are at time 𝑘 = 0, and similarly to that
presented in P2 we consider the minimization of the cost function
𝐻−1
𝑦̂(𝑖) = 𝐶𝑥̂(𝑖).
The ‘hat’ notation in 𝑥̂, 𝑢̂ and 𝑦̂ denotes that these are virtual variables, in the sense
that they are predictions made within the MPC, and thus distinguish them from the
real variables 𝑥, 𝑢 and 𝑦 that have been or will be applied. From the minimization
yields the control profile (𝑢̂(0), … , 𝑢̂(𝐻 − 1)), out of which we only apply the first, i.e.
𝑢(0) = 𝑢̂(0),
Similarly to what was previously done in P2, we can define 𝑧 as the concatenation of all
the control variables
𝐹 = 2𝑀, 𝑓 = 2𝑥 𝑇 (0)Π𝑇 𝑊,
where 𝑥(0) is the initial state, and 𝑀, Π and 𝑊 are as previously defined in P2. If we
do not want to model any constraints, we can simply call the function with
U = quadprog(F,f),
and the solution will be the same as that obtained with the method shown in P2.
This is known as the dense formulation, since only the control variables are collocated
as optimization variables, yielding a problem that is smaller in size but where the cost
and constraint matrices are denser. This is in contrast to the sparse formulation, where
both the state and control variables are collocated, defining instead the optimization
vector
𝑋
𝑧 = [ ],
𝑈
where
𝑥̂(0)
𝑋 = [ ⋮ ].
𝑥̂(𝐻)
0 ⋯ ⋯ 0 0 ⋯ 0 𝐼
𝐴 ⋯ 0 ⋮ 𝐵 ⋯ 0 0
𝐴̃ ∶= [ ], 𝐵̃ ∶= [ ] , 𝐸 ∶= [ ],
⋮ ⋱ ⋮ ⋮ ⋮ ⋱ ⋮ ⋮
0 ⋯ 𝐴 0 0 ⋯ 𝐵 0
𝑋 = 𝐴̃𝑋 + 𝐵̃ 𝑈 + 𝐸𝑥(0).
𝐴𝑒𝑞 𝑧 = 𝑏𝑒𝑞 ,
where
0 ⋯ ⋯ 0 𝑥̂(0)
⋮ 𝑄 ⋯ 0 𝑥̂(1)
𝐽(𝑋, 𝑈) = [𝑥̂ 𝑇 (0) 𝑥̂ 𝑇 (1) ⋯ 𝑥̂ 𝑇 (𝐻)] [ ][ ]
⋮ ⋮ ⋱ ⋮ ⋮
0 0 ⋯ 𝑄 𝑥̂(𝐻)
𝑅 ⋯ 0 𝑢̂(0)
+ [𝑢̂ 𝑇 (0) 𝑇 (𝐻
⋯ 𝑢̂ − 1)] [ ⋮ ⋱ ⋮][ ⋮ ].
0 ⋯ 𝑅 𝑢̂(𝐻 − 1)
0 ⋯ ⋯ 0
⋮ 𝑄 ⋯ 0 𝑅 ⋯ 0
𝑄̃ ∶= [ ], 𝑅̃ ∶= [ ⋮ ⋱ ⋮]
⋮ ⋮ ⋱ ⋮
0 ⋯ 𝑅
0 0 ⋯ 𝑄
𝑄̃ 0
𝐹 = 2[ ],
0 𝑅̃
and 𝑓 = 0.
The sparse and dense formulations are theoretically equivalent, however, depending
on the problem and on the optimization algorithm, one may be computationally faster
to solve than the other. Furthermore, it is easier in the sparse formulation to define
𝐺𝑦̂(𝑖) ≤ 𝑔, 𝑖 = 1, … , 𝐻,
for some matrix 𝐺 and vector 𝑔, we can write the constraints in matrix form as
𝐺 ⋯ 0 𝑦̂(1) 𝑔
[⋮ ⋱ ⋮ ] [ ⋮ ] ≤ [ ⋮ ].
⏟0 ⋯ 𝐺 𝑦̂(𝐻) 𝑔
⏟
∶=𝐺̃ ∶=𝑔̃
For the sparse formulation, we can compute the output vector 𝑌 from 𝑋 with
0 ⋯ 0 𝑥̂(0)
𝑦̂(1)
𝐶 ⋯ 0 𝑥̂(1)
[ ⋮ ]=[ ][ ],
⋮ ⋱ ⋮ ⋮
𝑦̂(𝐻)
⏟0 ⋯ 𝐶 𝑥̂(𝐻)
∶=𝐶̃
𝐺̃ 𝐶̃ 𝑋 ≤ 𝑔̃.
We can therefore implement these constraints with the function quadprog by defining
For the dense formulation, the output vector is instead computed from 𝑈 and 𝑥(0)
with
𝑌 = 𝑊𝑈 + Π𝑥(0).
The constraint
𝐺𝑦̂(𝑖) ≤ 𝑔, 𝑖 = 1, … , 𝐻
𝐺𝑦̂(𝑖) ≤ 𝑔 + 𝜂𝑖 , 𝑖 = 1, … , 𝐻,
𝑋
𝑧 = [𝑈],
𝜂
where 𝜂 = [𝜂1𝑇 , … , 𝜂𝐻
𝑇 ]𝑇
. The quadprog cost function parameters 𝐹 and 𝑓 as well as the
constraint parameters 𝐴𝑒𝑞 , 𝑏𝑒𝑞 , 𝐴𝑖𝑛𝑒𝑞 , 𝑏𝑖𝑛𝑒𝑞 , 𝑙𝑏 and 𝑢𝑏 must be changed accordingly to
accommodate for the new optimization variables. In particular, the 𝑙𝑏 parameter must
ensure 𝜂𝑖 ≥ 0, 𝑖 = 1, … , 𝐻, in order to ensure the constraint can only be relaxed, not
tightened.
MPC regulator
Let us again consider the single-heater TCLab system. We want to design a model
predictive controller that drives the incremental output Δ𝑦 to 0, such that the absolute
output 𝑦 is driven to the equilibrium 𝑦̅. Hence, assuming without loss of generality that
we are at time 𝑘 = 0, we define the following quadratic cost function
𝐻−1
Notice that we do not include the disturbance 𝑒 in the MPC model, since that is a
random variable that is obviously unknown a priori. Nonetheless, the MPC will reject
this disturbance by operating in closed loop, driving the system sufficiently close to the
origin despite it.
Let us now assume we instead want to control the output to a reference 𝑟 = 𝑦̅ + Δ𝑟.
We can design a model predictive controller that tracks the incremental reference Δ𝑟,
rather than regulating Δ𝑦 to the origin. One possibility to achieve this is by penalizing
the reference tracking error in the cost function
𝐻−1
This formulation, however, will have a steady-state tracking error unless the system
has integral action, due to the fact that tracking Δ𝑟 requires a non-zero control
increment Δ𝑢, and therefore the minimization of the tracking error is adversarial to
that of the control energy, and the result is something in-between. To overcome this,
we can determine the control increment Δ𝑢̅ that maintains the output Δ𝑦 at the value
Δ𝑟, and instead formulate the cost function
𝐻−1
in which case the control will not be penalized for maintaining Δ𝑟. The control Δ𝑢̅ can
be determined by solving the steady-state equations
Δ𝑟 = 𝐶Δ𝑥̅ ,
We can obtain an equivalent result using the same MPC regulator as before, by first
applying the change of variables
thus considering δ𝑥̂ and δ𝑢̂ as the new optimization variables instead (or δ𝑢̂ only in
case of the dense formulation), which are subject to the model
δ𝑦̂(𝑖) = 𝐶δ𝑥̂(𝑖).
Naturally, if control and output constraints are present, they must also consider this
change of variables.
Zero-offset tracking
The previous tracking formulation only guarantees no steady-state error if the real
plant matches the MPC model. In practice, however, this will not be the case, e.g. due
to changes in the ambient temperature. We can overcome this problem by admitting
that the system is affected by an input disturbance 𝑑, which results in the dynamics
Δ𝑟 = 𝐶Δ𝑥̅ ,
𝑑̅ = 𝑑̂ .
Once we have computed Δ𝑥̅ and Δ𝑢̅, we can simply use the same change-of-variable
strategy as described in the previous section.
Kalman filter
The model predictive controller requires an initial condition on the state Δ𝑥 in order to
perform the predictions, whereas we only have measurements from the output 𝑦.
Furthermore, it requires an estimate of the disturbance 𝑑. Therefore, we need to
implement a state observer, which uses the output measurements, command inputs,
and a model of the system to estimate the state, which, given sufficient time, must
converge to the real state. For this purpose, we will use a Kalman filter, which designs
a linear state observer in an optimal way.
which includes the input disturbance 𝑑 introduced in the previous section. The
Gaussian disturbances 𝑒 and 𝐾𝑒 𝑒 have the known covariances
𝑇
𝐸 [(𝐾𝑒 𝑒(𝑘))(𝐾𝑒 𝑒(𝑘)) ] = 𝑄𝐸 ,
𝐸[𝑒(𝑘)𝑇 𝑒(𝑘)] = 𝑅𝐸 .
Although in this case we have identified these covariances from experimental data,
typically this is very hard to do for more complex systems, and the covariances only
interpreted as tuning knobs for the filter, as discussed later.
Δ𝑥(𝑘 + 1) 𝐴 𝐵 Δ𝑥(𝑘) 𝐵
[ ]=[ ][ ] + [ ] Δ𝑢(𝑘),
𝑑(𝑘 + 1) 0 1 𝑑(𝑘) 0
Δ𝑥(𝑘)
𝑦(𝑘) = [𝐶 0] [ ]
𝑑(𝑘)
Δ𝑥
𝑥𝑑 = [ ],
𝑑
𝑥𝑑 (𝑘 + 1) = 𝐴𝑑 𝑥𝑑 (𝑘) + 𝐵𝑑 Δ𝑢(𝑘),
𝑦(𝑘) = 𝐶𝑑 𝑥𝑑 (𝑘),
where
𝐴 𝐵 𝐵
𝐴𝑑 = [ ], 𝐵𝑑 = [ ], 𝐶𝑑 = [𝐶 0].
0 1 0
𝑄𝐸 0
𝑄𝐸 𝑑 = [ ],
0 𝛿𝐸
The estimation is made in two steps: prediction and correction. At the beginning of the
sampling interval 𝑘, we first propagate the previous state estimate with the model and
the previously commanded control action, i.e. we compute
which is the prediction step. Afterwards, we correct this estimate using the latest
measurement 𝑦(𝑘), thus yielding the final estimate 𝑥̂(𝑘)
where 𝐿 is the observer gain. Note that here we use the ‘hat’ notation to denote an
estimate; this must not be confused with the ‘hat’ notation in the context of MPC,
which instead denotes a prediction.
The Kalman filter selects the observer gain that minimizes the variance of the
estimation error
We can compute the Kalman gain 𝐿 with the MATLAB dlqe function, with the syntax
L = dlqe(Ad,eye(n+1),Cd,QEd,RE),
Work to perform
0) In this part you will use the TCLab_simulation.m MATLAB script provided, which
simulates the TCLab system with the model identified in the previous part and
an open-loop control profile, starting at the equilibrium temperature for 𝑢 = 0
(ambient temperature). Run the script to test that it runs properly. You do not
need to put anything in the report for this question.
Tip: During design, it can be advantageous to momentarily disable the random
disturbance 𝑒 by setting the standard deviation variable e_std to 0, in order to
test the controller and observer in ideal conditions.
1) Develop a MATLAB function named mpc_solve, in order to solve the
unconstrained MPC regulator for a given initial condition using quadprog, using
either the sparse or dense formulations. Note that a lot of the code developed
One common rule of thumb when designing a MPC, is that the computation time is
less than 10% of the sampling time, since otherwise it introduces a non-negligible input
delay that may negatively affect performance. Although, most likely, this is not a
problem in this case, you must ensure that this rule is satisfied for your controller and,
if necessary, reduce the prediction horizon, choose a lower-order state dimension, or
select the dense/sparse formulation, in order to reduce the computational load.
Motivation
In P3, a linearised model for the TCLab system was identified and, in P4, a model
predictive controller and Kalman filter were designed for it. The controller obtained by
coupling these two elements will now be applied to the real thermal plant.
Objectives
The objective is to apply the previously developed controller and observer to the real
system.
In this section we will work on the script TCLab_closedloop, which is essentially a copy
of TCLab_openloop, with some placeholder suggestions of where to place the Kalman
filter and MPC. It should be straightforward to adapt your code from TCLab_simulation
to this script.
References
[1] Rawlings, James Blake, David Q. Mayne, and Moritz Diehl. Model predictive control:
theory, computation, and design. Vol. 2. Madison, WI: Nob Hill Publishing, 2017.
(Available at https://sites.engineering.ucsb.edu/~jbraw/mpc/)
[2] A. Botelho, B. Parreira, P. N. Rosa and J. M. Lemos. Predictive Control for Spacecraft
Rendezvous. Springer, 2021.