简体   繁体   中英

How to compute inverse of a matrix accurately?

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?

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.

在此处输入图片说明


As a side-note about error of order of magnitude 10^(-12) , besides the above mentioned inaccuracy of the inv() function, there's floating point accuracy. This post on MATLAB issues on it is rather insightful, with a more general computer science post on it here . Basically, if you are computing numerics, don't worry (too much at least) about errors 12 orders of magnitude smaller.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM