I'm are going to create a LQR to control a system. The problem is to choose the Q and R weighting matrices for the cost function. The Q and R matrices are going to minimize the cost function so the system are going to be optimal.

I'm using Scilab and Scilab have a good library for optimal control. Scilab have a built function named lqr() to compute the gain matrix K, which is the LQ regulator.

But the problem is still to choose those weighting matrices. I don't know here to start. I just might start with the identity matrix as Q and just a constant as R. But the gain matrix K does not make my model go smooth. No one can say that this is the real Q and R weighting matrices for the system. As a developer, I choose the weighting matrices. But why should I do that when I can choose the K gain matrix directly?

So I just made up my own numbers for the gain matrix K and now my model is very smooth. All I did just do was to guess some numbers for the gain matrix K and simulate and look at the result. Was it still bad, I might change the first element for the gain matrix to increase the position, or change the second element in the gain matrix K, to speed up the velocity for the position.

This works great for me! Guessing and simulate and look at the results. I choosing the LQ-technique for two main reasons: It gives multivariable action and can reduce noise by using a kalman filter. A PID cannot do that.

But here is my question:

Will this method give me an optimal control just by guessing the gain matrix K and changing the values depending how the simulation results looks like?

I'm I happy with the results, I might quit there and accept the gain matrix K as the optimal LQR for the system.

  • $\begingroup$ If you (randomly) guess a K matrix you run the risk that your system becomes unstable, while LQR will always return a stabilizing state feedback controller. And how does your state space model look like, because a state space realisation of a dynamical is not unique, which could lead to not very meaningful states, such that identity for Q might not give nice results. $\endgroup$
    – fibonatic
    Jun 14, 2017 at 21:40
  • $\begingroup$ Ok. But how do i choose Q and R? I know there is a cost function $ J = \int_{0}^{T} (x^{T}(t)Qx(t) + R^{T}u(t)R) dt $ to use, but I don't $x(t)$ and $R$ and $Q$. The signal $u(t)$ is know as a constant. So if I don't know $Q$ and $R$, I just might guessing the gain matrix K. $\endgroup$
    – euraad
    Jun 15, 2017 at 20:19

1 Answer 1


If you want to avoid having to deal with the non-uniqueness of a state space model representations of LTI systems and want to have some more control of the impact that changing $Q$ has on the resulting controller, then you could define the cost you want to minimize as

$$ J(u) = \int_0^\infty\left(y^\top\!(t)\, \hat{Q}\, y(t) + u^\top\!(t)\, \hat{R}\, u(t) + 2\,y^\top\!(t)\, \hat{N}\, u(t)\right) dt. $$

If your state space model is of the form

$$ \left\{\begin{array}{l} \dot{x} = A\, x + B\, u \\ y = C\, x + D\, u \end{array}\right. $$

then you can rewrite the cost function into a form which can be solved with LQR as follows

$$ J(u) = \int_0^\infty\left(x^\top\!(t)\, Q\, x(t) + u^\top\!(t)\, R\, u(t) + 2\,x^\top\!(t)\, N\, u(t)\right) dt, $$


$$ Q = C^\top \hat{Q}\, C, $$

$$ R = \hat{R} + D^\top \hat{Q}\, D + D^\top \hat{N} + \hat{N}^\top D, $$

$$ N = C^\top \hat{N} + C^\top \hat{Q}\, D. $$

Now you could start with $\hat{R}$ equal identity, $\hat{N}$ equal zero and $\hat{Q}$ diagonal (with only positive numbers on the diagonal), for example identity. Now by doing a simulation of your closed-loop system using the obtained LQR controller you can see if you have desired performance. If for example the $k$th output decays too slowly, you could increase the value of the $k$th diagonal element of $\hat{Q}$.

If you would also like to guarantee internal stability of the system you could also add an identity matrix multiplied by a small positive number to the expression for $Q$. In order to get some estimate of the size of this small number you could maybe use a couple of orders of magnitude smaller (say $10^{-6}$) than the largest eigenvalue of the initially obtained $Q$.

So using this transformation of $(\hat{Q},\hat{R},\hat{N})\to(Q,R,N)$ should give you some more intuitive tuning nobs when designing a LQR controller. This method should also not be affected by the state space realization you are using of the LTI system.

  • $\begingroup$ How do I compute $x(t)$ ? I know I can use $L^-1[(s*I-A)^-1]$ to compute the state transition matrix, but my Matlab cannot handle more that 4x4 systems. $\endgroup$
    – euraad
    Jun 24, 2017 at 23:29
  • $\begingroup$ Do I need to compute the state transition matrix? $\endgroup$
    – euraad
    Jun 25, 2017 at 12:09
  • $\begingroup$ @DanielMårtensson All you need to provide to MATLAB (the lqr function) are the $A$ and $B$ of your state space model and the obtained values for $Q$, $R$ and $N$. $\endgroup$
    – fibonatic
    Jun 25, 2017 at 15:16
  • $\begingroup$ and the $Q$ and $R$, I get from the cost function? $\endgroup$
    – euraad
    Jun 25, 2017 at 16:18
  • $\begingroup$ Can I not guessing the $Q$ and $R$ and then simulate and look if it's giving me good or bad resluts? $\endgroup$
    – euraad
    Jun 25, 2017 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.