error running getting started code

2 views (last 30 days)
Jennifer
Jennifer on 19 Sep 2024
Edited: Jatin on 19 Sep 2024
When I run the code from teh learning module "getting started" to muiltiply the matrix by it's inverse I do not get the example output.
"p = a*inv(a)
p = 3×3
1.0000 0.0000 -0.0000
0 1.0000 -0.0000
0 0.0000 1.0000"
Instead I get a warning and p = infinite values:
"a =
1 3 5
2 4 6
7 8 9
>> p=a*inv(a)
Warning: Matrix is singular to working precision.
p =
Inf Inf Inf
Inf Inf Inf
Inf Inf Inf"
WHy is this happening?

Answers (2)

Jatin
Jatin on 19 Sep 2024
Edited: Jatin on 19 Sep 2024
The warning "Matrix is singular to working precision" occurs when the matrix you're trying to invert, is close to being singular or is singular. A singular matrix is one that does not have an inverse, which can happen when its determinant is zero.
The formula for the inverse of a matrix involves dividing the cofactor matrix by the determinant of the matrix. When the matrix is singular, this operation essentially becomes division by zero, leading to "inf" values.
For example, the determinant of the matrix used in your case.
a = [1 3 5; 2 4 6; 7 8 9];
det(a)
ans = 0
The matrix used in MATLAB's inv documentation is different, with a non-zero determinant. As a result, performing the operation "a*inv(a)" does not produce the warning.
I hope this answers your question.

Steven Lord
Steven Lord on 19 Sep 2024
If you're using this matrix:
a = [1 3 5
2 4 6
7 8 9];
the warning is correct. The matrix you've provided is singular; it has no inverse. One way to see this is to note that the second column of the matrix is 0.5 times the first column plus 0.5 times the third column.
secondColumn1 = a(:, 2);
secondColumn2 = 0.5*a(:, 1) + 0.5*a(:, 3);
isequal(secondColumn1, secondColumn2)
ans = logical
1
I'm guessing the 'learning module "getting started" ' used a different matrix, one that is invertible. For example here's one with a nice pattern that is invertible:
A = [1 3 6; 2 5 8; 4 7 9]
A = 3×3
1 3 6 2 5 8 4 7 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
invA = inv(A)
invA = 3×3
2.2000 -3.0000 1.2000 -2.8000 3.0000 -0.8000 1.2000 -1.0000 0.2000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
format longg
A*invA % Should be close to the 3-by-3 identity matrix
ans = 3×3
1 -8.88178419700125e-16 2.22044604925031e-16 0 0.999999999999998 8.88178419700125e-16 0 0 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!