Named after Carl Friedrich Gauss, Gauss Elimination Method is a popular technique of linear algebra for solving system of linear equations. As the manipulation process of the method is based on various row operations of augmented matrix, it is also known as row reduction method. Gauss elimination method has various uses in finding rank of a matrix, calculation of determinant and inverse of invertible matrix.

In earlier tutorials, we discussed a C program and algorithm/flowchart for Gauss elimination method. Here, we’re going to write a program code for **Gauss elimination method in MATLAB**, go through its mathematical derivation, and compare the result obtained from MATLAB code with a numerical example.

## Derivation of Gauss Elimination Method:

Consider the following system of linear equations:

A_{1}x + B_{1}y + C_{1}z = D_{1} . . . . . . . ( 1 )

A_{2}x + B_{2}y + C_{2}z = D_{2} . . . . . . . . ( 2 )

A_{3}x + B_{3}y + C_{3}z = D_{3 }. . . . . . . . . . ( 3)

In order to apply Gauss elimination method, we need to express the above three linear equations in matrix form as given below:

A = B =

Arrange matrices A and B in the following form (augmented matrix):

Now, perform the following elementary row operations till it is reduced to echelon form by:

- Exchanging or swapping two rows
- Adding the certain multiple of one row to another row
- Multiplying a row by non-zero number

This procedure is repeated until the augmented matrix is reduced to following echelon form:

Thus, the solution of above system of linear equation is (a, b, c) i.e. x = a, y = b and z = c. The program for Gauss elimination method in MATLAB is based on this derivation.

## Gauss Elimination Method in MATLAB:

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 |
function C = gauss_elimination(A,B) % defining the function A= [ 1 2; 4 5] % Inputting the value of coefficient matrix B = [-1; 4] % % Inputting the value of coefficient matrix i = 1; % loop variable X = [ A B ]; [ nX mX ] = size( X); % determining the size of matrix while i <= nX % start of loop if X(i,i) == 0 % checking if the diagonal elements are zero or not disp('Diagonal element zero') % displaying the result if there exists zero return end X = elimination(X,i,i); % proceeding forward if diagonal elements are non-zero i = i +1; end C = X(:,mX); function X = elimination(X,i,j) % Pivoting (i,j) element of matrix X and eliminating other column % elements to zero [ nX mX ] = size( X); a = X(i,j); X(i,:) = X(i,:)/a; for k = 1:nX % loop to find triangular form if k == i continue end X(k,:) = X(k,:) - X(i,:)*X(k,j); % final result end |

The above source code for Gauss elimination method in MATLAB can be used to solve any number of linear equations. The order of augmented matrix relies on the number of the linear equations to be solved by using this method. As the matrix element data are embedded within the source code, the user doesn’t need to give input to the program.

This source code is written to solve the following system of linear equation:

2x + y – z = 8

-3x – y + 2z = -11

-2x + y +2z = -3

Therefore, the value of A and B in the source code are fixed to be [ 2 1 -1; -3 -1 2; -2 1 2] and [8 ; -11 ; -3 ]

Instead of creating a separate MATLAB file to define the function and give input, a single file is designed to perform all the tasks. In order to solve other equation using this source code, the user has to change the values of augmented matrices A and B defined in the source code.

The sample output of the MATLAB program is given below:

## Gauss Elimination Method Numerical Example:

Now, let’s analyze numerically the above program code of Gauss elimination in MATLAB using the same system of linear equations. So, we are to solve the following system of linear equation by using Gauss elimination (row reduction) method:

2x + y – z = 8

-3x – y + 2z = -11

-2x + y +2z = -3

**Solution:**

The given equations are:

2x + y – z = 8 . . . . . . . . . . . ( 1 )

-3x – y + 2z = -11 . . . . . . . . . ( 2 )

-2x + y +2z = -3 . . . . . . . . . . . ( 3 )

These can be expressed in the form of augmented matrix as:

Performing Row Operations: L_{2 }+ 3/2 L_{1} –> L_{2 }and L_{3} + L_{1} –>L_{3}

The new augmented matrix is:

Performing row operation: L_{3} – 4L_{2} –>L_{3}

The new augmented matrix is:

Row Operations: L_{2} + 1/3L_{3}—> L_{2} and L_{1} – L_{3} –> L_{1 }

Augmented Matrix:

Row operations: 2L_{2 }–> L_{2} and -L_{3}–> L_{3}

Augmented Matrix:

Row operations: L_{1} – L_{2} –> L_{1} and 1/2L_{1} –> L_{1}

Augmented matrix:

Thus, x= 2 , y = 3 and z = -1. This is the required solution which is same as that obtained from Gauss elimination method’s MATLAB code.

If you have any questions regarding Gauss elimination method, its MATLAB program code, or its mathematical derivation, bring them up from the comments. You can find more Numerical methods tutorial using MATLAB here.

Gaussian elimination uses row position changing. So, “if X(i,i) == 0” is not correct because rows can be change. But work is nice.