Model Predictive Control Using MATLAB
Model Predictive Control Using MATLAB
net/publication/357017341
CITATION READS
1 3,086
1 author:
Midhun T Augustine
Indian Institute of Technology Delhi
11 PUBLICATIONS 8 CITATIONS
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by Midhun T Augustine on 15 December 2021.
Midhun T. Augustine
Department of Electrical Engineering
Indian Institute of Technology Delhi, India
midhunta30@gmail.com
A BSTRACT
This technical note contains a brief introduction to the modern control approach called model
predictive control (MPC), and its numerical implementation using MATLAB. We discuss the basic
concepts and numerical implementation of the two major classes of MPC: Linear MPC (LMPC)
and Nonlinear MPC (NMPC). This includes the various aspects of MPC such as formulating the
optimization problem, constraints handling, feasibility, stability, and optimality.
1 Introduction
MPC is a feedback control approach that uses model-based optimization for computing the control input. In MPC, a
model of the system along with the current state (measured/estimated) is used to predict the future behavior (states) of
the system, for a control input sequence over a short period. The predicted behavior is characterized by a cost function
which is a function of the predicted state and control sequence. Then an optimization algorithm is used to find the
control sequence which optimizes the predicted behavior or cost function. The first element of the control sequence is
applied to the system which gives the next state, and the algorithm is repeated at the next time instant, which results in
a receding horizon scheme. Model predictive control (MPC) is also known as receding horizon control (RHC). The
name MPC originated from the model-based predictions used for optimization, whereas the name RHC comes from the
receding horizon nature of the control scheme. MPC which is originated from the optimal control approach has the
following advantages over the former:
1. It gives closed-loop control schemes whereas optimal control mostly results in open-loop control schemes.
2. MPC can handle complex systems such as nonlinear, higher-order, multi-variable, etc.
3. MPC can incorporate constraints easily.
Notations: N, Z and R denotes the set of natural numbers, integers and real numbers respectively. Rn stands for n -
dimensional Euclidean space and Rm×n refers to the space of m × n real matrices. Matrices and vectors are represented
by boldface letters (A, a), scalars by normal font (A, a), and sets by blackboard bold font (A, B, C). The notation P > 0
(P ≥ 0) indicates that P is a real symmetric positive definite (semidefinite) matrix. Finally, I, 0 represents identity
matrix and zero matrix of appropriate order.
MPC is associated with a number of terminologies which are defined below
1. Sampling time (T): It is the time difference between two consecutive state measurements or control updates.
In general T ∈ R+ , and for discrete-time systems T > 0, whereas for continuous-time systems T = 0.
2. Time horizon (NT ): It is the number of time instants the control input is applied to the system. In general,
NT ∈ N and if NT is infinity, the problem is called infinite horizon problem, otherwise finite horizon problem.
3. Prediction horizon (N ): It is the length of the prediction window over which the states are predicted and
optimized. In general N ∈ N and usually 2 ≤ N ≤ NT .
4. Control horizon (NC ): It is the length of the control window in which the control input is optimized, and
normally NC ≤ N . In this note, we mainly focus on the case for which the control horizon is same as the
prediction horizon, i.e. NC = N. If NC < N we optimize the control sequences over NC and the remaining
control sequences (of length N − NC ) are normally chosen as zero.
M ODEL P REDICTIVE C ONTROL USING MATLAB - D ECEMBER 15, 2021
>
Controller (MPC)
Predicted state sequence
xr
System
Output
>
Control or
Reference input State
> > >
>
>
>
k k+NC k+N
(a) (b)
Figure 1: (a) MPC General block diagram (b) Basic MPC strategy
The general block diagram of a dynamical system with MPC is given in Fig. 1(a) and the basic strategy of MPC is
given in Fig. 1(b). In MPC during the current time instant k, we consider the optimization over the next N instants
where N is the prediction horizon, i.e. the optimization window is from k to k + N. This indicates that the optimization
window moves with time and this feature is called moving horizon or receding horizon. In MPC, during every time
instant, we compute the sequence of control inputs over the control horizon, which optimize the future performance of
the system over the prediction horizon. Then the first element of the optimal control sequence is applied to the system,
which results in a receding horizon scheme. The first element of the control sequence and the next state under the MPC
scheme are represented in black color in Fig. 1(b). By repeating this at each time instant, we obtain the control inputs
and states with MPC over the time horizon.
Based on the nature of the system model used in the optimization, MPC can be grouped into the following two classes
1. Linear MPC: For which the system model and the constraints are linear. The cost function can be linear
or quadratic which results in linear programming or quadratic programming problems which are convex
optimization problems.
2. Nonlinear MPC: For which the system model is nonlinear and the constraints can be either linear or nonlinear.
The cost function is usually chosen as a linear or quadratic function of states and control inputs which results
in a nonlinear programming problem that can be non-convex.
Another classification is based on the implementation of the MPC algorithm which results in the following categories
1. Implicit MPC : This is also known as the traditional MPC in which the control input at each time instant is
computed by solving an optimization problem online. In this note, we will be focusing on implicit MPC which
is the most general MPC scheme.
2. Explicit MPC: In this, the online computation is reduced by transferring the optimization problem offline. In
explicit MPC the state constraint set is divided into a finite number of regions and the optimization problem is
solved offline for each of the regions which give the control input as a function of the state. This simplifies the
online computation to just a function evaluation.
When it comes to optimization the approaches can be classified into two categories
1. Iterative approach: In which the elements of the decision vector are optimized together. Here the optimal
decision vector is computed iteratively by starting with an initial guess which is then improved in each iteration.
Most of the linear programming and nonlinear programming algorithms are based on the iterative approach.
2. Recursive approach: In which the elements of the decision vector are optimized recursively, i.e., one at a
time. The popular optimization algorithm which uses the recursive approach is dynamic programming. Even
though both iterative approach and recursive approach are used in MPC, in this note we focus on the former.
2
M ODEL P REDICTIVE C ONTROL USING MATLAB - D ECEMBER 15, 2021
The cost function is chosen as a quadratic sum of the states and control inputs
T −1
NX
J = xTNT QNT xNT + xTk Qxk + uTk Ruk (3)
k=0
where QNT ∈ Rn×n , Q ∈ Rn×n , R ∈ Rm×m are the weighting matrices used for relatively weighting the states and
control inputs and to be chosen such that QNT ≥ 0, Q > 0, R > 0. The state and control sequence is defined as
X = x0 , x1 , ..., xNT , U = u0 , u1 , ..., uNT −1 which contains the state and control input over the time horizon. Now,
the optimal control problem for the LTI system is defined as follows which is also known as the constrained linear
quadratic regulator (CLQR) problem
Problem 1. For the linear system (1) with the initial state x0 , compute the control sequence U by solving the optimization
problem
inf J subject to
U
As NT → ∞ the problem is called infinite-horizon constrained LQR. One can solve constrained LQR with a large time
horizon (NT → ∞) using the MPC approach which usually results in suboptimal solutions with lesser computation.
MPC uses a prediction horizon N ≤ NT (in practice N << NT ) and during every time instant the control sequence
for the next N instants is computed for minimizing the cost over the next N instants. The cost function for the MPC
with a prediction horizon N at time instant k is defined as
X−1
k+N
Jk = xTk+N |k QN xk+N |k + xTi|k Qxi|k + uTi|k Rui|k (5)
i=k
in which xi|k , ui|k denotes the state and control input at time instant i predicted/computed at time instant k. Note that
here k denote the time instants within the time horizon and i denotes the time instants within the prediction horizon.
Similarly, the state and control sequence
for the MPC at time instant k is defined as Xk = xk|k , xk+1|k , ..., xk+N |k ,
Uk = uk|k , uk+1|k , ..., uk+N −1|k . Then the MPC problem for linear systems is defined as follows:
Problem 2. For the linear system (1) with the current state xk|k = xk given, compute the control sequence Uk , by
solving the optimization problem
inf Jk subject to
Uk
Uk ∈ UN , Xk ∈ XN +1 , k ∈ T (6)
xi+1|k = Axi|k + Bui|k , k ∈ T, i = k, ..., k + N − 1
.
3
M ODEL P REDICTIVE C ONTROL USING MATLAB - D ECEMBER 15, 2021
Here we represent the MPC optimization problem as a quadratic programming problem. From the solution of the state
equation for LTI systems we obtain
xk|k
I 0 0 . . . 0 uk|k
xk+1|k A B 0 . . . 0 uk+1|k
. = . xk + . .. .. .. (7)
. .. .. . .
. .
xk+N |k AN AN −1 B AN −2 B . . . B uk+N −1|k
By defining the following matrices
xk|k
uk|k
I 0 0 ... 0
xk+1|k uk+1|k A B 0 ... 0
Xk = .. , Uk =
.. AX = . , BU = .
.. .. .. .. (8)
. .
. .
xk+N |k uk+N −1|k A N
AN −1 B AN −2 B . . . B
the equation (7) is rewritten as
Xk = AX xk + BU Uk (9)
This indicates that, the predicted state Xk can be represented as a function of the current state xk and input sequence
Uk . Similarly, by defining
Q ... 0 0 R 0 ... 0
.. .. .. 0 R . . . 0
QX = . . . , RU = . . .. (10)
0 ... Q 0 .. .. .
0 ... 0 QN 0 0 ... R
the cost function (5) can be represented in terms of Xk and Uk as
Jk = XTk QX Xk + UTk RU Uk (11)
Finally, by defining
Fx 0 ... 0 gx Fu 0 ... 0 gu
0 Fx ... 0 gx 0 Fu ... 0 gu
FX =
... .. .. , gX =
... , FU = ...
.. .. , gU =
...
(12)
. . . .
0 0 ... Fx gx 0 0 ... Fu gu
the state and control constraints in (2) can be represented in terms of Xk and Uk as
FX Xk ≤ gX
(13)
FU Uk ≤ gU
Now by combining Xk and Uk , we can represent the cost function with a single decision vector. For that we define
Xk QX 0 F 0 g
z= , H= , F= X , g = X , Feq = [I −BU ] , geq = AX xk (14)
Uk 0 RU 0 FU gU
using this we can rewrite the cost function (11) and constraints (9),(13) and represent the optimization problem (6) as a
quadratic programming problem as below
inf zT Hz subject to
z
Fz ≤ g (15)
Feq z = geq
which can be solved using standard numerical optimization algorithms such as steepest-descent method, Newton
method, etc. For faster convergence of the numerical optimization method, the optimal solution for the current instant
can be used as the initial condition for the next instant. Note that here geq is a function of the state vector xk . Therefore
the current state information is required for solving the optimization problem. In MPC this optimization problem is
solved during each time instant k and the first element of U∗k is applied to the system, i.e., the control input with MPC is
uk = [U∗k ]1 = u∗k|k (16)
4
M ODEL P REDICTIVE C ONTROL USING MATLAB - D ECEMBER 15, 2021
Note that this algorithm is based on the assumption that, an optimal control sequence exists at each time instant. The
existence of an optimal control sequence depends on the system model and constraints, and this will be discussed in the
feasibility analysis section. The algorithm for linear MPC is given below
Algorithm 1 : LMPC
1: Require A, B, NT , N, n, m, Q, R, QNT , Fx , gx , Fu , gu
2: Initialize x0 , z0
3: Construct AX , BU , QX , RU , H, F, g
4: for k = 0 to NT − 1 do
5: xk = [X]k+1 (obtain xk from measurement/estimation)
6: Compute Feq , geq
∗
∗ Xk
7: Compute z = by solving the optimization problem (15).
U∗k
∗
8: Apply uk = [Uk ]1 to the system
9: Update z0 = z∗
10: end for
The optimization problem can be solved using the MATLAB function fmincon for solving constrained optimization
problems which is of the form
z∗ = fmincon(f, z0 , F, g, Feq , geq , lb, ub) (17)
in which lb, ub are the vectors containing the lower bound and upper bound of each elements in the decision vector z.
Here we discuss some methods for reducing online computation in which the basic idea is to reduce the number of
optimization variables and constraints. The first method uses the idea of eliminating the states from the decision vector
z. This method is useful when we have only control constraints, i.e., the state is unconstrained xk ∈ Rn or the state
constraints can be transferred to control constraints. We have from (11) the cost Jk is a function of the state sequence
Xk and control sequence Uk . Now, by substituting (9) in (11), we obtain
T
Jk = AX xk + BU Uk QX AX xk + BU Uk + UTk RU Uk
where H = BTU QX BU + RU , qTk = 2xTk ATX QX BU and rk = xTk ATX QX AX xk . Therefore we can represent the cost Jk as
a function of the current state xk and control sequence Uk , in which Uk is the decision vector. Similarly the constraint
inequalities (13) can be rewritten as
FX AX xk + BU Uk ≤ gX =⇒ FX BU Uk ≤ gX − FX AX xk
(19)
FU Uk ≤ gU
FX BU gX − FX AX xk
Now by defining z = Uk , F = ,g= we can represent the optimization problem (15) as a
FU gU
quadratic programming problem as below
inf zT Hz + qTk z + rk subject to
z (20)
Fz ≤ g
Note that here the parameters qk , rk and g are functions of xk . Therefore the current state information is required for
solving this optimization problem.
Another way to reduce the online computation is to use a control horizon NC lesser than the prediction horizon N.
This in turn reduces the number of optimization variables. In this case we define the control sequence as Uk =
uk|k , ..., uk+NC −1|k , 0, ..., 0 and this reduces the number of decision variables in z to mNc .
5
M ODEL P REDICTIVE C ONTROL USING MATLAB - D ECEMBER 15, 2021
So far we considered the stabilization problem in MPC for which the reference xr = 0. In this section we discuss the
set point tracking problem for which the reference xr 6= 0, and the objective is to track the nonzero set point. For the
nonzero reference xr , the steady state value of the control input will be nonzero, i.e. ur 6= 0 and in steady state we have
xk+1 = xk = xr . Substituting this in (1) gives
xr = Axr + Bur =⇒ ur = B−1 (I − A)xr (21)
−1
where B is the pseudo-inverse. The set point tracking can be transferred to a stabilization problem by defining the
error state and control xek = xk − xr , uek = uk − ur and consider the error dynamics for MPC design which gives
xek+1 = xk+1 − xr = Axk + Buk − xr = Axk − Axr + Buk − xr + Axr
(22)
= A[xk − xr ] + B[uk − B−1 (I − A)xr ] = Axek + Buek
Using the error state and control vectors the constraints can be rewritten as
Fx x ≤ gx =⇒ Fx (xek + xr ) ≤ gx =⇒ Fx xek ≤ gx − Fx xr
(23)
Fu u ≤ gu =⇒ Fu (uek + ur ) ≤ gu =⇒ Fu uek ≤ gu − Fu ur
Now the matrices
FX, gX , FU , gU can be defined as in (12) in which gx , gu are replaced by gx − Fx xr , gu − Fu ur . We
Xe k Xk − Xr
define z = = and the optimization problem is obtained as in (15), solving which the optimal control
Ue k Uk − Ur
input for the MPC problem is obtained as
uk = [U∗ek ]1 + ur (24)
(a) (b)
6
M ODEL P REDICTIVE C ONTROL USING MATLAB - D ECEMBER 15, 2021
Uk ∈ UN , Xk ∈ XN +1 , k ∈ T (28)
xi+1|k = f(xi|k , ui|k ), k ∈ T, i = k, ..., k + N − 1.
.
By defining Xk and Uk as in (8) we can rewrite the cost function and constraints for the nonlinear MPC problem as
Jk = XTk QX Xk + UTk RU Uk (29)
and
FX Xk ≤ gX
FU Uk ≤ gU (30)
feq (Xk , Uk ) = 0
where
xk|k − xk
xk+1|k − f(xk|k , uk|k )
feq (Xk , Uk ) = .. (31)
.
xk+N |k − f(xk+N −1|k , uk+N −1|k )
Now by defining z, H, F, g as in (14) the optimization problem is represented as a nonlinear programming problem as
below
inf zT Hz subject to
z
Fz ≤ g (32)
feq (z) = 0
Here the equality constraint is nonlinear which makes the optimization problem a nonlinear programming problem.
In MPC this optimization problem is solved during every time instant k and the first element of U∗k is applied to the
system, i.e., the control input with MPC is
uk = [U∗k ]1 = u∗k|k (33)
The algorithm for nonlinear MPC is summarized below:
Algorithm 2 : NMPC
1: Require f, NT , N, n, m, Q, R, QNT , Fx , gx , Fu , gu
2: Initialize x0 , z0
3: Construct QX , RU , H, F, g
4: for k = 0 to NT − 1 do
5: xk = [X]k+1 (obtain
∗ xk from measurement/estimation)
∗ Xk
6: Compute z = by solving the optimization problem (32).
U∗k
∗
7: Apply uk = [Uk ]1 to the system
8: Update z0 = z∗
9: end for
7
M ODEL P REDICTIVE C ONTROL USING MATLAB - D ECEMBER 15, 2021
The optimization problem (32) can be solved using the MATLAB function for solving constrained optimization
problems
z∗ = fmincon(f, z0 , F, g, lb, ub, feq ) (34)
Here we discuss the set point tracking problem for nonlinear systems for which the reference xr 6= 0. The reference
value or steady state value of the control input ur is computed by solving the steady state equation
xr = f(xr , ur ) (35)
By defining the error state and control vector as xek = xk − xr , uek = uk − ur , the constraints can be rewritten as in
(23). Similarly the equality constraint becomes
xek+1 = f(xk , uk ) − xr = f(xek + xr , uek + ur ) − xr (36)
Xe k Xk − Xr
Now by defining z = = , the optimization problem is obtained as in (32), solving which the optimal
Ue k Uk − Ur
control input for the MPC problem is obtained as
uk = [U∗ek ]1 + ur (37)
We consider the discrete-time model of the simple pendulum system which is defined by the state equation
x1k + T x2k
xk+1 = f(xk , uk ) = (38)
x2k + T − gl sin(x1k ) − MBl2 x2k + M1l2 uk
where M is the mass of the simple pendulum, B is the friction coefficient, l is the length of the pendulum, g is the
acceleration due to gravity and T is the sampling time. The system parameters are chosen as M = 1, B = 1, l = 1, g =
T
9.8, T = 0.1 and simulation parameters are chosen as Q = I2 , R = 1 and x0 = [2 1] . The constraint set parameters
is defined as
1 0 5
0 1 5 1 0.1
Fx = gx = Fu = gu = (39)
−1 0 5 −1 0
0 −1 5
which is equivalent to −5 ≤ x1k ≤ 5, −5 ≤ x2k ≤ 5, 0 ≤ uk ≤ 0.1. The response of the simple pendulum with the
MPC scheme is given in Fig. 3(a). The response shows the states converge to the origin and the constraints are satisfied.
T
Similarly for the set-point tracking problem, the state reference is chosen as xr = [0.5 0] for which the steady-state
control input is obtained by solving (36) for the nonlinear system (38) which gives ur = M gl sin(x1r ) = 4.69. Hence
we set the maximum value of control input as 5 for the set-point tracking problem. The simulation response for the
set-point tracking is given in Fig. 3(b) which shows the state converges to the desired reference.
(a) (b)
8
M ODEL P REDICTIVE C ONTROL USING MATLAB - D ECEMBER 15, 2021
>
xk
Predicted sequence 2
>
>
Predicted sequence 2
(a) (b)
Figure 4: Feasibility (a) State near the boundary of X (b) State away from the boundary of X
9
M ODEL P REDICTIVE C ONTROL USING MATLAB - D ECEMBER 15, 2021
Next, we discuss stability, which deals with the convergence of the solution, i.e., whether the state trajectory under the
MPC scheme converges to the desired reference or equilibrium point. In MPC, the stability analysis is mainly based
on the Lyapunov approach in which the basic idea is to design the control scheme in such a way that the optimal cost
function becomes a Lyapunov function, i.e., Vk = Jk∗ and it satisfies
∗
∆V = Jk+1 (xk+1 ) − Jk∗ (xk ) < 0 (43)
There exist different variants of the criteria (43) which give different upper bound for ∆V. In general, for stabilizable
LTI systems, by properly selecting the terminal weighting matrix and constraints, the value function of the MPC scheme
can be made as a Lyapunov function. But this may not always be possible for nonlinear systems. The terminal weighting
matrix QN and terminal constraints FxN , gxN can be easily incorporated in the MPC algorithm by adding them in
QX , FX , gX which results in
Q ... 0 0 Fx . . . 0 0 gx
.. .. .. . .. .. .
QX = . . . , FX = .. . . , gX = .. (44)
0 ... Q 0 0 ... F 0 g
x x
0 ... 0 QN 0 ... 0 FxN gxN
Finally, optimality is a term associated with the performance of the solution and depends on how fast the trajectory
converges to the equilibrium point and how much control effort is required. When it comes to optimality, the MPC
schemes usually result in a suboptimal solution. This is because of the reason that, in MPC during every time instant
we optimize the performance over the prediction horizon, not the entire time horizon. Consequently, as the prediction
horizon increases the MPC control law becomes more optimal and, in general as N → NT , the control law becomes
optimal.
5 Further Reading
This technical note attempts to discuss the basic theory of MPC and its numerical implementation using MATLAB.
For a more detailed study on linear MPC and nonlinear MPC, one can refer to [1] and [2], respectively. For a better
understanding of the numerical optimization methods for solving linear programming, quadratic programming, and
nonlinear programming problems, one may refer to [3]. For more advanced topics on MPC such as adaptive MPC,
Robust MPC, Distributed MPC, one can refer [4]-[7].
References
[1] F. Borrelli, A. Bemporad and M. Morari “Predictive Control for Linear and Hybrid Systems”, Cambridge University
Press, 2017.
[2] L. Grune and J. Pannek “Nonlinear Model Predictive Control Theory and Algorithms”, Springer, 2011.
[3] D. Luenberger and Y. Ye “Linear and Nonlinear Programming: Fourth edition ”, Springer, 2008.
[4] D. Mayne, “Model predictive control: Recent developments and future promise”, Automatica, Issue 50, pp.
2967-2986, 2014.
[5] M. Guay, V. Adetola and D. Dehaan,“Robust and Adaptive Model Predictive Control of Non-linear Systems ”, The
Institution of Engineering and Technology, 2015.
[6] B. Kouvaritakis and M. Cannon,“Model Predictive Control: Classical, Robust and Stochastic”, Springer, 2016.
[7] S. Rakovic and W. Levine, “Handbook of Model Predictive Control”, Springer, 2019.
10