Model Predictive Controller (MPC)

March 23, 2026 · View on GitHub

Overview & Motivation

Model Predictive Control (MPC) is a receding-horizon optimal control strategy that computes control inputs by solving a finite-horizon optimization problem at each time step. Unlike LQR, which applies a fixed gain computed offline, MPC re-solves the optimization online, enabling it to handle constraints on control inputs directly.

At each sample instant, MPC predicts the future system trajectory over a prediction horizon NpN_p, optimizes a sequence of control moves over a control horizon NcNpN_c \leq N_p, applies only the first control move, and repeats at the next step. This receding-horizon approach provides feedback and robustness to disturbances.

Key advantages over LQR:

  • Explicit handling of input constraints (actuator limits)
  • Preview and anticipation of future reference changes
  • Tunable trade-off between horizon length and computational cost

When to use MPC:

  • Systems with actuator saturation or operational limits
  • Multi-input multi-output (MIMO) systems requiring coordinated control
  • Applications where the computational budget allows online optimization (typically Ncm20N_c \cdot m \leq 20)

Mathematical Theory

Prerequisites

  • Discrete-time linear state-space model: xk+1=Axk+Bukx_{k+1} = Ax_k + Bu_k
  • Quadratic cost weighting matrices Q0Q \succeq 0 (state), R0R \succ 0 (control)
  • Optional terminal cost matrix P0P \succeq 0 (typically from DARE)

Core Definitions

Cost function over prediction horizon NpN_p with control horizon NcN_c:

J=k=0Np1xkTQxk+k=0Nc1ukTRuk+xNpTPxNpJ = \sum_{k=0}^{N_p-1} x_k^T Q\, x_k + \sum_{k=0}^{N_c-1} u_k^T R\, u_k + x_{N_p}^T P\, x_{N_p}

For kNck \geq N_c, the control input is held at uNc1u_{N_c-1} or zero (our implementation sets it to zero beyond NcN_c).

Prediction Matrices

The future state trajectory can be expressed as:

X=Ψx0+ΘU\mathbf{X} = \Psi\, x_0 + \Theta\, \mathbf{U}

where X=[x1T,x2T,,xNpT]T\mathbf{X} = [x_1^T, x_2^T, \ldots, x_{N_p}^T]^T and U=[u0T,u1T,,uNc1T]T\mathbf{U} = [u_0^T, u_1^T, \ldots, u_{N_c-1}^T]^T.

State propagation matrix Ψ\Psi (Npn×nN_p n \times n):

Ψ=[AA2ANp]\Psi = \begin{bmatrix} A \\ A^2 \\ \vdots \\ A^{N_p} \end{bmatrix}

Control-to-state matrix Θ\Theta (Npn×NcmN_p n \times N_c m):

Θ=[B00ABB0A2BAB0ANp1BANp2BANpNcB]\Theta = \begin{bmatrix} B & 0 & \cdots & 0 \\ AB & B & \cdots & 0 \\ A^2B & AB & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ A^{N_p-1}B & A^{N_p-2}B & \cdots & A^{N_p-N_c}B \end{bmatrix}

QP Formulation

Substituting predictions into the cost yields a quadratic program in U\mathbf{U}:

J=UTHU+2x0TFTU+constJ = \mathbf{U}^T H\, \mathbf{U} + 2\, x_0^T F^T \mathbf{U} + \text{const}

where:

H=ΘTQˉΘ+RˉH = \Theta^T \bar{Q}\, \Theta + \bar{R} F=ΘTQˉΨF = \Theta^T \bar{Q}\, \Psi

Qˉ\bar{Q} is block-diagonal with QQ on the first Np1N_p - 1 blocks and PP on the last block. Rˉ\bar{R} is block-diagonal with RR repeated NcN_c times.

Unconstrained Solution

Setting UJ=0\nabla_{\mathbf{U}} J = 0:

U=H1Fx0\mathbf{U}^* = -H^{-1} F\, x_0

Solved via Gaussian elimination (HU=Fx0H\, \mathbf{U}^* = -F\, x_0) rather than explicit matrix inversion.

Box Constraints

For control input constraints uminukumaxu_{\min} \leq u_k \leq u_{\max}, a simple projected approach clamps each element of the unconstrained solution. This is a first-order approximation; for tight constraints or state constraints, a full QP solver would be required.

Complexity Analysis

PhaseTimeSpaceNotes
OfflineO(Np2n3)O(N_p^2 \cdot n^3)O((Ncm)2+NpnNcm)O((N_c m)^2 + N_p n \cdot N_c m)Prediction matrices, Hessian, gradient precomputed
OnlineO((Ncm)3)O((N_c m)^3)O((Ncm)2)O((N_c m)^2)Gaussian elimination to solve HU=FxH\mathbf{U}=-Fx
Per stepO(Ncmn)O(N_c m \cdot n)O(Ncm)O(N_c m)Matrix-vector product FxFx + constraint clamping

For typical embedded use (n=2,m=1,Nc=10n=2, m=1, N_c=10): the online solve is a $10 \times 10$ linear system, well within real-time budgets.

Step-by-Step Walkthrough

System: Double integrator with dt=0.1dt = 0.1 s

A=[10.101],B=[0.0050.1]A = \begin{bmatrix} 1 & 0.1 \\ 0 & 1 \end{bmatrix}, \quad B = \begin{bmatrix} 0.005 \\ 0.1 \end{bmatrix}

Q=[10001],R=[0.1],Np=Nc=3Q = \begin{bmatrix} 10 & 0 \\ 0 & 1 \end{bmatrix}, \quad R = [0.1], \quad N_p = N_c = 3

Step 1 — Build Ψ\Psi:

Ψ=[AA2A3]=[10.10110.20110.301]\Psi = \begin{bmatrix} A \\ A^2 \\ A^3 \end{bmatrix} = \begin{bmatrix} 1 & 0.1 \\ 0 & 1 \\ 1 & 0.2 \\ 0 & 1 \\ 1 & 0.3 \\ 0 & 1 \end{bmatrix}

Step 2 — Build Θ\Theta:

Θ=[0.005000.1000.0150.00500.20.100.030.0150.0050.30.20.1]\Theta = \begin{bmatrix} 0.005 & 0 & 0 \\ 0.1 & 0 & 0 \\ 0.015 & 0.005 & 0 \\ 0.2 & 0.1 & 0 \\ 0.03 & 0.015 & 0.005 \\ 0.3 & 0.2 & 0.1 \end{bmatrix}

Step 3 — Compute HH and FF:

H=ΘTQˉΘ+RˉH = \Theta^T \bar{Q} \Theta + \bar{R} produces a $3 \times 3$ positive definite matrix.

F=ΘTQˉΨF = \Theta^T \bar{Q} \Psi produces a $3 \times 2$ matrix.

Step 4 — Online solve for x0=[1,0]Tx_0 = [1, 0]^T:

Solve HU=F[1,0]TH\, \mathbf{U}^* = -F \cdot [1, 0]^T via Gaussian elimination.

Apply first element u0u_0^* to the plant.

Pitfalls & Edge Cases

  • Horizon too short: If NpN_p is too small, the controller is myopic and may produce oscillatory or unstable behavior. Use DARE terminal cost PP to mitigate.
  • Ill-conditioned Hessian: Very large Q/RQ/R ratios create poorly conditioned HH. Keep QQ and RR within 3–4 orders of magnitude of each other.
  • Constraint infeasibility: Box constraints that are too tight for the required control effort will clamp every step, degrading performance. Monitor constraint activity.
  • Fixed-point overflow: For Q15/Q31 types, the Hessian elements grow with horizon length. Keep NcmN_c \cdot m small and scale weights carefully.
  • Stack usage: All matrices are stack-allocated. A system with n=3,m=2,Nc=10n=3, m=2, N_c=10 creates a $20 \times 20$ Hessian (1600 bytes for float). Monitor stack budget.
  • Terminal cost omission: Without terminal cost PP, the controller ignores cost-to-go beyond the horizon, leading to suboptimal or unstable behavior for short horizons.

Variants & Generalizations

VariantKey DifferenceUse Case
Nonlinear MPC (NMPC)Nonlinear prediction model, requires iterative optimizationNonlinear plants, high-accuracy
Explicit MPCPrecomputes control law as piecewise affine functionVery fast online evaluation
Move-Blocking MPCConstrains control moves to change only at specific stepsReduces QP size
Adaptive MPCUpdates plant model onlineTime-varying or uncertain systems
Distributed MPCDecomposes problem across subsystemsLarge-scale multi-agent systems
Robust MPCAccounts for bounded disturbances in constraintsSafety-critical applications

Applications

  • Autonomous vehicles: Path tracking with steering and acceleration limits
  • Process control: Chemical reactor temperature/pressure regulation with valve constraints
  • Robotics: Joint torque-constrained trajectory tracking
  • Power electronics: Voltage/current regulation in converters with switching constraints
  • HVAC systems: Energy-efficient building climate control with comfort bounds
  • Quadrotor control: Attitude and position control with thrust limits

Connections to Other Algorithms

graph LR
    DARE["Discrete Algebraic<br/>Riccati Equation"] -->|terminal cost P| MPC["Model Predictive<br/>Controller"]
    GE["Gaussian<br/>Elimination"] -->|solves H·U = -F·x| MPC
    LQR["LQR Controller"] -.->|special case<br/>N→∞, no constraints| MPC
    MPC -->|first control move| Plant["Plant Model"]
    Plant -->|state measurement| MPC
  • LQR is the infinite-horizon, unconstrained special case of MPC. As NpN_p \to \infty with DARE terminal cost, the MPC gain converges to the LQR gain.
  • DARE computes the terminal cost matrix PP, ensuring the finite-horizon problem approximates the infinite-horizon solution.
  • Gaussian Elimination solves the dense linear system HU=FxH\mathbf{U} = -Fx at each step. For constrained problems, iterative QP solvers could replace this.
  • Kalman Filter provides state estimates when full state measurement is unavailable (MPC + KF = output-feedback MPC).

References & Further Reading

  • Maciejowski, J.M., Predictive Control with Constraints, Prentice Hall, 2002.
  • Rawlings, J.B. and Mayne, D.Q., Model Predictive Control: Theory and Design, Nob Hill Publishing, 2009.
  • Camacho, E.F. and Bordons, C., Model Predictive Control, 2nd ed., Springer, 2007.
  • Borrelli, F., Bemporad, A., and Morari, M., Predictive Control for Linear and Hybrid Systems, Cambridge University Press, 2017.
  • Wang, L., Model Predictive Control System Design and Implementation Using MATLAB, Springer, 2009.