LQR Against MPC on a Finite Rail

An inverted pendulum balanced on a cart that runs on a finite rail

Open the Inverted Pendulum on a Cart app

Advanced Control Exercise 11 of 30 in Optimal Control · All exercises · Time: 15 minutes

An LQR and an MPC share one model, one sample interval and one set of weights on the same unstable pendulum. Only the MPC knows that the rail ends at 0.8 m and that the drive gives at most 20 N. This exercise measures what that knowledge is worth, and why a short MPC needs the LQR's own cost-to-go to stay stable.

Learning Outcomes

  • State the LQR problem and compute its gain from the discrete Riccati equation.
  • Compare an LQR and an MPC that share one model and one set of weights, and name what only the MPC can express.
  • Explain the terminal cost as the bridge between a finite-horizon MPC and the infinite-horizon LQR.

Book Chapter

Nonlinear Dynamics, Oscillations, and Stabilization (Volume 7), Chapter 7, Inverted Pendulum Simulator for Balancing a Pole Without Losing the Cart. Read the chapter, open the PDF, or watch the lecture slides.

Theory in Brief

The linear quadratic regulator minimizes a weighted sum of state error and input effort over an infinite horizon, with no limits. Here `Q` and `R` are weights:

$$\min_{u_0,u_1,\ldots} \; \sum_{k=0}^{\infty} \left( x_k^T Q\, x_k + u_k^T R\, u_k \right) \quad \mathrm{subject\;to} \quad x_{k+1} = A x_k + B u_k$$

Its solution is a constant state feedback `u_k = -K x_k`, with `P` from the discrete algebraic Riccati equation:

$$\begin{gathered} K = \left(R + B^T P B\right)^{-1} B^T P A, \\ P = Q + A^T P A - A^T P B \left(R + B^T P B\right)^{-1} B^T P A \end{gathered}$$

MPC solves the same problem over `N` steps and adds what the plant cannot do. The force limit is hard; the cart position `s_k` and the pole's lean `\theta_k` are soft, with slacks `\epsilon_k \ge 0` priced at `\rho`:

$$\begin{gathered} \min_{u,\epsilon} \sum_{k=0}^{N-1} \left( x_k^T Q\, x_k + u_k^T R\, u_k + \rho\, \epsilon_k \right) + x_N^T P\, x_N \\ \mathrm{s.t.}\quad |u_k| \le F_{max}, \\ |s_k| \le s_{max} + \epsilon_k, |\theta_k| \le \theta_{max} + \epsilon_k \end{gathered}$$

The last term is the terminal cost. With `P` from the Riccati equation it prices everything after the horizon as the LQR would carry on, so while no limit is active the MPC's first move equals `-K x_0`. The two differ only when a limit binds. Without the terminal cost a short plan can choose moves that look cheap for `N` steps and lose the plant afterward.

Model: Physics-based. A cart and pole linearized at upright, `\dot x = A x + B u`, discretized at 10 ms; the plant itself stays nonlinear.
Decision: The force on the cart every 10 ms, from a Riccati gain (LQR) or a quadratic program over a one-second plan (MPC).
Learning: None here. A learned policy for this plant, such as a reinforcement learning controller, would be judged against the cart travel and fall times measured here.

Exercise

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

Predict. The pole is kicked hard toward the end of the rail. Write one sentence on what the LQR does near the 0.8 m line, and one on what the MPC does. Then guess how much closer to the line the MPC's knowledge of the rail keeps the cart.

Run the Inverted Pendulum on a Cart app. Use the fastest speed, 2×, and press Run again to pause at the times in each step.

  1. Open 10 · LQR against MPC on the rail. The MPC is in charge. Press Run and pause at 6 s, after the kick at 2 s. Read the Cart travel and Force saturated tiles.
  2. Press Reset, set Controller structure to LQR before pressing Run, and run to 6 s. Read the same two tiles and note whether the cart crossed the dashed line.
  3. Open 11 · Terminal cost: Off against LQR at a short horizon: the same MPC with a 0.3 s horizon and Terminal cost set to Off. Run it until it stops or reaches 8 s, and read the Pole tile.
  4. Press Reset, switch to Advanced, set Terminal cost to LQR on the MPC card, and run to 8 s. Read Pole and Cart travel.

Explain.

  1. The LQR's gain is optimal for its cost. Why does its cart still cross the line that the MPC respects?
  2. What does the terminal cost stand in for, and why does a short plan without it lose an unstable plant?

Generative AI Learning

"Is MPC with a quadratic cost always better than LQR? Answer for an unstable cart-pole with a rail limit, and say what happens to a short-horizon MPC that has no terminal cost."

Check the answer against your run: A good answer says the two give the same move while no constraint is active, names the Riccati solution as the terminal weight, and predicts that the short plan without it can lose the pole. Your step 3 run is the test. The assistant is your tutor and reviewer; you answer for every claim you turn in.

What to Turn In

A table with one row per run and the numbers each step asks you to read, your written prediction, and three sentences that answer the two questions.

The MPC stops the cart at 0.768 m and the LQR at 0.809 m, against the 0.8 m line. The LQR's cost has no rail in it, so nothing tells it the line is there. The MPC sees the line inside its one-second plan before the LQR's cart reaches it. It holds the drive at its 20 N limit longer (0.24 s against 0.15 s for the LQR) and leans the pole less (28.0° at most, against 31.2°). That is about all the rail buys on this plant, because the travel after a kick is set mostly by the kick and the 20 N drive.

With a 0.3 s horizon and no terminal cost the pole is lost: the Pole tile reads fell at 3.04 s. Inside 0.3 s, stopping the cart costs angle, while the cart's travel after 0.3 s costs the plan nothing. Every plan is optimal for the wrong question. With the LQR's `P` as the terminal cost the same 0.3 s plan holds the pole (the tile reads up) with 0.509 m of cart travel, because `x_N^T P x_N` is the LQR's cost of everything after the horizon.

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.

The Inverted Pendulum page poses this plant in GEKKO. Solve its problem twice: once as given, and once with a bound `|s| \le 0.8` on the cart position `s`. Compare the force profiles. The difference is the price of the rail, which the solver reports as the bound's Lagrange multiplier.

Related

References

  1. Hedengren, J. D. (ed.), Nonlinear Dynamics, Oscillations, and Stabilization, Volume 7 of Interactive Control Engineering, Chapter 7, "Inverted Pendulum Simulator for Balancing a Pole Without Losing the Cart", pp. 158–187, Advanced Process Solutions, 2026. PDF (CC BY 4.0).
  2. Rawlings, J. B., Mayne, D. Q., Diehl, M., Model Predictive Control: Theory, Computation, and Design, 2nd ed., Nob Hill, 2017, Chapter 2 (terminal cost and stability).

Course Information

TCLab

Project

Applications

Advanced Control Exercises

Exams

Modeling

Machine Learning

Estimation

Control

Reinforcement Learning

Related Courses

Admin