MPC Controller Design in MATLAB
Model Predictive Control: Formulation and MATLAB Implementation
Core idea
Model predictive control (MPC) is a feedback-control method that repeatedly solves an optimization problem using a model of the process. At each sampling instant, it:
Predicts future outputs over a chosen time window.
Finds a sequence of future inputs that best meets the control objective while respecting constraints.
Applies only the first input from that sequence.
Measures the process again and repeats the calculation.
This repeated re-optimization is called receding-horizon control. Because MPC explicitly includes limits in its optimization, it is well suited to processes where inputs or outputs must stay within specified bounds. For a linear prediction model and a quadratic objective with linear constraints, the optimization is commonly a quadratic program (QP).
MPC can be used for both single-input, single-output (SISO) and multiple-input, multiple-output (MIMO) systems. Larger systems generally require more computation because the optimization contains more decision variables and predictions.
Prediction model and horizons
Finite impulse response model
A finite impulse response (FIR) model predicts the output using impulse-response coefficients h1,…,hγ. For example, the prediction one step ahead is
The prediction two steps ahead is
Thus, each future output is a weighted sum of recent and planned inputs. Inputs before the current time are already known; future inputs are the variables that MPC can choose.
Prediction horizon and control horizon
Prediction horizon, P: The number of future output samples considered in the objective.
Control horizon, M: The number of future input moves that are independently optimized.
The decision vector is
Usually, only these M input values are independent decision variables. For later predictions, the input is held at the final optimized value:
This reduces the number of optimization variables while still allowing predictions to extend across the full prediction horizon.
Matrix form of the predictions
Because the model is linear, the vector of predicted outputs can be written as an affine function of the decision variables:
Here:
Y^ contains the predicted outputs over the prediction horizon.
T multiplies the future input decisions.
OP contains known past input information.
S maps those known past inputs into the future predictions.
For a linear time-invariant model, these matrices do not change from one sampling instant to the next. They can therefore be calculated before the control loop begins, reducing the online computation required.
Objective function and constraints
Tracking objective
MPC seeks to keep predicted outputs close to a reference trajectory. A basic tracking objective is
where r(k+i) is the desired reference at future time k+i and y^(k+i) is the model prediction.
An input penalty can be added:
The parameter λ determines the relative importance of the input penalty compared with tracking. A larger value makes the optimizer more reluctant to use large input values; a smaller value gives tracking error greater influence.
Important distinction: The implementation described uses a penalty on the absolute input, u(k+i)2. A conventional “move suppression” penalty instead penalizes changes in input, such as Δu(k+i)=u(k+i)−u(k+i−1). These objectives are not equivalent: penalizing u2 favors small input magnitudes, while penalizing (Δu)2 favors smoother changes.
The weighting structure can be customized. For example, different future predictions may receive different weights, or separate weights may be assigned to tracking error and input effort.
Input constraints
The input can be restricted to an allowable range:
These limits can be imposed on each optimized input move. In the example implementation, the input bounds are −0.5 and 10.
Input constraints are a central advantage of MPC: the optimizer accounts for them while selecting its control action rather than relying on a separate mechanism to clip an otherwise unconstrained command.
Other possible constraints
MPC can be extended with additional constraints, including:
Output limits, requiring predicted outputs to stay between lower and upper bounds.
Input-change limits, restricting Δu to a permitted range.
Coincidence-point constraints, requiring an output prediction at a selected future time to equal a desired reference.
Terminal constraints, requiring the final predicted output to meet the final reference.
For example, a terminal constraint may be written as
A terminal condition can help improve closed-loop behavior and may contribute to stability when properly designed. However, adding constraints can make the optimization infeasible. Output constraints are especially vulnerable to this problem when the real plant differs from the model or when the prediction model is only an approximation. Allowing a constraint to be relaxed, for example by introducing a slack variable, can preserve feasibility while limiting the amount of violation.
MATLAB implementation structure
Defining and discretizing the plant
The example uses the continuous-time transfer function
It is discretized with a sampling time of 1 second. The impulse response is then calculated and used to obtain the FIR coefficients h1,…,hγ.
The first model used for prediction is identical to the simulated plant. This provides a baseline before introducing model–plant mismatch.
Simulation and tuning parameters
The simulation length is set to 100 seconds. Output and input trajectories are initialized, and the controller’s tuning parameters are specified. These include:
P, the prediction horizon.
M (called N in the code description), the control horizon.
The sampling time.
Bias-correction settings.
λ, the input-penalty weight.
Lower and upper input limits.
The reference or setpoint trajectory.
These parameters are application-dependent. Their selection affects tracking, input behavior, feasibility, and computation time.
Precomputing prediction matrices
For a linear time-invariant model, the matrices that map the decision variables and past inputs into future predictions remain constant. The implementation computes and stores these matrices in advance. This avoids rebuilding them at every control step and reduces online computation.
Solving the optimization at each step
The runMPC function receives the reference, the prediction model, tuning parameters, the previous input, and an initial guess for the optimization. It constructs repeated input bounds for the M decision variables; MATLAB’s repmat function can be used for this purpose.
At each sampling instant, the function:
Forms the predicted-output vector from the prediction matrices and the candidate input sequence.
Adds any bias correction to those predictions.
Evaluates the tracking and input portions of the objective.
Solves for the optimized sequence u(k),…,u(k+M−1).
Returns the solution to the main simulation code.
The controller applies only the first optimized input, u(k). At the next sampling instant, it uses the latest output information and solves a new optimization problem.
Predicted and actual outputs
The implementation calculates both the output predicted by the model and the output produced by the simulated plant. If the plant and model differ, these values will not match. Their difference can be used to update the prediction bias:
A bias estimate can then be added to future predictions. In the implementation described, the current bias is repeated across the prediction horizon when constructing the corrected prediction vector. Bias correction can improve tracking in the presence of persistent model error, but it may need tuning and may not fully address substantial mismatch.
Interpreting the example responses
Matching model and plant
With the prediction model initially identical to the plant and a constant reference of 2.5, the simulated output reaches the setpoint in roughly 10–15 seconds. The input initially is about 3.3 and then moves toward approximately 2.5.
Changing the objective changes the input and output behavior. With no input penalty, the optimizer focuses more strongly on reducing tracking error. Adding an input penalty can reduce the size of input values, but excessive weight may leave a steady tracking error. Reducing λ gives tracking more influence and can bring the output closer to the setpoint.
Plant–model mismatch
A model–plant mismatch occurs when the model used for predictions has different parameters from the actual simulated plant. The example changes one model coefficient while leaving the other parameters correctly estimated.
A substantial coefficient error produces an oscillatory response in the example. The input reaches its lower constraint of −0.5 for a short time, illustrating how input saturation can interact with an inaccurate prediction model. A smaller mismatch produces a more stable response, but a visible offset from the setpoint remains.
Bias correction can reduce this offset, but the correction must be tuned to the size and character of the mismatch. If the model differs significantly from the plant, a state estimator such as a Kalman filter can be used to estimate the process state from measurements and improve the values supplied to the MPC prediction. The estimator and MPC can then be used together.
Changing reference trajectories
The reference does not have to be a fixed setpoint. It can vary with time. In the example, the reference follows a first-order trajectory with a time constant of 5 seconds, and the controller tracks that changing trajectory.
Providing a reference trajectory allows the controller to plan toward a changing target rather than responding only to a single constant setpoint.
Changing the control and prediction horizons
The example begins with a control horizon of M=1. Increasing it to M=3 provides more independently adjustable future input moves, giving the optimizer more flexibility and producing a smoother input trajectory in the demonstrated case.
Increasing the prediction horizon can also help the controller anticipate future behavior. Larger horizons, however, increase computational effort. Horizon choices should be made with regard to the process dynamics, the desired response, and available computation.
Extending MPC beyond the basic example
MIMO systems
For a MIMO process, each impulse-response coefficient becomes a matrix relating multiple inputs to multiple outputs. For example, a coefficient matrix contains contributions from each input to each output. The prediction and objective structures must account for these cross-couplings, but the underlying MPC approach remains the same.
The computational burden grows with the number of inputs, outputs, and decision variables. A system with many inputs and outputs may require substantially more time to solve each online optimization than a small SISO example.
Nonlinear prediction models
The MPC framework does not inherently require a linear prediction model. A nonlinear model can be used to predict future outputs. However, this generally makes the optimization nonlinear rather than a QP. Nonlinear optimization may converge to a local optimum instead of a global optimum and can require more computation.
With a linear prediction model, a quadratic objective, and linear constraints, the optimization has the structure of a QP. Other combinations of models and objectives require a solver suited to the resulting optimization problem.
Custom objectives and constraints
The objective can be tailored to the application. In addition to tracking error and input effort, it can penalize input changes or place different weights on selected predictions. Constraints can likewise be added for inputs, outputs, input changes, and terminal behavior.
Every added constraint should be checked for feasibility. A controller may fail to find an admissible solution if the requested output target cannot be reached while satisfying all limits. Stability and feasibility therefore require careful design and, for rigorous guarantees, theoretical analysis.
Key takeaways
MPC repeatedly predicts, optimizes, applies the first input, and recalculates using new measurements.
The prediction horizon determines how far ahead outputs are considered; the control horizon determines how many future input moves are optimized.
FIR coefficients make each predicted output a linear combination of known past inputs and planned future inputs.
Input limits and other constraints can be incorporated directly into the optimization.
Objective weights govern the trade-off between tracking and input effort; penalizing absolute input differs from penalizing changes in input.
Model–plant mismatch can degrade performance. Bias correction or state estimation may improve predictions, but neither removes the need for a suitable model and careful tuning.
MIMO and nonlinear MPC are possible, though they can increase the complexity and computational cost of the optimization.

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