0

As data I get a matrix A but in my algorithm I need to work on its inverse. What I do is:

C = inv(A) + B;

Then in another line I update A. In the next cycles I also need (updated) A inverse, again for this algorithm. And so on. In the later cycles I get this:

Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.425117e-019

or this:

Warning: Matrix is singular to working precision.

or this:

Warning: Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN.

Can you help me how to avoid such singularity? Matrix is squared always.

Amro
  • 123,847
  • 25
  • 243
  • 454
josh130
  • 305
  • 1
  • 5
  • 12
  • -1 for using `inv` @RodyOldenhuis - can you take it from here? – Shai Jun 23 '13 at 19:11
  • @Shai you probably wanted to link to this question: [Matlab inverse operation and warning](http://stackoverflow.com/q/12877582) (instead of Rody's profile), right? – Eitan T Jun 23 '13 at 20:04
  • @EitanT sorry. I meant [this](http://stackoverflow.com/questions/17156379/matlab-how-to-vectorize-a-nested-loop-over-a-2d-set-of-vectors/17156570#comment24836840_17156570) comment – Shai Jun 23 '13 at 20:21
  • You can find [here](http://stackoverflow.com/questions/17156379/matlab-how-to-vectorize-a-nested-loop-over-a-2d-set-of-vectors/17156570#comment24836840_17156570) quite a lengthy discussion on why NEVER to use matlab's inv() function. Please show us some more of you algorithm so we can advise you on how to eliminate the use of inv, replacing it with more efficient and robust functions. – Shai Jun 23 '13 at 20:30
  • Can you show us a bit more of the context of your algorithm? I.e., I'd like to know *what* you are trying to compute, not *how* you are doing it. – Rody Oldenhuis Jun 24 '13 at 05:24
  • @Shai: hmmm I didn't get any notifications for this and just "randomly" came across it...weird – Rody Oldenhuis Jun 24 '13 at 05:34
  • @RodyOldenhuis - glad you came across. I believe an expert opinion is required here. – Shai Jun 24 '13 at 05:41
  • @josh130 can you show your matrix? – fatihk Jun 24 '13 at 05:56
  • @Shai, whilst it's good to make people aware (educate them) that there are often better ways of solving problems that might naively be solved using `inv` (for example, `x=A\b` is better than `x=inv(A)*b`), I'm not sure it's wise to claim, with capitals, that `inv` should never be used. It might be taken to imply that MATLAB implements the inverse operation poorly, which is not the case. – Sam Roberts Jun 24 '13 at 08:46
  • @SamRoberts - you are more then welcome to join the discussion [here](http://stackoverflow.com/questions/17156379/matlab-how-to-vectorize-a-nested-loop-over-a-2d-set-of-vectors/17156570#comment24836840_17156570) about this subject. – Shai Jun 24 '13 at 09:02
  • @RodyOldenhuis would you care to answer [SamRoberts' comment](http://stackoverflow.com/questions/17263873/singularity-for-inverse-matrix?noredirect=1#comment25036523_17263873) ? – Shai Jun 24 '13 at 09:03
  • 1
    @SamRoberts: The inverse has a lot of *theoretical* value, but not much *practical* value. MATLAB's implementation of `inv()` is not poor, the *use of it* is simply a sign of a poor program design (in any non-educational context). This statement is not limited to MATLAB; it is the outcome of a bunch of proven theorems in numerical math. In the context of finite-precision number systems, it's like trying to prove the superiority of the [aether](http://en.wikipedia.org/wiki/Aether_(classical_element)#Aether_and_Light) over [general relativity](http://en.wikipedia.org/wiki/General_relativity). – Rody Oldenhuis Jun 24 '13 at 09:20
  • @Shai @RodyOldenhuis Looks like there's been a longer discussion on the topic that I wasn't aware of. I won't waste additional space here adding to that thread - especially since I agree with Rody on all the important points. I've just found that many people have been told that `inv` is bad, without having it explained to them what that means. – Sam Roberts Jun 24 '13 at 12:47

1 Answers1

-1

You can add some minute identity matrix to A:

A = A + small_coeff * eye(size(A));

so that resulting matrix will be sufficiently non-singular

Tomer Levinboim
  • 992
  • 12
  • 18
fatihk
  • 7,789
  • 1
  • 26
  • 48
  • 2
    -1: yes, that'll make the warning go away, but completely corrupt your outcomes. Try this: `A = diag([eps(0) eps(0)]); B = A + eps*eye(2); A*inv(B)` The outcome *should* be `eye(2)`, but that's not quite the case. – Rody Oldenhuis Jun 24 '13 at 05:33
  • @Rody Oldenhuis, this is just an approximation and it naturally will not work on small valued matrices. – fatihk Jun 24 '13 at 05:38
  • ...which is sort of why he's getting the warning; singular `inv(A)` is the matrix equivalent of trying to divide by zero. In any case, your solution is non-general, and might corrupt his algorithm. We need to know more about the algorithm before we can debug it :) – Rody Oldenhuis Jun 24 '13 at 05:43
  • @Rody Oldenhuis, did you also try it for "reasonable" matrices? – fatihk Jun 24 '13 at 07:50
  • 1
    The point I'm trying to make is that for the OPs algorithm, you cannot assume blindly that "reasonable" matrices will *always* come along. – Rody Oldenhuis Jun 24 '13 at 09:09