问题
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\bis computed differently thanx = inv(A)*band 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\Bsolves the system of linear equationsA*x = B. The matricesAandBmust have the same number of rows. MATLAB® displays a warning message ifAis 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
mldividein 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.
来源:https://stackoverflow.com/questions/36166599/how-to-compute-inverse-of-a-matrix-accurately