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.
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.