Filter löschen
Filter löschen

Matlab 2014b bug: Warning: Matrix is singular to working precision.

2 Ansichten (letzte 30 Tage)
Zain
Zain am 19 Dez. 2018
Bearbeitet: John D'Errico am 20 Dez. 2018
Hallo,
I am getting warning when I try to invert matrix:
ph =
1.0e-06 *
0.4905 0.3451
0.3451 0.2428
>>inv(ph)
Warning: Matrix is singular to working precision.
>>ans
ans =
Inf Inf
Inf Inf
but when I copy values of the matrix after 'inv' command I get what I want:
>>A = inv(1.0e-06 * [0.4905 0.3451; 0.3451 0.2428])
A =
1.0e+11 *
-3.9803 5.6574
5.6574 -8.0410
why is this happening? And how to avoid this in a script?
Seems like there is a problem with floating point precision, because ph values in output are rounded to 4 decimals. Well, suppose that
ph=round(eig_ph,4,'significant');
is an acceptable answer.
P.S. It is my misstake for calling it bug.
  2 Kommentare
Stephen23
Stephen23 am 19 Dez. 2018
"Matlab 2014b bug"
What exactly is the bug supposed to be?
John D'Errico
John D'Errico am 20 Dez. 2018
Bearbeitet: John D'Errico am 20 Dez. 2018
This is NOT a problem with floating point precision!!!!!
The problem arose from copying from the command line a 4 digit approximation to a set of numbers, and then trying to use those approximations instead of the original numbers.
If you do not approecaite the difference, set your display format as:
format long g
And only then look at the matrix ph. You will see more numbers that follow the 4 digits originally reported.

Melden Sie sich an, um zu kommentieren.

Antworten (4)

Stephen23
Stephen23 am 19 Dez. 2018
Bearbeitet: Stephen23 am 19 Dez. 2018
"I am getting wierd warning when I try to invert matrix"
It is not weird at all: you confused how floating point numbers are displayed with the actual values stored in memory (these are two quite different things).
"why is this happening?"
Clearly the actual values in memory (stored with around fifteen significant figures of precision) define a (nearly) singular matrix. The values you copied have only four decimal places and so your input is different. Differerent input -> different output. Not weird at all.
But, given the values that you used and the likely inappropriate use of inv, either way you will still get garbage output.
"And how to avoid this in a script? "
  1. Don't try to invert (nearly) singular matrices and expect a meaningful output.
  2. Read the inv documentation, and use mldivide or mrdivide to solve systems of equations. Beginners use inv to solve systems of equations, because they don't know how bad it is numerically.
  3. If you really need an inverse consider pinv.
"Well, suppose that round(ph,4) is the answer."
Um, no. You will still get numeric garbage as an output (like you are getting now).
  1 Kommentar
Bruno Luong
Bruno Luong am 20 Dez. 2018
  • Read the inv documentation, and use mldivide or mrdivide to solve systems of equations. Beginners use inv to solve systems of equations, because they don't know how bad it is numerically.
I wouldn't blame on users, beginners or not. Why TMW don't implement
inv(A) = A \ eye(size(A))
to make at least more consistent with backslash?
Why they chose "bad" algorithm for INV (LU) and suggest not using it? Just wonder. In which case INV is useful?

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 20 Dez. 2018
syms d
A = 1.0e-06 * [0.4905+d 0.3451; 0.3451 0.2428];
d = solve(det(A))
double(1.0E-6*(0.4905+d))
ans =
0.490502512355848
That is, by changing a single entry of the four by 2 1/2 parts per million, the matrix becomes singular. This is a change too small to be displayed with the "format short" that you have in effect.

Bruno Luong
Bruno Luong am 19 Dez. 2018
Bearbeitet: Bruno Luong am 19 Dez. 2018
The problem is this
"Warning: Matrix is singular to working precision."
That means you matrix inversion is unstable, illposed, change a lot with truncation error.
This is NOT a BUG, when you copy the value of ph displayed on the screen you truncate the decimal of you matrix coefficients. Therefore the bahaviors change as stated above.
How to avoid it? Don't work with such matrix and don't use INV.

madhan ravi
madhan ravi am 19 Dez. 2018
I wasn't able to produce it in 2018b
ph =
1.0e-06 *
0.4905 0.3451
0.3451 0.2428
>> inv(ph)
ans =
1.0e+11 *
-3.9803 5.6574
5.6574 -8.0410
>>
  2 Kommentare
John D'Errico
John D'Errico am 20 Dez. 2018
You would infact not reproduce it, in ANY version of MATLAB. Why not?
ph_hat = 1e-06 * [0.4905 0.3451;0.3451 0.2428];
But is ph actually singular? No. In fact, ph is not singular at all, as we see by these measures:
rank(ph_hat)
ans =
2
>> cond(ph_hat)
ans =
881524.770544205
>> svd(ph_hat)
ans =
7.3330083185505e-07
8.31855049747893e-13
Even in single precision, ph_hat would only be close to singular.
The issue is ph was reported to 4 significant digits, then the OP used that 4 digit approximation to the true array ph, trying then to invert that array.
In reality, since MATLAB reported that the array stored in ph was singular, we know what happened. We know that the true array is indeed singular, and that it is possible to perturb ph_hat by perturbations on the order of 8e-13 so that the resulting matrix is indeed singular. (We learn that from the svd.)
madhan ravi
madhan ravi am 20 Dez. 2018
Well clarified John D'Errico , thank you!

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by