Manual de AMPL
Manual de AMPL
0 — Reference Manual
Johan Åkesson
1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2. Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3. Model Predictive Control – Background . . . . . . . . . . . . . 6
4. Linear Model Predictive Control . . . . . . . . . . . . . . . . . 7
4.1 Receding horizon control . . . . . . . . . . . . . . . . . . . . . 7
4.2 Model assumptions . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.3 An optimal control problem . . . . . . . . . . . . . . . . . . . . 8
4.4 Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.5 State estimation . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.6 Error-free tracking . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.7 Blocking factors . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.8 Linear properties of the MPC controller . . . . . . . . . . . . 14
5. Quadratic Programming Algorithms . . . . . . . . . . . . . . . 15
6. MPCtools — Overview . . . . . . . . . . . . . . . . . . . . . . . . 16
6.1 A quadratic programming solver . . . . . . . . . . . . . . . . . 17
6.2 MPCtools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
7. Case Studies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
7.1 The quadruple tank . . . . . . . . . . . . . . . . . . . . . . . . 20
7.2 The helicopter process . . . . . . . . . . . . . . . . . . . . . . . 23
8. MPCTools Command Reference . . . . . . . . . . . . . . . . . . 26
MPCinit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
MPCOptimizeSol . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
MPCSim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
MPCController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
MPCfrsp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
qp_as . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
qp_ip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
getfeasible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
9. References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3
4
1. Introduction
This report describes a set of Matlab functions, referred to as MPCtools. MPC-
tools implements an MPC controller for use with Matlab/Simulink, with the
following key features:
The main purpose of MPCtools is to enable users to simulate and analyse control
systems which includes MPC controllers, wihout having to get into the implemen-
tational details. In the development of MPCtools, ease of use has been considered
more important than adding additional features. A limitation of MPCtools is that
no support for stabilizing schemes is given.
MPCtools are primarily intended for research and teaching but is free to use for
all purposes. The author accepts no responsibility for the consequences of using
MPCtools.
2. Getting Started
MPCtools requires no installation or compilation, but it is convenient to add
the directory containing the MPCtools functions to the Matlab path. The cur-
rent distribution contains two application examples, demonstrating the key fea-
tures of MPCtools. Both examples are described in detail in this report, and the
corresponding scripts for setting up the problem and producing plots showing
simulation results are included in the MPCtools distribution.
The following steps shows how to set up the path and how to run the example
scripts.
1 Start Matlab
2 To add the directory containing MPCtools to the Matlab path, enter
>> addpath(’/usr/matlab/MPCtools-1.0’)
>> cd /usr/matlab/MPCtools-1.0/QuadTank
>> Simulate_QuadTank_MPC
5
5 Run the script Simulate_QT_Simulink_MPC by entering
>> Simulate_QT_Simulink_MPC
>> cd /usr/matlab/MPCtools-1.0/HelicopterII
>> Simulate_Helicopter_MPC
6
Constrained Quadratic Programming (LICQP) problem, which is a convex opti-
mization problem, and efficient solution algorithms exist. In particular, existence
of a unique global minimum is guaranteed. For non-linear system, there exist
MPC formulations that guarantee stability under mild conditions. However, the
resulting optimization problem is, in general, non-convex and usually no guar-
antee of finding a global minimum exits. Non-linear MPC remains a very active
field of research and recent results have shown that optimality is not a neces-
sary condition for stability, see [Scokaert et al., 1999]. These promising results
show that many non-linear MPC schemes may be stabilizing although finding
the global minimum is difficult.
In this report, Matlab tools for a standard formulation of linear MPC will be
presented. State estimation, error-free tracking and stability are important is-
sues that will be covered in the following. The aim of developing the MPC tools
have been to simulate, in detail, the behavior of an MPC controller. A detailed
analysis of controller behavior also requires understanding of the algorithm used
to solve the LICQP problem. Accordingly, the tools include implementations of
common LICQP algorithms for this purpose.
When the solution of the optimal control problem has been obtained, the value of
the first first control variable in the optimal trajectory, u( kp k), is applied to the
process. The rest of the predicted control variable trajectory is discarded, and at
the next sampling interval the entire procedure is repeated.
7
r( k)
ẑ( k)
z( k)
û( k)
u( k)
k k + Hu k + Hp t
Figure 1 The idea of MPC. Here r ( k) is the set point trajectory, z( k) represents the
controlled output and u( k) the control signal.
x( k + 1) = Ax( k) + Bu( k)
y( k) = Cy x( k)
(1)
z( k) = Cz x( k) + D zu( k)
zc ( k) = Cc x( k) + Dc u( k)
∆ umin ≤ ∆ u( k) ≤ ∆ umax
umin ≤ u( k) ≤ umax (2)
zmin ≤ zc ( k) ≤ zmax
8
the MPC algorithm. Consider the following quadratic cost function:
H p + Hw − 1 H u −1
where ẑ( k + ip k) are the predicted controlled outputs at time k and ∆ û( k + ip k) are
the predicted control increments. The matrices Q ≥ 0 and R > 0 are weighting
matrices, which are assumed to be constant over the prediction horizon. The
length of the prediction horizon is H p, and the first sample to be included in the
horizon is Hw. Hw may be used to shift the control horizon, but in the following
presentation we will assume that Hw = 0. The control horizon is given by Hu .
In the cost function (3) ∆ u( k) is penalized rather than u( k), which is common
in LQ control. The reason for this is that for a non-zero set point, r( k), the
corresponding steady state control signal u( k) is usually also non-zero. By avoid
penalizing u( k), this conflict is avoided. A different method that has been used
is to introduce a set point also for the control variable, ru ( k), and to penalize
deviations of u( k) from ur . This approach may be implemented in the above
formulation by choosing Cz = 0 and D z = I, and thereby let u( k) be part of the
controlled variables.
2
J ( k) = qZ ( k) − T ( k)qQ + q∆U qR2
where
ẑ( kp k) r( kp k)
.. ..
Z ( k) =
.
T ( k) =
.
ẑ( k + H p − 1p k) r( k + H p − 1p k)
Q 0 ... 0
∆ u( kp k)
0 Q ... 0
..
∆U ( k) = . Q= .. .. . . ..
. . . .
∆ u( k + Hu − 1p k)
0 0 ... Q
0 ... 0
R
0 R ... 0
R= .. .. . . ..
. . . .
0 0 ... R
Z ( k) = Ψ x( k) + Γ u( k − 1) + Θ∆U ( k) (4)
9
where
Cz
Cz A
Cz A2
Ψ =
..
.
Cz A H p −1
Dz
Cz B + D z
Cz AB + Cz B + D z
Γ =
..
.
P H p −2 i
Cz i=0 A B + D z
Dz 0 ⋅⋅⋅ 0
Cz B + D z Dz
.. ..
. .
Cz AB + Cz B + D z
.. ..
Θ = . . 0
C P Hu − 2 A i B + D
z i=0 z ⋅⋅⋅ Dz
. ..
.. ..
.
.
P H p −2 i P H p − Hu−1
Cz i=0 A B + D z ⋅ ⋅ ⋅ Cz i=0 Ai B + D z
Also, let
E ( k) = T ( k) − Ψ x( k) − Γ u( k − 1).
This quantity could be interpreted as the free response of the system, if all the
decision variables at t = k, ∆U ( k), were set to zero. Inserting the prediction
expressions into the cost function (3) we obtain
J ( k) = ∆U T H ∆U − ∆U T G + E T QE (5)
where
G = 2Θ T QE ( k)
H = ΘTQ Θ + R
The problem of minimizing the the cost function (5) is a quadratic programming
(QP) problem. If H is positive definite, the problem is convex, and the solu-
tion may be written on closed form. Positive definiteness of H follows from the
assumption that Q ≥ 0 and R > 0. The solution is given by
1 −1
∆U = H G.
2
Notice that the matrix H −1 does not depend on k, and may be pre-calculated. In
fact, the controller is linear, and may be calculated off-line. This will be discussed
in detail in Section 4.8.
10
4.4 Constraints
Let us now introduce constraints on the constrained and control variables, zc
and u. In general, linear constraints may be expressed as:
W ∆U ( k) ≤ w (6)
FU ( k) ≤ f (7)
GZ c ( k) ≤ (8)
(9)
This formulation allows for very general constraints, but in the following, only
the constraints specified by (2) will be considered. Using (2), we obtain
1 0 ⋅⋅⋅ 0 1 0 ⋅⋅⋅ 0
−1 −1
.. ..
0 1 . 0 1 .
−1 −1
W=F= G=
. .. . ..
.. . 0 .. . 0
1 1
0 ⋅⋅⋅ 0 −1 0 ⋅ ⋅ ⋅ 0 −1
∆ umax umax zmax
−∆ u −u −z
min min min
.. .
..
..
w=
. f
=
=
.
∆ umax umax zmax
−∆ umin −umin − zmin
Notice that W and F may not be of the same size as G, though the structure is
the same. Since U ( k) and Z c ( k) are not explicitly included in the optimization
problem, we rewrite the above constraints in terms of ∆U ( k). This gives
−F 1 u( k − 1) + f
F
G Θ c ∆U ≤ − G (Ψ c x( k) + Γ c u( k − 1)) + (10)
W w
where
1 0 ⋅⋅⋅ 0 1
−1 −1
..
1
1 1 .
= −1 −1 −1
F F1 =
..
. ..
..
. 0 .
1 1 1
−1 ⋅ ⋅ ⋅ −1 −1
11
present estimation of the state vector, and should thus be evaluated at each
sample.
The optimization problem can now be rewritten using (3) and (10)
min J ( k) = ∆U T H ∆U − ∆U T G + E T QE
subject to Ω∆U ≤ ω
The problem is still recognized as a quadratic programming problem, but now
with linear inequality constraints. The problem is convex, but due to the con-
straints, it is not possible to write the solution on closed form. Rather iterative
algorithms have to be employed. This issue will be discussed further in Section
5
A stabilizing feedback control law may then be calculated based on the extended
model. The integrator state is implemented in the controller, and used for feed-
back together with the true or estimated states. From the definition of the ex-
tended system model, it is clear that in steady state, z = r. This approach does
12
not work so well for MPC controllers. In particular, it is not clear how the in-
tegral state should be introduced in the cost function in order for the integral
action to work properly.
A different approach to achieve integral action is to use a disturbance observer.
In summary, the extended system model
x( k + 1) 0
A B x( k) B
v ( k + 1 ) 0
= I 0 va ( k) + 0 u(t)
a
d( k + 1) 0 0 I d( k) 0
h ih iT
z( k) = yz( k) = Cz 0 0 x( k)T va ( k) T
d( k) T
h ih iT
ya ( k) = Ca I 0 x( k)T va ( k)T d( k)T
is used. It is assumed that the number of controlled outputs, z, equals the num-
ber of inputs, u. Additional outputs, if any, are denoted ya . Also, the controlled
variables are assumed to be included in the set of measured variables. A detailed
treatment of this method is given in [Åkesson and Hagander, 2003].
13
4.8 Linear properties of the MPC controller
The behavior of the MPC controller is intrinsically nonlinear, since constraints on
state and control variables are taken into account. However, if no constraints are
present in the problem formulation, the controller is linear. Also, the controller
behaves linearly during operation when no constraints are active. In the first
case, the control law, could (and should) be calculated off-line, whereas in the
second case, the optimization procedure must be done each sample. There are
however, methods for avoiding on-line solution of the optimization problem. Using
the observation that the MPC control law is piecewise linear in the states, it is
possible to calculate, off-line, all possible control laws. The on-line optimization
problem is then transformed into a search problem, where the objective is to
find the appropriate partition in the state space, identifying the corresponding
control law. This approach is described in [Bemporad et al., 2002].
We will now analyze the linear properties of the MPC controller. The analysis
is valid for the case when no constraints are present or the controller operates
so that no constraints are active. In this case, the minimizing solution of the
quadratic programming problem is
∆U ( k) = (Θ T Q Θ + R )−1 Θ T QE ( k)
I r( k)
.
= (Θ T Q Θ + R )−1 Θ T Q .
. −Γ −Ψ u( k − 1)
I x̂( k)
r( k)
= K̄ s u( k − 1)
x̂( k)
Now, since only the first of the predicted control signals are applied we can write
the control law as
h iT
∆ u( kp k) = ∆ u( k) = K s r T ( k) uT ( k − 1) x̂T ( k)
h ih iT
= K sr K su K sx r T ( k) uT ( k − 1) x̂ T ( k)
where K s is given by the first m rows of K̄ s . This control law is linear, and
the constant gain matrix K s may be calculated off-line. The block diagram of
the controller may now be drawn as in Figure 2. In this figure, the transfer
function (matrix) of the plant is given by P( z), and Hu ( z) and H y( z) represents
the observer. This block diagram is readily converted into a feedback system on
standard form shown in Figure 3, with
P( z) = Cy( zI − A)−1 B
F ( z) = K sr
H ( z) = − K sx H y( z)
−1
1
z z
K ( z) = I− K su − K sx Hu ( z)
z−1 z−1 z−1
H y( z) = ( zI − A + K Cy)−1 K
Hu ( z) = ( zI − A + K Cy)−1 B
14
r( k) ∆ u( k) u( k) y( k)
z
K sr z−1
I P( z)
−
K su z−1 I
K sx Hu ( z)
− K sx H y( z)
r( k) u( k) y( k)
F ( z) K ( z) P( z)
−
H ( z)
It is now straight forward to apply standard linear analysis methods. For exam-
ple, the poles and zeros of the closed loop system may be calculated, as well as
the sensitivity of the system.
min J ( k) = ∆U T H ∆U − ∆U T G + E T QE
subject to Ω∆U ≤ ω .
This problem has several nice features. For example, the objective function is
convex, since it is quadratic with positive definite Hessian. Also, the constraints
are also convex. Given these conditions, it is a well known result that a local
minimum, if it exists, is also a global minimum. (See for example Theorem 4.3.8
in [Bazaraa et al., 1993].) When designing numerical algorithms, this property
is of course very valuable, since we know in advance that if we find a minimum,
it is indeed a global minimum.
There exist several algorithms for constrained optimization, see for example
[Fletcher, 1987]. For quadratic programming problem the two most common ap-
proaches are primal-dual internal point methods and active set methods [Ma-
ciejowski, 2002].
15
The active set algorithm assumes an initial point in the decision variable space
that fulfills the constraints. The active set is defined as the set of all active con-
straints at this point. A constraint is said to be active if a particular point in
the search space is at the boundary of the feasible region defined by the con-
straint. In the case of linear constraints, these boundary surfaces are given by
hyper-planes. In each iteration step, a quadratic programming problem with lin-
ear equality constraints (namely those in the active set) is solved. The solution
of this problem may be written on closed form. Possibly this solution leads to
the introduction of a new constraint into the active set. By calculating the La-
grange multipliers for the problem at each iteration, it is possible to conclude if
a constraint may be relaxed, that is, removed from the active set. The algorithm
terminates when the gradient of the associated Lagrange function is identically
zero, and all Lagrange multipliers are positive.
One problem remains to deal with; the feasible initial point. In order to start the
active set algorithm, we need a feasible solution, that is, we would like to find
a solution that fulfills the constraints Ω∆U ≤ ω . Of course, such a solution is
not likely to be unique. Several methods exist for obtaining the desired solution.
For example, the problem may be cast as an LP problem, and solved by the
simplex algorithm. Another and possibly more attractive alternative is given in
[Fletcher, 1987]. This strategy employs an active set technique similar to the
one for solving the main quadratic programming problem. However, in this case,
the objective function is defined at each iteration as the sum of the violated
constraint functions.
Primal-dual interior point methods on the other hand, explores the Karush-
Kuhn-Tucker conditions explicitly. The name of this family of QP algorithms
stems from the fact that the primal and the dual problems are solved simultane-
ously. It is important to note however, that the term interior point refers to the
fact that the algorithm maintains a solution in the interior of the dual space.
In fact, the primal solution may not be feasible during the optimization run,
except at the optimal point. This constitutes an important difference between
active set methods and primal-dual interior point methods. Specifically, in the
former case it is possible to terminate the optimization algorithm prematurely
and still obtain a feasible, but sub-optimal, solution whereas in the latter case,
the algorithm may terminate only when the optimal solution is found.
6. MPCtools — Overview
In this section, Matlab tools implementing the algorithms described in the previ-
ous sections are presented. The main objective for implementing the MPC tools
has been to enable detailed study of the behavior of an MPC controller. In this
section, the functionality of the tools is described briefly, see Section 8 for a
detailed description.
16
6.1 A quadratic programming solver
Obviously, a quadratic programming solver is essential for the implementation
of the MPC controller. A solver of this type is available for example from the
Matlab Optimization Toolbox; quadprog. However, the aim of the development
of the Matlab tools has been to create a complete implementation of an MPC
controller. Since this also includes an algorithm to solve the QP problem, a solver
based on active sets as well as a primal-dual interior point QP solver have been
implemented. In addition, an algorithm for finding an initial feasible solution
for the active set solver has been implemented. The following Matlab functions
implement the necessary algorithms:
• getfeasible
This function obtains a feasible point given the constraints Ax ≤ b. The
algorithm is described in [Fletcher, 1987, p. 166].
• qp_as
This is an active set solver, based on [Fletcher, 1987, p. 240]. The algorithm
finds the solution of the optimization problem
1 T
min x Hx + fTx
2
s.t.
Ax ≤ b
The MPC controller described in the next section enables the user to explore
either quadratic programming method, as well as the Optimization Toolbox func-
tion quadprog.
6.2 MPCtools
The Matlab functions presented in this section implement an MPC controller
as described in Section 4. The tools enable the user to simulate a linear or
nonlinear plant model, with an MPC controller engaged. Most of the features
described in Section 4 are implemented, including constraint handling, observer
support, blocking factors and error-free tracking. Also, a basic tool for analyzing
the linear properties of the controller is supplied.
In summary, the same assumptions as in Section 4 are made for the Matlab
implementation of the MPC controller. We assume that a linear discrete time
model on the form (1), with linear inequality constraints (2) is available. Also,
we assume that the design variables Hw, H p, Hu and the blocking factors are
specified, as well as the weighting matrices Q, R and, if applicable, W and V for
the design of a Kalman filter.
17
An important goal in designing the MPC tools has been to enable the user to
experiment with different configurations of the MPC controller. Consequently,
the MPC tools support several modes of operation:
Both state and output feedback is supported. Also, two different strategies for
achieving error-free tracking are provided; explicit integrators and an observer
based solution. Both methods are described in Section 4.
The MPC tools are implemented in five functions:
• MPCInit
Typically, MPCInit is used to initialize the MPC controller. Most of the ma-
trices needed to solve the optimization problem may be calculated off-line in
advance, for example the Hessian of the QP problem. MPCInit implements
pre-processing of matrices, which drastically improves the performance of
the controller.
• MPCSim
MPCSim simulates a linear plant model controlled by the MPC controller.
Reference trajectories as well as input disturbance trajectories can be sup-
plied. The function produces the state and control variable trajectories, as
well as the predicted trajectories for the controlled and control variables.
The latter feature enables the user to examine the predicted solutions ob-
tained at each sample, and explore the effect of different choices of predic-
tion horizons.
• MPCfrsp
As noted in Section 4.8, the MPC controller behaves linearly if no con-
straints are active. It is then interesting to use standard tools for analysis
of linear systems. The function MPCfrsp provides frequency response plots
for relevant transfer functions corresponding to, e.g., the closed loop system
and input and output sensitivity functions.
• MPCOptimizeSol
MPCOptimizeSol is a low level function used by other functions rather than
by the user. This function provides the control signal of the MPC controller
given the current state measurement (or estimation) and reference value.
• MPCController
Most real world plants are nonlinear. It is therefore of interest to investigate
the performance of the linear MPC controller applied to a nonlinear plant
model, if it is available. This functionality is provided by the simulation
environment Simulink for Matlab. The MPC controller has been adapted
to Simulink, and is implemented by a Matlab S-function. The S-function
18
Figure 4 A Simulink model where the MPC controller is used to control a nonlinear
plant.
Using the MPC tools described above, it is possible to simulate, at a high level
of detail, the behavior of an MPC controller. For example, the consequences of
various assumptions regarding measurements, integral action schemes, choice
of QP algorithm and prediction horizons are easily explored. Also, the use of
Simulink significantly increases the applicability of the tools, enabling the user
to simulate the behavior of the MPC controller when applied to nonlinear plants.
19
1 −γ1 1 −γ2
Tank 3 Tank 4
γ1 γ2
Pump 1
Pump 2
Tank 1 Tank 2
u1 u2
7. Case Studies
In this section, two cases studies are reported, illustrating the main functionality
of the MPC tools.
a1 p a3 p γ 1 k1
ẋ1 = − 2 x1 + 2 x3 + u1
A2 A1 A1
a2 p a4 p γ 2 k2
ẋ2 = − 2 x2 + 2 x4 + u2
A2 A2 A2
(12)
a3 p (1 − γ 2 ) k2
ẋ3 = − 2 x3 + u2
A3 A3
a4 p (1 − γ 1 ) k1
ẋ4 = − 2 x4 + u1
A4 A4
where the Ai :s and the ai :s represent the cross section area of the tanks and
the holes respectively. The parameters γ i :s determine the position of the valves
which control the flow rate to the upper and lower tanks respectively. The control
signals are given by the the ui :s. The objective is to control the levels of the two
20
Table 1 Parameter values of the Quadraple Tank
lower tanks, i.e. x1 and x2 . Numerical values of the parameters are given in Table
1.
An interesting feature of this multivariate process is that the linearized system
has an adjustable zero. By adjusting the parameters γ 1 and γ 2 it is possible
to obtain a zero with negative real part, or a non minimum phase zero with
positive real part. For the simulations, the valve positions were set to γ 1 = 0.30
and γ 2 = 0.30, yielding a non-minimum phase system.
The process is nonlinear, and in order to apply the MPC tools, a linearized model
must be derived. Introducing ∆ x = x − x0 , ∆ u = u − u0 and ∆ y = y − y0, we obtain
− T11
γ k
0 A4
A1 T3 0 1 1
A1 0
0 1 A4
0 γ 2 k2
0
− T 2 A T
2 4 ∆x + A 2
∆ ẋ = 1 (1−γ 2 ) k2 ∆ u
0 0 − T3 0 0 A3
(13)
(1−γ 1 ) k1
0 0 0 − T14 A4 0
" #
kc 0 0 0
∆y = ∆x
0 kc 0 0
where s
Ai 2xi0
Ti = .
ai
The stationary operating conditions, x0 and u0 , are given in Table 2. The par-
ticular choice of valve positions γ 1 = 0.25 and γ 2 = 0.35 yields a non-minimum
phase system. The measured outputs of the system are the levels of the lower
tanks, represented by a voltage ranging from 0 to 10 V. The objective of the
control system is to control, independently, the level of the lower tanks, while
preventing overflow in any of the four tanks. The maximum level of the tanks
is 20 cm, corresponding to 10 V. In the simulations, the tank level constraints
were set to 19.8 cm to ensure some safety margin. Also, the operation of the
pumps is limited to 0 to 10 V. We notice that the stationary level of the second
tank is close to the maximum level. The combination of a non-minimum phase
system and an operating point close to a constraint yields a quite challenging
control problem, where two of the main benefits of the MPC controller, namely
constraint handling and MIMO support, will be useful. The plant was discretized
using the sampling interval h = 3 s, resulting in a discrete time model used for
the MPC control design.
21
Table 2 Stationary values corresponding to γ 1 = 0.25 and γ 2 = 0.35.
Parameter Value
Hp 30
Hw 1
Hu 10
Ip Only every other sample was included
in the optimization problem.
Iu Every other control increment was as-
sumed to be constant.
Q diag(4, 1)
R diag(0.01, 0.01)
W diag(1, 1, 1, 1)/diag(1, 1, 1, 1, 1, 1)
V diag(0.01, 0.01)
Obviously, since all states are not measurable, an observer must be used. In
the first simulation, controller mode 2 was used: a Kalman filter was designed,
but no mechanism to ensure integral action was assumed. The result of the
simulation is shown in Figure 6, represented by the dashed curves. The dotted
curves represent the set points for the lower tanks. A step change in the set
point of level 1 of size 6 cm is applied at t = 60 s, while the set point of level
2 is held constant. As we can see, the controller achieves the correct set points,
and in particular, the level of tank 2 does not exceed 20 cm. At t = 600 s, a
unit step disturbance is applied to the input channel 2. As expected, the MPC
controller does not manage to achieve error-free tracking in the presence of load
disturbances.
In a second design, control mode 4 was assumed. In this case, the disturbance
observer described in [Åkesson and Hagander, 2003] was used to estimate the
state vector and the disturbance states. The response of the system is identical
to the previous case during the step response. However, the input disturbance is
now rejected.
22
12 10
10 8
h3 [cm]
h4 [cm]
8
6
6
4 4
2 2
0 200 400 600 800 1000 1200 0 200 400 600 800 1000 1200
16 20
h1 [cm] 14 19.5
h2 [cm]
12
19
10
8 18.5
6 18
0 200 400 600 800 1000 1200 0 200 400 600 800 1000 1200
4 8
3
6
u1 [V]
u2 [V]
2
4
1
0 2
0 200 400 600 800 1000 1200 0 200 400 600 800 1000 1200
t [s] t [s]
Control of the nonlinear model As noted above, the true Quadruple Tank
system is nonlinear. A more realistic scenario would then be to apply the MPC
controller designed above to the nonlinear plant model. The plant model was im-
plemented in Simulink, and an S-function block representing the MPC controller
was connected to the plant model block. The design parameters of the MPC con-
troller were identical to the previous case, see Table 3. Also the same simulation
scenario was assumed. The result of the simulation can be seen in Figure 7. The
dashed curves represent the case when an observer without disturbance states
is employed. As we can see, the controller fails to achieve error-free tracking
even when no disturbances are present, which is due to the fact that there is a
model-mismatch between the linear and the nonlinear plant. Obviously, the con-
troller also fails to compensate fully for the load disturbance. If the disturbance
observer is employed however, the controller gives zero steady-state error. This
is shown by the solid curves. In both cases, however, the controller respects the
constraints: no tank level exceed 20 cm.
23
14 10
12
8
10
h3 [cm]
h4 [cm]
8 6
6
4
4
2 2
0 200 400 600 800 1000 1200 0 200 400 600 800 1000 1200
16 20
14 19.5
h2 [cm]
h1 [cm]
12
19
10
18.5
8
18
6
0 200 400 600 800 1000 1200 0 200 400 600 800 1000 1200
4 8
3
6
u1 [V]
u2 [V]
2
4
1
0 2
0 200 400 600 800 1000 1200 0 200 400 600 800 1000 1200
t [s] t [s]
the angle of the bar. A simple dynamical model of the system is given by
θ¨ e = ( K f la / J e )( V f + Vb) − T / J e
θ¨r = −( F la / Jt ) sin θ p (14)
θ¨ p = ( K f lh / J p)( V f − Vb )
where the coefficients are given in Table 4. The input signals to the system are
V f and Vb which represent the voltages fed to the propeller motors. In addition,
the elevation and pitch angles are constrained, so that
−0.5 ≤ θ e ≤ 0.6 −1 ≤ θ p ≤ 1.
24
Figure 8 The helicopter process.
Parameter Value
Hp 30
Hw 1
Hu 10
Ip Only every other sample was included
in the optimization problem.
Iu Every other control increment was as-
sumed to be constant.
Q diag(1,1)
R diag(0.1,0.1)
and then discretized using the sampling interval h = 0.2 s. This gives a discrete-
time state space model of the process to be used to design the MPC controller.
The task of the control system is to control, independently, the elevation angle
and the rotation angle. The main constraint is that the pitch angle must not
be too large, reflecting the fact that for large θ p, the control authority in the
θ e direction is small. This is not accounted for in the linearized model, and the
controller is likely to have degraded performance when θ p is large.
The design parameters of the MPC controller are given in Table 5. Again, the
choice of prediction and control horizons have been made considering the domi-
nating time constants of the system. We also assume that the entire state vector
is measurable, enabling the use of the state feedback configuration of the MPC
controller corresponding to mode 0. In order to achieve fast sampling, (which
requires a QP problem of moderate complexity) while maintaining sufficiently
long prediction and control horizons, the blocking factor 2 was specified for both
horizons.
The result of the simulation when the MPC controller is applied to the nonlinear
plant (14) is shown in Figure 9. Reference trajectories specifying step sequences
for the elevation and rotation angles respectively are applied to the system. As
25
0.4 4
0.3 3
Elevation [rad]
Rotation [rad]
0.2 2
0.1 1
0 0
−0.1 −1
0 10 20 30 0 10 20 30
4
1
3
0.5
2
Pitch [rad]
V , V [V]
b
0 1
f
0
−0.5
−1
−1
−2
0 10 20 30 0 10 20 30
t [s] t [s]
Figure 9 A simulation of the MPC controller applied to the nonlinear helicopter plant.
we can see, the controller manages to track the set points, while keeping the
pitch angle within the specified limits.
26
MPCinit
Purpose
Initializes the MPC data structure.
Syntax
md = MPCInit(Ad, Bd, Cyd, Czd, Dzd, Ccd, Dcd, Hp,
Hw, zblk, Hu, ublk, du_max, du_min,
u_max, u_min, z_max, z_min, Q, R, W, V,
h, cmode, solver)
Input arguments
Ad, Bd, Cyd System matrices for the plant; Ad , Bd and Cd .
Czd, Dzd Matrices defining the controlled outputs; Cz and D z.
Ccd, Dcd Matrices defining the constrained outputs; Cc and Dc .
Hw, Hp, Hu Integers defining the prediction and control horizons; Hw,
H p and Hu .
zblk, ublk Blocking factors defining the sets Ip and Iu .
u_min, u_max Control variable limits; umin and umax .
du_min, du_max Control increment limits; ∆ umin and ∆ umax .
z_min, z_max Controlled variable limits; zmin and zmax .
Q, R Weighting matrices for the cost function.
W, V Weighting matrices for the Kalman filter design, if
applicable.
h Sampling interval.
cmode Controller mode.
solver Solver to be used for the quadratic programming problem.
Output arguments
md Contains the pre-computed matrices needed by the MPC controller.
Description
MPCInit creates the data structure used by the MPC controller. A discrete time
model, with sampling interval h, of the controlled system is assumed,
x( k + 1) = Ax( k) + Bu( k)
y( k) = Cy x( k)
(15)
z( k) = Cz x( k) + D zu( k)
zc ( k) = Cc x( k) + Dc u( k)
where z( k) are the controlled outputs, y( k) the measured outputs and zc ( k) the
constrained outputs. The constraints of the system are given by
∆ umin ≤ ∆ u( k) ≤ ∆ umax , k ∈ Iu
umin ≤ u( k) ≤ umax , k ∈ Iu (16)
zmin ≤ zc ( k) ≤ zmax , k ∈ Ip
27
where ∆ u( k) = u( k) − u( k − 1) are the control increments. Ip and Iu are the
sets of samples for which the controlled and control variables are included in the
cost function and for which the constraints are enforced. The first sample to be
included in the optimization procedure is Hw, and the total number of samples
in the prediction horizon is H p. The entries in the array z_blk indicates the
time distance between two consecutive predicted samples. Also, the last entry in
z_blk is used as distance between the remaining samples (if length(z_blk) <
H p).
Example
Assume that Hw = 1, H p = 6 and z_blk = [1 2 2 5]. Then the samples [1 2 4 6
11 16] will be included in the cost function evaluation.
Equivalently, Hu indicates the number of predicted control moves to be calculated
at each sample. The array u_blk contains the blocking factors indicating over
which sampling intervals the control signal should be constant. For example, a
blocking factor of 2 indicates that 2 consecutive control signals are equal.
Example
Assume that Hu = 3, u_blk = [1 2]. Then it is assumed that at each sample, for
the predicted control signal, û, we have that û( k + 1) = û( k + 2) and û( k + 3) =
û( k+4). Only û( k), û( k+1) and û( k+3) will be calculated. Q and R are weighting
matrixes for the cost function, where Q penalizes the controlled outputs and R
penalizes control increments. W and V are weighting matrices for the design
of the Kalman filter used to estimate the state and load disturbances. W is the
covariance matrix of the state and V is the covariance matrix of the measurement
noise.
The controller supports several control modes. cmode specifies which mode should
be used. The following modes are supported:
28
system is augmented to include also the disturbance states. If the number
of measured outputs exceed the number of inputs, constant load distur-
bances on the additional measured outputs are assumed. The controlled
variables should be the same as the first pz measured variables. Also, the
number of controlled outputs should be equal to the number of inputs.
The argument solver should be a string containing one of the alternatives qp_as,
qp_ip or quadprog, indicating which solver should be used to solve the quadratic
programming problem. Notice that quadprog requires Optimization Toolbox.
See Also
MPCOptimizeSol, MPCSim and MPCfrsp.
29
MPCOptimizeSol
Purpose
This function solves the MPC optimization problem.
Syntax
[duPred, zPred, J] = MPCOptimizeSol(x_est,u_last,du_old,
r,md)
Input arguments
x_est The current estimate of the state vector
u_last The control signal applied to the plant at the last sample.
du_old The optimal decision vector from the last sample that is used to hot
start the quadratic programming algorithm.
r The reference vector for the controlled variables.
md The data structure containing pre-computed matrices.
Output arguments
duPred Optimal control increment trajectory.
zPred Optimal trajectory of the controlled variables.
J Optimal value of the cost function.
Description
MPCOptimizeSol solves the MPC optimization problem. duPred and zPred are the
predicted control increments and controlled outputs for the specified prediction
horizons.
An estimate of the current state vector is given by x_est, and u_last is the last
applied control input. As an initial starting point for the optimization algorithm,
the last predicted control input vector, du_old, is supplied. r is the desired set
point for the controlled outputs and md is the data structure containing matrices
needed to solve the optimization problem.
See Also
MPCInit, MPCSim and MPCfrsp
30
MPCSim
Purpose
Simulates a linear system model controlled by the MPC controller.
Syntax
[x, u, y, z, zPredTraj, uPredTraj] = MPCSim(md,r,d)
Input arguments
md Data object containing the pre-computed matrices.
r Reference trajectory for the controlled variables.
d Input disturbance trajectory.
Output arguments
x State trajectory of the plant.
u Control variable trajectory.
y Measured variable trajectory.
z Controlled variable trajectory.
zPredTraj Optimal predicted trajectories for the controlled variables at
each sample.
uPredTraj Optimal predicted trajectories for the control variables at each
sample.
Description
MPCSim simulates the MPC controller specified by the data object md. The refer-
ence trajectory for the controlled outputs is given by r and a load disturbance
acting on the input is given by d.
See Also
MPCInit, MPCOptimizeSol and MPCfrsp
31
MPCController
Purpose
S-function for simulation of the MPC controller in the Simulink environment.
Syntax
[sys, x0, str, ts] = MPCController(t, x, u, flag, md)
Input arguments
t Supplied by the Simulink environment.
x Supplied by the Simulink environment.
u Supplied by the Simulink environment.
flag Supplied by the Simulink environment.
md Data object containing the pre-computed matrices needed by the MPC
controller. Supplied as a parameter in the S-function block.
Output arguments
sys Needed by the Simulink environment.
x0 Needed by the Simulink environment.
str Needed by the Simulink environment.
ts Needed by the Simulink environment.
Description
MPCController is an S-function implementing the MPC controller intended for
use with Simulink. The argument md, which is the only user supplied argu-
ment, contains the data structures needed by the controller. The input to the
S-function block is a vector signal consisting of the measured outputs and the
reference values for the controlled outputs. The output of the S-function block is
a vector signal consisting of the control variables and the estimated state vector,
potentially including estimated disturbance states.
See Also
MPCOptimizeSol and MPCInit
32
MPCfrsp
Purpose
Calculates the linear controller corresponding to the MPC controller if no con-
straints are active.
Input arguments
md MPC data object.
fignbr If this argument fignbr is given, two plots with numbers
fignbr and fignbr+1 are drawn.
Output arguments
Sys_CL Closed loop system from r to z.
Sys_S Sensitivity function from v to y.
Sys_CS Complimentary sensitivity function: from n to y.
Sys_SU Control signal sensitivity function: from n to u.
F See block diagram.
H See block diagram.
K See block diagram.
h A vector of handles to the figures where the transfer functions are
plotted.
See Figure 10 for explanation of the notation.
Description
MPCfrsp calculates the frequency responses of the MPC controller. When no con-
straints are active, the MPC controller is a linear controller and may be analyzed
using linear methods. Some important transfer functions are also plotted.
See Also
MPCInit
r u y
F ( z) K ( z) P( z)
−
H ( z)
33
qp_as
Purpose
Solves a quadratic program using an active set method.
Syntax
[xopt, lambda, J, x_hist] = qp_as(H,f,A,b,x0)
Input arguments
H H-matrix (Hessian) of the QP problem.
f f -vector of the QP problem.
A A-matrix defining linear constraints.
b b-vector defining linear constraints.
x0 Initial solution guess.
Output arguments
xopt The optimal solution.
lambda Lagrange multipliers.
J The optimal value of the cost function.
x_hist History of x during the optimization run. Each column in x_hist
represents the solution at the corresponding iteration.
Description
qp_as solves the quadratic programming problem
1 T
min x Hx + fTx
2
s.t.
Ax ≤ b
See Also
get feasible
34
qp_ip
Purpose
Solves a quadratic program using a primal-dual interior point method.
Syntax
[xopt, lambda, J, x_hist] = qp_ip(H,f,A,b,x0)
Input arguments
H H-matrix (Hessian) of the QP problem.
f f -vector of the QP problem.
A A-matrix defining linear constraints.
b b-vector defining linear constraints.
x0 Initial solution guess.
Output arguments
xopt The optimal solution.
lambda Lagrange multipliers.
J The optimal value of the cost function.
x_hist History of x during the optimization run. Each column in x_hist
represents the solution at the corresponding iteration.
Description
qp_ip solves the quadratic programming problem
1 T
min x Hx + fTx
2
s.t.
Ax ≤ b
35
getfeasible
Purpose
Finds a feasible solution subject to linear inequality constraints.
Syntax
[x, as, iter] = getfeasible(A,b)
Input arguments
A A-matrix defining linear constraints.
b b-vector defining linear constraints.
Output arguments
x A feasible solution.
as The indices of active constraints, if any.
iter The number of iterations.
Description
getfeasible finds a feasible vector that fulfills the linear inequality constraints
Ax ≤ b.
See Also
qp_as
36
9. References
Åkesson, J. and P. Hagander (2003): “Integral action—a disturbance observer
approach.” In Proceedings of European Control Conference.
Åström, K. J. and B. Wittenmark (1990): Computer Controlled Systems—Theory
and Design. Prentice-Hall, Englewood Cliffs, New Jersey.
Bartlett, R. A., A. Wächter, and L. T. Biegler (2000): “Active set vs. interior
point strategies for model predictive control.” In Proceedings of the American
Control Conference. Chicago, Illinois.
Bazaraa, M. S., H. D. Sherall, and C. M. Shetty (1993): Nonlinear Programming:
Theory and Algorithms. John Wiley & Sons, Inc.
Bemporad, A., M. Morari, V. Dua, and E. N. Pistikopoulos (2002): “The explicit
linear quadratic regulator for constrained systems.” Automatica, 38:1, pp. 3–
20.
Fletcher, R. (1987): Practical Methods of Optimization. John Wily & Sons Ltd.
Johansson, K. H. (1997): Relay Feedback and Multivariable Control. PhD thesis
ISRN LUTFD2/TFRT- -1048- -SE, Department of Automatic Control, Lund
Institute of Technology, Sweden.
Maciejowski, J. M. (2002): Predictive Control with Constraints. Pearson Educa-
tion.
Mayne, D. Q., J. B. Rawlings, C. V. Rao, and P. O. M. Scokaert (2000):
“Constrained model predictive control: Stability and optimality.” Automatica,
36:6, pp. 789–814.
Qin, S. J. and T. A. Badgwell (2003): “A survey of industrial model predictive
control technology.” Control Engineering Practice, 11, pp. 733–764.
Scokaert, P. O. M., D. Q. Mayne, and J. B. Rawlings (1999): “Suboptimal
model predictive control (feasibility implies stability).” IEEE Transactions
of Automatic Control, 44:3, pp. 648–654.
Wright, S. J. (1997): Primal-Dual Interior-Point Methods. SIAM.
37