error running getting started code
25 views (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
Jatin
on 19 Sep 2024 at 19:13
Edited: Jatin
on 19 Sep 2024 at 19:31
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)
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.
0 Comments
Steven Lord
on 19 Sep 2024 at 19:18
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)
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]
invA = inv(A)
format longg
A*invA % Should be close to the 3-by-3 identity matrix
0 Comments
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!