the optimal performance function satisfies:$$J^*(X(0),U^*(0,N-1))=\min_{U(0,k-1)} [J(X(0),U(0,k-1)+\min_{U(k,N-1)}J(X(k),U(k,N-1))],\forall k\in (0,N-1)$$
State equation of discrete systems:$X(k+1)=f(X(k),U(k),k)$
Satisfy a performance indicator function:$$\min_{U(0,k-1)} J(u)=\min_{U(0,k-1)} [S(X(k_f),k_f)+\sum_{k=k_0}^{k_f-1}L(X(k),U(k),k)]$$
Then the Bellman's recurrence formula is:$$\begin{cases}J^*(X(k),U^*(k,k_f-1))=\min_{U(k)\in U} [L(X(k),U(k),k)+J^*(X(k+1),U^*(k+1,k_f-1))]\\J^*(X(k_f),k_f)=S(X(k_f,k_f)\end{cases}$$
Matlab code
[P,L,K]=dare(G,H,Q,R);
$K=(H^TPH+R)^{-1}H^TPG$
[K,P,L]=dlqr(G,H,Q,R);
L represents a pole array
! to be continued