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
编辑: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
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
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)
M = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
rank(M) % M is singular
ans = 3
D = decomposition(M);
isIllConditioned(D)
ans = logical
1
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]
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 4.625929e-18.
x1 = 4×1
-2.6492 -7.9475 7.9475 2.6492
x2 = D\[1; 0; 0; 0]
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 4.625929e-18.
x2 = 4×1
-2.6492 -7.9475 7.9475 2.6492
D.CheckCondition = false;
x3 = D\[1; 0; 0; 0]
x3 = 4×1
-2.6492 -7.9475 7.9475 2.6492

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Linear Algebra 的更多信息

标签

尚未输入任何标签。

产品


版本

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by