# 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``````

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=-1
a=2
a=4
a=3
a=6
a=-5
a=1
a=6
a=1
a=-9
a=1
a=9

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