Is a flag set on the possibility of a singular matrix
조회 수: 2 (최근 30일)
이전 댓글 표시
I'm using the equation solver A=B\C . The matrix can become singular and MATLAB sends out a 'warning' to this effect.
Is there an associated flag that is set also in this case so I can make some decisions based on the possibility of a singular matrix?
채택된 답변
MathWorks Support Team
2021년 3월 3일
편집: MathWorks Support Team
2021년 3월 3일
To obtain the last warning issued by MATLAB, you can use the "lastwarn" function. A possible method to use the function is as follows:
1. Invert a matrix which becomes singular:
>>A = inv([1 1; 1 1]);
2. Use the "lastwarn" function to obtain the warning message and ID:
>> [m,id]=lastwarn()
3. Check if "id" is equal to "MATLAB:singularMatrix":
>> strcmp(id,'MATLAB:singularMatrix')
The documentation for the "lastwarn" function can be found here:
Depending on the application, a good practice before operating on a matrix is to check its condition number. A very large value of condition number indicates a poorly conditioned matrix. You can find the condition number of a matrix by using the “cond” function.
The documentation for the “cond” function can be found here:
댓글 수: 2
John D'Errico
2016년 7월 13일
편집: John D'Errico
2016년 7월 13일
lastwarn can work here as shown, with one caveat. You need to reset lastwarn after checking it. Otherwise, consider this example:
clear
A1 = ones(2);
B1 = inv(A1);
Warning: Matrix is singular to working precision.
[m,id]=lastwarn()
m =
Matrix is singular to working precision.
id =
MATLAB:singularMatrix
So, A1 was singular, and lastwarn tells us what it saw.
Now, create a new matrix, and invert it.
A2 = rand(2);
B2 = inv(A2);
[m,id]=lastwarn()
m =
Matrix is singular to working precision.
id =
MATLAB:singularMatrix
So lastwarn still retains the warning from the previous call.
The point is, if you only test the value of lastwarn, then you need to have reset lastwarn, after EVERY call to lastwarn. Part of me wants this to be the expected behavior of lastwarn, that if you ask the question a second time, it won't repeat the warning. Or perhaps it should be an option to lastwarn, an ask, THEN reset the last warning to empty automatically.
So, lets see whta happens if we reset lastwarn, then redo the computation for B2.
lastwarn('')
B2 = inv(A2);
[m,id]=lastwarn()
m =
''
id =
''
Of course, if you use cond or rank before the computation, you would have predicted that A1 was singular before even trying to compute the inverse.
Or, you might decide to use pinv instead of inv. Sometimes that is the right thing to do, depending on the problems you are trying to solve.
Steven Lord
2021년 3월 3일
Another option, particularly if you're going to solve multiple systems with the same coefficient matrix, is to create a decomposition for the matrix and ask it if the matrix isIllConditioned.
M = magic(4)
rank(M) % M is singular
D = decomposition(M);
isIllConditioned(D)
You can also control whether or not \ and / issue warnings when called on the decomposition if the matrix is ill conditioned using its CheckCondition property.
x1 = M\[1; 0; 0; 0]
x2 = D\[1; 0; 0; 0]
D.CheckCondition = false;
x3 = D\[1; 0; 0; 0]
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear Algebra에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!