In the following code I have implemented Gaussian elimination without partial pivoting for a general square linear system Ax = b.
However I am looking for some help with implementing the following two requirements,
1) I want to make sure that my function terminates if a zero pivot is encountered.
2) And I want to test it by solving Ax = b where A is a random 100x100 matrix and b is a random 100x1 vector.
Looking for some help with adding these two things into the code I have produced! thanks!
def linearsolver(A,b):
n = len(A)
M = A
i = 0
for x in M:
x.append(b[i])
i += 1
for k in range(n):
for i in range(k,n):
if abs(M[i][k]) > abs(M[k][k]):
M[k], M[i] = M[i],M[k]
else:
pass
for j in range(k+1,n):
q = float(M[j][k]) / M[k][k]
for m in range(k, n+1):
M[j][m] -= q * M[k][m]
x = [0 for i in range(n)]
x[n-1] =float(M[n-1][n])/M[n-1][n-1]
for i in range (n-1,-1,-1):
z = 0
for j in range(i+1,n):
z = z + float(M[i][j])*x[j]
x[i] = float(M[i][n] - z)/M[i][i]
print (x)


I'm answering this here anyways. People will say you should answer things on Stackoverflow if it is primarily coding, however, mathematical questions on StackOverflow are instantly overlooked.
Gaussian Elimination without Pivoting
Generates random matrix with given condition number
This part here is testing
Matlab Implementation Of Partial Pivoting From a Numerical Methods Book
There is an implementation here