-->

How to compute inverse of a matrix accurately?

2019-01-08 00:26发布

问题:

I'm trying to compute an inverse of a matrix P, but if I multiply inv(P)*P, the MATLAB does not return the identity matrix. It's almost the identity (non diagonal values in the order of 10^(-12)). However, in my application I need more precision.

What can I do in this situation?

回答1:

Only if you explicitly need the inverse of a matrix you use inv, otherwise you just use the backslash operator \.

The documentation on inv explicitly states:

x = A\b is computed differently than x = inv(A)*b and is recommended for solving systems of linear equations.

This is because the backslash operator, or mldivide uses whatever method is most suited for your specific matrix:

x = A\B solves the system of linear equations A*x = B. The matrices A and B must have the same number of rows. MATLAB® displays a warning message if A is badly scaled or nearly singular, but performs the calculation regardless.

Just so you know what algorithm MATLAB chooses depending on your input matrices, here's the full algorithm flowchart as provided in their documentation

The versatility of mldivide in solving linear systems stems from its ability to take advantage of symmetries in the problem by dispatching to an appropriate solver. This approach aims to minimize computation time. The first distinction the function makes is between full (also called "dense") and sparse input arrays.



回答2:

You have what's called an ill-conditioned matrix. It's risky to try to take the inverse of such a matrix. In general, taking the inverse of anything but the smallest matrices (such as those you see in an introduction to linear algebra textbook) is risky. If you must, you could try taking the Moore-Penrose pseudoinverse (see Wikipedia), but even that is not foolproof.