Gauss Elimination Method
In this method, we first change the coefficient matrix into an upper triangular matrix using elimination. Then we find out the unknowns via back substitution.
Interactive calculator
This section demonstrates an interactive calculator for solving a linear system using the Gauss-elimination method with partial pivoting.
The above system can be represented in matrix notation as:
We can clearly see that the coefficient matrix has been turned into an upper triangular matrix. Now we can proceed with back substitution as follow:
From
From
From
Algorithm
Following represents the algorithm for the basic Gauss-Elimination method without pivoting. This section is represented in mathematical notations.
- Start
- Declare and read the size of the system
- Declare variable for system, as a 2D array of size . Such that represents the element at row and column.
- For each elements of input a value from user
- Start from
- Take as pivot row and then eliminate from the rows for by applying modifying
- Increase by . Goto step 6 if
- Find for
- Stop
Pseudo Code
The pseudo-code for the basic Gauss-Elimination method can be written as:
Read N
Declare S = Array(N, N+1)
-- Read the input
for i=1:N
for j=1:N+1
Read S[i][j]
endfor
endfor
-- Perform elimination
for k=1:N-1
for i=k+1:N
factor = S[i][k]/S[k][k]
for j=k+1:N+1
S[i][j] = S[i][j] - factor * S[k][j]
end
end
end
-- Perform back substitution
for k=N:1
sum = 0
for i=k+1:N
sum += S[k][i]
end
Print x[k] = (S[k][N+1]-sum)/S[k][k]
end
Programs
- C
- Python
/*
* Basic Gauss Elimination Method in C
*/
#include <stdio.h>
#define MAX_SYSTEM_SIZE 20
int main()
{
// The size of linear system
int N = 3;
// Preallocate enough memory for the system
double S[MS][MS+1];
double X[MS];
// Get the size of the system
printf("Enter the size of linear system: ");
scanf("%d", &N);
// Limit the max system size
if (N >= MS)
{
printf("Please enter size below %d", MS);
return -1;
}
// Ask for the system
printf("\nEnter the matrix in augmented matrix form:\n");
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N + 1; j++)
{
printf("Enter S[%d][%d] = ", i + 1, j + 1);
scanf("%lf", &S[i][j]);
}
}
// Perform Elimination
for (int k = 0; k < N - 1; k++)
{
for (int i = k + 1; i < N; i++)
{
// Precalculate factor as S[i][k] will
// change in the following loop
double factor = S[i][k] / S[k][k];
for (int j = k; j < N + 1; j++)
{
S[i][j] = S[i][j] - factor * S[k][j];
}
}
}
// Perform Back substitution
for (int k = N - 1; k >= 0; k--)
{
double sum = 0;
for (int i = k + 1; i < N; i++)
{
sum += S[k][i] * X[i];
}
X[k] = (S[k][N] - sum) / S[k][k];
}
printf("\nSolution for the system is: \n");
for (int i = 0; i < N; i++)
{
printf("x%d=%lf\n", i + 1, X[i]);
}
return 0;
}
#
# Basic Gauss Elimination Method
#
N = int(input("Enter the size of the system "))
S = [[0]*(N+1) for _ in range(N)]
X = [0]*N
# Ask for input
print("Enter the system in augmented matrix form:")
for i in range(N):
for j in range(N+1):
S[i][j] = float(input(f"Enter S[{i+1}][{j+1}] = "))
# Perform elimination
for k in range(N-1):
for i in range(k+1, N):
# Precalculate factor as S[i][k] will change in the following loop
factor = S[i][k]/S[k][k]
for j in range(k, N+1):
S[i][j] = S[i][j] - factor*S[k][j]
# Perform back substitution
for k in reversed(range(0, N)):
sum = 0
for i in range(k+1, N):
sum += S[k][i]*X[i]
X[k] = (S[k][N]-sum)/S[k][k]
# Print the solution
for i in range(N):
print(f"x{i+1} = {X[i]}")
Complexity
The time complexity of the basic Gauss Elimination method is where is the size of the system. The elimination step requires 3 nested loops which gives the time complexity to be . Similarly, the space complexity is which comes due to the matrix used to represent the system.
Advantages
- The Gauss Elimination method is one of the fastest direct methods of solving linear systems.
Disadvantages
- The basic Gauss Elimination method can't deal with zero pivot elements.