# inverse matrix calculation problem

30 views (last 30 days)
Semen Funderat on 28 Sep 2020 at 10:01
Edited: John D'Errico on 28 Sep 2020 at 11:02
Hey guys, I am new at MatLab, so I decided to start with an official tutorial! On Matrices and Arrays part we create
>> a=[1 2 3;4 5 6;7 8 9]
and then multiply it with an inverse matrix
>> a*inv(a)
what I get is not the identity matrix, but
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 2.202823e-18.
ans =
0 0 0
-4 0 0
-4 0 0.
What may be wrong?
P.S. When I try with another example it works well:
>> X = [1 0 2; -1 5 0; 0 3 -9]
X =
1 0 2
-1 5 0
0 3 -9
>> X*inv(X)
ans =
1.000000000000000 0 0
0.000000000000000 1.000000000000000 0
0 0 1.000000000000000

N. Shamsundar on 28 Sep 2020 at 10:19
Read a linear algebra book or article and learn about singular and non-singular matrices.
Work out the inverse of your first 3 X 3 matrix using only pencil, paper and mind, and you may learn from the experience

John D'Errico on 28 Sep 2020 at 10:46
Edited: John D'Errico on 28 Sep 2020 at 11:02
You need to understand that the matrix
a=[1 2 3;4 5 6;7 8 9]
is singular. Therefore it has no inverse.
You may as well have said, sometimes when I compute x/x, for scalar x, I get the expected answer: 1. But every once in a while, that does not happen. Why is that?
>> x = 2;x/x
ans =
1
>> x = pi;x/x
ans =
1
>> x = 0;x/x
ans =
NaN
WAIT. What happened? Do you see that what you did was no different?
You cannot compute the value of 0/0, because 0 has no multiplicative inverse. Therefore while MATLAB does compute something, that something is an indeterminate result.
In the case of a matrix, while MATLAB computes something, the result is effectively meaningless garbage. (Garbage in, garbage out.) Did you see the warning? As your other answer says, you might want to go back and do some reading. If you will do computations but not undertand what you are doing, what do you expect? Suppose you pick up a sharp knife, without understanding what a knife is or what it does. Should we be surprised if you cut yourself? Of course not. We might post a warning: DO NOT PLAY WITH KNIVES. But then, you did get a warning!
rank(a)
ans =
2
cond(a)
ans =
50522794445385096
You could have tested to see if a was singular. Thus, compute the rank, which is 2. Or the condition number, which is seriously large. In either case, they would tell you the matrix is singular. (DO NOT TEST THAT USING DET. Even if you may have learned to use a determinant in some long almost forgotten class to learn if a matrix is singular, this is the wrong thing to do.)

R2020a

### Community Treasure Hunt

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

Start Hunting!