# Too much matrix inverse error?

I use the `inv()` to do the inverse of the matrix, but I find that I don’t get the inverse matrix, or is it due to too much error?

This is my matrix

,
but `inv(M1)*M1` not equal to unit matrix,

Why is it happen?

Are you sure you really want the inverse matrix and not the solution of the equations which would be `y \ M`.

Also your matrix looks very sparse, maybe try to call explicitly

``````julia> using SparseArrays

julia> sparse(zeros((4,4)))
4×4 SparseMatrixCSC{Float64, Int64} with 0 stored entries:
⋅    ⋅    ⋅    ⋅
⋅    ⋅    ⋅    ⋅
⋅    ⋅    ⋅    ⋅
⋅    ⋅    ⋅    ⋅
``````
2 Likes

Maybe your matrix is badly conditioned (i.e., nearly singular), in which case floating-point roundoff errors are enormously amplified when you try to invert the matrix. Try computing `cond(M1)` to get the condition number — probably it is huge (\sim 10^{15}).

If you are solving systems with an ill-conditioned matrix, you probably need to think carefully about what you are doing. (e.g. if you need a regularization, or if the problem is exactly singular and there is some other aspect of your problem that indicates which solution you want.)

6 Likes

You shouldn’t be inverting matrices. It’s not as bad as numerical algebra courses will tell you (as I fairly recently learned on here), but it’s still very rarely the best way to achieve what you want.