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.

The Principle of Optimality and the Hamilton–Jacobi–Bellman Equation

Earlier sections developed optimality conditions through the calculus of variations, Pontryagin’s minimum principle, and direct numerical methods. Those approaches typically produce an open-loop optimal trajectory for a specified initial condition and model.

Dynamic programming takes a different viewpoint. Instead of solving only for one optimal trajectory from one initial condition, it asks:

What is the minimum remaining cost from every possible state and every possible time?

The answer is encoded in the value function, also called the optimal cost-to-go. The partial differential equation satisfied by this function is the Hamilton–Jacobi–Bellman equation.

Open-Loop and Closed-Loop Optimal Control

An open-loop control is determined as a function of time:

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

It is computed for a particular initial state and a particular model. Once computed, the control does not explicitly change in response to deviations of the measured state.

A closed-loop control is a feedback law:

u(t)=π(x(t),t).\boxed{ \boldsymbol{u}^*(t)=\boldsymbol{\pi}^*(\boldsymbol{x}(t),t). }

The current state is used to determine the control. Dynamic programming naturally generates this feedback form because it computes the optimal action for every state-time pair.

The Principle of Optimality

Suppose an optimal trajectory connects point AA to point BB. Let DD and EE be any two points lying on that trajectory.

Then the segment of the trajectory connecting DD to EE must itself be optimal for the subproblem that begins at DD and ends at EE.

A shorter geometric statement is:

Every subarc of an optimal trajectory is optimal for its own endpoints.\boxed{ \text{Every subarc of an optimal trajectory is optimal for its own endpoints.} }

What the Principle Does Not Mean

Suppose CC is not on the optimal trajectory from AA to BB. Even if the path from AA to CC is optimal and the path from CC to BB is optimal, the concatenated path

ACBA\rightarrow C\rightarrow B

need not be optimal from AA to BB.

The principle of optimality applies only to subarcs of the original optimal path.

Geometric Illustration

The principle of optimality applies to subarcs of the optimal trajectory.

Figure 1:The principle of optimality applies to subarcs of the optimal trajectory.

Planning Backward and Executing Forward

Dynamic programming is based on a backward-planning idea:

  1. Start from the terminal objective.

  2. Determine the minimum remaining cost from states near the terminal time.

  3. Continue backward in time.

  4. Once the full policy is known, execute it forward in time.

This is often summarized by the statement:

Life is lived forward, but it is planned backward.

In control, one determines the optimal cost remaining from the current state to the terminal time. If the system deviates from the nominal path, the same value function provides the new optimal action from the new state.

Continuous-Time Optimal Control Problem

Consider

x˙(t)=f(x(t),u(t),t),x(t0)=x0,\begin{aligned} \dot{\boldsymbol{x}}(t) &= \boldsymbol{f}(\boldsymbol{x}(t),\boldsymbol{u}(t),t), \\ \boldsymbol{x}(t_0) &= \boldsymbol{x}_0, \end{aligned}

with Bolza cost

J=S(x(tf),tf)+t0tfV(x(t),u(t),t)dt.J = S(\boldsymbol{x}(t_f),t_f) + \int_{t_0}^{t_f} V(\boldsymbol{x}(t),\boldsymbol{u}(t),t)\,\mathrm{d} t.

The objective is to minimize JJ over admissible controls.

The Cost-to-Go Function

Instead of starting only at (x0,t0)(\boldsymbol{x}_0,t_0), define a problem beginning at an arbitrary state x\boldsymbol{x} and time tt:

J(x,t;u)=S(x(tf),tf)+ttfV(x(τ),u(τ),τ)dτ.J(\boldsymbol{x},t;\boldsymbol{u}) = S(\boldsymbol{x}(t_f),t_f) + \int_t^{t_f} V(\boldsymbol{x}(\tau),\boldsymbol{u}(\tau),\tau)\,\mathrm{d}\tau.

The optimal cost-to-go, or value function, is

J(x,t)=minu()[S(x(tf),tf)+ttfV(x(τ),u(τ),τ)dτ].\boxed{ J^*(\boldsymbol{x},t) = \min_{\boldsymbol{u}(\cdot)} \left[ S(\boldsymbol{x}(t_f),t_f) + \int_t^{t_f} V(\boldsymbol{x}(\tau),\boldsymbol{u}(\tau),\tau)\,\mathrm{d}\tau \right]. }

The terminal boundary condition is

J(x,tf)=S(x,tf).\boxed{ J^*(\boldsymbol{x},t_f) = S(\boldsymbol{x},t_f). }

Interpretation of the Value Function

The value function answers the question:

If the system is currently at state x\boldsymbol{x} at time tt, what is the smallest possible cost that remains from now until the terminal time?

At the initial condition,

J(x0,t0)J^*(\boldsymbol{x}_0,t_0)

is the optimal value of the original problem.

Along an optimal trajectory,

J(x(t),t)J^*(\boldsymbol{x}^*(t),t)

decreases as cost is accumulated and eventually reaches the terminal cost.

Total Derivative Along the Optimal Trajectory

Along an optimal trajectory,

J=J(x(t),t).J^*=J^*(\boldsymbol{x}^*(t),t).

Its total derivative is

dJdt=Jt+(Jx)Tx˙(t).\frac{\,\mathrm{d} J^*}{\,\mathrm{d} t} = \frac{\partial J^*}{\partial t} + \left( \frac{\partial J^*}{\partial \boldsymbol{x}} \right)^{\mathsf{T}} \dot{\boldsymbol{x}}^*(t).

Because

J(x(t),t)=S(x(tf),tf)+ttfV(x(τ),u(τ),τ)dτ,J^*(\boldsymbol{x}^*(t),t) = S(\boldsymbol{x}^*(t_f),t_f) + \int_t^{t_f} V(\boldsymbol{x}^*(\tau),\boldsymbol{u}^*(\tau),\tau)\,\mathrm{d}\tau,

differentiating with respect to the lower integration limit gives

dJdt=V(x(t),u(t),t).\frac{\,\mathrm{d} J^*}{\,\mathrm{d} t} = - V(\boldsymbol{x}^*(t),\boldsymbol{u}^*(t),t).

Therefore,

V(x,u,t)=Jt+(Jx)Tx˙.- V(\boldsymbol{x}^*,\boldsymbol{u}^*,t) = \frac{\partial J^*}{\partial t} + \left( \frac{\partial J^*}{\partial \boldsymbol{x}} \right)^{\mathsf{T}} \dot{\boldsymbol{x}}^*.

Substituting

x˙=f(x,u,t)\dot{\boldsymbol{x}}^* = \boldsymbol{f}(\boldsymbol{x}^*,\boldsymbol{u}^*,t)

yields

Jt+V(x,u,t)+(Jx)Tf(x,u,t)=0.\boxed{ \frac{\partial J^*}{\partial t} + V(\boldsymbol{x}^*,\boldsymbol{u}^*,t) + \left( \frac{\partial J^*}{\partial \boldsymbol{x}} \right)^{\mathsf{T}} \boldsymbol{f}(\boldsymbol{x}^*,\boldsymbol{u}^*,t) = 0. }

Hamiltonian Form

Define the Hamiltonian

H(x,u,p,t)=V(x,u,t)+pTf(x,u,t),\mathcal{H}(\boldsymbol{x},\boldsymbol{u},\boldsymbol{p},t) = V(\boldsymbol{x},\boldsymbol{u},t) + \boldsymbol{p}^{\mathsf{T}}\boldsymbol{f}(\boldsymbol{x},\boldsymbol{u},t),

where

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

Then, along the optimal trajectory,

Jt+H(x,u,xJ,t)=0.\frac{\partial J^*}{\partial t} + \mathcal{H} \left( \boldsymbol{x}^*, \boldsymbol{u}^*, \nabla_{\boldsymbol{x}}J^*, t \right) = 0.

Because the control must minimize the Hamiltonian, the general HJB equation is

Jt+minu[V(x,u,t)+(xJ)Tf(x,u,t)]=0.\boxed{ \frac{\partial J^*}{\partial t} + \min_{\boldsymbol{u}} \left[ V(\boldsymbol{x},\boldsymbol{u},t) + \left( \nabla_{\boldsymbol{x}}J^* \right)^{\mathsf{T}} \boldsymbol{f}(\boldsymbol{x},\boldsymbol{u},t) \right] = 0. }

The terminal condition is

J(x,tf)=S(x,tf).\boxed{ J^*(\boldsymbol{x},t_f) = S(\boldsymbol{x},t_f). }

The Hamilton–Jacobi–Bellman Equation

The compact Hamiltonian form is

Jt(x,t)+minuH(x,u,xJ,t)=0.\boxed{ J_t^*(\boldsymbol{x},t) + \min_{\boldsymbol{u}} \mathcal{H} \left( \boldsymbol{x},\boldsymbol{u},\nabla_{\boldsymbol{x}}J^*,t \right) = 0. }

This is a nonlinear first-order partial differential equation with a terminal boundary condition.

Why the HJB Equation Is a Backward-Time Equation

The boundary condition is given at tft_f, not at t0t_0:

J(x,tf)=S(x,tf).J^*(\boldsymbol{x},t_f)=S(\boldsymbol{x},t_f).

Therefore, the HJB equation is integrated backward from the terminal time toward the initial time.

This matches the cost-to-go interpretation: one begins with the cost remaining at the end and works backward to determine the cost remaining at all earlier times.

Feedback Control from the HJB Equation

Once J(x,t)J^*(\boldsymbol{x},t) is known, the optimal control is obtained from

u(x,t)=argminu[V(x,u,t)+(xJ)Tf(x,u,t)].\boxed{ \boldsymbol{u}^*(\boldsymbol{x},t) = \arg\min_{\boldsymbol{u}} \left[ V(\boldsymbol{x},\boldsymbol{u},t) + \left( \nabla_{\boldsymbol{x}}J^* \right)^{\mathsf{T}} \boldsymbol{f}(\boldsymbol{x},\boldsymbol{u},t) \right]. }

This is a state-feedback law.

The central advantage is that the solution is defined for all states in the domain, not merely along one nominal trajectory.

Relationship to the Costate

In Pontryagin’s minimum principle,

λ(t)\boldsymbol{\lambda}^*(t)

is the costate.

In dynamic programming,

xJ(x,t)\nabla_{\boldsymbol{x}}J^*(\boldsymbol{x},t)

is the gradient of the value function.

Along an optimal trajectory,

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

Thus, the costate is the gradient of the optimal cost-to-go evaluated along the optimal path.

Connection to the Costate Equation

Differentiate

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

with respect to time:

λ˙(t)=ddtxJ(x(t),t).\dot{\boldsymbol{\lambda}}^*(t) = \frac{\,\mathrm{d}}{\,\mathrm{d} t} \nabla_{\boldsymbol{x}}J^*(\boldsymbol{x}^*(t),t).

The HJB equation implies the same costate dynamics as Pontryagin’s minimum principle:

λ˙=Hx.\boxed{ \dot{\boldsymbol{\lambda}}^* = - \frac{\partial \mathcal{H}}{\partial \boldsymbol{x}}. }

Therefore, the HJB equation and the minimum principle are consistent descriptions of optimality.

Why HJB Is More General Than a Single Open-Loop Solution

Pontryagin’s principle typically produces an extremal trajectory for a specified initial state.

The HJB equation produces the full value function

J(x,t),J^*(\boldsymbol{x},t),

which contains the optimal cost from every state and time.

Consequently, the HJB equation can generate feedback control even after disturbances or modeling errors move the system away from the nominal trajectory.

Replanning Interpretation

Suppose the nominal trajectory is interrupted by an unexpected change. The current state becomes

xnew.\boldsymbol{x}_{\mathrm{new}}.

One does not need to return to the original initial condition. Instead, the optimal policy evaluates

u(xnew,t)\boldsymbol{u}^*(\boldsymbol{x}_{\mathrm{new}},t)

and continues optimally from the current state.

This is the mathematical version of replanning a route after encountering a road closure.

Computational Challenge

The HJB equation is a PDE in state and time. If the state dimension is nxn_x, then the value function is defined over an (nx+1)(n_x+1)-dimensional domain.

The computational burden grows rapidly with nxn_x. This is known as the curse of dimensionality.

This makes direct solution of the HJB equation impractical for many high-dimensional systems.

Introductory Scalar Linear–Quadratic Example

Consider

J=12x2(tf)+120tf[x2(t)+u2(t)]dt\boxed{ J = \frac{1}{2}x^2(t_f) + \frac{1}{2} \int_0^{t_f} \left[ x^2(t)+u^2(t) \right]\,\mathrm{d} t }

subject to

x˙(t)=2x(t)+u(t),x(0)=x0.\boxed{ \dot{x}(t) = -2x(t)+u(t), \qquad x(0)=x_0. }

The running cost is

V(x,u,t)=12x2+12u2.V(x,u,t) = \frac{1}{2}x^2 + \frac{1}{2}u^2.

The terminal cost is

S(x(tf),tf)=12x2(tf).S(x(t_f),t_f) = \frac{1}{2}x^2(t_f).

The dynamics are

f(x,u,t)=2x+u.f(x,u,t) = -2x+u.

Hamiltonian for the Example

The HJB Hamiltonian is

H(x,u,Jx,t)=12x2+12u2+Jx(2x+u).\mathcal{H}(x,u,J_x,t) = \frac{1}{2}x^2 + \frac{1}{2}u^2 + J_x(-2x+u).

The minimizing control satisfies

Hu=u+Jx=0.\frac{\partial \mathcal{H}}{\partial u} = u+J_x = 0.

Therefore,

u(x,t)=Jx(x,t).\boxed{ u^*(x,t) = - J_x^*(x,t). }

Substituting into the Hamiltonian gives

H=12x2+12(Jx)2+Jx(2xJx)=12x212(Jx)22xJx.\begin{aligned} \mathcal{H}^* &= \frac{1}{2}x^2 + \frac{1}{2}(J_x^*)^2 + J_x^* \left( -2x-J_x^* \right) \\ &= \frac{1}{2}x^2 - \frac{1}{2}(J_x^*)^2 - 2xJ_x^*. \end{aligned}

Hence, the HJB equation is

Jt+12x212(Jx)22xJx=0.\boxed{ J_t^* + \frac{1}{2}x^2 - \frac{1}{2}(J_x^*)^2 - 2xJ_x^* = 0. }

The terminal condition is

J(x,tf)=12x2.\boxed{ J^*(x,t_f) = \frac{1}{2}x^2. }

Quadratic Value-Function Ansatz

Because the dynamics are linear and the cost is quadratic, assume

J(x,t)=12P(t)x2.\boxed{ J^*(x,t) = \frac{1}{2}P(t)x^2. }

The terminal condition gives

12P(tf)x2=12x2,\frac{1}{2}P(t_f)x^2 = \frac{1}{2}x^2,

so

P(tf)=1.\boxed{ P(t_f)=1. }

The derivatives are

Jx=P(t)x,Jt=12P˙(t)x2.\begin{aligned} J_x^* &= P(t)x, \\ J_t^* &= \frac{1}{2}\dot{P}(t)x^2. \end{aligned}

Optimal Feedback Law

Using

u=Jx,u^* = - J_x^*,

the optimal control becomes

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

This is a time-varying linear state-feedback law.

The feedback gain is

K(t)=P(t).K(t)=P(t).

Riccati Differential Equation

Substitute the quadratic ansatz into the HJB equation:

12P˙x2+12x212P2x22Px2=0.\frac{1}{2}\dot{P}x^2 + \frac{1}{2}x^2 - \frac{1}{2}P^2x^2 - 2Px^2 = 0.

For nonzero xx, divide by 12x2\frac{1}{2}x^2:

P˙+1P24P=0.\dot{P} + 1 - P^2 - 4P = 0.

Therefore,

P˙=P2+4P1,P(tf)=1.\boxed{ \dot{P} = P^2+4P-1, \qquad P(t_f)=1. }

This differential equation is integrated backward in time.

Closed-Loop State Dynamics

Substituting

u=P(t)xu^*=-P(t)x

into the system dynamics gives

x˙(t)=[2+P(t)]x(t).\boxed{ \dot{x}^*(t) = -\left[2+P(t)\right]x^*(t). }

Once P(t)P(t) is found backward in time, the optimal state can be propagated forward from

x(0)=x0.x(0)=x_0.

Backward Planning and Forward Execution

The solution procedure has two stages:

  1. Integrate the Riccati equation backward from

P(tf)=1.P(t_f)=1.
  1. Integrate the closed-loop state equation forward from

x(0)=x0.x(0)=x_0.

This is the precise mathematical realization of planning backward and executing forward.

Numerical MATLAB Template

tf = 5;

% Backward Riccati equation
riccati = @(t,P) P.^2 + 4*P - 1;

% Integrate backward from P(tf) = 1
[tP, P] = ode45(riccati, [tf 0], 1);

% Reverse arrays for interpolation in increasing time
tP = flipud(tP);
P  = flipud(P);

Pfun = @(t) interp1(tP, P, t, 'pchip');

% Initial state
x0 = 1;

% Closed-loop dynamics
closedLoop = @(t,x) -(2 + Pfun(t))*x;

% Integrate state forward
[tx, x] = ode45(closedLoop, [0 tf], x0);

% Optimal control
u = -arrayfun(Pfun, tx).*x;

Verification Checks

For the scalar example, verify:

  1. P(tf)=1P(t_f)=1;

  2. the Riccati residual is small;

  3. u(t)=P(t)x(t)u^*(t)=-P(t)x^*(t);

  4. the state satisfies

x˙=2x+u;\dot{x}^*=-2x^*+u^*;
  1. the HJB residual is small;

  2. the value function decreases consistently with the accumulated running cost.

HJB Residual

For an approximate value function J^(x,t)\widehat{J}(x,t) and control u^(x,t)\widehat{u}(x,t), define

RHJB=J^t+V(x,u^,t)+J^xf(x,u^,t).R_{\mathrm{HJB}} = \widehat{J}_t + V(x,\widehat{u},t) + \widehat{J}_x f(x,\widehat{u},t).

A high-quality approximation should satisfy

RHJB0.\boxed{ R_{\mathrm{HJB}}\approx 0. }

Comparison with Pontryagin’s Minimum Principle

For this example, the Hamiltonian in the minimum principle is

H=12x2+12u2+λ(2x+u).\mathcal{H} = \frac{1}{2}x^2 + \frac{1}{2}u^2 + \lambda(-2x+u).

Stationarity gives

u=λ.u^*=-\lambda.

Dynamic programming gives

u=Jx.u^*=-J_x^*.

Therefore,

λ(t)=Jx(x(t),t)=P(t)x(t).\boxed{ \lambda(t) = J_x^*(x^*(t),t) = P(t)x^*(t). }

The two methods produce the same optimal control.

Summary

  1. The principle of optimality states that every subarc of an optimal trajectory is itself optimal.

  2. The value function is the minimum remaining cost from a state-time pair.

  3. The terminal condition is specified at tft_f.

  4. The HJB equation is solved backward in time.

  5. The general continuous-time HJB equation is

Jt+minu[V+(xJ)Tf]=0.J_t^* + \min_{\boldsymbol{u}} \left[ V + (\nabla_{\boldsymbol{x}}J^*)^{\mathsf{T}}\boldsymbol{f} \right] = 0.
  1. The optimal feedback law is obtained by minimizing the HJB Hamiltonian.

  2. Along the optimal trajectory,

λ=xJ.\boldsymbol{\lambda}^*=\nabla_{\boldsymbol{x}}J^*.
  1. Dynamic programming produces a feedback law rather than only one open-loop trajectory.

  2. The HJB equation suffers from the curse of dimensionality.

  3. For linear systems with quadratic costs, a quadratic value function leads to a Riccati differential equation.

  4. The scalar example produces

u(t)=P(t)x(t).u^*(t)=-P(t)x^*(t).

Connection. Applying the HJB equation to finite-horizon LQR shows how a quadratic value function turns a nonlinear PDE into a matrix differential equation.