Documentation Center

  • Trial Software
  • Product Updates

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.

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.

    Note   MPCobj 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


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:


To display the list of properties and their current values, type


For the CSTR example this displays:

ManipulatedVariables (MV): [1x1 struct]            
        OutputVariables (OV): [1x2 struct]            
   DisturbanceVariables (DV): [1x1 struct]            
                 Weights (W): [1x1 struct]            
                       Model: [1x1 struct]            
                          Ts: 1                       
                   Optimizer: [1x1 struct]            
       PredictionHorizon (P): 10                      
              ControlHorizon: 2                       
                     History: [2e+003 7 21 20 18 20.1]
                       Notes: {}                      
                    UserData: []                      

(Your History entry will differ.) To alter one of these properties, you can use the syntax

ObjName.PropName = value;

where ObjName is the object name, and PropName is the property you want to set. For example, to change the prediction horizon from 10 (the default) to 15, type:

MPCobj.P = 15;

    Note   You can abbreviate property names provided that the abbreviation is unambiguous.

As shown above, many of the properties are MATLAB® structures containing additional properties. For example, type


which displays:

           Min: -Inf
           Max: Inf
        MinECR: 0
        MaxECR: 0
       RateMin: -Inf
       RateMax: Inf
    RateMinECR: 0
    RateMaxECR: 0
        Target: 'nominal'
          Name: 'T_c'
         Units: ''

This shows that the default controller has no constraints on the manipulated variable. To include constraints as shown in Entering CSTR Manipulated Variable Constraints, you could type

MPCobj.MV.Min = -10;
MPCobj.MV.Max = 10;
MPCobj.MV.RateMin = -4;
MPCobj.MV.RateMax = 4;
MPCobj.MV.Units = 'Deg C';

or use the set command:

set(MPCobj, 'MV', struct('Min', -10, 'Max', 10, ...
	'RateMin', -4, 'RateMax', 4, 'Units', 'Deg C'));

    Note   The Units property is for display purposes only and is optional.

There are two outputs in this case, so MPCobj.OV is a 1-by-2 structure. To set measurement units to the values shown in Controller Options — Weight Tuning Tab, you could type

MPCobj.OV(1).Units = 'Deg C';
MPCobj.OV(2).Units = 'kmol/m^3';

Finally, check the default weights by typing


which displays:

        ManipulatedVariables: 0
    ManipulatedVariablesRate: 0.1000
             OutputVariables: [1 1]
                         ECR: 100000

Change to the values shown in Controller Options — Weight Tuning Tab by typing:

MPCobj.W.ManipulatedVariablesRate = 0.3;
MPCobj.W.OutputVariables = [1 0];

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,


forces MV to change more and more slowly along the prediction horizon. The constraint of -2.5 and 2.5 is used for the fourth step and beyond.

You could also alter the controller's disturbance rejection characteristics using functions that parallel the design tool's disturbance modeling options (described in Disturbance Modeling and Estimation). See the reference pages for the setestim, setindist, and setoutdist functions.

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.

When you use the above syntax (no output variables), sim automatically plots the plant inputs and outputs (not shown, but see Improved Setpoint Tracking for CSTR Temperature and Plant Inputs for Modified Rate Weight for results of a similar scenario).

Simulation Options

You can modify simulation options using the mpcsimopt function. For example, the code

MPCopts = mpcsimopt;
MPCopts.Constraints = 'off';
sim(MPCobj, T, r, MPCopts)

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:

plot(t, y)
Was this topic helpful?