# lqr

## Syntax

```[K,S,e] = lqr(SYS,Q,R,N) [K,S,e] = LQR(A,B,Q,R,N) ```

## Description

`[K,S,e] = lqr(SYS,Q,R,N) ` calculates the optimal gain matrix `K`.

For a continuous time system, the state-feedback law u = –Kx minimizes the quadratic cost function

`$J\left(u\right)={\int }_{0}^{\infty }\left({x}^{T}Qx+{u}^{T}Ru+2{x}^{T}Nu\right)dt$`

subject to the system dynamics

`$\stackrel{˙}{x}=Ax+Bu.$`

In addition to the state-feedback gain `K`, `lqr` returns the solution `S` of the associated Riccati equation

`${A}^{T}S+SA-\left(SB+N\right){R}^{-1}\left({B}^{T}S+{N}^{T}\right)+Q=0$`

and the closed-loop eigenvalues` e = eig(A-B*K)`. K is derived from S using

`$K={R}^{-1}\left({B}^{T}S+{N}^{T}\right).$`

For a discrete-time state-space model, u[n] = –Kx[n] minimizes

`$J=\sum _{n=0}^{\infty }\left\{{x}^{T}Qx+{u}^{T}Ru+2{x}^{T}Nu\right\}$`

subject to x[n + 1] = Ax[n] + Bu[n].

`[K,S,e] = LQR(A,B,Q,R,N)` is an equivalent syntax for continuous-time models with dynamics $\stackrel{˙}{x}=Ax+Bu.$

In all cases, when you omit the matrix `N`, `N` is set to 0.

## Limitations

The problem data must satisfy:

• The pair (A,B) is stabilizable.

• R > 0 and $Q-N{R}^{-1}{N}^{T}\ge 0$.

• $\left(Q-N{R}^{-1}{N}^{T},\text{\hspace{0.17em}}A-B{R}^{-1}{N}^{T}\right)$ has no unobservable mode on the imaginary axis (or unit circle in discrete time).

## Tips

`lqr` supports descriptor models with nonsingular E. The output `S` of `lqr` is the solution of the Riccati equation for the equivalent explicit state-space model:

`$\frac{dx}{dt}={E}^{-1}Ax+{E}^{-1}Bu$`

## Version History

Introduced before R2006a