Design Controller at the Command Line

Design Controller Using the Design Tool shows how to use
the Model Predictive Control Toolbox™ design tool to create a controller
and test it. You might prefer to use functions instead. They allow
access to options not available in the design tool, as well as automation
of repetitive tasks using scripts and customized plotting.

Create a Controller Object

This topic uses the CSTR model described
in CSTR Model as an example. You
must have the linear plant model CSTR in the base
workspace before beginning this tutorial.

Use the mpc function to create a controller.
For example, type

Ts = 1;
MPCobj = mpc(CSTR,Ts);

to create one based on the CSTR model with
a control interval of 1 time unit and all other parameters at their
default values.

NoteMPCobj is an MPC object. It
contains a complete controller definition for use with Model Predictive Control Toolbox software.

To display the controller's properties in the Command Window,
type

display(MPCobj)

or type the object's name without a trailing semicolon.

View and Alter Controller Properties

Once you've defined an MPC object, it's easy to alter its properties.
For a description of the editable properties, type:

You can also specify time-varying weights and constraints. The
time-varying weights and constraints are defined for the prediction
horizon, which shifts at each time step. This implies that as long
as the property is not changed, the set of time-varying parameters
is the same at each time step. To learn how to specify time-varying
constraints and weights in the GUI, see "Constraints
Tab" and "Weight
Tuning Tab".

The time-varying weights modify the tuning of the unconstrained
controller response. To specify a different weight for each step in
the prediction horizon, modify the Weightproperty.
For example,

MPCobj.W.OutputVariables = [0.1 0;0.2 0;0.5 0;1 0];

deemphasizes setpoint tracking errors early in the prediction
horizon. The default weight of 1 is used for the fourth step and beyond.

Constraints can also be time varying. The time-varying constraints
have a nonlinear effect when they are active. For example,

Review Controller Design

Use the review command to
examine the potential controller stability and performance issues
at runtime. review generates a report on the
results of the tests it performs.

review(MPCobj)

In this example, the review command found
two potential issues in this design. The first warning asks whether
the user intends to have zero weight on the C_A output.
The second warning asks the user to avoid having hard constraints
on both MV and MVRate.

Perform Linear Simulations

To run a linear simulation, use the sim function.
For example, given the MPCobj controller defined
in the previous section, type:

T = 26;
r = [2 0];
sim(MPCobj,T,r);

This simulates the closed-loop response for a duration of 26
control intervals with a setpoint of 2 for the first output (the reactor
temperature) and 0 for the second output (the residual concentration).
Recall that the second output's tuning weight is zero (see the discussion
in Output Weights), so its setpoint is ignored.

By default, the same linear model is used for controller predictions
and the plant, i.e., there is no plant/model mismatch. You can alter
this as shown in Simulation Options.

Simulation Options

runs an unconstrained simulation. Comparing to the case described
earlier, the controller's first move now exceeds 4 units (the specified
rate constraint).

Other options include the addition of a specified noise sequence
to the manipulated variables or measured outputs, open-loop simulations,
a look-ahead option for better setpoint tracking or measured disturbance
rejection, and plant/model mismatch.

For example, the following code defines a new plant model having
gains 50% larger than those in the CSTR model used
in the controller, then repeats the above simulation:

Plant = 1.5*CSTR;
MPCopts.Model = Plant;
sim(MPCobj,T,r,MPCopts)

In this case, the plant/model mismatch degrades controller performance,
but only slightly. Degradation can be severe and must be tested on
a case-by-case basis.

Save Calculated Results

If you'd like to save simulation results in your workspace,
use the following sim function format:

[y,t,u] = sim(MPCobj,T,r);

This suppresses automatic plotting, instead creating variables y, t,
and u, which hold the computed outputs, time, and
inputs, respectively. A typical use is to create customized plots.
For example, to plot both outputs on the same axis versus time, you
could type: