Question

In: Computer Science

Gauss Jordan elimination using python for a 2d list (3x3) without using any library. Thanks

Gauss Jordan elimination using python for a 2d list (3x3) without using any library.

Thanks

Solutions

Expert Solution

gaus_jordon.py

def print_matrix(a, n):
    """
        * function to print the matrix of size n
    """
    for i in range(n):
        for j in range(n+1):
            print(a[i][j], " ", end="")
        print()

def print_result(a, n, flag):
    """
        * Function to print the desired result if unique solutions exists, otherwise
        prints no solution or infinite solutions depending upon the input given.
    """
    print("Result is : ")
    if flag == 2:
      print("Infinite Solutions Exists")
    elif flag == 3:
      print("No Solution Exists")
    else :
        for i in range(n):
            print(a[i][n] / a[i][i], " ")

def check_consistency(a, n, flag):
    """
        * To check whether infinite solutions exists or no solution exists
        * flag == 2 for infinite solution
        * flag == 3 for No solution
    """
    flag = 3
    for i in range(n):
        sum = 0
        for j in range(n):
            sum = sum + a[i][j]
        if sum == a[i][j]:
            flag = 2
    return flag

def perform_operation(a, n):
    """
        * function to reduce matrix to reduced row echelon form. 
    """
    k = 0
    flag = 0
    m = 0
    pro = 0

    for i in range(n):
        if a[i][i] == 0:
            c = 1
            while a[i + c][i] == 0 and (i + c) < n:
                c += 1
            if (i + c) == n:
                flag = 1
                break
            j = i
            for k in range(n+1):
                tmp = a[j][k]
                a[j][k] = a[j+c][k]
                a[j+c][k] = tmp

        for j in range(n):
            if i != j:
                pro = a[j][i] / a[i][i]
                for k in range(n+1):
                    a[j][k] = a[j][k] - (a[i][k]) * pro
    return flag

if __name__=="__main__":
    matrix =    [
                    [0, 2, 1, 4],
                    [1, 1, 2, 6],
                    [2, 1, 1, 7]
                ]
    size = 3
    flag = 0
    flag = perform_operation(matrix, size)

    if flag == 1:
        flag = check_consistency(matrix, size, flag)

    print_matrix(matrix, size)
    print_result(matrix, size, flag)

Note : the example is for linear equations:

2y + z = 4
x + y + 2z = 6
2x + y + z = 7

Related Solutions

Show and explain Gauss-Jordan elimination using Matlab.
Show and explain Gauss-Jordan elimination using Matlab.
Solve the system using either Gaussian elimination with back-substitution or Gauss-Jordan elimination. (If there is no...
Solve the system using either Gaussian elimination with back-substitution or Gauss-Jordan elimination. (If there is no solution, enter NO SOLUTION. If the system has an infinite number of solutions, express x, y, z, and w in terms of the parameters t and s.) 4x + 12y − 7z − 20w = 20 3x + 9y − 5z − 28w = 36 (x, y, z, w) = ( ) *Last person who solved this got it wrong
1) Solve the system of linear equations, using the Gauss-Jordan elimination method. (If there is no...
1) Solve the system of linear equations, using the Gauss-Jordan elimination method. (If there is no solution, enter NO SOLUTION. If there are infinitely many solutions, express your answer in terms of the parameters t and/or s.) 3y + 2z = 1 2x − y − 3z = 4 2x + 2y − z = 5 (x, y, z) = 2) Solve the system of linear equations, using the Gauss-Jordan elimination method. (If there is no solution, enter NO SOLUTION....
Use either Gaussian elimination or Gauss-Jordan elimination to solve the given system or show that no...
Use either Gaussian elimination or Gauss-Jordan elimination to solve the given system or show that no solution exists. (Please show clear steps and explain them) x1 + x2 + x3 = 7 x1 − x2 − x3 = −3 3x1 + x2 + x3 = 11
Solve the following system of equations using Gaussian or​ Gauss-Jordan elimination. w + x + y...
Solve the following system of equations using Gaussian or​ Gauss-Jordan elimination. w + x + y + z = -2 2w +2x - 2y - 2z = -12 3w - 2x + 2y + z = 4 w - x + 7y + 3z = 4
Solve the system of linear equations using the Gauss-Jordan elimination method. 2x + 2y + z...
Solve the system of linear equations using the Gauss-Jordan elimination method. 2x + 2y + z = 3 x + z = 2 4y − 3z = 13 solve for x,y,x
Solve the system of linear equations using the Gauss-Jordan elimination method. 2x + 3y - 2z...
Solve the system of linear equations using the Gauss-Jordan elimination method. 2x + 3y - 2z = 8 3x - 2y + 2z = 2 4x - y + 3z = 2 (x, y, z) = ?
1) Solve the system of linear equations using the Gauss-Jordan elimination method. 2x + 4y −...
1) Solve the system of linear equations using the Gauss-Jordan elimination method. 2x + 4y − 6z = 56 x + 2y + 3z = −2 3x − 4y + 4z = −21 (x, y, z) = 2) Solve the system of linear equations using the Gauss-Jordan elimination method. 5x + 3y = 9 −2x + y = −8 (x, y) =
1. Find the : (a) Inverse using explicit Gauss-Jordan elimination (b) The eigenvalues (c) Respective eigenvectors...
1. Find the : (a) Inverse using explicit Gauss-Jordan elimination (b) The eigenvalues (c) Respective eigenvectors (2 0 -2 0 4 0 -2 0 5)
Please Answer 1-3 for me 1. Solve the system of linear equations using the Gauss-Jordan elimination...
Please Answer 1-3 for me 1. Solve the system of linear equations using the Gauss-Jordan elimination method. 2x1 − x2 + 3x3 = −16 x1 − 2x2 + x3 = −5 x1 − 5x2 + 2x3 = −11 (x1, x2, x3) = ( ) 2. Formulate a system of equations for the situation below and solve. For the opening night at the Opera House, a total of 1000 tickets were sold. Front orchestra seats cost $90 apiece, rear orchestra seats...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT