MPC Controller Design in MATLAB
Model Predictive Control: Formulation, Implementation, and Tuning
Model predictive control (MPC) is a method for choosing control actions by repeatedly predicting what a system will do and solving an optimization problem to select the best actions. It is useful when a system has important limits—such as bounds on its inputs—and when its behavior needs to be planned over time rather than corrected only from its current error.
The central idea is to make a plan for several future time steps, apply only the first action in that plan, then measure the system again and make a new plan. This repeated process lets MPC respond to changing conditions and correct errors as they appear.
The basic MPC idea
At each time step, MPC uses a model of the process to predict future outputs. It compares those predictions with a desired output, called the reference or setpoint, and chooses control inputs that minimize a cost.
For a single-input, single-output system, the input is often written as u and the output as y. A typical objective penalizes two things:
- The difference between predicted output and desired output.
- The size of the control input or its changes.
A general form is:
J=i=1∑P(yref(k+i)−y^(k+i))2+λi=0∑M−1(control penalty at k+i)Here, k is the current time step, y^ is the predicted output, P is the prediction horizon, and M is the control horizon. The weight λ determines how much importance is placed on the control penalty relative to tracking the reference.
The first part of the objective encourages the output to follow the desired trajectory. The second discourages excessive control effort or rapid input changes. These goals can compete: a controller may track the reference more aggressively by using larger or faster-changing inputs, while a stronger input penalty tends to produce gentler control actions.
MPC does not choose an input merely from the current error. It chooses inputs by optimizing their predicted effects over a future horizon.
Prediction and control horizons
The prediction horizon P is how far into the future the controller predicts system behavior. A longer horizon can help MPC anticipate consequences that take time to appear.
The control horizon M is how many future input moves the optimizer is allowed to choose independently. Usually, M is no greater than P. After the independently chosen moves, the input is held at its last planned value. In the formulation described here, that means:
u(k+i)=u(k+M−1),M≤i≤PThis is a practical way to limit the number of decision variables. Rather than optimizing a separate input at every future prediction step, the controller chooses only M input values and holds the last one for the remainder of the prediction horizon.
Why MPC handles constraints naturally
Because MPC solves an optimization problem, constraints can be included directly. For example, an input can be required to remain within lower and upper limits:
umin≤u(k+i)≤umaxThese limits can represent physical actuator bounds or operating restrictions. The optimizer then searches for the best plan among those that satisfy the constraints.
A benefit of this approach is that input limits are part of the plan itself, rather than being added afterward to an otherwise unconstrained control calculation. However, adding constraints can make the optimization problem impossible to satisfy, a situation called infeasibility.
Think About It: Why might a controller need to accept slower tracking if an actuator has a strict limit on how far it can move?
Predicting output from an impulse-response model
One model used in the example is a finite impulse response (FIR) model. It describes predicted output as a weighted sum of current and previous inputs. The weights, called impulse-response coefficients, are written as h1,h2,…,hΓ, where Γ is the number of coefficients used.
For example, a prediction at the next time step can be expressed as:
y^(k+1)=h1u(k)+h2u(k−1)+⋯+hΓu(k−Γ+1)The current input u(k) may be an optimization variable. Earlier inputs, such as u(k−1), are already known from the system’s history. Thus, a prediction combines unknown future inputs with known past inputs.
At later future times, the same pattern shifts forward. For instance:
y^(k+2)=h1u(k+1)+h2u(k)+⋯+hΓu(k−Γ+2)This repeated structure makes it possible to arrange all predicted outputs into a vector and express them as a matrix calculation:
Y^=TU+SOPHere, U contains the future input moves being optimized, OP contains known past input information, and T and S collect the appropriate impulse-response coefficients. The term TU describes the contribution of the planned inputs; SOP describes the contribution of inputs already applied.
For a linear time-invariant system, these matrices do not change from one time step to the next, provided the horizons and model remain fixed. They can therefore be computed in advance, reducing the work required during each optimization.
Think About It: Which part of a prediction depends on the optimizer’s choices, and which part comes from the system’s already-known input history?
Receding-horizon control: plan, apply, repeat
Although MPC calculates a sequence of future inputs, it normally applies only the first one. At the next sampling instant, the controller obtains a new output measurement, updates its information, and solves the optimization again.
This is called receding-horizon control:
- Predict future outputs using the model.
- Optimize a sequence of future control moves.
- Apply the first control move.
- Measure the system again.
- Repeat the prediction and optimization with the new information.
Applying only the first move is important because the model may not perfectly match the real process. A fresh measurement lets the next optimization correct the plan rather than blindly following the entire sequence computed earlier.
Implementing MPC in MATLAB
The example uses a single-input, single-output process represented by the continuous-time transfer function:
G(s)=2s2+5s+13s+1The system is discretized with a sampling time of 1 second. An impulse-response function is then used to obtain the coefficients needed for the FIR prediction model.
A typical implementation has several stages:
1. Define the process and prediction model
The process is specified, discretized, and represented by its impulse-response coefficients. Initially, the prediction model is set equal to the simulated plant. This gives a baseline case in which the model and plant agree.
The implementation can later change the model to examine plant–model mismatch—the difference between the model used for prediction and the actual system.
2. Choose simulation and tuning settings
The example sets a simulation length of 100 seconds and initializes the input and output trajectories. It also defines tuning quantities such as:
- P, the prediction horizon.
- M, the control horizon.
- The sampling time.
- Bias-correction settings.
- λ, the weight on the control penalty.
- Lower and upper input limits.
- The reference trajectory.
In the illustrated case, the input limits are −0.5 and 10, and the initial setpoint is 2.5.
3. Precompute the prediction matrices
The matrices that connect future input choices to future output predictions are stored in the tuning structure. Precomputing them is efficient because the system is linear and time-invariant in this example.
4. Solve the optimization at each time step
A function such as runMPC receives the reference, the model, tuning settings, the previous input information, and an initial guess for the optimizer. The input bounds are repeated across the control horizon so that each optimized move obeys the same limits.
The optimizer returns a planned sequence of inputs. The simulation applies the first planned input, then advances the plant to the next time step. The process repeats.
The implementation can calculate both the predicted output and the actual output. When the model and plant differ, these may not match. Their difference provides information that can be used for bias correction.
How objective choices affect performance
A tracking objective alone encourages the output to reach its reference, but it may produce relatively large or abrupt input movements. Adding a control penalty changes that trade-off.
The example compares objectives with and without a term based on the input. The exact effect depends on how the penalty is defined and weighted. A penalty on absolute input discourages large input values; a penalty on input changes discourages rapid movement from one input value to the next. These are related but not identical goals, so the objective should match the desired operating behavior.
If the control penalty is given too much weight, the optimizer may prefer small control actions even when those actions leave a persistent tracking error. Reducing λ gives more emphasis to tracking the reference. Tuning therefore means balancing output accuracy against control effort or smoothness.
With the model matching the plant, the example shows the output reaching the setpoint in roughly 10 to 15 seconds. The input changes as the system moves toward that setpoint. This illustrates how the objective and the input limits jointly determine the response.
Think About It: If a stronger input penalty makes the control action gentler, what trade-off might appear in the output response?
Model mismatch and bias correction
A prediction model is an approximation. If its coefficients differ from the plant’s true coefficients, the predicted output can systematically differ from the measured output. This can cause poor tracking or oscillation, particularly when the optimizer is also trying to respect active input constraints.
In the example, one model coefficient is deliberately changed from its plant value of 0.6323 to 0.1323. The resulting mismatch is large, and the simulated response becomes oscillatory, with the input reaching its lower limit. A smaller mismatch, using 0.3323, gives a more stable-looking response but still leaves a deviation from the setpoint.
One basic correction is to estimate a bias from the difference between predicted and measured output and include that correction in future predictions. Bias correction can reduce steady prediction error, but it must be tuned. A small correction may not remove enough mismatch; a poorly chosen correction can also fail to give the desired response.
Another approach is to use a state estimator, such as a Kalman filter, to estimate internal states from measurements. The estimate can then be used alongside MPC to improve predictions. This is particularly useful when a simple fixed bias does not adequately represent the way the model error changes over time.
Tracking a changing reference and tuning horizons
MPC does not require the target to be a single constant setpoint. The reference can be a trajectory that changes with time. The example uses a first-order reference response with a time constant of 5 seconds. The controller predicts against that changing trajectory and is able to follow it.
The horizons also affect behavior. A control horizon of M=1 allows only one independently chosen future input move. Increasing it to M=3 gives the optimizer more freedom to shape the input sequence; in the example, this produces a smoother response. Increasing prediction and control horizons can improve the controller’s ability to plan ahead, although it also increases the optimization’s computational workload.
Horizon length is therefore a design choice, not a setting that should always be made as large as possible. Longer horizons can provide more foresight and flexibility, but they may require more computation and do not automatically guarantee better performance.
Think About It: Why might a changing reference be easier to follow when the controller plans over several future steps rather than reacting only to the current reference value?
Additional constraints and their consequences
Input limits are only one possible kind of constraint. MPC can also include:
- Output limits, requiring predicted outputs to stay within permitted bounds.
- Input-change limits, requiring changes in the input to stay within specified bounds.
- Terminal constraints, which impose a condition on the predicted output at the end of the prediction horizon.
- Coincidence-point constraints, which require a predicted output at a selected future time to match a reference value.
For example, a terminal constraint can be written as:
y^(k+P)=yref(k+P)Such a condition may encourage the prediction to end at the desired target and can help with stability. However, it does not by itself guarantee stability for every model and application; theoretical analysis is still needed.
Output constraints can be difficult to satisfy because predictions are only as accurate as the model. If the real process differs from the model, the optimizer may calculate a feasible predicted trajectory that the plant cannot actually achieve. Constraints may therefore need to allow some flexibility, and the feasibility of the complete optimization problem must be checked.
Every added constraint narrows the set of possible solutions. If the constraints conflict with one another or with the process’s capabilities, no feasible solution may exist. This is why adding constraints can improve safety or performance while also making controller design more demanding.
Extending MPC beyond the basic example
Multiple inputs and outputs
The same basic framework can be extended from a SISO system to a multiple-input, multiple-output (MIMO) system. In that case, impulse-response coefficients become matrices: each input can affect each output, and the prediction model must account for all those interactions.
The optimization may then contain many more decision variables and prediction terms. A system with many inputs and outputs can require substantially more computation than a small SISO example. The general idea remains the same, but the dimensions and structure of the prediction matrices and objective function must be adapted.
Nonlinear prediction models
MPC is not restricted in principle to linear prediction models. A nonlinear model can be used to calculate predicted outputs. However, the optimization then becomes a nonlinear optimization problem. Such a problem may be more difficult to solve and may produce a local optimum rather than a global optimum.
A quadratic-program formulation is especially useful when the prediction model and constraints produce a suitable quadratic objective with linear constraints. In that setting, the problem can often be solved efficiently. The choice of prediction model therefore influences not only prediction accuracy but also the type and difficulty of the optimization.
Custom objectives
The objective function can be customized to represent the priorities of a particular application. It may emphasize accurate tracking, small input values, small changes in input, or other stated performance goals. Different predicted outputs or future times can also receive different weights.
Customization is powerful, but it should be deliberate. A change in the objective alters what the optimizer considers “best,” while additional constraints can affect whether a feasible solution exists at all.
Stability and feasibility
A working optimization code does not automatically establish that an MPC controller is stable or that it will always find a feasible solution. Those properties depend on the model, objective, horizons, constraints, and operating conditions. In a real application, stability and feasibility require appropriate theoretical analysis and careful validation.
MATLAB’s MPC Toolbox provides a packaged way to develop controllers and offers diagnostic facilities. A hand-coded implementation, like the one described here, makes the prediction and optimization structure more visible and can be adapted to a system’s requirements. Both approaches depend on sound modeling and thoughtful tuning.
Comprehensive review
MPC repeatedly predicts future behavior, optimizes a sequence of control moves, applies only the first move, and then replans using updated measurements. Its prediction model connects planned inputs to predicted outputs; its objective balances tracking against control effort or smoothness; and its constraints describe what the inputs and outputs are allowed to do.
The prediction horizon determines how far ahead the controller looks, while the control horizon determines how many future input moves it chooses independently. Linear FIR predictions can be arranged into matrices and computed efficiently. Model mismatch can lead to prediction errors and poor control, so bias correction or state estimation may be needed. Changing references, multiple inputs and outputs, nonlinear models, and additional constraints can all be accommodated, but each introduces design choices and possible computational or feasibility challenges.
The most important design principle is that an MPC controller’s behavior comes from the combination of its model, objective, horizons, and constraints. Reliable performance depends on choosing and validating all four for the process being controlled.

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