Foundations to Frontier
Tests
L. Reinforcement Learning and Autonomy › L6 Planning and Control

Optimal control and LQR

The linear–quadratic regulator (LQR) is the one control problem with a closed-form optimal answer: for linear dynamics and quadratic cost, the optimal action is a fixed linear function of the state, u = −Kx, and the gain K comes from the Riccati equation, which is dynamic programming carried out exactly. This page linearises a cart-pole, solves the Riccati equation, balances the real nonlinear system, measures how the control weight trades effort against speed, and uses iterative LQR (iLQR) to swing a pendulum up and keep it there on a mismatched model.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Steering by a simple rule

Balancing a broom on your hand, you watch how far it leans and how fast it is falling, and move your hand by an amount proportional to both. Engineers turned that intuition into mathematics: if a system’s motion is roughly linear and you measure cost as squared errors plus squared effort, the best possible controller really is a weighted sum of what you measure. Choosing the weights is the whole problem, and the Riccati equation solves it exactly.

Planning and control lab
angle (±0.40 rad)force (±18.6 N)4.0 s
Cost Σ xᵀQx + Ru² with Q = diag(1, 1, 10, 1) on (cart position, velocity, angle, angular velocity). Gain K = [-2.78, -5.32, -46.44, -12.05], computed on the model linearised at upright and applied to the nonlinear cart-pole. Result over 4 s: balanced, peak force 18.6 N. A small R buys fast, forceful corrections; a large R is gentle but slow. The gain is only a local answer: tilt far enough, or cap the force, and the linear model stops describing what the controller can do.
Why engineers care

LQR and its descendants keep rockets, drones, satellites, walking robots and chemical plants on track; iLQR and model predictive control extend the same idea to nonlinear systems.

📝 Quick check (Level 0)
0/3 answered
  1. 1. An LQR controller computes the action as…
  2. 2. LQR assumes the dynamics are…
  3. 3. LQR's cost penalises…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Linear dynamics: x_(t+1) = A x_t + B u_t, with state x and control u.
  • Quadratic cost: Σ_t (x_tᵀQx_t + u_tᵀRu_t) + x_TᵀQ_f x_T, with Q positive semidefinite (penalises error) and R positive definite (penalises effort).
  • Riccati recursion: P ← Q + AᵀPA − AᵀPB(R + BᵀPB)⁻¹BᵀPA, starting from P = Q_f and running backward in time; the gain is K = (R + BᵀPB)⁻¹BᵀPA.
  • Value function: the optimal cost-to-go from x is xᵀPx, a quadratic.
  • Stationary LQR: for an infinite horizon, iterate the recursion to its fixed point (the discrete algebraic Riccati equation, DARE) and use the constant gain K.
  • Linearisation: a nonlinear system x′ = f(x, u) is approximated near an equilibrium by the Jacobians A = ∂f/∂x, B = ∂f/∂u.
  • Controllability: (A, B) is controllable if the matrix [B, AB, …, Aⁿ⁻¹B] has full rank; then any unstable mode can be stabilised.
  • iLQR: for nonlinear systems, repeatedly linearise along the current trajectory, solve a time-varying LQR problem for corrections, and roll out the improved controls.
📝 Quick check (Level 1)
0/3 answered
  1. 1. The LQR value function is…
  2. 2. The Riccati recursion runs…
  3. 3. Controllability is tested by…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Balancing a cart-pole with LQR

Exercise 1.

  • The classic cart-pole (cart 1 kg, pole 0.1 kg, half-length 0.5 m), simulated with Euler steps of 0.02 s; state = (cart position, velocity, pole angle from upright, angular velocity), control = horizontal force.
  • Linearise at upright by central differences: the largest eigenvalue magnitude is above 1, so the upright position is unstable.
  • Solve the Riccati equation with Q = diag(1, 1, 10, 1) and R = 0.1, and check the answer three ways: the value x₀ᵀP₀x₀, the simulated cost with the time-varying gains, and a direct least-squares minimisation over all 200 controls.

Practice.

  • Start with LQR on a linearised model whenever you can write down or identify the dynamics near an operating point; tune Q and R rather than gains.
  • Use iLQR or differential dynamic programming for nonlinear trajectories such as swing-ups, jumps and manipulation, and keep the feedback gains it returns.
  • Add constraints with model predictive control (topic L6-4), which re-solves a finite-horizon problem at every step.
  • Learn the model when you have none: fit A and B from data, or a neural model, and plan with it (topic L6-5).
  • Tools: python-control and SciPy (solve_discrete_are), Drake, Crocoddyl and acados for trajectory optimisation.
def riccati(A, B, Q, R, horizon=None, tol=1e-12):
    P = Q.copy(); Ks = []
    for k in range(horizon or 100000):
        K = np.linalg.solve(R + B.T @ P @ B, B.T @ P @ A)    # optimal gain
        P2 = Q + A.T @ P @ (A - B @ K)                         # cost-to-go one step earlier
        Ks.append(K)
        if horizon is None and np.max(np.abs(P2 - P)) < tol:
            return K, P2                                       # stationary solution
        P = P2
    return Ks[::-1], P                                         # gains K_0 … K_(T−1)
🐍 Exercise 1: LQR for the cart-pole
Linearise the cart-pole at upright, solve the Riccati equation for the stationary and finite-horizon gains, and confirm the Riccati value against simulation and a direct least-squares solution.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Constraints on the control are handled by…
  2. 2. For a nonlinear swing-up use…
  3. 3. SciPy's Riccati solver is…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why the cost-to-go stays quadratic. Suppose the cost-to-go one step later is xᵀPx. The cost of taking u now is xᵀQx + uᵀRu + (Ax + Bu)ᵀP(Ax + Bu), which is quadratic in u with positive-definite curvature R + BᵀPB. Setting its gradient to zero gives u = −(R + BᵀPB)⁻¹BᵀPAx = −Kx, a linear function of x. Substituting back leaves a quadratic in x with matrix Q + AᵀPA − AᵀPB(R + BᵀPB)⁻¹BᵀPA. By induction from the terminal cost, every cost-to-go is quadratic and every optimal policy is linear: this is the Bellman equation (topic L1-5) solved in closed form.

Stationary solution and stability. If (A, B) is controllable (stabilisable is enough) and Q is positive definite, the recursion converges to the unique positive-definite solution of the DARE, and A − BK has all eigenvalues inside the unit circle. The infinite-horizon cost xᵀPx is finite exactly because the closed loop decays.

Only the ratio of Q and R matters. Multiplying both by the same constant multiplies P by it and leaves K unchanged. Raising R relative to Q makes control expensive: smaller forces, slower recovery and more accumulated error.

Local and nonlinear. LQR is optimal for the linear model. On a nonlinear system it is a good controller near the linearisation point, and its region of attraction is limited, especially when the actuators saturate. iLQR applies the same backward pass around a trajectory rather than a point, and its time-varying gains correct deviations from the planned trajectory.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The optimal LQR policy is linear because the cost-to-go is…
  2. 2. Scaling Q and R by the same constant changes K by…
  3. 3. LQR on a nonlinear system is reliable…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How does the control weight R trade force against speed and error for the cart-pole, how far from upright can LQR recover with and without a force limit, and does iLQR’s feedback make a swing-up robust to a wrong model?

History.

  • R. Bellman (1957) formulated dynamic programming; R. Kalman (1960) solved the linear–quadratic problem and introduced controllability.
  • D. Mayne (1966) and D. Jacobson and D. Mayne (1970) introduced differential dynamic programming.
  • W. Li and E. Todorov (2004) introduced iterative LQR; Y. Tassa, T. Erez and E. Todorov (2012) used it for real-time control of complex humanoids.
  • J. Rawlings, D. Mayne and M. Diehl’s book (2017) covers model predictive control.

Exercise 2.

  • Stationary LQR with Q = diag(1, 1, 10, 1) and R = 0.01, 0.1, 1 and 10, run on the nonlinear cart-pole from a 0.2 rad tilt for 10 s. Measured: peak force, Σ angle²·dt, Σ force²·dt and the settling time (after which the angle stays within 0.01 rad).
  • The largest starting angle, in steps of 0.05 rad, from which the controller balances the pole (angle below 0.05 rad and cart within 0.5 m after 10 s) from every smaller angle too, with no force limit and with the force clipped to ±10 N.
  • Riccati value against simulated cost from 5 random small starts.
  • iLQR swings a pendulum (m = l = 1, damping 0.1, steps of 0.05 s) up from hanging down in 5 s, with cost Σ θ² + 0.1ω² + 0.01u² and terminal weights 100 and 10. The plan is then run on a pendulum whose motor is 10% weaker and whose friction is doubled, once open loop and once with iLQR’s feedback gains.

Seed 0 (the study is deterministic apart from the 5 random starts; seeds 1 and 2 gave the same table):

R peak force (N) Σ angle²·dt Σ force²·dt settling time (s) largest angle, no limit (rad) largest angle, ±10 N (rad)
0.01 18.07 0.0071 12.76 1.36 1.00 0.55
0.1 9.29 0.0100 6.50 1.62 1.05 0.60
1 6.17 0.0125 4.28 2.30 1.10 0.65
10 5.09 0.0135 3.37 3.44 1.10 0.70

Findings.

  • R trades effort for speed: from R = 0.01 to 10 the peak force fell from 18.07 N to 5.09 N and the force energy from 12.76 to 3.37, while the angle error grew from 0.0071 to 0.0135 and settling slowed from 1.36 s to 3.44 s.
  • A force limit shrinks the region LQR can recover from to roughly half: 0.55 to 0.70 rad with ±10 N against 1.00 to 1.10 rad without. The gentler gains coped better with the limit, because they saturate less.
  • The Riccati value matched simulation to a relative error of 1.4e-14, 1.4e-14 and 1.0e-14 on the three seeds.
  • iLQR reduced the swing-up cost from 1973.9 to 111.20, within 1% of the final value after 6 iterations, ending 3.8e-7 rad from upright with a peak torque of 28.59.
  • Feedback made the plan robust: on the mismatched pendulum the open-loop controls ended 2.769 rad from upright (the swing-up failed), while adding iLQR’s gains ended 2.3e-6 rad from upright.
🐍 Exercise 2 (research): how does the control weight R trade force against speed and error for the cart-pole, how far from upright can LQR recover with and without a force limit, and does iLQR's feedback make a swing-up robust to a wrong model?
Measure LQR's force–speed trade-off and region of recovery on the cart-pole, and test iLQR's swing-up on a pendulum with a wrong model.

Optimal control in practice

  • Aerospace: LQR and its variants stabilise aircraft, rockets and satellites; the Apollo programme used Kalman’s work.
  • Robotics: legged robots and humanoids run iLQR, DDP or model predictive control at hundreds of hertz.
  • Reinforcement learning: LQR is the standard testbed for analysing policy gradients and model-based RL, because its optimum is known.
  • Learning and control combined: guided policy search used iLQR to teach neural network policies.

References

  1. R. Kalman (1960). “Contributions to the theory of optimal control.” Boletín de la Sociedad Matemática Mexicana.
  2. D. Jacobson and D. Mayne (1970). Differential Dynamic Programming. Elsevier.
  3. W. Li and E. Todorov (2004). “Iterative linear quadratic regulator design for nonlinear biological movement systems.” ICINCO.
  4. Y. Tassa, T. Erez and E. Todorov (2012). “Synthesis and stabilization of complex behaviors through online trajectory optimization.” IROS.
  5. B. Recht (2019). “A tour of reinforcement learning: the view from continuous control.” Annual Review of Control, Robotics, and Autonomous Systems.