← Demos

Finite-horizon LQ control

Minimise xNᵀS xN + Σ (xkᵀQ xk + ukᵀR uk) over N steps. The backward Riccati recursion gives a time-varying gain Kk; far from the end it settles on the infinite-horizon LQR gain K∞, close to the end it bends towards whatever the terminal weight asks for.

Optimal cost J*
x₀ᵀP₀x₀, finite horizon
Cost with K∞
same N steps and S
Gain settled
Kk within 1% of K∞
K∞
steady-state LQR gain

Gains Kk K₁ K₂ K∞

States xk x₁ x₂ with K∞

Input uk u with K∞

Phase plane (x₁, x₂) finite N with K∞

Cost-to-go xkᵀPkxk finite N xᵀP∞x

How it works

The plant is a continuous-time second-order system, discretised with a zero-order hold at Ts = 0.1 s, so x[k+1] = A x[k] + B u[k]. The optimal input is a linear state feedback u[k] = −K[k] x[k], found by running the Riccati recursion backwards from the terminal weight:

P[N] = S
K[k] = (R + BᵀP[k+1]B)⁻¹ BᵀP[k+1]A
P[k] = Q + AᵀP[k+1](A − B K[k])        k = N−1 … 0

Things to try. Set S = P∞: the recursion starts at its own fixed point, so every gain equals K∞ and the finite and infinite-horizon answers coincide. With S = 0 the controller stops caring near the end and the gain drops to zero in the last step, letting the state drift. A large s pulls the state hard to the origin at k = N, which shows up as a gain spike at the end. Make N short and the gain never reaches K∞ at all. On the unstable plant, look at how the K∞ cost compares with J*: the finite-horizon controller is optimal for its own cost, but only K∞ keeps working after step N.