2

A= [1 2 3;3 4 5;5 6 7], B=[1;1;1].I need to solve the equation AX=B. Here am using Matlab code like X=linsolve(A,B). But, using this a warning is occurred...

"Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.541976e-18."

How to correct it?

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
Jaya
  • 81
  • 2
  • 7

3 Answers3

1

You can't do what you want: "convert singular matrix into nonsingular without changing the data", but you can find one solution to your system Ax = B by using the pseudoinverse, pinv.

The answer is the same you get when using mldivide. The warning you got using mldivide (or \) is only a warning, not an error. Check this link, to see how you can suppress the warning if you need to work with singular matrices and get tired of the warnings.

x = pinv(A)*B;
x =
  -5.0000e-01
   1.2490e-16
   5.0000e-01

Which gives:

A*x
ans =
   1.00000
   1.00000
   1.00000

From Egons answer to a similar question:

But do remember that such a system does not have a unique solution, so both the pseudo-inverse and the backslash operator may (and in this case will) return very different solutions, whether any of them is acceptable really depends on your application.

Community
  • 1
  • 1
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
  • 1
    Regarding `mldivide` vs. `pinv`, [this](http://stackoverflow.com/a/19718075/2778484) may be of interest. – chappjc Jan 06 '14 at 06:13
  • Though mathematically correct, solving equations by calculating an inverse first is NOT the way to go in matlab. This can lead to precision problems, even in cases where the regular methods work fine. See [this Blog by Loren](http://blogs.mathworks.com/loren/2007/05/16/purpose-of-inv/#2) on the issue. – Dennis Jaheruddin Jan 06 '14 at 14:09
1

The three vectors [1,2,3],[3,4,5],[5,6,7] lie in a single plane. How do I know? It's because I can see that [3,4,5]-[1,2,3]=[2,2,2], and [3,4,5]+[2,2,2]=[5,6,7].

Thus, when the question is "what linear combination of these three vectors gets me to this point", there are infinitely many such solutions if the point is in the plane, and none if it is not. Just by inspection you can see

[1,1,1] = ( [3,4,5]-[1,2,3] ) / 2

Meaning a solution is [-0.5 0.5 0]

Or

[1,1,1] = ( [5,6,7] -  [3,4,5] ) / 2

Meaning a solution is [0 -0.5 0.5]

Etc.

You can't make a problem something it is not - and in this case it is ill conditioned so there are infinitely many solutions. Matlab handles it in this case, but warns you. Pencil and paper will lead you to the same conclusion. There is no unique answer.

Floris
  • 45,857
  • 6
  • 70
  • 122
1

Assuming that you are aware that THE solution may not exist, you can simply ask for a second output argument. This will tell Matlat that you are aware of the problem and just want to get the best possible solution.

Here is how it is done:

[X, R] = linsolve(A,B)

Mentioned in the doc of course.

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122