Skip to content

Commit 87fba35

Browse files
authored
Add files via upload
1 parent 2cbd8e6 commit 87fba35

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

ex8.2GaussianElimination.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#Solving the system of equations for w, x, y, and z using Gauss-Elimination method.
2+
3+
def gauss(A):
4+
'''Converts a matrix into the identity
5+
matrix by Gaussian elimination, with
6+
the last column containing the solutions
7+
for the variables'''
8+
rows = len(A)#rows
9+
cols = len(A[0])#columns
10+
for j,rowNo in enumerate(A):
11+
#diagonal term to be 1 by dividing row by diagonal term
12+
if rowNo[j] != 0: #diagonal entry can't be zero
13+
divisor = rowNo[j]
14+
for i, term in enumerate(rowNo):
15+
rowNo[i] = term / divisor
16+
#add the other rows to the additive inverse for every row
17+
for i in range(rows):
18+
if i != j: #don't do it to row j
19+
#calculate the additive inverse
20+
addinv = -1*A[i][j]
21+
#for every term in the ith row
22+
for ind in range(cols):
23+
#add the corresponding term in the jth row multiplied by the additive inverse to the term in the ith row
24+
A[i][ind] += addinv*A[j][ind]
25+
return A
26+
#example:
27+
'''
28+
2w- x+5y+ z=-3
29+
3w+2x+2y-6z=-32
30+
w+3x+3y- z=-47
31+
5w-2z-3y+3z=49
32+
'''
33+
A = [[2,-1,5,1, -3],
34+
[3,2,2,-6, -32],
35+
[1,3,3,-1, -47],
36+
[5,-2,-3,3, 49]]
37+
print(gauss(A))

0 commit comments

Comments
 (0)