Accelerating the pace of engineering and science

# Model Predictive Control Toolbox

## Understanding Control Behavior by Examining Optimal Control Sequence

This example shows how to inspect the optimized sequence of manipulated variables computed by model predictive controller at each sample time.

The plant is a double integrator subject to input saturation.

Design MPC Controller

The basic setup of the MPC controller includes:

• A double integrator as the prediction model

• Prediction horizon of 20

• Control horizon of 10

• Input constraints -1 <= u(t) <= 1

Define the MPC controller setup:

```Ts = 0.1;                                       % Sampling time
p = 20;                                         % Prediction horizon
m = 10;                                         % Control horizon
mpcobj = mpc(tf(1,[1 0 0]),Ts,p,m);     % MPC object
mpcobj.MV = struct('Min',-1,'Max',1);   % Input saturation constraints
nu=1;                                           % Number of manipulated variables
```
```-->The "Weights.ManipulatedVariables" property of "mpc" object is empty. Assuming default 0.00000.
-->The "Weights.ManipulatedVariablesRate" property of "mpc" object is empty. Assuming default 0.10000.
-->The "Weights.OutputVariables" property of "mpc" object is empty. Assuming default 1.00000.
```

To run this example, Simulink® is required.

```if ~mpcchecktoolboxinstalled('simulink')
disp('Simulink(R) is required to run this example.')
return
end
```

Open the Simulink® model. You see an outport mv.seq in the MPC Controller block. Selecting Enable control sequence outport option in the MPC Controller Block Parameters dialog box creates this new outport. The outport exports the optimal control sequence computed by the controller at each sample time to MATLAB Workspace. The logged signal has size of [p, Nu, samples] where p is prediction horizon and Nu is the number of manipulated variables.

```mdl = 'mpc_sequence';
sim(mdl);           % Start simulation
```
```-->Converting the "Model.Plant" property of "mpc" object to state-space.
-->Converting model to discrete time.
-->The "Model.Noise" property of the "mpc" object is empty. Assuming white noise on each measured output channel.
```

Analyze Optimal Control Sequences

Plot the optimal control sequence at specific time instants:

```times = [0 0.2 1 2 2.1 2.2 3 3.5 5];
figure('Name','Optimal sequence history');
for t = 1:9
ct = times(t)*10+1;
subplot(3,3,t);
h = stairs(0:p-1,useq.signals.values(ct,:));
h.LineWidth = 1.5;
hold on
plot((0:p-1)+.5,useq.signals.values(ct,:),'*r');
xlabel('prediction step')
ylabel('u')
title(sprintf('Simulation time t=%5.1f',useq.time(ct)));
grid
axis([0 p -1.1 1.1]);
hold off
end
```

From the plot, you see that the MPC controller uses the first two seconds to bring the output very close to the new set point. The controller output is at the high limit (+1) for one second and then switched to the low limit (-1) for the next second, which is the best control strategy under the input constraint limits.

```bdclose(mdl);
```