Backslash for inverse to solve Ax=B

43 visualizaciones (últimos 30 días)
hp
hp el 18 de Mzo. de 2022
Comentada: hp el 18 de Mzo. de 2022
HI,
In my model, I am solving Ax=B system using backslash instead of inv and pinv. It gives answers with 'Matrix is close to singular or badly scaled......'. But answers are correct and I could not find any error in my system(in the way I define A and B metrices).
What is the exact reason for getting correc answers with the warning?
I used inv() and pinv() and INV function didnot solve as the matrix is singular. Error of the solution using PINV is larger than backslash.
Appreciate your comments.

Respuesta aceptada

John D'Errico
John D'Errico el 18 de Mzo. de 2022
Editada: John D'Errico el 18 de Mzo. de 2022
When you have a singular system, then pinv and backslash will both attempt to solve the problem, to the extent they can.
I seriusly doubt that the pinv solution is "worse" in terms of a larger error, at least if you consider the magnitude of the differences, and then think about floating point issues, like is the difference even significant in context?
The fact is there is no correct answer when your matrix is singular. At least there is no way to mathematically arrive at one. All possible solutions out of infinitely many are equally wrong, or right, depending on your point of view.
inv fails, because the inverse of a singular matrix does not exist. So you cannot use inv, when your matrix is singular. Even when the matrix is ill-conditioned, there is no gain to using inv. Just use backslash.
What does backslash do? It solves the problem in such a way that SOME of the elements will be zero. If the matrix is rank deficient, then the rank deficiency tell you how many elements are forced to zero.
For example, consider this matrix:
A = magic(8)
A = 8×8
64 2 3 61 60 6 7 57 9 55 54 12 13 51 50 16 17 47 46 20 21 43 42 24 40 26 27 37 36 30 31 33 32 34 35 29 28 38 39 25 41 23 22 44 45 19 18 48 49 15 14 52 53 11 10 56 8 58 59 5 4 62 63 1
Now, suppose I decided to solve the problem A*x=b? First, what is the rank of A?
rank(A)
ans = 3
UGH. A is REALLY, seriously rank deficient. I would initially expect 8-3=5 zero elements in a solution from backslash, but that may not happen due to numerical issues. Now, suppose I know the ground truth solution, as
Xtrue = (-3:4)'
Xtrue = 8×1
-3 -2 -1 0 1 2 3 4
And that would imply a vector b.
b = A*Xtrue
b = 8×1
122 138 138 122 122 138 138 122
Now, can we recover Xtrue using either backslash, or pinv, or inv? First, note that inv must fail.
inv(A)
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 2.238603e-34.
ans = 8×8
1.0e+30 * 4.3215 -1.5571 3.7699 -3.3732 -2.6357 1.5571 -3.7699 1.6873 0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 0.0000 -0.0000 0.0000 -0.0000 -4.4792 1.5568 -3.7692 4.0063 2.1599 -1.5568 3.7692 -1.6870 -4.1113 1.5575 -3.7708 2.5291 3.2700 -1.5575 3.7708 -1.6877 0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.0000 -0.0000 0.0000 0.0000 4.2690 -1.5572 3.7701 -3.1622 -2.7942 1.5572 -3.7701 1.6874
Pure, useless garbage results when we try to use inv. There is virtually never a good reason to use the matrix inverse for a problem like this. INV is a nice toy for students to play with, but that is usually where it should stop. Until and unless you know enough about linear algebra to know when a matrix inverse is and is not a good thing to compute, then you should not be using inv for anything.
format long g
Xbackslash = A\b
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 2.238603e-34.
Xbackslash = 8×1
26.7817349263138 -3.17496658807827e-14 2.71852457363952e-14 -24.8295777544742 -30.0512778220999 4.00000000000001 -2.00000000000001 30.0991206502603
There were 2 effectively zero elements. (I was sort of expecting 5 zero elements, because A has rank only 3, but floating point arithmetic got in the way there.) It did give us a warning message, telling us that it was worried about the result.
But backslash did not do wonderfully well, at least in terms of finding a solution close to our ground truth vector in Xtrue.
Xpinv = pinv(A)*b
Xpinv = 8×1
-0.166666666666667 0.976190476190476 0.785714285714286 0.404761904761905 0.595238095238096 0.214285714285714 0.0238095238095242 1.16666666666667
PINV did not do that well either, in terms of coming close to Xtrue, but the mathematics behind pinv yields a solution with minimum norm over all possible solutions. And norm(Xpinv) is pretty small, compared to norm(Xbackslash). But we need to recognize that both solutions were actually pretty good.
A*[Xbackslash,Xpinv]
ans = 8×2
122 122 138 138 138 138 122 122 122 122 138 138 138 138 122 122
In fact, to within floating point trash, they are exact solutions. Which is "correct"? Neither.
In the end, I'm not sure how I want to give you the exact reason for any of this, not without teaching an entire course on linear algebra. What you are asking is to understand the mathematics behind linear systems of equations, what pinv does, and what a solver like backslash does, and what happens when your matrices are rank deficient. As I said, a complete course in linear algebra, and I've already been writing this for far too long.
  1 comentario
hp
hp el 18 de Mzo. de 2022
I got the point. Thank you very much for the explanation.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by