# lqr

Linear-Quadratic Regulator (LQR) design.

## 📝 Syntax

* \[K, S, P] = lqr(sys, Q, R, N)
* \[K, S, P] = lqr(A, B, Q, R, N)

## 📥 Input argument

* sys - LTI model
* Q - State-cost weighted matrix
* R - Input-cost weighted matrix
* N - Optional cross term matrix: 0 by default.
* A - State matrix: n x n matrix.
* B - Input-to-state matrix: n x m matrix.

## 📤 Output argument

* K - Optimal gain: row vector.
* S - Solution of the Algebraic Riccati Equation.
* p - Poles of the closed-loop system: column vector.

## 📄 Description

In the context of continuous-time state-space matrices A and B, the command \[K, S, P] = lqr(A, B, Q, R, N) computes the optimal gain matrix K, the solution S to the associated algebraic Riccati equation, and the closed-loop poles P.

This syntax is applicable exclusively to continuous-time models.

When applied to a continuous-time or discrete-time state-space model represented by sys, the command \[K, S, P] = lqr(sys, Q, R, N) computes the optimal gain matrix K, the solution S to the associated algebraic Riccati equation, and the closed-loop poles P.

The weight matrices Q and R govern the importance of states and inputs, and the cross term matrix N is zero by default when not specified.

## 💡 Example

```matlab
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 = 1;
Ts = 1.2;
sys1 = ss(A, B, C, D, Ts);
sys2 = ss(A, B, C, D);

P = 2;
Q = P * C' * C;
R = 2;
[K1, S1, e1] = lqr(sys1, Q, R)
[K2, S2, e2] = lqr(sys2, Q, R)

```

## 🔗 See also

[care](https://nelson-9.gitbook.io/nelson/markdown/en/control_system/care), [dare](https://nelson-9.gitbook.io/nelson/markdown/en/control_system/dare), [lqe](https://nelson-9.gitbook.io/nelson/markdown/en/control_system/lqe).

## 🕔 History

| Version | 📄 Description  |
| ------- | --------------- |
| 1.0.0   | initial version |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nelson-9.gitbook.io/nelson/markdown/en/control_system/lqr.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
