A state-space model describes a dynamic system by a vector of states (here position x₁ and velocity x₂) evolving as ẋ = Ax + Bu. The Linear-Quadratic Regulator (LQR) finds the feedback law u = −Kx that minimises a cost combining how far the state strays from the origin and how much control effort is spent, by solving the algebraic Riccati equation for a matrix P and setting K = R⁻¹BᵀP.
The same Riccati-equation machinery used here for a one-dimensional cart also underlies spacecraft attitude control, robot arm regulation and autopilot design — only the A, B, Q and R matrices change size and meaning.
A mass sliding on a frictionless rail is regulated back to rest by an optimal LQR feedback controller, while a linked 3D plot extrudes the live state (position, velocity) forward through time as a spiral that collapses onto the origin.
The controller solves the continuous algebraic Riccati equation in real time from your Q and R weightings, deriving the feedback gains k₁, k₂ that minimise a trade-off between state error and control effort.
Raise Q₁ or Q₂ to penalise position or velocity error more heavily, raise R to make control effort more costly, and set the initial offset x₀ — then watch the gains, the rail mass, and the state-space spiral respond together.
LQR was formalised by Rudolf Kálmán in the early 1960s and, alongside the Kalman filter, became foundational to the Apollo guidance computer and nearly every modern autopilot.