Search code examples
pythonnumpymatrixlinear-algebralinear-equation

Solving System of linear equation using Cramer's method in Python


I am trying to solve the following system of linear equations:

10x1+ 40x2+ 70x3= 300
20x1+ 50x2+ 80x3= 360
30x1+ 60x2+ 80x3= 390

by using Cramer's method implementing a function by scratch:

def cramer(mat, constant): # takes the matrix and the costants
    
    D = np.linalg.det(mat) # calculating the determinant of the original matrix

# substitution of the column with costant and creating new matrix
    mat1 = np.array([constant, mat[1], mat[2]])
    mat2 = np.array([mat[0], constant, mat[2]])
    mat3 = np.array([mat[0], mat[1], constant])  
    
#calculatin determinant of the matrix
    D1 = np.linalg.det(mat1)
    D2 = np.linalg.det(mat2)
    D3 = np.linalg.det(mat3)
    
#finding the X1, X2, X3
    X1 = D1/D
    X2 = D2/D
    X3 = D3/D
    
    print(X1, X2, X3)

By using the above function on the system

a = np.array([[10, 40, 70],
             [20, 50, 80],
             [30, 60, 80]])

b = np.array([300, 360, 390])

I get the following result:

cramer(a,b)
-22.99999999999996 21.999999999999964 2.999999999999993

I have solved the system using the numpy function np.linalg.solve and I get another result:

x = np.linalg.solve(a, b)
[1. 2. 3.]

I cannot spot the formula error in the function I have witten. What should I adjust in the fuction in order to make it working properly?


Solution

  • The main problem is how you select the columns of a, i.e. you are actually selecting the rows of a rather than the columns. You can fix it by changing the matrix initializations to this:

        mat1 = np.array([constant, mat[:,1], mat[:,2]])
        mat2 = np.array([mat[:,0], constant, mat[:,2]])
        mat3 = np.array([mat[:,0], mat[:,1], constant])  
    

    Basically mat[:,1] is saying something like mat[all rows, column 1].