Main Content

Design MPC Controller in Simulink

This example shows how to design a model predictive controller for a continuous stirred-tank reactor (CSTR) in Simulink® using MPC Designer.

This example requires Simulink Control Design™ software to define the MPC structure by linearizing a nonlinear Simulink model.

If you do not have Simulink Control Design software, you must first create an mpc object in the MATLAB® workspace. For more information, see Design Controller Using MPC Designer and Design MPC Controller at the Command Line.

CSTR Model

The nonlinear model of a Continuously Stirred Tank Reactor (CSTR) is described in CSTR Model. In the model, the inputs are:

  • Feed Concentration (CAi) — The concentration of reagent A in the feed stream (kgmol/m3)

  • Feed Temperature (Ti) — Feed stream temperature (K)

  • Coolant Temperature (Tc) — Reactor coolant temperature (K)

while the state variables (and outputs) are:

  • CSTR Temperature (T) — Reactor temperature (K)

  • Concentration (CA) — Concentration of reagent A in the product stream, also referred to as the residual concentration (kgmol/m3)

The control objective is to maintain the residual concentration, CA, at its nominal setpoint by adjusting the coolant temperature, Tc. Changes in the feed concentration, CAi, and feed temperature, Ti, cause disturbances in the CSTR reaction.

The reactor temperature, T, is usually measured. However, for this example, ignore the reactor temperature, and assume that the residual concentration is measured directly.

Open the Simulink model.

open_system('CSTR_ClosedLoop')

Connect Measured Disturbance Signal

In the Simulink model window, double-click the MPC Controller block.

In the Block Parameters dialog box, on the General tab, select the Measured disturbance (md) option.

Click Apply to add the md input port to the controller block.

In the Simulink model window, connect the Feed Temperature block output to the md input port.

Linearize Simulink Model

In this example, you linearize the Simulink model from within MPC Designer, which requires Simulink Control Design software. For more information, see Linearize Simulink Models Using MPC Designer.

If you do not have Simulink Control Design software, you must first create an mpc object in the MATLAB workspace and specify that controller object in the MPC Controller block.

To open MPC Designer, open the MPC Controller block and click Design.

In MPC Designer, on the MPC Designer tab, in the Structure section, click MPC Structure.

In the Define MPC Structure By Linearization dialog box, in the Controller Sample Time section, specify a sample time of 0.1.

In the MPC Structure section, click Change I/O Sizes to add the unmeasured disturbance and measured disturbance signal dimensions.

In the MPC Block Signal Sizes dialog box, specify the number of input/output channels of each type.

Click OK.

In the Define MPC Structure By Linearization dialog box, in the Simulink Signals for Plant Inputs section, the app adds a row for Unmeasured Disturbances (UD).

The manipulated variable, measured disturbance, and measured output are already assigned to their respective Simulink signal lines, which are connected to the MPC Controller block.

In the Simulink Signals for Plant Inputs section, select the Unmeasured Disturbances (UD) row, and click Select Signals.

In the Simulink model window, click the output signal from the Feed Concentration block.

The signal is highlighted and its block path is added to the Select Signal dialog box.

In the Select Signals dialog box, click Add Signal(s).

In the Define MPC Structure By Linearization dialog box, in the Simulink Signals for Plant Inputs table, the Block Path for the unmeasured disturbance signal is updated.

In this example, you linearize the Simulink model at a steady-state equilibrium operating point where the residual concentration is 2 kgmol/m3. To compute such an operating point, add the CA signal as a trim output constraint, and specify its target constraint value.

In the Simulink model window, select the signal line connected to CA output port of the CSTR block.

On the Apps tab, click Linearization Manager. Then, on the Linearization tab, in the Insert Analysis Points gallery, select Trim Output Constraint.

The CA signal can now be used to define output specifications for calculating a model steady-state operating point.

In the Define MPC Structure By Linearization dialog box, in the Simulink Operating Point section, in the drop-down list, select Trim Model.

When using MPC Designer in MATLAB Online™, trimming is not supported. You must linearize your model at the model initial conditions.

In the Trim the model dialog box, on the Outputs tab, check the box in the Known column for Channel-1 and specify a Value of 2.

This setting constrains the value of the output signal during the operating point search to a known value.

Click Start Trimming.

In the Define MPC Structure By Linearization dialog box, in the Simulink Operating Point section, the computed operating point, op_trim1, is added to the drop-down list and selected.

In the drop-down list, under View/Edit, click Edit op_trim1.

In the Edit dialog box, on the State tab, in the Actual dx column, the near-zero derivative values indicate that the computed operating point is at steady-state.

To set the initial states of the Simulink model to the operating point values in the Actual Values column, click Initialize model. Doing so enables you to later simulate the Simulink model at the computed operating point rather than at the default model initial conditions.

In the Initialize Model dialog box, click OK.

When setting the model initial conditions, MPC Designer exports the operating point to the MATLAB workspace. Also, in the Simulink Configuration Parameters dialog box, in the Data Import/Export section, it selects the Input and Initial state parameters and configures them to use the states and inputs in the exported operating point.

To reset the model initial conditions, for example if you delete the exported operating point, clear the Input and Initial state parameters.

Close the Edit dialog box.

In the Define MPC Structure By Linearization dialog box, linearize the model by clicking Define and Linearize.

In the Data Browser, the app adds the following items.

  • Linearized plant model plant

  • Default MPC controller mpc1 created using the linearized plant as an internal prediction model

  • Default simulation scenario scenario1

Define Input/Output Channel Attributes

On the MPC Designer tab, in the Structure section, click I/O Attributes.

In the Input and Output Channel Specifications dialog box, in the Name column, specify meaningful names for each input and output channel.

In the Unit column, specify appropriate units for each signal.

The Nominal Value for each signal is the corresponding steady-state value at the computed operating point.

Click OK.

Define Disturbance Rejection Simulation Scenarios

The primary objective of the controller is to hold the residual concentration CA at the nominal value of 2 kgmol/m3. To do so, the controller must reject both measured and unmeasured disturbances.

On the MPC Designer tab, in the Scenario section, select Edit Scenario > scenario1.

In the Simulation Scenario dialog box, in the Reference Signals table, in the Signal drop-down list select Constant to hold the output setpoint at its nominal value.

In the Measured Disturbances table, in the Signal drop-down list, select Step.

Specify a step Size of 10 and a step Time of 0.

Click OK.

In the Data Browser, under Scenarios, click scenario1. Click scenario1 a second time, and rename it MD_reject.

In the Scenario section, click Plot Scenario > New Scenario.

In the Simulation Scenario dialog box, in the Unmeasured Disturbances table, in the Signal drop-down list, select Step.

Specify a step Size of 1 and a step Time of 0.

Click OK.

In the Data Browser, under Scenarios, rename NewScenario to UD_reject.

Arrange Output Response Plots

To make viewing the tuning results easier, arrange the plot area to display the Output Response plots for both scenarios at the same time.

On the View tab, in the Tiles section, click Top/Bottom. The View tab is not supported in MATLAB Online.

The plot display area changes to display the Input Response plots above the Output Response plots.

Drag and select the plots so that MD_reject: Output tab is in the upper plot area and the UD_reject: Output plot is in the lower plot area.

Tune Controller Performance

On the Tuning tab, in the Horizon section, specify a Prediction horizon of 20 and a Control horizon of 5.

The Output Response plots update based on the new horizon values.

Use the default controller constraint and weight configurations.

In the Performance Tuning section, drag the Closed-Loop Performance slider to the right, which leads to tighter control of outputs and more aggressive control moves. Drag the slider until the MD_reject: Output response reaches steady state in less than two seconds.

Drag the State Estimation slider to the right, which leads to more aggressive unmeasured disturbance rejection. Drag the slider until the UD_reject: Output response reaches steady state in less than 3 seconds.

Update Simulink Model with Tuned Controller

In the Analysis section, select Export Controller > Update Block Only. The app exports tuned controller mpc1 to the MATLAB workspace. In the Simulink model, the MPC Controller block is updated to use the exported controller.

Simulate Unmeasured Disturbance Rejection

In the Simulink model window, on the Simulation tab, change Stop Time to 5 seconds.

The model initial conditions are set to the nominal operating point used for linearization.

To simulate a unit step in the feed concentration at time zero, open the Feed Concentration block and increase its Constant value parameter from 10 to 11.

In the Simulink model window, open the Concentration scope and run the simulation.

The output response is similar to the UD_reject response, however the settling time is around 1 second later. The different result is due to the mismatch between the linear plant used in the MPC Designer simulation and the nonlinear plant in the Simulink model.

Simulate Measured Disturbance Rejection

To simulate the measured disturbance rejection, first return the Feed Concentration block to its nominal value of 10.

To simulate a step change in the feed temperature at time zero, open the Feed Temperature block and increase its Constant value parameter from 300 to 310.

Run the simulation.

The output response is similar to the MD_reject response from the MPC Designer simulation.

See Also

Apps

Blocks

Related Topics