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:

a_{11}x_{1} + a_{12}x_{2 } + a_{13}x_{3 } + a_{14}x_{4 } . . . . . . . . . . .. . . . + a_{1n}x_{n }= b_{1 }– – – – – (1)

a_{21}x_{1} + a_{22}x_{2 } + a_{23}x_{3 } + a_{24}x_{4 } . . . . . . . . . . .. . . . + a_{2n}x_{n }= b_{2} – – – – – (2)

a_{31}x_{1} + a_{32}x_{2 } + a_{33}x_{3 } + a_{34}x_{4 } . . . . . . . . . . .. . . . + a_{3n}x_{n }= b_{3}– – – – – – (3)

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

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

a_{n1}x_{1} + a_{n2}x_{2 } + a_{n3}x_{3 } + a_{n4}x_{4 } . . . . . . . . . . .. . . . + a_{nn}x_{n }= b_{n }– – – – – – ( 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:**

- List out the given linear equations and write down the augmented matrix of given system.
- 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. - 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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
function [x,err]=gauss_jordan_elim(A,b) A = [1 1 1;2 3 5; 4 0 5] % input for augmented matrix A b = [5 ; 8; 2] % intput for matrix B [n,m]=size(A); % finding the size of matrix A err =0; % calculation of error x=zeros(n,1); % calling fuction zero if n ~= m disp('error: n~=m'); % displaying error if found err = 1; end % end of the scope of if if length(b) ~= n % finding the legth of matrix B disp('error: wrong size of b'); % displaying error, if found err = 2; else if size(b,2) ~= 1 b=b'; end % end of the scope of if-else if size(b,2) ~= 1 disp('error: b is a matrix'); % displaying erron in matrix B err = 3; end end if err == 0 Aa=[A,b]; for i=1:n [Aa(i:n,i:n+1),err]=gauss_pivot(Aa(i:n,i:n+1)); if err == 0 Aa(1:n,i:n+1)=gauss_jordan_step(Aa(1:n,i:n+1),i); end end x=Aa(:,n+1); end A=0; function A1=gauss_jordan_step(A,i) % calling of fuction function [n,m]=size(A); % determination of size of matrix A A1=A; % assigning A to A1 s=A1(i,1); A1(i,:) = A(i,:)/s; k=[[1:i-1],[i+1:n]]; for j=k s=A1(j,1); A1(j,:)=A1(j,:)-A1(i,:)*s; end % end of for loop function [A1,err]=gauss_pivot(A) % calling of fucntion [n,m]=size(A); % finding the size of matrix A A1=A; % process of assigning err = 0; % error flag if A1(1,1) == 0 check = logical(1); % logical(1) - TRUE i = 1; while check i = i + 1; if i > n disp('error: matrix is singular'); err = 1; check = logical(0); else if A(i,1) ~= 0 & check check = logical(0); b=A1(i,:); % process to change row 1 to i A1(i,:)=A1(1,:); A1(1,:)=b; end end end end |

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.