3

I'm having an error in my code, I hope you can help me!: (When I paste the code something weird happens (not all of it is written like code) but here we go:

I want to linalg.solve(A,Res) . The first one (A) has 10 rows and 10 columns,i.e, matrix([10 arrays, 10 elements]) and the second one has 10 rows and 1 column, i.e, matrix([1 array, 10 elements]).

When I executed the code it throws the following error: Singular Matrix

I don't know what to do. When I don't ask to linalg.solve, but ask to print both matrices, both are fine: 10 equations, 10 variables. So I don't know what's going on. Please Help!!!

If you need me to paste the code (as horrible as it looks) I can do it.

Thank you

Tarik Riadi
  • 39
  • 1
  • 1
  • 4
  • If the code is too big/unreadable to paste here, I suggest you create a small test case (I guess you don't need to have 10 rows/columns) and see if you can get that to work. If not, post that small example code here, otherwise people have to guess what your problem might be. – FriendFX May 04 '15 at 00:43
  • If the system is under determined you can use numpy.linalg.lstsq to obtain one of the solutions. – Ben May 04 '15 at 01:08

1 Answers1

4

A singular matrix is a matrix that cannot be inverted, or, equivalently, that has determinant zero. For this reason, you cannot solve a system of equations using a singular matrix (it may have no solution or multiple solutions, but in any case no unique solution). So better make sure your matrix is non-singular (i.e., has non-zero determinant), since numpy.linalg.solve requires non-singular matrices.

Here is some decent explanation about what's going on for 2 x 2 matrices (but the generalization is straightforward to N x N).

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • Ok, but these matrices I built are the result of a truss calculation. So if the matrix is singular, it means the truss cannot be solved? I'm confused, I mean, I know linear algebra, but even if your matrix has as many equations as variables, it may not be solved? (I don't quite remember that one) – Tarik Riadi May 04 '15 at 01:08
  • 1
    Yes, the determinant is the quantity that makes the difference. Think about this: Solve `[1 1 ; 1 1][x y]^T = [1 2]^T`. It has no solution (determinant is zero). Or, the other extreme, `[1 1; 1 1][x y]^T = [1 1]`, which has an infinite number of solutions. So, unless the determinant is non-zero (i.e. the coefficient matrix is non-singular), your system of equations either has no solution or has an infinite number of solutions. I suggest you take a look at the link I posted, it is quite useful for a quick brush-up. – vsoftco May 04 '15 at 01:10