MPC Controller Design in MATLAB


MPC Controller Design in MATLAB

Maria
Welcome to our MATLAB process-control tutorial. Today we’re looking at model predictive control, or MPC: the basic idea, how to implement a simple SISO example by hand in MATLAB, and what happens when we tune it or introduce model mismatch.

Michael
Let’s begin with what makes MPC different. At each time step, it uses a model to predict future outputs and solves an optimization problem to choose input moves. It then applies the input decision for the current step and repeats the process as new measurements arrive.

Maria
The optimization aims to minimize a chosen objective, typically tracking error over a prediction horizon, possibly combined with a penalty on the control input or its movement. In many practical MPC formulations, the optimization is a quadratic program. Constraints are also a major advantage: limits on inputs, for example, can be included directly.

Michael
And MPC isn’t limited to one input and one output. This tutorial demonstrates a SISO system, but the formulation can extend to MIMO systems. The prediction matrices just become more involved, and the calculation can become more demanding as the number of inputs and outputs grows.

Maria
That computational cost matters. The tutorial notes that a system with 25 outputs and 20 inputs can require substantially more time to calculate an optimal solution than a small example. The examples here are intended to be fast enough to run interactively.

Michael
Now let’s unpack the prediction and control horizons. Let P be the prediction horizon: how many future output steps we consider. Let M be the control horizon: how many future input moves we optimize. We optimize inputs from u at time k through u at k plus M minus one.

Maria
After those M moves, the input is held at its last optimized value through the rest of the prediction horizon. In other words, for future indices from M through P, the input is constrained to equal the value at the end of the control horizon. That reduces the number of decision variables.

Michael
The tutorial represents the model with a finite impulse response: coefficients h one through h gamma describe how past and current inputs contribute to predicted output. For instance, the prediction at the next step combines h one times the current input with h two times the previous input, continuing through the available coefficients.

Maria
As we predict farther ahead, some of those inputs are still known from the past, while others are the future moves we’re optimizing. That lets us arrange the predicted outputs into a matrix expression: one matrix multiplies the decision variables, and another accounts for known input history.

Michael
That matrix structure is useful in MATLAB. For a linear time-invariant model, those prediction matrices don’t change from one time step to the next. So the code can calculate them ahead of time and reuse them, reducing the computation needed during each optimization.

Maria
The example plant is the transfer function three s plus one, divided by two s squared plus five s plus one. It’s discretized with a sampling time of one second, and the impulse coefficients are calculated using MATLAB’s built-in impulse function.

Michael
For the initial test, the model used by the controller is the same as the plant. Later, the tutorial changes a model coefficient to explore plant-model mismatch. The simulation lasts 100 seconds, and the output and input trajectories are initialized before the controller is run.

Maria
The tuning section defines P and M, along with the sampling time and bias-correction parameters. It also sets input limits from minus 0.5 to 10, and starts with a setpoint of 2.5. These are example choices, not universal settings: the horizons and limits need to suit the application.

Michael
There’s also a parameter lambda that weights the second part of the objective. In the demonstrated code, the two competing aims are tracking the reference and penalizing input magnitude. Changing lambda adjusts how strongly the optimizer cares about that input penalty relative to tracking.

Maria
The runMPC function receives the reference, the model, the tuning settings, and the previous input. It also uses an initial guess for the optimization. First it builds repeated lower and upper input bounds for the M decision variables, using MATLAB’s repmat function.

Michael
The function then evaluates the objective over the prediction horizon: it sums the squared differences between predicted outputs and their reference values. When the input penalty is included, it also sums squared input values over the control horizon, with lambda setting its weight.

Maria
The predicted output vector is assembled using the precomputed matrices: the decision-variable vector contains the future inputs being optimized, and the previous-input data supplies the known history. The code also includes a bias term that can be updated across iterations.

Michael
Once the optimizer returns the planned sequence of inputs, the controller applies the first input move to the plant. At the next time step, it predicts and optimizes again. That repeated re-optimization is the practical heart of receding-horizon control.

Maria
The script calculates predicted output and actual plant output separately. When plant and model match, those predictions should align as expected. When they differ, the gap provides information that can be used in real time for bias correction.

Michael
With the initial matching model, the output reaches the setpoint fairly quickly—around 10 to 15 seconds in the demonstration. The input changes as it drives the output toward the target, and the plotted results let you inspect both the output response and the control effort.

Maria
Then the tutorial compares objectives with and without the squared-input term. Adding that term can encourage smaller input values, but it can also bias the tracking response. Adjusting lambda lets you reduce the influence of that second objective when accurate setpoint tracking is more important.

Michael
Next comes model mismatch. The actual second model coefficient is 0.6323, but the controller’s estimate is changed to 0.1323. With that substantial difference and no bias correction, the response oscillates; the input reaches its lower limit of minus 0.5 for a couple of seconds.

Maria
A smaller mismatch—using 0.3323 as the estimated coefficient—produces a more stable response in the demonstration, but the output still has a deviation from the 2.5 setpoint. The lesson is that performance depends on how accurately the model represents the plant, especially when constraints are active.

Michael
The tutorial then turns on bias correction. A small bias adjustment improves the setup but, with the chosen value, still doesn’t fully eliminate the offset. The bias parameters need tuning according to the size and nature of the mismatch.

Maria
Another proposed approach is to add a filter, such as a Kalman filter, to estimate states online from measurements. The filter can be tuned and coupled with MPC to improve control when the model and plant differ.

Michael
MPC can also track a changing reference rather than a single fixed setpoint. In the example, the reference follows a first-order response with a time constant of five seconds, and the controller follows that trajectory using the matching model.

Maria
The control horizon affects how much freedom the optimizer has. In the demonstration, increasing M from one to three gives it more moves to optimize and produces a smoother input variation. The tutorial also suggests increasing the prediction horizon together with the control horizon to improve the prediction and control.

Michael
There are more ways to customize the objective. You can weight different predicted outputs differently, and you can choose to penalize input magnitude, changes in input, or other quantities, depending on what the application needs.

Maria
Constraints can be added too: input bounds are already present, and the tutorial discusses output bounds and limits on changes in input, often written as delta u. But output constraints may be hard to satisfy exactly if the prediction model is imperfect or relies on a linear finite impulse response approximation.

Michael
That’s why feasibility matters. If constraints are too strict, the optimizer may have no solution. Allowing some room for output constraints to be relaxed can help preserve a feasible optimization problem.

Maria
A coincidence-point constraint can specify that a predicted output at a selected future time should equal its reference. That can help the controller reach the setpoint faster, but it’s an output constraint, so it can also cause feasibility problems.

Michael
The tutorial also presents a terminal constraint: require the predicted output at the end of the prediction horizon to equal the reference there. It describes this as straightforward to implement and as a way to stabilize the controller and avoid oscillatory responses in the demonstrated setting.

Maria
For MIMO systems, the impulse-response coefficients become matrices. A coefficient can describe the effect of several inputs on several outputs, so the prediction matrices grow accordingly. The underlying optimization idea remains the same, though the structure and computational load change.

Michael
The prediction model doesn’t have to be linear in principle. A nonlinear model can be used to predict future output, but then the optimization becomes nonlinear. The tutorial cautions that nonlinear optimization may find a local optimum rather than a global one.

Maria
The broader point is that MPC is a flexible framework, not a single fixed objective or model. You choose the prediction model, the objective, the horizons, and the constraints to fit the application. But each added equality or output constraint can affect feasibility, and stability and feasibility need to be established for the particular design.

Michael
Finally, MATLAB has an MPC Toolbox that provides diagnostic facilities and additional options. This tutorial instead demonstrates the traditional approach: constructing and coding the MPC implementation by hand, so you can see how the prediction, optimization, and tuning pieces fit together.

Maria
So the key workflow is: predict with a model, optimize a constrained sequence of future inputs, apply the first move, compare prediction with the plant, and repeat. Then tune the horizons, weights, bias correction, and constraints while checking both performance and feasibility.

Ask anything
We've read this whole study set, so questions about it get the best answers.