Gauss-Jordan Method
In the Gauss-Jordan method, we reduce the coefficient matrix into an identity matrix. We eliminate variables from all rows except the pivot row and also normalize the pivot row. Once the method is complete, we will be left with an identity matrix and the solution vector.
This page presents an online interactive calculator for the Gauss-Jordan method with a step-by-step explanation, algorithm, flowchart, pseudo-code and programs in C and Python.
Interactive Calculator
The following is an interactive calculator for the Gauss-Jordan method with partial pivoting:
The above system can be represented in matrix notation as:
Algorithm
The following represents the algorithm for the Gauss-Jordan method without pivoting:
- Start
- Declare and read the size of the system
- Declare variable for system in augmented matrix notation, as a 2D array of size . Such that represents the element at row and column. The last column represents the constant vector.
- For each elements of input a value from user
- Start from
- Take as pivot row and then then eliminate from the rows for and by applying modifying .
- Increase by . Goto step 6 if
- Normalize by applying for
- Print solution
- Stop
Pseudo Code
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
and normalization
-- Perform elimination
for k=1:N
for i=1:N
factor = S[i][k]/S[k][k]
if i!=k:
for j=1:N+1
S[i][j] = S[i][j] - factor * S[k][j]
end
end
end
end
-- Perform normalization and print solution
for k=1:N-1
print "xk=", S[k][N+1]/S[k][k]
end
Program
- C
- Python
/*
* Gauss Jordan 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[MAX_SYSTEM_SIZE][MAX_SYSTEM_SIZE+1];
// Get the size of the system
printf("Enter the size of linear system: ");
scanf("%d", &N);
// Limit the max system size
if (N >= MAX_SYSTEM_SIZE)
{
printf("Please enter size below %d", MAX_SYSTEM_SIZE);
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; k++)
{
for (int i = 0; i < N; i++)
{
if(i!=k){
// Precalculate factor as S[i][k] will
// change in the following loop
double factor = S[i][k] / S[k][k];
for (int j = 0; j < N + 1; j++)
{
S[i][j] = S[i][j] - factor * S[k][j];
}
}
}
}
// Print solution by performing normalization
printf("\nSolution for the system is: \n");
for (int i = 0; i < N; i++)
{
printf("x%d = %lf\n", i + 1, S[i][N]/S[i][i]);
}
return 0;
}
#
# Gauss Jordan Method in Python
#
N = int(input("Enter the size of the system "))
S = [[0]*(N+1) for _ in range(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):
for i in range(N):
if i!=k:
# Precalculate factor as S[i][k] will change in the following loop
factor = S[i][k]/S[k][k]
for j in range(N+1):
S[i][j] = S[i][j] - factor*S[k][j]
# Print the solution by performing normalization
print("Solution for the system is:");
for i in range(N):
print(f"x{i+1} = {S[i][N]/S[i][i]}")
Complexity
The complexity of the Gauss-Jordan method is . This comes from the three nested loops in the forward elimination step. The space complexity is which comes from the size of the system.
Advantages
- Simpler implementation.
- It can be used for calculating the inverse of a square matrix.
Disadvantages
- Requires 50% more operations than the Gauss Elimination method.