×

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

Python Word Tokenizer

Python Overview Python is an Object-Oriented high-level language. Python is designed to be highly beginner-friendly. Python has an English-like syntax, which is very easy to read. Python is an interpreted language...

4 minutes read.

Recursive Multiplication Python

Recursive Multiplication Python In this tutorial, we will learn how to write a Python program to get the multiplication of two numbers using the recursive approach. In the recursive multiplication approach, we...

2 minutes read.

Python elif

Python elif The elif statement is used to check multiple conditions and execute the specific block of statements depending upon the true condition among them. Syntax if expression1: statement elif expression2: statement elif expression3: statement else: statement The elif statement can be optional...

3 minutes read.

Continue and Pass Statements in Python

Difference between continue and pass statements in Python The tasks can be repeated and automated efficiently using loops in Python. Sometimes we have to exit from the entire loop completely, we...

2 minutes read.

Copying a file from one folder to Another with Python

In this article, we shall discuss copying a file from one folder to another using python functions. A file is a collection of data or information stored on a computer....

3 minutes read.

Python Set issubset() method

Python Set issubset() method The set.issubset() method in Python returns True if all items in the set exists in the specified set, otherwise it returns False. Syntax set.issubset(set1) Parameter set- This parameter represents the set to check whether...

2 minutes read.

List Subtract in Python

The List is one of the most unique Data Structures in Python. It is generally used to store multiple values in just one single variable. It is one of the...

4 minutes read.

Python K-Means Clustering

K-Means Clustering is a basic yet incredible calculation in information scienceThere are a plenty of true utilizations of K-Means clustering (a couple of which we will cover here)This far reaching...

25 minutes read.

Python tuple() function

Python tuple() function The tuple() function in Python creates a tuple object. Syntax tuple([iterable]) Parameter Iterable: This parameter represents a sequence, collection or an iterator object. Return This function returns a tuple object. Example 1 # Python Program explaining #...

1 minute read.

Python Control Statements

Control statements are under the roof topic of loops and loops in python are defined as iteratively and repeatedly working on source code. Loop control statements are defined as they...

3 minutes read.

Python Marshmallow

Python:  Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

How to read data from com port in python

Comport, the I/O interface is known as a COM port that allows the connection for a serial device to a computer. COM ports are sometimes referred to as serial ports....

3 minutes read.

How to create a dictionary in Python?

How to create a dictionary in python Dictionary is a data structure in Python that represents our data in the form of keys and values. Each value in a dictionary can be...

5 minutes read.

Python CGI Programming

The Concept of CGI CGI is an abbreviation for Common Gateway Interface. It is not a type of language but a set of rules (specification) that establishes a dynamic interaction between...

9 minutes read.

Curdir Python

Python Programming Language Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Python Linear regression

In this tutorial, we will understand the meaning, usage, and types of Linear Regression. Further, we will comprehend the terms cost function and Optimization. Linear Regression is the widely used Machine...

3 minutes read.

Python MongoDB Tutorial

An Introduction to MongoDB MongoDB is a document-oriented database application. It is an Open-source and platform-independent program. MongoDB is similar to few NoSQL databases that store the data in the documents...

17 minutes read.

How to Declare a Variable in Python?

The concept of constants and variables is something that we are studying right from our primary classes. We know that constants are the fixed values whereas variables are those whose...

4 minutes read.

Python TypeError

What is TypeError in python? TypeError is one of the exceptions in the python programming language. This exception occurs when an operation is performed on an unsupported object type or can...

6 minutes read.

Working with CSV files in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

7 minutes read.