S9 · Lesson 40 · Model predictive control
Optimizing the next few moves at every sample, with the limits built in
Lesson 38's LQR finds the best linear feedback for an infinite future, but it knows nothing about limits: when the actuator saturates (lesson 43), the optimal law isn't optimal anymore, and may not even be stable. Chemical plants, power converters, autonomous cars and data-center cooling are full of limits on valves, currents and temperatures. Model predictive control handles them directly: at every sample, use a model to predict the next steps, solve an optimization for the best input sequence that respects every constraint, apply only the first input, and repeat at the next sample with fresh measurements. Without constraints and with a long enough horizon, it reproduces LQR; with them, it does what no fixed gain can.
- Feedback gains for the scalar system with state weight 1, against the input weight . The solid curve is the infinite-horizon LQR gain, from the closed-form solution of the scalar Riccati equation; the dashed curve is a one-step MPC, .
- When inputs are cheap (small ) the two agree; as grows, the one-step controller under-reacts, because it can't see that a little effort now saves a lot later. A longer horizon closes the gap.
- Push above 1 (an unstable plant) and watch the one-step gain fall below what stability needs, , for large .
Predict
For with cost over one step, what input does MPC apply when = 1?
Minimizing : the derivative gives = −1.571. It doesn't cancel the state completely (that would be −2.2), because the input costs something. The closed loop becomes : stable.
Name it
The receding horizon
At each sample , with the current state measured or estimated (lesson 39's Kalman filter):
- Predict the states over a horizon of steps for any candidate inputs , using the model.
- Choose the inputs that minimize a cost, typically , subject to constraints such as and .
- Apply only . At the next sample, measure again and re-solve.
Without constraints
For a linear model and quadratic cost, the optimal inputs follow from a backward Riccati recursion over the horizon, (terminal weight), and
The first input is with . As grows, converges to the solution of lesson 38's Riccati equation and MPC becomes LQR.
With constraints
Bounds on inputs and states turn each step into a quadratic program: a convex optimization with variables ( inputs). Clipping the unconstrained answer isn't the same: the optimizer plans ahead, for example braking early because it knows the actuator will saturate. Solvers for small QPs run in microseconds, fast enough for power converters; larger problems use slower sample rates.
Practical issues
- Stability: a short horizon without a terminal cost can be unstable even when each optimization succeeds; a terminal weight equal to the LQR solution, or a long horizon, guarantees it.
- Feasibility: if a disturbance makes the constraints impossible to meet, the solver fails; soft constraints with penalties keep it answering.
- Model error and offset: MPC is only as good as its model; integral action comes from estimating a disturbance state and including it in the model.
Worked example
For with = 1 and = 0.1, compare the one-step MPC gain and the LQR gain, and the closed-loop poles.
Show the solution
- One-step MPC gain. With terminal weight :
- Its closed loop.
- LQR Riccati equation. Iterating converges to P = 1.375.
- LQR gain. with the closed loop at 0.248.
- Compare. The one-step controller is a little less aggressive; with a horizon of a few steps the MPC gain matches LQR.
Answer the one-step MPC gain is 1.571 (pole at 0.314) and the LQR gain is 1.704 (pole at 0.248).
Worked example
The same plant starts at = 4 with the input limited to . What does the LQR law command at the first step, and what happens if the command is simply clipped?
Show the solution
- What LQR commands. far beyond the limit.
- Clip it. Clipped to −2, the state goes to
- The next step. LQR then asks for −5.8, clipped to −2 again: and so on, recovering only slowly.
- Compare MPC. Here clipping happens to give the same first moves an MPC would choose, since saturating as hard as possible is the best anyone can do.
- Where they differ. With state constraints, or plants where an early move prevents a later violation, MPC's planned inputs differ from clipped LQR.
- A more unstable plant. If , no input sequence could recover from large states: the limits define a region of attraction, which MPC makes explicit.
Answer LQR commands −6.8; clipped to −2, the state goes from 4 to 3.4 to 2.74 and recovers only slowly.
Worked example
An MPC for a three-phase converter has 2 inputs, a horizon of 10 steps and runs at 20 kHz. How many decision variables does each QP have, and how long does the solver get?
Show the solution
- Decision variables. per solve.
- Time per sample. minus the time to measure and apply: perhaps 30 µs for the solver.
- Is that enough? A dense QP of that size takes a few thousand floating-point operations per iteration and converges in tens of iterations on a microcontroller or an FPGA, which is why short horizons are common in power electronics.
Answer 20 decision variables, with perhaps 30 µs of each 50 µs sample for the solver.
Check
A one-step MPC for minimizes . What feedback gain (with ) does it apply?
Check
With that one-step MPC gain, where is the closed-loop pole of (weights = 2, = 1)?
Check
An MPC has 2 inputs and a horizon of 5 steps. How many decision variables does each optimization have?
Check
An unstable plant has its input limited to 2. Beyond what can no input sequence bring it back?
Check
Why does MPC apply only the first input of its optimized sequence instead of the whole sequence?
Check
Compute MPC gains by the backward Riccati recursion for , = 1, terminal weight , input weight r = 0.5. Write k0(N), the first-step gain for horizon N. Set K_lqr (iterate the recursion 10 000 times), N_match, the smallest horizon with , and K1 = k0(1).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
A building's chiller plant is controlled by MPC with a 24-hour horizon and electricity prices that change by the hour. Explain why MPC can save money where a PID loop on each chiller can't.
A PID loop reacts only to the current error: it holds each zone's temperature at its setpoint and runs chillers whenever cooling is needed, whatever the electricity costs at that hour. MPC uses a model of the building's thermal mass, a forecast of weather and occupancy, and the price schedule to plan the whole day's cooling as an optimization: it can precool the building in the cheap early hours, letting temperatures drift up within their allowed band during expensive afternoon peaks, while respecting every constraint on comfort and equipment capacity. It applies the first hour's plan, then replans with new measurements and forecasts, which corrects for model and forecast errors. The savings come from planning with knowledge of the future and of the limits, which a feedback loop with fixed gains doesn't have.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.