Why do I get different results for the same inverse calculation in command line and a function?

I have a matrix M =
0.0719 0.1797 -0.0621 -0.0279
0.1797 0.9838 -0.6216 -0.3661
-0.0621 -0.6216 0.5026 0.3248
-0.0279 -0.3661 0.3248 0.2173
and want to calculate its inverse:
M_inverse = inv(M);
But the solution is different when I do it in command line to when I do it in a function. The numbers and code are exactly the same, but when I use the function it gives a warning (in command line it does not!):
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 5.116735e-18.
Why do the results differ? Calculating the inverse outside matlab states that the command line solution is correct whilst the useage of inv in a function produces a false conditioning number and false solution. I use Matlab r2015b.
Thank you in advance for your answers.

3 Comments

Also, rank(M) = 4 when I calculate it in command line, but rank(m)=3 inside the function. How is that even possible??
Post all your code from the function and double check that the matrix is absolutely identical.
I suspect your command window matrix M has greater precision than the one in the file/function... In the command window, to see if this is the case, try:
num2str(M)

Sign in to comment.

 Accepted Answer

What you show is NOT the matrix M. What you show is a 4 digit approximation to the real matrix M. The rounding errors make it rank 4, even though the original, fell precision matrix was singular.
NEVER just copy a 4 digit approximation to a number, and hope it is the same number.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!