Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Riccati Equation, Optimal Feedback Law, and Numerical Implementation

Matrix Riccati Differential Equation

From the quadratic-form identity derived in Part I,

S˙+QSBR1BTS+SA+ATS=0.\dot{\boldsymbol{S}} + \boldsymbol{Q} - \boldsymbol{S}\boldsymbol{B}\boldsymbol{R}^{-1}\boldsymbol{B}^\mathsf{T}\boldsymbol{S} + \boldsymbol{S}\boldsymbol{A} + \boldsymbol{A}^\mathsf{T}\boldsymbol{S} = \boldsymbol{0}.

Solving for S˙\dot{\boldsymbol{S}} gives the differential Riccati equation:

S˙=ATSSA+SBR1BTSQ.\boxed{ \dot{\boldsymbol{S}} = - \boldsymbol{A}^\mathsf{T}\boldsymbol{S} - \boldsymbol{S}\boldsymbol{A} + \boldsymbol{S}\boldsymbol{B}\boldsymbol{R}^{-1}\boldsymbol{B}^\mathsf{T}\boldsymbol{S} - \boldsymbol{Q}. }

The terminal condition is

S(tf)=Sf.\boxed{ \boldsymbol{S}(t_f)=\boldsymbol{S}_f. }

This equation is integrated backward in time.

Equivalent Backward-Time Form

Define the backward-time variable

τ=tft.\tau=t_f-t.

Then

dSdτ=dSdt.\frac{\,\mathrm{d}\boldsymbol{S}}{\,\mathrm{d}\tau} = - \frac{\,\mathrm{d}\boldsymbol{S}}{\,\mathrm{d} t}.

Therefore,

dSdτ=ATS+SASBR1BTS+Q,\boxed{ \frac{\,\mathrm{d}\boldsymbol{S}}{\,\mathrm{d}\tau} = \boldsymbol{A}^\mathsf{T}\boldsymbol{S} + \boldsymbol{S}\boldsymbol{A} - \boldsymbol{S}\boldsymbol{B}\boldsymbol{R}^{-1}\boldsymbol{B}^\mathsf{T}\boldsymbol{S} + \boldsymbol{Q}, }

with

S(0)=Sf.\boldsymbol{S}(0)=\boldsymbol{S}_f.

This form can be convenient for numerical integration because the independent variable increases forward.

Optimal Feedback Law

From Part I,

u=R1BTJx.\boldsymbol{u}^* = - \boldsymbol{R}^{-1}\boldsymbol{B}^\mathsf{T}J_{\boldsymbol{x}}^*.

Using

Jx=S(t)x,J_{\boldsymbol{x}}^*=\boldsymbol{S}(t)\boldsymbol{x},

we obtain

u(t)=R1(t)BT(t)S(t)x(t).\boxed{ \boldsymbol{u}^*(t) = - \boldsymbol{R}^{-1}(t)\boldsymbol{B}^\mathsf{T}(t)\boldsymbol{S}(t)\boldsymbol{x}^*(t). }

Define the time-varying feedback gain

K(t)=R1(t)BT(t)S(t).\boxed{ \boldsymbol{K}(t) = \boldsymbol{R}^{-1}(t)\boldsymbol{B}^\mathsf{T}(t)\boldsymbol{S}(t). }

Then

u(t)=K(t)x(t).\boxed{ \boldsymbol{u}^*(t) = - \boldsymbol{K}(t)\boldsymbol{x}^*(t). }

Closed-Loop Dynamics

Substituting the feedback law into the state equation gives

x˙=Ax+Bu=[ABR1BTS]x.\begin{aligned} \dot{\boldsymbol{x}}^* &= \boldsymbol{A}\boldsymbol{x}^* + \boldsymbol{B}\boldsymbol{u}^* \\ &= \left[ \boldsymbol{A} - \boldsymbol{B}\boldsymbol{R}^{-1}\boldsymbol{B}^\mathsf{T}\boldsymbol{S} \right] \boldsymbol{x}^*. \end{aligned}

Therefore,

x˙(t)=[A(t)B(t)K(t)]x(t).\boxed{ \dot{\boldsymbol{x}}^*(t) = \left[ \boldsymbol{A}(t)-\boldsymbol{B}(t)\boldsymbol{K}(t) \right] \boldsymbol{x}^*(t). }

Once S(t)\boldsymbol{S}(t) has been computed backward in time, the closed-loop state is propagated forward from x(t0)=x0\boldsymbol{x}(t_0)=\boldsymbol{x}_0.

Backward Planning and Forward Execution

The LQR solution consists of two distinct integrations:

  1. Integrate the Riccati equation backward:

S(tf)=Sf.\boldsymbol{S}(t_f)=\boldsymbol{S}_f.
  1. Compute K(t)\boldsymbol{K}(t) from S(t)\boldsymbol{S}(t).

  2. Integrate the closed-loop state equation forward:

x(t0)=x0.\boldsymbol{x}(t_0)=\boldsymbol{x}_0.
  1. Recover the optimal control:

u(t)=K(t)x(t).\boldsymbol{u}^*(t)=-\boldsymbol{K}(t)\boldsymbol{x}^*(t).

This is the exact mathematical realization of Bellman’s idea: plan backward and execute forward.

Interpretation of the Matrix S(t)\boldsymbol{S}(t)

The value function is

J(x,t)=12xTS(t)x.J^*(\boldsymbol{x},t) = \frac{1}{2}\boldsymbol{x}^\mathsf{T}\boldsymbol{S}(t)\boldsymbol{x}.

Thus, S(t)\boldsymbol{S}(t) describes the local curvature of the optimal cost-to-go with respect to the state.

Large eigenvalues of S(t)\boldsymbol{S}(t) indicate directions in state space for which deviations are expensive. Small eigenvalues indicate directions that are relatively inexpensive.

Relationship to the Costate

Along the optimal trajectory,

λ(t)=xJ(x(t),t).\boldsymbol{\lambda}^*(t) = \nabla_{\boldsymbol{x}}J^*(\boldsymbol{x}^*(t),t).

Hence,

λ(t)=S(t)x(t).\boxed{ \boldsymbol{\lambda}^*(t) = \boldsymbol{S}(t)\boldsymbol{x}^*(t). }

Substituting this into the Pontryagin control law

u=R1BTλ\boldsymbol{u}^* = - \boldsymbol{R}^{-1}\boldsymbol{B}^\mathsf{T}\boldsymbol{\lambda}^*

recovers the HJB feedback law.

Consistency with Pontryagin’s Minimum Principle

For the LQR problem, the PMP Hamiltonian is

H=12xTQx+12uTRu+λT(Ax+Bu).\mathcal{H} = \frac{1}{2}\boldsymbol{x}^\mathsf{T}\boldsymbol{Q}\boldsymbol{x} + \frac{1}{2}\boldsymbol{u}^\mathsf{T}\boldsymbol{R}\boldsymbol{u} + \boldsymbol{\lambda}^\mathsf{T}(\boldsymbol{A}\boldsymbol{x}+\boldsymbol{B}\boldsymbol{u}).

Stationarity gives

u=R1BTλ.\boldsymbol{u}^* = - \boldsymbol{R}^{-1}\boldsymbol{B}^\mathsf{T}\boldsymbol{\lambda}.

The costate equation is

λ˙=QxATλ.\dot{\boldsymbol{\lambda}} = - \boldsymbol{Q}\boldsymbol{x} - \boldsymbol{A}^\mathsf{T}\boldsymbol{\lambda}.

Assume

λ=Sx.\boldsymbol{\lambda}=\boldsymbol{S}\boldsymbol{x}.

Then

λ˙=S˙x+Sx˙.\dot{\boldsymbol{\lambda}} = \dot{\boldsymbol{S}}\boldsymbol{x} + \boldsymbol{S}\dot{\boldsymbol{x}}.

Substituting the closed-loop dynamics and equating terms yields the same differential Riccati equation.

Therefore, HJB and PMP are fully consistent for the LQR problem.

Why the LQR Problem Is Special

The LQR problem is unusually tractable because:

A small nonlinear perturbation may destroy this structure and return the problem to a high-dimensional nonlinear PDE.

Curse of Dimensionality

For a general nonlinear system with nxn_x states, the HJB equation is defined over an (nx+1)(n_x+1)-dimensional state-time domain.

If each state dimension is discretized with MM grid points, the number of state-grid points is approximately

Mnx.M^{n_x}.

This exponential growth is the curse of dimensionality.

The LQR problem avoids this difficulty because the unknown value function is represented by the matrix S(t)\boldsymbol{S}(t) rather than by values on a full state-space grid.

Time-Varying Versus Time-Invariant LQR

For time-varying matrices, the Riccati equation is

S˙=ATSSA+SBR1BTSQ.\dot{\boldsymbol{S}} = - \boldsymbol{A}^\mathsf{T}\boldsymbol{S} - \boldsymbol{S}\boldsymbol{A} + \boldsymbol{S}\boldsymbol{B}\boldsymbol{R}^{-1}\boldsymbol{B}^\mathsf{T}\boldsymbol{S} - \boldsymbol{Q}.

For an infinite-horizon time-invariant problem, one seeks a constant solution S\boldsymbol{S}_\infty satisfying

ATS+SASBR1BTS+Q=0.\boxed{ \boldsymbol{A}^\mathsf{T}\boldsymbol{S}_\infty + \boldsymbol{S}_\infty\boldsymbol{A} - \boldsymbol{S}_\infty\boldsymbol{B}\boldsymbol{R}^{-1}\boldsymbol{B}^\mathsf{T}\boldsymbol{S}_\infty + \boldsymbol{Q} = \boldsymbol{0}. }

This is the continuous algebraic Riccati equation.

Finite-Horizon MATLAB Implementation

The following template integrates the Riccati equation backward and the state equation forward.

function demo_finite_horizon_lqr

    t0 = 0;
    tf = 10;

    A = [0 1; -2 -0.5];
    B = [0; 1];
    Q = eye(2);
    R = 1;
    Sf = eye(2);

    n = size(A,1);

    % Backward Riccati equation
    riccati = @(t,svec) riccati_rhs( ...
        t, svec, A, B, Q, R, n);

    [tS, Svec] = ode45( ...
        riccati, [tf t0], Sf(:));

    % Reverse for increasing-time interpolation
    tS = flipud(tS);
    Svec = flipud(Svec);

    Sfun = @(t) reshape( ...
        interp1(tS, Svec, t, 'pchip'), n, n);

    x0 = [1; 0];

    % Closed-loop state dynamics
    closed_loop = @(t,x) ...
        (A - B*(R\(B.'*Sfun(t))))*x;

    [tx, x] = ode45(closed_loop, [t0 tf], x0);

    u = zeros(length(tx),1);
    for k = 1:length(tx)
        S = Sfun(tx(k));
        K = R\(B.'*S);
        u(k) = -K*x(k,:).';
    end

    figure;
    plot(tx,x,'LineWidth',1.5);
    xlabel('Time');
    ylabel('State');
    legend('x_1','x_2');
    grid on;

    figure;
    plot(tx,u,'LineWidth',1.5);
    xlabel('Time');
    ylabel('Control');
    grid on;
end

function ds = riccati_rhs(~,svec,A,B,Q,R,n)

    S = reshape(svec,n,n);

    dS = -A.'*S - S*A ...
         + S*B*(R\B.')*S - Q;

    ds = dS(:);
end

Numerical Symmetry Preservation

In exact arithmetic, S(t)\boldsymbol{S}(t) remains symmetric if Sf\boldsymbol{S}_f is symmetric.

Numerical integration may introduce small asymmetry. A common correction is

S12(S+ST).\boldsymbol{S} \leftarrow \frac{1}{2} (\boldsymbol{S}+\boldsymbol{S}^\mathsf{T}).

This should be used only to remove numerical roundoff, not to hide a significant implementation error.

Verification of the Riccati Solution

A computed solution should be checked using the residual

RDRE(t)=S˙+ATS+SASBR1BTS+Q.\boldsymbol{R}_{\mathrm{DRE}}(t) = \dot{\boldsymbol{S}} + \boldsymbol{A}^\mathsf{T}\boldsymbol{S} + \boldsymbol{S}\boldsymbol{A} - \boldsymbol{S}\boldsymbol{B}\boldsymbol{R}^{-1}\boldsymbol{B}^\mathsf{T}\boldsymbol{S} + \boldsymbol{Q}.

A valid numerical solution should satisfy

RDRE(t)1.\|\boldsymbol{R}_{\mathrm{DRE}}(t)\| \ll 1.

Also verify

S(tf)Sf1.\|\boldsymbol{S}(t_f)-\boldsymbol{S}_f\| \ll 1.

Verification of the Value Function

For the optimal state and control,

J(x0,t0)=12x0TS(t0)x0.J^*(\boldsymbol{x}_0,t_0) = \frac{1}{2} \boldsymbol{x}_0^\mathsf{T}\boldsymbol{S}(t_0)\boldsymbol{x}_0.

The same quantity should be recovered from numerical integration:

Jsim=12xT(tf)Sfx(tf)+12t0tf(xTQx+uTRu)dt.J_{\mathrm{sim}} = \frac{1}{2}\boldsymbol{x}^\mathsf{T}(t_f)\boldsymbol{S}_f\boldsymbol{x}(t_f) + \frac{1}{2} \int_{t_0}^{t_f} \left( \boldsymbol{x}^\mathsf{T}\boldsymbol{Q}\boldsymbol{x} + \boldsymbol{u}^\mathsf{T}\boldsymbol{R}\boldsymbol{u} \right)\,\mathrm{d} t.

The difference

JsimJ(x0,t0)|J_{\mathrm{sim}}-J^*(\boldsymbol{x}_0,t_0)|

should be small.

Feedback Interpretation

The HJB derivation generates

u(t)=K(t)x(t).\boldsymbol{u}^*(t) = - \boldsymbol{K}(t)\boldsymbol{x}(t).

Thus, the control depends on the current state, not only on the nominal state trajectory.

If a disturbance changes the state, the same gain matrix maps the new state to a corrective control.

Open-Loop and Closed-Loop Views

The LQR solution can be interpreted in two ways:

  1. An open-loop optimal trajectory is obtained by propagating the optimal closed-loop system from a specified initial state.

  2. The feedback law itself is valid for every state in the domain.

The HJB solution therefore contains more information than a single open-loop trajectory.

Common Derivation Errors

Dropping the Factor of One-Half

The factors of one-half simplify derivatives. Removing them inconsistently produces factor-of-two errors.

Incorrect Gradient Orientation

Use a consistent column-gradient convention:

Jx=xJ.J_{\boldsymbol{x}}=\nabla_{\boldsymbol{x}}J.

Forgetting Matrix Symmetry

The derivative of

12xTSx\frac{1}{2}\boldsymbol{x}^\mathsf{T}\boldsymbol{S}\boldsymbol{x}

is Sx\boldsymbol{S}\boldsymbol{x} only when S\boldsymbol{S} is symmetric.

Incorrect Riccati Sign Convention

The finite-horizon Riccati equation is commonly written either in forward-time derivative form with a terminal condition or in backward-time variable form. Mixing the two conventions causes sign errors.

Integrating from the Wrong Boundary

The Riccati equation must satisfy

S(tf)=Sf.\boldsymbol{S}(t_f)=\boldsymbol{S}_f.

Assuming Numerical Optimality Without Verification

The Riccati residual, terminal condition, closed-loop dynamics, and cost identity must all be checked.

  1. Define A(t)\boldsymbol{A}(t), B(t)\boldsymbol{B}(t), Q(t)\boldsymbol{Q}(t), R(t)\boldsymbol{R}(t), and Sf\boldsymbol{S}_f.

  2. Confirm symmetry and definiteness assumptions.

  3. Integrate the Riccati equation backward.

  4. Verify the terminal condition.

  5. Verify symmetry of S(t)\boldsymbol{S}(t).

  6. Compute K(t)\boldsymbol{K}(t).

  7. Integrate the closed-loop state equation forward.

  8. Compute u(t)\boldsymbol{u}^*(t).

  9. Evaluate the cost numerically.

  10. Compare it with the value-function prediction.

  11. Check Riccati and state-equation residuals.

Summary

  1. The HJB equation reduces to the differential Riccati equation for the LQR problem.

  2. The matrix S(t)\boldsymbol{S}(t) is integrated backward from Sf\boldsymbol{S}_f.

  3. The optimal feedback gain is

K(t)=R1BTS(t).\boldsymbol{K}(t)=\boldsymbol{R}^{-1}\boldsymbol{B}^\mathsf{T}\boldsymbol{S}(t).
  1. The optimal control is

u(t)=K(t)x(t).\boldsymbol{u}^*(t)=-\boldsymbol{K}(t)\boldsymbol{x}^*(t).
  1. The costate is

λ(t)=S(t)x(t).\boldsymbol{\lambda}^*(t)=\boldsymbol{S}(t)\boldsymbol{x}^*(t).
  1. HJB and PMP produce the same Riccati equation.

  2. The LQR problem avoids the curse of dimensionality because the value function is represented by a matrix.

  3. Numerical solutions require residual, symmetry, boundary-condition, and cost checks.

Connection. A feedback law is one component of a larger system, so the discussion now places optimization alongside guidance, navigation, estimation, and inner-loop control.