The Wayback Machine - https://web.archive.org/web/20250505173901/https://www.geeksforgeeks.org/gaussian-elimination/
Open In App

Gaussian Elimination to Solve Linear Equations

Last Updated : 21 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Gaussian Elimination Method is a widely used technique for solving systems of linear equations. A system of linear equations involves multiple equations with unknown variables. The goal of solving such a system is to find the values of these unknowns that satisfy all the given equations simultaneously.

Gaussian Elimination is a powerful method used in real-life applications, such as traffic flow analysis. It helps solve systems of linear equations that represent traffic movement at intersections, optimizing the flow of traffic and reducing congestion.

In LU decomposition, Gaussian elimination is used to convert the matrix A into an upper triangular matrix (U).

Categories of Linear Equation Systems:

  • Consistent Independent System: Has exactly one solution.
  • Consistent Dependent System: Has infinite solutions.
  • Inconsistent System: Has no solution.

Gaussian Elimination Method

Gaussian elimination is a row reduction algorithm for solving linear systems. It involves a series of operations on the augmented matrix (which includes both coefficients and constants) to simplify it into a row echelon form or reduced row echelon form. This method can also help in determining the rank, determinant, and inverse of matrices.

Gaussian elimination is a method for solving systems of equations in matrix form.

Let’s for example we have system of linear equations:

[Tex]a_1x+b_1y+c_1z=d_1[/Tex]
[Tex]a_2x+b_2y+c_2z=d_2[/Tex]
[Tex]a_3x+b_3y+c_3z=d_3[/Tex]
Matrix form of this system of linear eqations: [Tex]\begin{bmatrix}a_1 & b_1 & c_1 & |&d_1 \\a_2 & b_2 & c_2 & |&d_2 \\a_3 & b_3 & c_3 &| &d_3\end{bmatrix}[/Tex]

Goal:

Turn matrix into row-echelon form like, [Tex]\begin{bmatrix}0 & a & b & |&d \\0 & 1 & c & |&e \\0 & 0 & 1 &| &f\end{bmatrix}[/Tex] .

Once in this form, we can say that 𝑧 = 𝑓 and use back substitution to solve for y and x.

Elementary Row Operations

  1. Interchanging Rows: Swap two rows.
  2. Multiplying a Row by a Scalar: Multiply all elements of a row by a non-zero number.
  3. Adding a Scalar Multiple of One Row to Another: Add or subtract a multiple of one row to/from another.

These operations simplify solving systems of linear equations without changing the solution set.

matrix-660

Follow these steps:
1) Get a 1 in the first column, first row
2) Use the 1 to get 0’s in the remainder of the first column
3) Get a 1 in the second column, second row
4) Use the 1 to get 0’s in the remainder of the second column
5) Get a 1 in the third column, third row.

The resulting matrix is in row echelon form. A matrix is in reduced row-echelon form if all leading coefficients are 1, and the columns containing these leading coefficients have zeros elsewhere. This final form is unique, regardless of the sequence of row operations used. An example below illustrates this concept.

Example

To solve the system of equations:

  1. x+y=3
  2. 3x−2y=4

Step 1: Write the Coefficient Matrix

The coefficients of the unknowns (x and y) are written as a matrix:

[Tex]\begin{bmatrix} 1 & 1 \\ 3 & -2 \end{bmatrix}[/Tex]

Step 2: Write the Augmented Matrix

Include the constants from the right-hand sides of the equations as an additional column:

[Tex]\begin{bmatrix} 1 & 1 & |&3 \\ 3 & -2 & |&4 \end{bmatrix}[/Tex]

Step 3: Perform Row Operations

The goal is to simplify the augmented matrix to solve for x and y.

Subtract [Tex]3 \times r_1[/Tex] from [Tex]r_2[/Tex] (to eliminate the x-term in the second row): [Tex]r_2→r_2−3r_1[/Tex]

Calculation:[Tex]\begin{bmatrix} 1 & 1 & 3 \\ 3 & -2 & 4 \end{bmatrix} \to \begin{bmatrix} 1 & 1 & 3 \\ 0 & -5 & -5 \end{bmatrix}[/Tex]

Simplify the second row:
Divide [Tex]r_2[/Tex]​ by −5 to solve for y: [Tex] \to \frac{r_2}{-5}[/Tex]

Result: [Tex]\begin{bmatrix} 1 & 1 & 3 \\ 0 & 1 & 1 \end{bmatrix}[/Tex]

Step 4: Back-Substitute

Use the second row (y=1) to substitute into the first row (x + y = 3): x + 1 = 3  âŸđ  x= 2

Solution

The solution to the system is: (x, y )=(2, 1)

Algorithm for Gaussian Elimination

  1. Partial Pivoting: Select the pivot element with the largest absolute value from the column. Swap rows to move this element into the pivot position. This improves numerical stability by reducing rounding errors.
  2. Elimination: For each row below the pivot, subtract a multiple of the pivot row to eliminate the entries below the pivot.
  3. Repeat for each column and row.

Back Substitution: After obtaining the row echelon form, back-substitute to find the values of the unknowns.

Below is the implementation of the above algorithm.  

C++
// C++ program to demonstrate working of Gaussian Elimination
// method
#include<bits/stdc++.h>
using namespace std;

#define N 3        // Number of unknowns

// function to reduce matrix to r.e.f.  Returns a value to 
// indicate whether matrix is singular or not
int forwardElim(double mat[N][N+1]);

// function to calculate the values of the unknowns
void backSub(double mat[N][N+1]);

// function to get matrix content
void gaussianElimination(double mat[N][N+1])
{
    /* reduction into r.e.f. */
    int singular_flag = forwardElim(mat);

    /* if matrix is singular */
    if (singular_flag != -1)
    {
        printf("Singular Matrix.\n");

        /* if the RHS of equation corresponding to
           zero row  is 0, * system has infinitely
           many solutions, else inconsistent*/
        if (mat[singular_flag][N])
            printf("Inconsistent System.");
        else
            printf("May have infinitely many "
                   "solutions.");

        return;
    }

    /* get solution to system and print it using
       backward substitution */
    backSub(mat);
}

// function for elementary operation of swapping two rows
void swap_row(double mat[N][N+1], int i, int j)
{
    //printf("Swapped rows %d and %d\n", i, j);

    for (int k=0; k<=N; k++)
    {
        double temp = mat[i][k];
        mat[i][k] = mat[j][k];
        mat[j][k] = temp;
    }
}

// function to print matrix content at any stage
void print(double mat[N][N+1])
{
    for (int i=0; i<N; i++, printf("\n"))
        for (int j=0; j<=N; j++)
            printf("%lf ", mat[i][j]);

    printf("\n");
}

// function to reduce matrix to r.e.f.
int forwardElim(double mat[N][N+1])
{
    for (int k=0; k<N; k++)
    {
        // Initialize maximum value and index for pivot
        int i_max = k;
        int v_max = mat[i_max][k];

        /* find greater amplitude for pivot if any */
        for (int i = k+1; i < N; i++)
            if (abs(mat[i][k]) > v_max)
                v_max = mat[i][k], i_max = i;

        /* if a principal diagonal element  is zero,
         * it denotes that matrix is singular, and
         * will lead to a division-by-zero later. */
        if (!mat[k][i_max])
            return k; // Matrix is singular

        /* Swap the greatest value row with current row */
        if (i_max != k)
            swap_row(mat, k, i_max);


        for (int i=k+1; i<N; i++)
        {
            /* factor f to set current row kth element to 0,
             * and subsequently remaining kth column to 0 */
            double f = mat[i][k]/mat[k][k];

            /* subtract fth multiple of corresponding kth
               row element*/
            for (int j=k+1; j<=N; j++)
                mat[i][j] -= mat[k][j]*f;

            /* filling lower triangular matrix with zeros*/
            mat[i][k] = 0;
        }

        //print(mat);        //for matrix state
    }
    //print(mat);            //for matrix state
    return -1;
}

// function to calculate the values of the unknowns
void backSub(double mat[N][N+1])
{
    double x[N];  // An array to store solution

    /* Start calculating from last equation up to the
       first */
    for (int i = N-1; i >= 0; i--)
    {
        /* start with the RHS of the equation */
        x[i] = mat[i][N];

        /* Initialize j to i+1 since matrix is upper
           triangular*/
        for (int j=i+1; j<N; j++)
        {
            /* subtract all the lhs values
             * except the coefficient of the variable
             * whose value is being calculated */
            x[i] -= mat[i][j]*x[j];
        }

        /* divide the RHS by the coefficient of the
           unknown being calculated */
        x[i] = x[i]/mat[i][i];
    }

    printf("\nSolution for the system:\n");
    for (int i=0; i<N; i++)
        printf("%lf\n", x[i]);
}

// Driver program
int main()
{
    /* input matrix */
    double mat[N][N+1] = {{3.0, 2.0,-4.0, 3.0},
                          {2.0, 3.0, 3.0, 15.0},
                          {5.0, -3, 1.0, 14.0}
                         };

    gaussianElimination(mat);

    return 0;
}
Java
// Java program to demonstrate working of Gaussian Elimination
// method
import java.io.*;
class GFG
{

  public static int N = 3; // Number of unknowns

  // function to get matrix content
  static void gaussianElimination(double mat[][])
  {

    /* reduction into r.e.f. */
    int singular_flag = forwardElim(mat);

    /* if matrix is singular */
    if (singular_flag != -1) 
    {
      System.out.println("Singular Matrix.");

      /* if the RHS of equation corresponding to
               zero row  is 0, * system has infinitely
               many solutions, else inconsistent*/
      if (mat[singular_flag][N] != 0)
        System.out.print("Inconsistent System.");
      else
        System.out.print(
        "May have infinitely many solutions.");

      return;
    }

    /* get solution to system and print it using
           backward substitution */
    backSub(mat);
  }

  // function for elementary operation of swapping two
  // rows
  static void swap_row(double mat[][], int i, int j)
  {
    // printf("Swapped rows %d and %d\n", i, j);

    for (int k = 0; k <= N; k++)
    {
      double temp = mat[i][k];
      mat[i][k] = mat[j][k];
      mat[j][k] = temp;
    }
  }

  // function to print matrix content at any stage
  static void print(double mat[][])
  {
    for (int i = 0; i < N; i++, System.out.println())
      for (int j = 0; j <= N; j++)
        System.out.print(mat[i][j]);
    System.out.println();
  }

  // function to reduce matrix to r.e.f.
  static int forwardElim(double mat[][])
  {
    for (int k = 0; k < N; k++) 
    {

      // Initialize maximum value and index for pivot
      int i_max = k;
      int v_max = (int)mat[i_max][k];

      /* find greater amplitude for pivot if any */
      for (int i = k + 1; i < N; i++)
        if (Math.abs(mat[i][k]) > v_max) 
        {
          v_max = (int)mat[i][k];
          i_max = i;
        }

      /* if a principal diagonal element  is zero,
             * it denotes that matrix is singular, and
             * will lead to a division-by-zero later. */
      if (mat[k][i_max] == 0)
        return k; // Matrix is singular

      /* Swap the greatest value row with current row
             */
      if (i_max != k)
        swap_row(mat, k, i_max);

      for (int i = k + 1; i < N; i++)
      {

        /* factor f to set current row kth element
                 * to 0, and subsequently remaining kth
                 * column to 0 */
        double f = mat[i][k] / mat[k][k];

        /* subtract fth multiple of corresponding
                   kth row element*/
        for (int j = k + 1; j <= N; j++)
          mat[i][j] -= mat[k][j] * f;

        /* filling lower triangular matrix with
                 * zeros*/
        mat[i][k] = 0;
      }

      // print(mat);        //for matrix state
    }

    // print(mat);            //for matrix state
    return -1;
  }

  // function to calculate the values of the unknowns
  static void backSub(double mat[][])
  {
    double x[]
      = new double[N]; // An array to store solution

    /* Start calculating from last equation up to the
           first */
    for (int i = N - 1; i >= 0; i--)
    {

      /* start with the RHS of the equation */
      x[i] = mat[i][N];

      /* Initialize j to i+1 since matrix is upper
               triangular*/
      for (int j = i + 1; j < N; j++) 
      {

        /* subtract all the lhs values
                 * except the coefficient of the variable
                 * whose value is being calculated */
        x[i] -= mat[i][j] * x[j];
      }

      /* divide the RHS by the coefficient of the
               unknown being calculated */
      x[i] = x[i] / mat[i][i];
    }

    System.out.println();
    System.out.println("Solution for the system:");
    for (int i = 0; i < N; i++)
    {
      System.out.format("%.6f", x[i]);
      System.out.println();
    }
  }

  // Driver program
  public static void main(String[] args)
  {

    /* input matrix */
    double mat[][] = { { 3.0, 2.0, -4.0, 3.0 },
                      { 2.0, 3.0, 3.0, 15.0 },
                      { 5.0, -3, 1.0, 14.0 } };

    gaussianElimination(mat);
  }
}

// This code is contributed by Dharanendra L V.
Python
# Python3 program to demonstrate working of
# Gaussian Elimination method
N = 3

# function to get matrix content
def gaussianElimination(mat):

    # reduction into r.e.f.
    singular_flag = forwardElim(mat)

    # if matrix is singular
    if (singular_flag != -1):

        print("Singular Matrix.")

        # if the RHS of equation corresponding to
        #   zero row  is 0, * system has infinitely
        #   many solutions, else inconsistent*/
        if (mat[singular_flag][N]):
            print("Inconsistent System.")
        else:
            print("May have infinitely many solutions.")

        return

    # get solution to system and print it using
    #   backward substitution
    backSub(mat)

# function for elementary operation of swapping two rows
def swap_row(mat, i, j):

    for k in range(N + 1):

        temp = mat[i][k]
        mat[i][k] = mat[j][k]
        mat[j][k] = temp

# function to reduce matrix to r.e.f.
def forwardElim(mat):
    for k in range(N):
      
        # Initialize maximum value and index for pivot
        i_max = k
        v_max = mat[i_max][k]

        # find greater amplitude for pivot if any
        for i in range(k + 1, N):
            if (abs(mat[i][k]) > v_max):
                v_max = mat[i][k]
                i_max = i

        # if a principal diagonal element  is zero,
        # it denotes that matrix is singular, and
        # will lead to a division-by-zero later.
        if not mat[k][i_max]:
            return k    # Matrix is singular

        # Swap the greatest value row with current row
        if (i_max != k):
            swap_row(mat, k, i_max)

        for i in range(k + 1, N):

            # factor f to set current row kth element to 0,
            # and subsequently remaining kth column to 0 */
            f = mat[i][k]/mat[k][k]

            # subtract fth multiple of corresponding kth
            # row element*/
            for j in range(k + 1, N + 1):
                mat[i][j] -= mat[k][j]*f

            # filling lower triangular matrix with zeros*/
            mat[i][k] = 0

        # print(mat);        //for matrix state

    # print(mat);            //for matrix state
    return -1

# function to calculate the values of the unknowns
def backSub(mat):

    x = [None for _ in range(N)]    # An array to store solution

    # Start calculating from last equation up to the
    #  first */
    for i in range(N-1, -1, -1):

        # start with the RHS of the equation */
        x[i] = mat[i][N]

        # Initialize j to i+1 since matrix is upper
        #  triangular*/
        for j in range(i + 1, N):
          
            # subtract all the lhs values
            # except the coefficient of the variable
            # whose value is being calculated */
            x[i] -= mat[i][j]*x[j]

        # divide the RHS by the coefficient of the
        #  unknown being calculated
        x[i] = (x[i]/mat[i][i])

    print("\nSolution for the system:")
    for i in range(N):
        print("{:.8f}".format(x[i]))

# Driver program

# input matrix
mat = [[3.0, 2.0, -4.0, 3.0], [2.0, 3.0, 3.0, 15.0], [5.0, -3, 1.0, 14.0]]
gaussianElimination(mat)

# This code is contributed by phasing17
C#
// C# program to demonstrate working 
// of Gaussian Elimination method
using System;

class GFG{
    
// Number of unknowns
public static int N = 3; 

// Function to get matrix content
static void gaussianElimination(double [,]mat)
{

    /* reduction into r.e.f. */
    int singular_flag = forwardElim(mat);
    
    /* if matrix is singular */
    if (singular_flag != -1) 
    {
        Console.WriteLine("Singular Matrix.");
        
        /* if the RHS of equation corresponding to
           zero row  is 0, * system has infinitely
           many solutions, else inconsistent*/
        if (mat[singular_flag,N] != 0)
            Console.Write("Inconsistent System.");
        else
            Console.Write("May have infinitely " + 
                          "many solutions.");
        
        return;
    }
    
    /* get solution to system and print it using
    backward substitution */
    backSub(mat);
}

// Function for elementary operation of swapping two
// rows
static void swap_row(double [,]mat, int i, int j)
{
    
    // printf("Swapped rows %d and %d\n", i, j);
    
    for(int k = 0; k <= N; k++)
    {
        double temp = mat[i, k];
        mat[i, k] = mat[j, k];
        mat[j, k] = temp;
    }
}
    
// Function to print matrix content at any stage
static void print(double [,]mat)
{
    for(int i = 0; i < N; i++, Console.WriteLine())
        for(int j = 0; j <= N; j++)
            Console.Write(mat[i, j]);
            
    Console.WriteLine();
}

// Function to reduce matrix to r.e.f.
static int forwardElim(double [,]mat)
{
    for(int k = 0; k < N; k++) 
    {
        
        // Initialize maximum value and index for pivot
        int i_max = k;
        int v_max = (int)mat[i_max, k];
        
        /* find greater amplitude for pivot if any */
        for(int i = k + 1; i < N; i++)
        {
            if (Math.Abs(mat[i, k]) > v_max) 
            {
                v_max = (int)mat[i, k];
                i_max = i;
            }
        
            /* If a principal diagonal element  is zero,
            *  it denotes that matrix is singular, and
            *  will lead to a division-by-zero later. */
            if (mat[k, i_max] == 0)
                return k; // Matrix is singular
            
            /* Swap the greatest value row with 
               current row
            */
            if (i_max != k)
                swap_row(mat, k, i_max);
            
            for(int a = k + 1; a < N; a++)
            {
            
                /* factor f to set current row kth element
                *  to 0, and subsequently remaining kth
                *  column to 0 */
                double f = mat[i, k] / mat[k, k];
                
                /* subtract fth multiple of corresponding
                   kth row element*/
                for(int j = k + 1; j <= N; j++)
                    mat[i, j] -= mat[k, j] * f;
                
                /* filling lower triangular matrix with
                *  zeros*/
                mat[i, k] = 0;
            }
        }
        // print(mat);        //for matrix state
    }
    
    // print(mat);            //for matrix state
    return -1;
}

// Function to calculate the values of the unknowns
static void backSub(double [,]mat)
{
    
    // An array to store solution
    double []x = new double[N]; 
    
    /* Start calculating from last equation up to the
       first */
    for(int i = N - 1; i >= 0; i--)
    {
    
        /* start with the RHS of the equation */
        x[i] = mat[i,N];
        
        /* Initialize j to i+1 since matrix is upper
        triangular*/
        for(int j = i + 1; j < N; j++) 
        {
        
            /* subtract all the lhs values
             * except the coefficient of the variable
             * whose value is being calculated */
            x[i] -= mat[i,j] * x[j];
        }
        
        /* divide the RHS by the coefficient of the
        unknown being calculated */
        x[i] = x[i] / mat[i,i];
    }
    
    Console.WriteLine();
    Console.WriteLine("Solution for the system:");
    for(int i = 0; i < N; i++)
    {
        Console.Write("{0:F6}", x[i]);
        Console.WriteLine();
    }
}

// Driver code
public static void Main(String[] args)
{
    
    /* input matrix */
    double [,]mat = { { 3.0, 2.0, -4.0, 3.0 },
                      { 2.0, 3.0, 3.0, 15.0 },
                      { 5.0, -3, 1.0, 14.0 } };
    
    gaussianElimination(mat);
}
}

// This code is contributed by shikhasingrajput 
JavaScript
// JavaScript program to demonstrate working of Gaussian Elimination
// method

let N = 3;

// function to get matrix content
function gaussianElimination(mat)
{
    /* reduction into r.e.f. */
    let singular_flag = forwardElim(mat);

    /* if matrix is singular */
    if (singular_flag != -1)
    {
        console.log("Singular Matrix.");

        /* if the RHS of equation corresponding to
           zero row  is 0, * system has infinitely
           many solutions, else inconsistent*/
        if (mat[singular_flag][N])
            console.log("Inconsistent System.");
        else
            console.log("May have infinitely many solutions.");

        return;
    }

    /* get solution to system and print it using
       backward substitution */
    backSub(mat);
}

// function for elementary operation of swapping two rows
function swap_row(mat, i, j)
{
    //printf("Swapped rows %d and %d\n", i, j);

    for (var k=0; k<=N; k++)
    {
        let temp = mat[i][k];
        mat[i][k] = mat[j][k];
        mat[j][k] = temp;
    }
}

// function to print matrix content at any stage
function print(mat)
{
    for (var i=0; i<N; i++, console.log(""))
        for (var j=0; j<=N; j++)
            process.stdout.write("" + mat[i][j]);
    
    console.log("");
}

// function to reduce matrix to r.e.f.
function forwardElim(mat)
{
    for (var k=0; k<N; k++)
    {
        // Initialize maximum value and index for pivot
        var i_max = k;
        var v_max = mat[i_max][k];

        /* find greater amplitude for pivot if any */
        for (var i = k+1; i < N; i++)
            if (Math.abs(mat[i][k]) > v_max)
                v_max = mat[i][k], i_max = i;

        /* if a principal diagonal element  is zero,
         * it denotes that matrix is singular, and
         * will lead to a division-by-zero later. */
        if (!mat[k][i_max])
            return k; // Matrix is singular

        /* Swap the greatest value row with current row */
        if (i_max != k)
            swap_row(mat, k, i_max);


        for (var i=k+1; i<N; i++)
        {
            /* factor f to set current row kth element to 0,
             * and subsequently remaining kth column to 0 */
            let f = mat[i][k]/mat[k][k];

            /* subtract fth multiple of corresponding kth
               row element*/
            for (var j=k+1; j<=N; j++)
                mat[i][j] -= mat[k][j]*f;

            /* filling lower triangular matrix with zeros*/
            mat[i][k] = 0;
        }

        //print(mat);        //for matrix state
    }
    //print(mat);            //for matrix state
    return -1;
}

// function to calculate the values of the unknowns
function backSub(mat)
{
    let x = new Array(N);  // An array to store solution

    /* Start calculating from last equation up to the
       first */
    for (var i = N-1; i >= 0; i--)
    {
        /* start with the RHS of the equation */
        x[i] = mat[i][N];

        /* Initialize j to i+1 since matrix is upper
           triangular*/
        for (var j=i+1; j<N; j++)
        {
            /* subtract all the lhs values
             * except the coefficient of the variable
             * whose value is being calculated */
            x[i] -= mat[i][j]*x[j];
        }

        /* divide the RHS by the coefficient of the
           unknown being calculated */
        x[i] = Math.round(x[i]/mat[i][i]);
    }

    console.log("\nSolution for the system:");
    for (var i=0; i<N; i++)
        console.log(x[i].toFixed(8));
}

// Driver program

/* input matrix */
let mat =   [[3.0, 2.0,-4.0, 3.0],
             [2.0, 3.0, 3.0, 15.0],
             [5.0, -3, 1.0, 14.0]];

gaussianElimination(mat);


// This code is contributed by phasing17
PHP
<?php
// PHP program to demonstrate working 
// of Gaussian Elimination method

$N = 3; // Number of unknowns


// function to get matrix content
function gaussianElimination($mat)
{
    global $N;
    
    /* reduction into r.e.f. */
    $singular_flag = forwardElim($mat);

    /* if matrix is singular */
    if ($singular_flag != -1)
    {
        print("Singular Matrix.\n");

        /* if the RHS of equation corresponding to
        zero row is 0, * system has infinitely
        many solutions, else inconsistent*/
        if ($mat[$singular_flag][$N])
            print("Inconsistent System.");
        else
            print("May have infinitely many solutions.");

        return;
    }

    /* get solution to system and print it using
    backward substitution */
    backSub($mat);
}

// function for elementary operation
// of swapping two rows
function swap_row(&$mat, $i, $j)
{
    global $N;
    //printf("Swapped rows %d and %d\n", i, j);

    for ($k = 0; $k <= $N; $k++)
    {
        $temp = $mat[$i][$k];
        $mat[$i][$k] = $mat[$j][$k];
        $mat[$j][$k] = $temp;
    }
}

// function to print matrix content at any stage
function print1($mat)
{
    global $N;
    for ($i=0; $i<$N; $i++, print("\n"))
        for ($j=0; $j<=$N; $j++)
            print($mat[$i][$j]);

    print("\n");
}

// function to reduce matrix to r.e.f.
function forwardElim(&$mat)
{
    global $N;
    for ($k=0; $k<$N; $k++)
    {
        // Initialize maximum value and index for pivot
        $i_max = $k;
        $v_max = $mat[$i_max][$k];

        /* find greater amplitude for pivot if any */
        for ($i = $k+1; $i < $N; $i++)
            if (abs($mat[$i][$k]) > $v_max)
                {
                    $v_max = $mat[$i][$k];
                    $i_max = $i;
                }

        /* if a principal diagonal element is zero,
        * it denotes that matrix is singular, and
        * will lead to a division-by-zero later. */
        if (!$mat[$k][$i_max])
            return $k; // Matrix is singular

        /* Swap the greatest value row with current row */
        if ($i_max != $k)
            swap_row($mat, $k, $i_max);


        for ($i = $k + 1; $i < $N; $i++)
        {
            /* factor f to set current row kth element to 0,
            * and subsequently remaining kth column to 0 */
            $f = $mat[$i][$k]/$mat[$k][$k];

            /* subtract fth multiple of corresponding kth
            row element*/
            for ($j = $k + 1; $j <= $N; $j++)
                $mat[$i][$j] -= $mat[$k][$j] * $f;

            /* filling lower triangular matrix with zeros*/
            $mat[$i][$k] = 0;
        }

        //print(mat); //for matrix state
    }
    //print(mat);     //for matrix state
    return -1;
}

// function to calculate the values of the unknowns
function backSub(&$mat)
{
    global $N;
    $x = array_fill(0, $N, 0); // An array to store solution

    /* Start calculating from last equation up to the
    first */
    for ($i = $N - 1; $i >= 0; $i--)
    {
        /* start with the RHS of the equation */
        $x[$i] = $mat[$i][$N];

        /* Initialize j to i+1 since matrix is upper
        triangular*/
        for ($j = $i + 1; $j < $N; $j++)
        {
            /* subtract all the lhs values
            * except the coefficient of the variable
            * whose value is being calculated */
            $x[$i] -= $mat[$i][$j] * $x[$j];
        }

        /* divide the RHS by the coefficient of the
        unknown being calculated */
        $x[$i] = $x[$i] / $mat[$i][$i];
    }

    print("\nSolution for the system:\n");
    for ($i = 0; $i < $N; $i++)
        print(number_format(strval($x[$i]), 6)."\n");
}

// Driver program

    /* input matrix */
    $mat = array(array(3.0, 2.0,-4.0, 3.0),
                        array(2.0, 3.0, 3.0, 15.0),
                        array(5.0, -3, 1.0, 14.0));

    gaussianElimination($mat);

// This code is contributed by mits
?>

Output
Solution for the system:
3.000000
1.000000
2.000000


Illustration: 

Another Implementation of Gaussian Elimination

Another approach for implementing Gaussian Elimination is using Partial Pivoting. The steps involved in Partial Pivoting Gaussian Elimination are:

  1. Start with the given augmented matrix.
  2. Find the row with the largest absolute value in the first column and swap that row with the first row.
  3. Divide the first row by the pivot element to make it equal to 1.
  4. Use the first row to eliminate the first column in all other rows by subtracting the appropriate multiple of the first row from each row.
  5. Repeat steps 2-4 for the remaining columns, using the row with the largest absolute value as the pivot row for each column.
  6. Back-substitute to obtain the values of the unknowns.
C++
#include <iostream>
#include <cmath>
using namespace std;

const int MAXN = 100;

void partial_pivot(double A[MAXN][MAXN+1], int n) {
    for (int i = 0; i < n; i++) {
        int pivot_row = i;
        for (int j = i+1; j < n; j++) {
            if (abs(A[j][i]) > abs(A[pivot_row][i])) {
                pivot_row = j;
            }
        }
        if (pivot_row != i) {
            for (int j = i; j <= n; j++) {
                swap(A[i][j], A[pivot_row][j]);
            }
        }
        for (int j = i+1; j < n; j++) {
            double factor = A[j][i] / A[i][i];
            for (int k = i; k <= n; k++) {
                A[j][k] -= factor * A[i][k];
            }
        }
    }
}

void back_substitute(double A[MAXN][MAXN+1], int n, double x[MAXN]) {
    for (int i = n-1; i >= 0; i--) {
        double sum = 0;
        for (int j = i+1; j < n; j++) {
            sum += A[i][j] * x[j];
        }
        x[i] = (A[i][n] - sum) / A[i][i];
    }
}

int main() {
    int n = 3;
    double A[MAXN][MAXN+1] = {{3.0, 2.0,-4.0, 3.0},
                              {2.0, 3.0, 3.0, 15.0},
                              {5.0, -3, 1.0, 14.0}
                             };
    double x[MAXN];
    
    partial_pivot(A, n);
    back_substitute(A, n, x);
    
    cout << "Solution for the system:\n";
    for (int i = 0; i < n; i++) {
      
        cout << x[i] << endl;
    }
    
    return 0;
}
Java
public class Main {
    
    private static final int MAXN = 100;
    
    public static void partialPivot(double[][] A, int n) {
        for (int i = 0; i < n; i++) {
            int pivotRow = i;
            for (int j = i+1; j < n; j++) {
                if (Math.abs(A[j][i]) > Math.abs(A[pivotRow][i])) {
                    pivotRow = j;
                }
            }
            if (pivotRow != i) {
                for (int j = i; j <= n; j++) {
                    double temp = A[i][j];
                    A[i][j] = A[pivotRow][j];
                    A[pivotRow][j] = temp;
                }
            }
            for (int j = i+1; j < n; j++) {
                double factor = A[j][i] / A[i][i];
                for (int k = i; k <= n; k++) {
                    A[j][k] -= factor * A[i][k];
                }
            }
        }
    }
    
    public static void backSubstitute(double[][] A, int n, double[] x) {
        for (int i = n-1; i >= 0; i--) {
            double sum = 0;
            for (int j = i+1; j < n; j++) {
                sum += A[i][j] * x[j];
            }
            x[i] = (A[i][n] - sum) / A[i][i];
        }
    }
    
    public static void main(String[] args) {
        int n = 3;
        double[][] A = {{3.0, 2.0,-4.0, 3.0},
                        {2.0, 3.0, 3.0, 15.0},
                        {5.0, -3, 1.0, 14.0}
                       };
        double[] x = new double[MAXN];
        
        partialPivot(A, n);
        backSubstitute(A, n, x);
        
        System.out.println("Solution for the system:");
        for (int i = 0; i < n; i++) {
            System.out.println(x[i]);
        }
    }
}
Python
import numpy as np
MAXN = 100

# Function to perform partial pivot for Gaussian elimination
def partial_pivot(A, n):
    # Iterate through each row in the matrix
    for i in range(n):
        pivot_row = i
        # Find the row with the maximum absolute value in the current column
        for j in range(i + 1, n):
            if abs(A[j][i]) > abs(A[pivot_row][i]):
                pivot_row = j
        # Swap the current row with the row having the maximum absolute value
        if pivot_row != i:
            A[[i, pivot_row]] = A[[pivot_row, i]]
        # Perform Gaussian elimination on the matrix
        for j in range(i + 1, n):
            factor = A[j][i] / A[i][i]
            A[j] -= factor * A[i]

# Function to perform back substitution to solve the system of equations
def back_substitute(A, n):
    x = np.zeros(n)
    # Iterate through each row in reverse order
    for i in range(n - 1, -1, -1):
        sum_val = sum(A[i][j] * x[j] for j in range(i + 1, n))
        # Solve for x[i] using the previously calculated values of x
        x[i] = (A[i][n] - sum_val) / A[i][i]
    return x
# Driver code
if __name__ == "__main__":
    n = 3
    # Coefficient matrix augmented with the constant terms
    A = np.array([[3.0, 2.0, -4.0, 3.0],
                  [2.0, 3.0, 3.0, 15.0],
                  [5.0, -3, 1.0, 14.0]])
    
    # Perform Gaussian elimination with partial pivot
    partial_pivot(A, n)
    x = back_substitute(A, n)
    
    print("Solution for the system:")
    for i in range(n):
        print(x[i])
#This code is contributed by Vikram_Shirsat 
C#
using System;

class Program {
  const int MAXN = 100;

  // Function to find the partial pivot
  static void partial_pivot(double[, ] A, int n)
  {
    for (int i = 0; i < n; i++) {
      int pivot_row = i;
      for (int j = i + 1; j < n; j++) {
        if (Math.Abs(A[j, i])
            > Math.Abs(A[pivot_row, i])) {
          pivot_row = j;
        }
      }
      if (pivot_row != i) {
        for (int j = i; j <= n; j++) {
          double temp = A[i, j];
          A[i, j] = A[pivot_row, j];
          A[pivot_row, j] = temp;
        }
      }
      for (int j = i + 1; j < n; j++) {
        double factor = A[j, i] / A[i, i];
        for (int k = i; k <= n; k++) {
          A[j, k] -= factor * A[i, k];
        }
      }
    }
  }

  // Function to perform the back
  // substitution
  static void back_substitute(double[, ] A, int n,
                              double[] x)
  {
    for (int i = n - 1; i >= 0; i--) {
      double sum = 0;
      for (int j = i + 1; j < n; j++) {
        sum += A[i, j] * x[j];
      }
      x[i] = (A[i, n] - sum) / A[i, i];
    }
  }

  // Driver Code
  static void Main(string[] args)
  {
    int n = 3;
    double[, ] A
      = new double[, ] { { 3.0, 2.0, -4.0, 3.0 },
                        { 2.0, 3.0, 3.0, 15.0 },
                        { 5.0, -3, 1.0, 14.0 } };
    double[] x = new double[n];

    partial_pivot(A, n);
    back_substitute(A, n, x);

    Console.WriteLine("Solution for the system:");
    for (int i = 0; i < n; i++) {
      Console.WriteLine(Math.Round(x[i]));
    }
  }
}
JavaScript
// This program solves a system of linear equations using Gaussian Elimination with partial pivoting
const MAXN = 100;

// Function to perform partial pivoting on the matrix A
function partial_pivot(A, n) {
    for (let i = 0; i < n; i++) {
        let pivot_row = i;
        // Find the row with the largest absolute value in the ith column
        for (let j = i + 1; j < n; j++) {
            if (Math.abs(A[j][i]) > Math.abs(A[pivot_row][i])) {
                pivot_row = j;
            }
        }
        // Swap rows if necessary
        if (pivot_row != i) {
            for (let j = i; j <= n; j++) {
                [A[i][j], A[pivot_row][j]] = [A[pivot_row][j], A[i][j]];
            }
        }
        // Perform Gaussian Elimination on the matrix
        for (let j = i + 1; j < n; j++) {
            let factor = A[j][i] / A[i][i];
            for (let k = i; k <= n; k++) {
                A[j][k] -= factor * A[i][k];
            }
        }
    }
}

// Function to perform back substitution on the matrix A to find the solution vector x
function back_substitute(A, n, x) {
    for (let i = n - 1; i >= 0; i--) {
        let sum = 0;
        for (let j = i + 1; j < n; j++) {
            sum += A[i][j] * x[j];
        }
        x[i] = (A[i][n] - sum) / A[i][i];
    }
}

// Driver code
let n = 3;
let A = [
    [3.0, 2.0, -4.0, 3.0],
    [2.0, 3.0, 3.0, 15.0],
    [5.0, -3, 1.0, 14.0]
];
let x = Array(n);

partial_pivot(A, n);
back_substitute(A, n, x);

console.log("Solution for the system:");
for (let i = 0; i < n; i++) {
    console.log(Math.round(x[i]));
}

Output
Solution for the system:
3
1
2

Time Complexity of Gaussian Elimination

The time complexity of Gaussian Elimination is O(n3) for an n×n matrix. This is because for each pivot, we scan through the matrix to eliminate variables, which involves:

  • Finding the pivot: O(n)
  • Row elimination: O(n) for each row
  • Repeating for all rows: O(n3) total time.

Applications of Gaussian Elimination Method

  • Computing Determinants: Gaussian elimination simplifies finding the determinant of a square matrix by reducing it to row echelon form. The determinant is calculated by multiplying the diagonal elements and adjusting for row swaps and scalar multiplications.
  • Finding the Inverse of a Matrix: Gauss–Jordan elimination, a variant of Gaussian elimination, is used to find the inverse of a matrix. By augmenting the matrix with the identity matrix and applying row operations, the matrix is transformed into the inverse if it exists.
  • Computing Ranks and Bases: Gaussian elimination can be applied to any matrix to determine its rank and the basis for its column space. The row echelon form reveals information about the number of linearly independent rows and the columns that form a basis for the matrix’s column space.

Gaussian Elimination Method Practice Problems

1. Solve the following system of equations using Gaussian elimination method.

x + y + z = 6
3x + y + 6z = 10
2x +4 y – z = 1

2. Solve the following linear system using the Gaussian elimination method.

x – y = -6
6x – 2y = 0

3. Using Gaussian elimination method, solve:

x – 9y + 3z = 1
x + y + z = 4
2x – y + z = 2



Next Article
Article Tags :
Practice Tags :

Similar Reads