Explicit MPC: The Optimizer Solved in Advance

A steel ball suspended below an electromagnet with a position sensor

Open the Magnetic Levitation app

Advanced Control Exercise 19 of 30 in Linear MPC · All exercises · Time: 15 minutes

A magnetically levitated ball is open-loop unstable and decides its fate in milliseconds, too fast for most online optimizers. Explicit MPC solves the quadratic program for every possible state in advance: the result is a table of regions, each with its own affine law. This exercise steps the ball into the current limit under a PID and under the explicit MPC, and compares the table lookup with an online solve.

Learning Outcomes

  • Explain that the solution of a linear MPC's quadratic program is a piecewise affine function of the state.
  • Compare a PID and an explicit MPC on a step whose transient needs the whole current range.
  • Judge when a plant is too fast for an online solve and what a stored table costs instead.

Book Chapter

Nonlinear Dynamics, Oscillations, and Stabilization (Volume 7), Chapter 6, Magnetic Suspension Virtual Laboratory to Teach Control of Unstable Systems. Read the chapter, open the PDF, or watch the lecture slides.

Theory in Brief

For a linear model, quadratic cost and linear constraints, the MPC problem at state `x` is a multiparametric quadratic program in the move vector `z`:

$$z^*(x) = \arg\min_z \; \tfrac{1}{2} z^T H z + x^T F z \quad \mathrm{s.t.} \quad G z \le w + S x$$

For each set of active constraints the optimality conditions are linear in `x`, so the solution is affine within a polyhedral region, and the regions tile the state space:

$$u^*(x) = K_i x + k_i \qquad \text{for} \quad x \in \mathcal R_i = \{x : H_i x \le h_i\}$$

Region 0, where no constraint is active, is the unconstrained LQR. Online, the controller only finds the region that contains the state and evaluates one affine law: microseconds instead of a solve. The price is the table, whose number of regions can grow combinatorially with the horizon and the constraints; here a short horizon of a few steps keeps it small. A PID has no constraint in its design; on a large step its proportional term kicks the current to a limit before the ball has moved.

Model: Physics-based. The ball and coil linearized at the operating gap, with the current limit and slew limit as constraints.
Decision: The coil current command every sample, read from the precomputed region table (or from an online QP for comparison).
Learning: None. The table is computed offline from the model, much as a learned policy is trained offline, but exactly.

Exercise

Read 2 min · Predict 1 min · Run 7 min · Explain 3 min · Check your AI 2 min.

Predict. At 1 s the setpoint steps from 20 to 26 mm, where the ball needs 2.31 A of a 3 A amplifier. Will the PID catch the ball at 26 mm? Will a controller that plans three milliseconds ahead inside the 3 A limit?

Run the Magnetic Levitation app. Use the fastest speed, 2×, and press Run again to pause at the times in each step.

  1. Open 9 · Explicit MPC against the PID: a step into the 3 A ceiling. Keep Controller set to Explicit MPC and press Run; pause at 3 s. Watch the MPC region badge after the step, and read Gap error and Peak current.
  2. Set Controller to PID, press Reset, and run the same step to 3 s or until the ball is lost.
  3. Set the controller back to Explicit MPC, set MPC evaluation to Online QP (Advanced), press Reset and run again. Compare the current trace with step 1.

Explain.

  1. The PID has a derivative term. Why does it still lose the ball when the step drives the current to its limit?
  2. The online QP and the table give the same current. When would you choose each?

Generative AI Learning

"What is explicit MPC? Show why the optimal input of a linear MPC with quadratic cost and linear constraints is a piecewise affine function of the state, and state the main limitation of the approach."

Check the answer against your run: A good answer uses the optimality conditions for a fixed active set to get an affine law per region, and names the growth in the number of regions as the limitation. Your step 1 badge shows regions changing. The assistant is your tutor and reviewer; you answer for every claim you turn in.

What to Turn In

A three-row table (explicit MPC, PID, online QP) with gap error, peak current and regions visited, your written prediction, and three sentences that answer the two questions.

Under the explicit MPC the gap error at 3 s is 0.00 mm with a Peak current of 3.00 A. The plan visits 3 regions of its table during the step and holds the command on the 3 A ceiling for 60 ms. Under the PID the ball is lost at 1.24 s: its proportional term meets the 6 mm error by dropping the current to zero, the ball falls, and when the command pins at 3 A the derivative's braking comes too late.

The MPC's law is the LQR's wherever no limit binds, with three times the PID's rate gain and feedback on the coil current, so it starts braking while the ball is still accelerating. Most of the gain is that state feedback: the app's case text notes that the same LQR clipped at the limits does almost as well, and that the PID holds the step with a 6 A amplifier. What the table adds is the plan's view of the limit over the next few milliseconds. The online QP gives the same current; the badge's time per decision is a wall-clock reading in the browser and too coarse to rank the two here, but a lookup is one region search and one affine evaluation, while a solve iterates. The table wins when the sample time is too short to solve reliably or the hardware is small; the online solve wins when the horizon or constraints make the table too large, or when the model changes and the table would have to be rebuilt.

Measured with the app build of December 3, 2026 by a scripted replay of the steps above. Numbers that no tile shows come from the replay's record; your readings from the trends will be close. A later app release may move the last digit.

Control Tuning discusses MPC horizons. With a double integrator `\ddot y = u`, `|u| \le 1` and a horizon of 3, solve the QP on a grid of initial states in Python and color each point by its set of active constraints. The colored patches are the explicit MPC's regions; count them as the horizon grows.

Related

References

  1. Hedengren, J. D. (ed.), Nonlinear Dynamics, Oscillations, and Stabilization, Volume 7 of Interactive Control Engineering, Chapter 6, "Magnetic Suspension Virtual Laboratory to Teach Control of Unstable Systems", pp. 131–157, Advanced Process Solutions, 2026. PDF (CC BY 4.0).
  2. Bemporad, A., Morari, M., Dua, V., Pistikopoulos, E. N., The explicit linear quadratic regulator for constrained systems, Automatica 38(1), 3–20, 2002.

Course Information

TCLab

Project

Applications

Advanced Control Exercises

Exams

Modeling

Machine Learning

Estimation

Control

Reinforcement Learning

Related Courses

Admin