×

Gaussian elimination in python

Linear and polynomial equations are used in almost all fields of numerical simulation. However, its most common use in engineering is in the area of linear system analysis. The broader domain of linear systems includes structures, flexible materials, heat flow, electromagnetism, electrical circuits, and many other phenomena.

Mathematical equations of the form Ax = b, where x is the input matrix and b is the system response vector, are produced when analyzing integral equations. Independent of the input vector, A, also known as the matrix of coefficients, reflects the inherent characteristics of the system. The quadratic equation system we wish to assess will still have the identical coefficients matrix A but will have a different reaction vector b if the input is altered.

Systems of Linear Equations Solving Techniques

Along with iterative processes, there are also supposedly easy techniques, which we won't cover in this article. They work together to simplify the original equation into a system with attributes similar to the original system but easier to solve.

To carry out this conversion, we may employ the following three main operations:

  • When two lines of matrix A are switched, the numerical value of the determinant change direction;
  • The scalar using which the row of the matrix A is multiplied is applied towards the numerical value of the determinant of A;
  • If we add a row from A to another row that has been scaled by a scalar, the determinant of A remains unchanged;

Gauss elimination method

To solve a linear system of equations, use linear algebra. In general, a coefficients matrix goes through several steps. The following actions are involved:

  1. It can switch two rows.
  2. Another name for Gaussian elimination is row reduction. A row is scaled by being multiplied by a scaler.
  3. Extending the matrix's rows by another row

1. Forward elimination: To row echelon form reduction. It may be used to determine if there are infinitely many solutions, a single solution, or neither.

2. Back substitution: To lower row echelon structure after further reduction.

Gauss elimination algorithm in python

Let us conder three equations to proceed with the program.

Write the following table the for the given equations, which consists of augmented matric[A],[B], and the answer

-x1+2x2+4x3=3
6x1-5x2+x3=6
x1-9x2+x3=9
Augmented Matrix[A][B]
-1        2         4  
6        -5        1  
1        -9        1
3
6
9
x
  0.09 -0.86   1.20

Program

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 u in range(n):
    for v in range(n+1):
        a[u][v] = float(input( 'a['+str(u)+']['+ str(v)+']='))
for u in range(n):
    if a[u][u] == 0.0:
        sys.exit('Divide by zero detected!')
         
    for v in range(u+1, n):
        ratio = a[v][u]/a[u][u]
        for w in range(n+1):
            a[v][w] = a[v][w] - ratio * a[u][w]
x[n-1] = a[n-1][n]/a[n-1][n-1]
 
for u in range(n-2,-1,-1):
    x[u] = a[u][n]
     
    for v in range(u+1,n):
        x[u] = x[u] - a[u][v]*x[v]
     
    x[u] = x[u]/a[u][u]
 
print('\nThe solution is: ')
for u in range(n):
    print('X%d = %0.2f' %(u,x[u]), end = '\t')

Output

Enter number of unknowns: 3
Enter Augmented Matrix Coefficients:
a[0][0]=-1
a[0][1]=2
a[0][2]=4
a[0][3]=3
a[1][0]=6
a[1][1]=-5
a[1][2]=1
a[1][3]=6
a[2][0]=1
a[2][1]=-9
a[2][2]=1
a[2][3]=9


The solution is: 
X0 = 0.09	X1 = -0.86	X2 = 1.20	

Related Topics

SKLearn Linear Module

The SK learn linear module is one such module that helps to study the relationship between the independent and dependent variables.The linear module can be implemented by using the best...

3 minutes read.

Python Logistic Regression with Sklearn & Scikit

In this article, we'll walk through a tutorial for utilising the Python Sklearn (formerly known as Scikit Learn) package to implement logistic regression. To assist you in remembering the concept,...

18 minutes read.

Python program to add two number

Python program to add two number This program will add the two numbers and display their sum on the screen. Example: Input: Number1 = 20        Number2 = 30   Output: Sum =...

2 minutes read.

Dictionary to JSON Python

In python JSON (JavascriptObject Notation). In the programming language, the text file is made using the script file.We can use many built-in packages which arenamedJSON.Before using the packages, we have...

3 minutes read.

Best resources to learn Numpy and Pandas in python

In this tutorial, we will discuss the different resources where we can learn about NumPy and Pandas libraries of Python in a more efficient way. NumPy NumPy, a Python library used for...

4 minutes read.

How to Install Tweepy in Python

In this article, we will learn or understand how to install Tweepy in Python and what Tweepy is. Firstly, let’s understand What Tweepy is. As we all know, one of the...

3 minutes read.

How to build an Auto Clicker using Python?

What is an Auto Clicker? An Auto Clicker is software that is used for automating the click of a mouse on any particular element on the computer screen and controlling the...

8 minutes read.

Python Comment Block

In this tutorial, we will see what comment blocks mean in Python. Further, we will see the commenting methods supported in Python. We will understand the topics deeply with the...

6 minutes read.

Is Python Case Sensitive

Case sensitivity is the mode of dealing with the written alphabet. The cases of the alphabet are examined and based on these words are being treated. The uppercase and lowercase...

3 minutes read.

Python Letter to Number

Python Letter to Number In this tutorial, we will convert the given letters into numbers using a Python. We will convert the given letter into the letter value as defined in...

3 minutes read.

Python program to check if two strings are anagram or not

Python program to check if two strings are anagram or not Problem: This is a python program that takes two strings and checks if given strings are anagram or not. Examples: Input: string1...

1 minute read.

How to Concatenate Two Strings in Python

How to Concatenate Two Strings in Python Like the other data types, operations on strings are quite useful when we deal with some real-life applications. Here we will talk about a simple...

4 minutes read.

Python format() function

Python format() function The format() function in Python formats a specified value into the given format. A ‘TypeErrorexception’ is raised if the method search reaches the object and the format_spec is non-empty, or if either the format_spec or the...

1 minute read.

Python Combination

Python Combination Permutations and combinations are the important part of our mathematical tools. We use them often in our Python programs. In this tutorial, we will learn about combinations in Python...

6 minutes read.

Python Encapsulation

What is meant by Encapsulation? The process of restricting access to the methods and variables so that accidental modification of data can be prevented is known as Encapsulation. The motive of Encapsulation:...

3 minutes read.

Python Dictionary get() method

Python Dictionary get() method The dictionary.get() method returns the value of the item with the specified key. Syntax dictionary.get(keyname, value) Parameter keyname- This parameter represents the key to be searched in the dictionary. value- The parameter...

1 minute read.

Python List index() method

Python List index() method The list.index () method in Python returns the position at the first occurrence of the specified value. Syntax list.index(x[, start[, end]]) Parameter element – This parameter represents the element whose lowest index will be returned. start (Optional)...

2 minutes read.

How to Open a File in Python with a Path?

File in Python File handling is an important operation; it allows the programmer to access the data in the files, such as text, binary values and excel sheets. The CSV files...

7 minutes read.

Python exe() function

Python exe() function The exec() function in Python executes the specified Python code and accepts large blocks of code. It supports dynamic execution of Python code where the object must be either a string...

1 minute read.

Python Parse Text File

We will learn different ways of read text records in Python. TL;DR The accompanying tells the best way to read all texts from the readme.txt document into a string: with open('readme.txt') as f: lines...

5 minutes read.