the matlab code and the output i was expecting (gauss elimination method)
my code in python:
import numpy as np A = np.array([ [1,2,-1,1], [-1,4,3,1], [2,1,1,1]]) n = rows = len(A) col = len(A[0]) for i in range(n): A[i,:] = A[i,:] / A[i,i] for j in range(n): if i==j: pass else: A[j,:] = A[j,:] - (A[i,:] * A[j,i]) print(A)
the output i got:
[[1 0 0 1] [0 1 0 0] [0 0 1 0]]
Advertisement
Answer
Your problem is related to casting. Without info, numpy
cast your matrix to integer numbers, so when you divide, the result is not a float. For example 2 / 6 = 0
and not 0.33333
.
If you put
A = np.array([ [1,2,-1,1], [-1,4,3,1], [2,1,1,1]], dtype=float)
your result will be
[[1. 0. 0. 0.33333333] [0. 1. 0. 0.33333333] [0. 0. 1. 0.]]
In matlab there is not this problem because your starting matrix is already casted to floating point numbers.