S940 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 N 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 xk+1=axk+0.5uk with state weight 1, against the input weight r. 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, ab/(r+b2).
  • When inputs are cheap (small r) the two agree; as r 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 a above 1 (an unstable plant) and watch the one-step gain fall below what stability needs, |a−bK|<1, for large r.

Predict

For xk+1=1.1xk+0.5uk with cost xk+12+0.1uk2 over one step, what input does MPC apply when xk = 1?

Name it

The receding horizon

At each sample k, with the current state xk measured or estimated (lesson 39's Kalman filter):

  1. Predict the states over a horizon of N steps for any candidate inputs uk,…,uk+N−1, using the model.
  2. Choose the inputs that minimize a cost, typically ∑i=0N−1(xk+iTQxk+i+uk+iTRuk+i)+xk+NTPxk+N, subject to constraints such as |u|≤umax and |x|≤xmax.
  3. Apply only uk. 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, PN=P (terminal weight), and

Pi=Q+ATPi+1A−ATPi+1B(R+BTPi+1B)−1BTPi+1A

The first input is uk=−K0xk with K0=(R+BTP1B)−1BTP1A. As N grows, P1 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 N×m variables (m 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 xk+1=1.1xk+0.5uk with q = 1 and r = 0.1, compare the one-step MPC gain and the LQR gain, and the closed-loop poles.

Show the solution
  1. One-step MPC gain. With terminal weight q: K=qabr+qb2=0.55/0.35=1.571
  2. Its closed loop. a−bK=1.1−0.5×1.571=0.314
  3. LQR Riccati equation. Iterating P=q+a2P−(abP)2r+b2P converges to P = 1.375.
  4. LQR gain. K=abPr+b2P=1.704 with the closed loop at 0.248.
  5. 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 x0 = 4 with the input limited to |u|≤2. What does the LQR law command at the first step, and what happens if the command is simply clipped?

Show the solution
  1. What LQR commands. −1.704×4=−6.8 far beyond the limit.
  2. Clip it. Clipped to −2, the state goes to 1.1×4−1=3.4
  3. The next step. LQR then asks for −5.8, clipped to −2 again: 1.1×3.4−1=2.74 and so on, recovering only slowly.
  4. 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.
  5. Where they differ. With state constraints, or plants where an early move prevents a later violation, MPC's planned inputs differ from clipped LQR.
  6. A more unstable plant. If a>1+b×2/|x|, 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
  1. Decision variables. 2×10=20 per solve.
  2. Time per sample. 1/20kHz=50μs minus the time to measure and apply: perhaps 30 µs for the solver.
  3. 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 xk+1=1.1xk+0.5uk minimizes 2xk+12+1uk2. What feedback gain K (with u=−Kx) does it apply?

Check

With that one-step MPC gain, where is the closed-loop pole of xk+1=1.5xk+0.5uk (weights q = 2, r = 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 xk+1=1.2xk+0.5uk has its input limited to |u|≤ 2. Beyond what |x| 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 xk+1=1.1x+0.5u, q = 1, terminal weight PN=q, 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 |K0−Klqr|<10−6, 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.

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.