# Gauss-Jordan Method in MATLAB

0
5518

Gauss-Jordan Method is  a popular process of solving system of linear equation in linear algebra. This method solves the linear equations by transforming the augmented matrix into reduced-echelon form with the help of various row operations on augmented matrix. Gauss-Jordan method is an elimination maneuver and is useful for solving linear equation as well as for determination of inverse of a matrix.

Earlier, we discussed a C program and algorithm/flowchart for Gauss Jordan. In this tutorial, we’re going to write a program for Gauss-Jordan method in Matlab, going through its theoretical background, working procedure (steps) of the method along with a numerical example.

## Theory of Gauss-Jordan Method:

Consider the following system of linear equations:

a11x1 + a12x2  + a13x3  + a14x4  . . .  . . . . . . . ..  . . . + a1nxn = b1 – – – – – (1)

a21x1 + a22x2  + a23x3  + a24x4  . . .  . . . . . . . ..  . . . + a2nxn = b2 – – – – – (2)

a31x1 + a32x2  + a33x3  + a34x4  . . .  . . . . . . . ..  . . . + a3nxn = b3– – – – – – (3)

. .  . . . . .  . . . . . .  . . . . . . . .  . . . . . . . .  . . . . . . . . . . . . . . . . . . .

. .  . . . . .  . . . . . .  . . . . . . . .  . . . . . . . .  . . . . . . . . . . . . . . . . . . .

an1x1 + an2x2  + an3x3  + an4x4  . . .  . . . . . . . ..  . . . + annxn = bn – – – – – – ( n )

The above ‘n’ numbers of linear equations can be represented by the general form:

Ax = B

where, A is the matrix of coefficients of variables, x is the matrix of variable and B is the matrix of constants on right hand side of linear equations.

Now, express these matrices in the following form:

Perform row operations and reduce the above matrix into echelon form as given below:

s1, s2, —- and sn are the solution of equation. The program we’re going to write here for Gauss Jordan in MATLAB is based on this derivation.

Steps for Solving Linear Equation Using Gauss-Jordan Method:

1. List out the given linear equations and write down the augmented matrix of given system.
2. Perform row operation to transfer the augmented matrix into reduced-echelon form by using the sub-steps given below:
-> The rows with entire zeros need to be grouped together at the bottom of matrix.
-> In each row that does not consists of entirely zeros, the left most element should be 1.
-> In all columns which has leading element as 1, all other entries are made zero.
-> Convert the matrix into echelon form using the appropriate operation on step c.
3. Stop the process in the step 2 when the all the diagonal elements are 1 and non-diagonal elements are zero. The elements in the right-most columns are the solution of given system of linear equations.

## Gauss Jordan Method in MATLAB Code:

The above program code for Gauss Jordan method in MATLAB is written for solving the following set of linear equations:

x + y + z = 5

2x + 3y + 5z = 8

4x + 5z  = 2

Therefore, in the program, the value of A is assigned to A = [1 1 1;2 3 5; 4 0 5] and that of B is assigned to b = [5 ; 8; 2]. If the code is to be used for solving other system of linear equation, the values of A and B in the code should be changed by strictly following the MATLAB syntax.

In order to run the program, copy the source code given above in MATLAB editor and save as file_name.m file and run. This MATLAB program doesn’t need any input. When the code is run in the MATLAB workspace, the output is displayed in command window.

A sample output of the program is given below:

## Gauss-Jordan Method Example:

Here, we’re going to analyze mathematically the aforementioned program for Gauss Jordan method in MATLAB using the same set of linear equations. The question here is:

Solve the following system of linear equations by using Gauss-Jordan Method:

x + y + z = 5

2x + 3y + 5z = 8

4x + 5z  = 2

Solution:

The given equations are:

x + y + z = 5

2x + 3y + 5z = 8

4x + 5z  = 2

The augmented matrix of the system is:

Now, performing row operations as follows:

Thus, the solutions are:  x = 3, y = 4, and z = -2, which is same as that obtained from the Matlab code for Gauss Jordan.

If you have any question regarding Gauss Jordan method, its theoretical background, working steps, or its MATLAB program, bring them up from the comments section. You can find more Numerical Methods tutorials using Matlab here.