Positive definite and LQR

10 views (last 30 days)
Scott
Scott on 26 Apr 2023
Commented: Sam Chak on 27 Apr 2023
Getting error using lqr, it mentions [Q N;N' R] needs to be positive definite. I cannot seem to make my matrices be positive definite. But when I use example matrices in https://www.mathworks.com/help/control/ref/lti.lqr.html, it is not positive defintie. Please advise:
% check MATLAB lqr examples https://www.mathworks.com/help/control/ref/lti.lqr.html
R = 1;
C = [0 0 1];
Q1 = 25*C'*C;
N = zeros(3,1);
matrix = [Q1 N;N' R];
try chol(matrix)
disp('Matrix is symmetric positive definite.')
catch ME
disp('Matrix is not symmetric positive definite')
end
Matrix is not symmetric positive definite

Accepted Answer

Scott
Scott on 26 Apr 2023
From MATLAB support (very quick resolution):
As for the error you encountered, I would recommend you checking the rest conditions in the limitation listed on the documentation. According to the example, I believe non-negative definite should be the right one. If you have additional questions, please provide the implementation of the system and the required matrices for us to reproduce this error.
As for the inconsistency between the error message and the documentation, I have informed our internal team, and they will consider to address this issue in the future.
  2 Comments
Paul
Paul on 26 Apr 2023
I see what you're talking about. I tried a case that failed and got the error message:
Error using lqr (line 42)
Cannot compute the stabilizing Riccati solution S for the LQR design.
This could be because:
* R is singular,
* [Q N;N' R] needs to be positive definite,
* The E matrix in the state equation is singular.
However, my 2021B doc page says that the condition is:
R > 0 and QN*inv(R)*transpose(N) ≥ 0
Are the two conditions on the doc page equivalent to the middle statement in the error message, with "definite" changed to "semi-definite"?
.
Sam Chak
Sam Chak on 27 Apr 2023
Hey @Scott, thanks for the update.
Previously, I was confused by the aircraftPitchModel example that you said you took from the LQR documentation. I applied the lqr() function but no error.

Sign in to comment.

More Answers (2)

John D'Errico
John D'Errico on 26 Apr 2023
Edited: John D'Errico on 26 Apr 2023
R = 1;
C = [0 0 1];
Q1 = 25*C'*C;
N = zeros(3,1);
matrix = [Q1 N;N' R];
The matrix Q1 has rank 1. You formed it from a vector, as an outer product. Then you add one more non-zero diagonal element. So matrix is diagonal, and it has rank 2.
matrix
matrix = 4×4
0 0 0 0 0 0 0 0 0 0 25 0 0 0 0 1
rank(matrix)
ans = 2
matrix is only non-negative definite. And therefore chol MUST fail.
I have posted the function nearestSPD on the file exchange. But in this case, it is simple to perturb matrix to be SPD. Just add a tiny multiple of the 4x4 identity matrix.
format long g
matrixhat = matrix + eps*eye(4)
matrixhat = 4×4
2.22044604925031e-16 0 0 0 0 2.22044604925031e-16 0 0 0 0 25 0 0 0 0 1
chol(matrixhat)
ans = 4×4
1.49011611938477e-08 0 0 0 0 1.49011611938477e-08 0 0 0 0 5 0 0 0 0 1
Whereas you can see that chol fails on matrix itself.
chol(matrix)
Error using chol
Matrix must be positive definite.
  3 Comments
John D'Errico
John D'Errico on 26 Apr 2023
I'm sorry. I don't have the control systems toolbox, nor any experience with LQR. It seems you are using CHOL to test to see if that matrix is SPD, but the failure comes from LQR on your actual problem.
An issue may be that chol will fail on a non-negative definite matrix, which apparently LQR is willing to accept.
This suggests that your real problem is not just non-negative definite, but actually has either negative eigenvalues, or it may not be symmetric. I can't really even try to diagnose it, without seeing the real problem you have. (I'd need to do any testing here online in Answers, since I lack that toolbox.)
An idea might be that you can send in your matrices into tech support. I have found them to be amazingly responsive, having sent in several issues recently. I've always gotten an immediate response the next (working) day.
Scott
Scott on 26 Apr 2023
Thank you John, I will try tech support as you suggest.

Sign in to comment.


Sam Chak
Sam Chak on 26 Apr 2023
Not exactly sure what you want. But I think there is no absolute rule saying that the state-cost weighted matrix has to be . In fact, you can try the other two suggestions, depending on what you really want to achieve with the LQR.
R = 1;
C = [0 0 1];
% Q1 = 25*C'*C;
% Q1 = 25*eye(3); % <-- can try this one
Q1 = 25*blkdiag(1e-4, 1e-2, 1); % <-- and also this one
N = zeros(3, 1);
matrix = [Q1 N; N' R]
matrix = 4×4
0.0025 0 0 0 0 0.2500 0 0 0 0 25.0000 0 0 0 0 1.0000
try chol(matrix)
disp('Matrix is symmetric positive definite.')
catch ME
disp('Matrix is not symmetric positive definite')
end
ans = 4×4
0.0500 0 0 0 0 0.5000 0 0 0 0 5.0000 0 0 0 0 1.0000
Matrix is symmetric positive definite.
  5 Comments
Sam Chak
Sam Chak on 26 Apr 2023
The lqr() function does not return any error message.
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;
Q = 25*C'*C
Q = 3×3
0 0 0 0 0 0 0 0 25
R = 1;
N = zeros(3, 1)
N = 3×1
0 0 0
M = [Q N; N' R]
M = 4×4
0 0 0 0 0 0 0 0 0 0 25 0 0 0 0 1
eig(M)
ans = 4×1
0 0 1 25
[K, S, P] = lqr(A, B, Q, R, N)
K = 1×3
-0.6273 136.6776 5.0000
S = 3×3
1.0e+03 * 0.0015 -0.0482 -0.0019 -0.0482 7.2833 0.2676 -0.0019 0.2676 0.0159
P =
-0.1521 + 0.0000i -1.6079 + 1.8015i -1.6079 - 1.8015i
sys = ss(A-B*K, B, C, D);
step(sys)
Scott
Scott on 26 Apr 2023
Yes, I know that. But if you check [Q N;N' R], you will see that it is not positive definite. For my system (cannot share), I am getting an error stating that my [Q N;N' R] must be positive definite (even though the help page says "non-negative definite"). So I am confused why MATLAB's example works; it seems to be that the error message I am receiving is incorrect. I am trying to clarify that with Mathworks support.

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!