Skip to main content

Gauss-Jordan pseudo-code

Short answer

Gauss-Jordan method reduces augmented matrix [A|b] to [I|x] by row operations. Algorithm: for k=1 to n, find pivot row with max |a[p][k]|, swap if needed, normalize pivot row, then eliminate column k from all other rows. Solution x[i] = a[i][n+1].

Write a pseudo-code (algorithm) to solve a system of linear equations Ax = b of 'N' unknowns using the Gauss Jordan method.

Gauss-Jordan method reduces the augmented matrix [A | b] to [I | x] using row operations, so the solution is read directly without back substitution. Partial pivoting is included for safety.

Pseudo-code

Algorithm: Gauss-Jordan method
Input : n, matrix a[1..n][1..n+1]  (augmented [A | b])
Output: solution x[1..n]

1.  Read n and the augmented matrix a
2.  For k = 1 to n do
3.      // partial pivoting
        Find row p (k <= p <= n) with largest |a[p][k]|
        If a[p][k] = 0 then
            print "No unique solution"; stop
        If p != k then swap row p and row k
4.      // normalise pivot row
        piv = a[k][k]
        For j = k to n+1 do
            a[k][j] = a[k][j] / piv
        End for
5.      // eliminate column k from all other rows
        For i = 1 to n do
            If i != k then
                m = a[i][k]
                For j = k to n+1 do
                    a[i][j] = a[i][j] - m * a[k][j]
                End for
            End if
        End for
6.  End for
7.  For i = 1 to n do
        x[i] = a[i][n+1]
    End for
8.  Print x[1..n]
9.  Stop

Notes

  • The final coefficient matrix is the identity, so x[i] is the last column.
  • Operations count is about n³/2 multiplications, more than Gauss elimination (n³/3).
  • The same procedure applied to [A | I] gives the inverse of A.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Numerical Method chapter 3 questions →