1
$\begingroup$

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?

$\endgroup$

2 Answers 2

6
$\begingroup$

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}}

$\endgroup$
5
  • $\begingroup$ ...but using LeastSquares[] is better. $\endgroup$ Commented Jun 9, 2016 at 0:39
  • $\begingroup$ @J.M., thank you. Updated with that alternative. $\endgroup$
    – kglr
    Commented Jun 9, 2016 at 0:44
  • $\begingroup$ @J.M.: I don't disagree that it is an alternate approach, but why is it better? Speed, numerical accuracy, ...? $\endgroup$
    – Moo
    Commented Jun 9, 2016 at 0:55
  • 2
    $\begingroup$ @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. $\endgroup$ Commented Jun 9, 2016 at 1:01
  • $\begingroup$ @J.M.: Thanks, that makes more sense. $\endgroup$
    – Moo
    Commented Jun 9, 2016 at 1:06
2
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.