MATLAB Answers

Inverse matrix in Matlab very different to Excel

3 views (last 30 days)
Rosie G
Rosie G on 10 Dec 2019
Commented: Fabio Freschi on 10 Dec 2019
Replicating calculations carried out in Excel in Matlab in order to learn. Inverting the matrix below gave a very different answer in Matlab to excel.
Original Matrix:
1322252 0 2010 -1006
0 1322252 1006 2010
2010 1006 4 0
-1006 2010 0 4
Inverted Matrix using inv(matrix)
1.68867574048431e-05 5.0821976835258e-21 -0.00848559559593365 0.00424701948731803
5.0821976835258e-21 1.68867574048431e-05 -0.00424701948731803 -0.00848559559593364
-0.00848559559593365 -0.00424701948731803 5.58213718801715 -1.28379263845227e-15
0.00424701948731803 -0.00848559559593364 -1.28379263845227e-15 5.58213718801714
Inverted Matrix using Excel
1.68868e-05 -1.34343e-20 -0.008485596 0.004247019
2.82121e-20 1.68868e-05 -0.004247019 -0.008485596
-0.008485596 -0.004247019 5.582137188 -6.91897e-15
0.004247019 -0.008485596 6.91897e-15 5.582137188
Inverting the below matrix did work as expected (same answer in Excel and Matlab)
419 163 1 0
-163 419 0 1
586 340 1 0
-340 586 0 1

  1 Comment

Jeremy Marcyoniak
Jeremy Marcyoniak on 10 Dec 2019
This is probably a case of different methods and/or numerical tolerances. The results are approximately the same, 5.0821976835258e-21 and -1.34343e-20 are both essentially zero

Sign in to comment.

Accepted Answer

Fabio Freschi
Fabio Freschi on 10 Dec 2019
The answer is not very different. As Jeremy commented, the results are similar a part from numerical tolerances. You can verify yourself comparing the matrices. In the following I load your matrices (note that excel data have less digits):
A1 = [ 1.68867574048431e-05 5.0821976835258e-21 -0.00848559559593365 0.00424701948731803
5.0821976835258e-21 1.68867574048431e-05 -0.00424701948731803 -0.00848559559593364
-0.00848559559593365 -0.00424701948731803 5.58213718801715 -1.28379263845227e-15
0.00424701948731803 -0.00848559559593364 -1.28379263845227e-15 5.58213718801714];
A2 = [ 1.68868e-05 -1.34343e-20 -0.008485596 0.004247019
2.82121e-20 1.68868e-05 -0.004247019 -0.008485596
-0.008485596 -0.004247019 5.582137188 -6.91897e-15
0.004247019 -0.008485596 6.91897e-15 5.582137188];
Then you can compare the two matrices entry-by-entry:
A1-A2
ans =
-4.2595e-11 1.8516e-20 4.0407e-10 4.8732e-10
-2.3130e-20 -4.2595e-11 -4.8732e-10 4.0407e-10
4.0407e-10 -4.8732e-10 1.7150e-11 5.6352e-15
4.8732e-10 4.0407e-10 -8.2028e-15 1.7140e-11
And you can see the errors are "small" in absolute value (the largest difference is at the 10th digit)
As a global index you can use the (relative) norm of the matrices
norm(A1-A2)/norm(A1)
ans =
1.1581e-10

  2 Comments

Rosie G
Rosie G on 10 Dec 2019
Thank you so much for your fast reply, and I feel very foolish now!
Fabio Freschi
Fabio Freschi on 10 Dec 2019
No problem, sometimes obvious things slip under the radar!

Sign in to comment.

More Answers (0)

Sign in to answer this question.