Skip to content
Advertisement

Solve linear system in python with different set of roots

I’m having a problem where I have this linear system for example

| a b c |   | x1 |   | 0 |
| d e f | x | x2 | = | 0 |
| g h i |   | x3 |   | 0 |

I need to solve for x1, x2, and x3 but every library I used gave me only x1=x2=x3=0 as a solution it’s correct but the system accepts other solution. I am looking for a solution to avoid zeros as answer. Thanks for helping.

Advertisement

Answer

That’s more of a math problem than a python problem.

I mean you’re general formula would be:

A(matrix) * x(vector) = b(vector)

so what you do is compute the inverse of the Matrix and add multiply it from the left to both sides:

Inv(A) * A *x = Inv(A) *b

now as Inv(A)*A is 1(Matrix with diagonal elements being 1) and 1(matrix)*x = x, you’d have your solution:

x = Inv(A)*b

Now there a function like numpy.linalg.inv that let you compute the inverse if one exists (check the conditions for that) and numpy.dot which lets you multiply a matrix and a vector. And there are function that let you solve it in one step like: numpy.linalg.solve. However whether that works kinda depends on what values you’re dealing with an if the operations are mathematically allowed. Also for obvious reasons if you multiply your inverse matrix with 0,0,0 then the resulting vector will be zero as:

|a' b' c'|     |0|    |a'*0+b'*0+c'*0|    |0 + 0 + 0|   |0|
|d' e' f'|  *  |0| =  |d'*0+e'*0+f'*0| =  |0 + 0 + 0| = |0| 
|g' h' i'|     |0|    |g'*0+h'*0+i'*0|    |0 + 0 + 0|   |0|

where a’ and so on are the elements of the inverse matrix whatever they end up going to be.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement