# Use of Inverse Matrix

I solved a matrix as follows:

{{0,1,1},{0,2,4},{0,3,9}}.{{0},{25},{20}}


Resulting:

{{45},{130},{255}}


I tried to use an inverse matrix to solve:

Inverse[{{0,1,1},{0,2,4},{0,3,9}}].{{45},{130},{255}}


Wishing the following solution:

{{0},{25},{20}}


But this matrix is singular:

How should I proceed?

You need PseudoInverse:

mat = {{0, 1, 1}, {0, 2, 4}, {0, 3, 9}};
PseudoInverse[mat].{{45}, {130}, {255}}


{{0}, {25}, {20}}

or, LeastSquares (thanks: J.M.)

LeastSquares[mat, {{45}, {130}, {255}}]


{{0}, {25}, {20}}

• ...but using LeastSquares[] is better. Jun 9, 2016 at 0:39
• @J.M., thank you. Updated with that alternative.
– kglr
Jun 9, 2016 at 0:44
• @J.M.: I don't disagree that it is an alternate approach, but why is it better? Speed, numerical accuracy, ...?
– Moo
Jun 9, 2016 at 0:55
• @Moo, it's a more expensive and potentially less stable way to get the same result; for LeastSquares[] (and LinearSolve[], for that matter), one generates an appropriate matrix decomposition which is then easily applied to the right-hand side. For PseudoInverse[] (and Inverse[], for that matter), one has to generate the required matrix from the previously mentioned decompositions, which is already expensive in itself; the additional dot product is yet another opportunity for instability to show up. Jun 9, 2016 at 1:01
• @J.M.: Thanks, that makes more sense.
– Moo
Jun 9, 2016 at 1:06

LinearSolve will also solve underdetermined systems.

LinearSolve[{{0, 1, 1}, {0, 2, 4}, {0, 3, 9}}, {{45}, {130}, {255}}]
(*  {{0}, {25}, {20}}  *)


One can use Solve as well, although the solution has a different form:

Solve[{{0, 1, 1}, {0, 2, 4}, {0, 3, 9}}.{x, y, z} == {{45}, {130}, {255}},
{x, y, z}]
(*  {{y -> 25, z -> 20}}  *)


Here x is a free variable, which can be assigned any real number.