Gaussian Elimination in Python: Illustration and Implementation

Hello coders!! In this article, we will be learning about Gaussian elimination in Python. We will first understand what it means, learn its algorithm, and then implement it in Python. So, let us begin!

What is Gaussian Elimination?

Gaussian elimination is also known as row reduction. It is an algorithm of linear algebra used to solve a system of linear equations. Basically, a sequence of operations is performed on a matrix of coefficients. The operations involved are:

  • Swapping two rows
  • Multiplying a row by a nonzero number
  • Adding a multiple of one row to another row

These operations are performed until the lower left-hand corner of the matrix is filled with zeros, as much as possible.

Illustration of Gaussian Elimination with Example:

Let us consider the following system of linear equations:

  • 2x + y – z = 8………………….L1
  • -3x – y + 2z = -11…………..L2
  • -2x + y + 2z = -3…………..L3

The augmented matrix of the above system of equations will be:

Illustration of Gaussian Elimination with Example

Our objective is to fill the lower left-hand corner of the matrix with zeros as much as possible. For that, we will perform a sequence of operations.

L2 + 3/2L1 -> L2

L3 + L1 -> L3

When we perform the above equation on the augmented matrix, we get:

 augmented matrix gaussian elimination python

Now, we will proceed with the next step of row operation.

L3 + -4L2 -> L3

L3 + -4L2 -> L3

When we perform the above-given operation, we obtain the above-augmented matrix as a result. As you can see, the matrix is now in echelon form (triangular form).

L2 + 1/2L3 -> L2

L1 – L3 -> L1

On performing the above operation, we get the following matrix:

matrix gaussian elimination python

We can still add more zeroes to this matrix, so let us continue.

2L2 -> L2

-L3 -> L3

When we perform the above operations, we get the following matrix:

matirx

L1 – L2 -> L1

1/2 L2 -> L1

As a result of the above row operation, we get the following result:

python gaussian elimination code

As we cannot reduce the matrix any further, we will stop the algorithm. The solution of the above equations are:

  • x = 2
  • y = 3
  • z = -1

Implementation in Python:

import numpy as np
import sys

n = int(input('Enter number of unknowns: '))
a = np.zeros((n,n+1))
x = np.zeros(n)
print('Enter Augmented Matrix Coefficients:')
for i in range(n):
    for j in range(n+1):
        a[i][j] = float(input( 'a['+str(i)+']['+ str(j)+']='))
for i in range(n):
    if a[i][i] == 0.0:
        sys.exit('Divide by zero detected!')
        
    for j in range(i+1, n):
        ratio = a[j][i]/a[i][i]
        
        for k in range(n+1):
            a[j][k] = a[j][k] - ratio * a[i][k]

x[n-1] = a[n-1][n]/a[n-1][n-1]

for i in range(n-2,-1,-1):
    x[i] = a[i][n]
    
    for j in range(i+1,n):
        x[i] = x[i] - a[i][j]*x[j]
    
    x[i] = x[i]/a[i][i]

print('\nThe solution is: ')
for i in range(n):
    print('X%d = %0.2f' %(i,x[i]), end = '\t')

Output & Explanation:

Implementation in Python

So, this will be the output of the above code. Let me now explain this code step by step.

  • First, we imported the necessary libraries we will use in our program.
  • We then asked the user for the number of unknown variables that we store in the variable ‘n’.
  • After that, we created a numpy array ‘a’ of size nx(n+1) and initialized it to zero. We will be storing our augmented matrix in this array.
  • Another array ‘x’ of size n is also created and initialized to zero. We will use this array to store the solution vector.
  • We then used a loop to get the input of the augmented matrix.
  • After that, we applied the Gaussian elimination method.
  • If any of the coefficients is 0, an error is raised as division by zero is not possible.
  • After that, we apply the back substitution method to obtain the desired output.

Must Read

Conclusion:

With this, we come to an end with this article. I hope you learned about Gaussian elimination and its implementation in Python.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ali
Ali
2 years ago

The procedure does not respond to the code. The procedure shows forward and backward elimination while the code provides forward elimination and back substitution, not elimination. Thanks anyway.