Gauss Elimination Method MATLAB Program

1
3893

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:

A1x + B1y + C1z = D1 . . .  . . . . ( 1 )
A2x + B2y + C2z = D2 . . . . . . . .  ( 2 )
A3x + B3y + C3z = D3 . . . . . . . . . . ( 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 =

        Gauss Elimination Method in MATLAB - Matrix A                                    Gauss Elimination Method in MATLAB - Matrix B

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

Gauss Elimination Method in MATLAB - 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:

Gauss Elimination Method in MATLAB - 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:

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 in MATLAB - Output

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:

Gauss Elimination Method in MATLAB - Augmented Matrix 1

Performing Row Operations: L2 + 3/2 L1 –> L2 and L3  + L1 –>L3

The new augmented matrix is:

Gauss Elimination Method in MATLAB - Augmented Matrix 2

Performing row operation: L3 – 4L2 –>L3

The new augmented matrix is:

Gauss Elimination Method in MATLAB - Augmented Matrix 3

Row Operations: L2 + 1/3L3—> L2 and L1 – L3 –> L1

Augmented Matrix:

Gauss Elimination Method in MATLAB - Augmented Matrix 4

Row operations: 2L–> L2 and -L3–> L3

Augmented Matrix:

Gauss Elimination Method in MATLAB - Augmented Matrix 5

Row operations: L1 – L2 –> L1  and 1/2L1 –> L1

Augmented matrix:

Gauss Elimination Method in MATLAB - Final 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.

1 COMMENT

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

LEAVE A REPLY