UI /

# Closed-loop simulations

MPT3 introduces a special object to represent closed-loop systems, consisting of a dynamical system and an MPC controller:

loop = ClosedLoop(mpc, sys)

where mpc represents an MPC controller (either on-line or explicit MPC), and sys describes the dynamical system.

After a closed-loop object is constructed, you can simulate it by

data = loop.simulate(x0, Nsim)

where x0 is the initial state for the simulation, and Nsim is the number of simulation steps.

As an example, consider MPC for a linear system {$x^+ = A x + B u$} with following properties:

• {$A = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}$}, {$B = \begin{bmatrix} 1 \\ 0.5 \end{bmatrix}$}
• state constraints {$\begin{bmatrix} -5 \\ -5 \end{bmatrix} \le x \le \begin{bmatrix} 5 \\ 5 \end{bmatrix}$}
• input constraints {$-1 \le u \le 1$}
• prediction horizon 4
• quadratic cost function with unity weighting matrices
prediction_model = LTISystem('A', [1 1; 0 1], 'B', [1; 0.5]);
prediction_model.x.min = [-5; -5];
prediction_model.x.max = [5; 5];
prediction_model.u.min = -1;
prediction_model.u.max = 1;
prediction_model.x.penalty = QuadFunction([1 0; 0 1]);
N = 4;
mpc = MPCController(prediction_model, N);

Now we want to verify how the controller performs if connected to a different system, say {$x^+ = A x + B u$} with {$A = \begin{bmatrix} 1 & 1 \\ 0.2 & 1 \end{bmatrix}$}, {$B = \begin{bmatrix} 1 \\ 0.8 \end{bmatrix}$}:

simulation_model = LTISystem('A', [1 1; 0.2 1], 'B', [1; 0.8]);
loop = ClosedLoop(mpc, simulation_model);
x0 = [4; 0];
Nsim = 20;
data = loop.simulate(x0, Nsim)

data =

X: [2x21 double]
U: [1x20 double]
Y: []
cost: [1x20 double]

plot(data.X')

NOTE: if you change the controller and/or the system in your MATLAB workspace, the change(s) will automatically propagate to the closed-loop object without the need to re-create it. In other words, in the example above we can as well do:

mpc.N = 10;
data = loop.simulate(x0, Nsim)

and the simulation will automatically use the new controller with prediction horizon 10.