Jaya
Jaya

Reputation: 81

How to solve linear equation using Matlab?

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?

Upvotes: 2

Views: 1591

Answers (3)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

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.

Upvotes: 1

Floris
Floris

Reputation: 46375

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.

Upvotes: 1

Stewie Griffin
Stewie Griffin

Reputation: 14939

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.

Upvotes: 1

Related Questions