# lqr

Linear-quadratic regulator for a continuous-time model.

## Syntax

[G, X, L] = lqr(SYS, Q, R)

[G, X, L] = lqr(SYS, Q, R, N)

[G, X, L] = lqr(A, B, Q, R)

[G, X, L] = lqr(A, B, Q, R, N)

[G, X, L] = lqr(A, B, Q, R, N, E)

## Inputs

`SYS`- Continuous or discrete-time LTI model.
`Q`- The state weighting matrix (n x n), which is symmetric, positive semi-definite.
`R`- The input weighting matrix (p x p), which is symmetric, positive definite.
`N`- The state/input cross product weighting matrix, such that Q - N*inv(R)*N' is positive semi-definite.
`A`- The state matrix (n x n).
`B`- The input matrix (n x p).
`E`- The descriptor matrix (n x n).

## Outputs

- G
- The feedback gain matrix.
- X
- The solution of the Continuous Algebraic Riccati Equation.
- L
- The closed-loop pole locations, the eigenvalues of the matrix A-BK.

## Examples

LQR design:

```
A=[0.8 0;0.8 1];
B=[0.6;0.6];
C=[0 1];
Q=C'*C;
R=1;
[G, X, L] = lqr(A, B, Q, R)
```

`G = [Matrix] 1 x 2
-1.77636e-15 6.16228e+00
X = [Matrix] 2 x 2
8.21637 -8.21637
-8.21637 18.48683
L = [Matrix] 2 x 1
-1.26491
-0.63246`

LQR design from a state-space model:

```
A = [-0.313 56.7 0; -0.0139 -0.426 0; 0 56.7 0];
B = [0.232; 0.0203; 0];
C = [0 0 1];
D = [0];
sys = ss(A, B, C, D);
p = 2;
Q = p * transpose(C) * C;
R = 1;
[G, X, L] = lqr(sys, Q, R)
```

`G = [Matrix] 1 x 3
-0.50335 52.86451 1.41421
X = [Matrix] 3 x 3
1.41413 -40.95711 -1.66409
-40.95711 3072.24438 88.68382
-1.66409 88.68382 3.64894
L = [Matrix] 3 x 1
-0.78084 + 1.12564i
-0.78084 - 1.12564i
-0.13369 + 0.00000i`

## Comments

[G, X, L] = lqr(SYS, Q, R, N) calculates the optimal gain matrix
G.

```
Equations:
.
x = A x + B u, x(0) = x0
inf
J(x0) = INTEGRAL (x' Q x + u' R u + 2 x' N u) dt
0
L = eig (A - B*G)
```

## Comments

The function minimizes a quadratic cost function for a linear state-space system model.