Gauss-Seidel Method MATLAB Program

1
3966

Gauss-Seidel method is a popular iterative method of solving linear system of algebraic equations. It is applicable to any converging matrix with non-zero elements on diagonal. The method is named after  two German mathematicians: Carl Friedrich Gauss and Philipp Ludwig von Seidel.

Gauss-Seidel is considered an improvement over Gauss Jacobi Method. In this method, just like any other iterative method, an approximate solution of the given equations is assumed, and iteration is done until the desired degree of accuracy is obtained.

In earlier tutorials, we’ve already gone through the C program and algorithm/flowchart for Gauss-Seidel method. Here, we’re going to write a program code for Gauss-Seidel method in MATLAB, discuss its theoretical background, and analyze the MATLAB program’s result with a numerical example.

Gauss-Seidel Method Theory:

Let’s go through a brief theoretical/mathematical background of Gauss-Seidel method. The matrices, iterations, and the procedure explained below cover the basic guidelines to write the program code for Gauss-Seidel method in MATLAB.

Consider the following system of linear equations:

a11x1 + a12x2 + a13x3 + a14x4 + a15x5 + a16x6 ……. + a1nxn = b1
a21x1 + a22x2 + a23x3 + a24x4 + a25x5 + a26x6 ……. + a2nxn = b2
a31x1 + a32x2 + a33x3 + a34x4 + a35x5 + a36x6 ……. + a3nxn = b3
. . . . . . . . . . . . . .  . . . . . . . . .   . . . . . . .  . ….. . . . . . . . ..  . . . . . . . . . . . . ..  . . .. .  . .
. . . . . . . . . . . . . .  . . . . . . . . .   . . . . . . .  . ….. . . . . . . . ..  . . . . . . . . . . . . ..  . . .. .  . .
an1x1 + an2x2 + an3x3 + an4x4 + an5x5 + an6x6 ……. + annxn = bn

where, aij represents the coefficient of unknown terms xi.

The above equations can be presented in matrix form as follows:

Gauss-Seidel method in MATLAB - Matrix Form

Or simply, it can be written as: [A][X] = [B]

Now, decomposing the matrix A into its lower triangular component and upper triangular component, we get:

A = L x U

where,

Gauss-Seidel method in MATLAB - Lower and Upper Triangular Matrix

Further, the system of linear equations can be expressed as:

L x X = B – UX     —–(a)

In Gauss-Seidel method, the equation (a) is solved iteratively by solving the left hand value of x and then using previously found x on right hand side. Mathematically, the iteration process in Gauss-Seidel method can be expressed as:

X(k+1) = L -1( B –UX(k) )

Applying forward substitution, the elements of X(k+1) can be computed as follows:

Gauss-Seidel method in MATLAB - Forward Substitution

The same procedure aforementioned is followed in the MATLAB program for this method. The process of iteration is continued till the values of unknowns are under the limit of desired tolerance.

Gauss-Seidel Method in MATLAB:

Also see,
Gauss-Seidel C Program
Gauss-Seidel Algorithm/Flowchart

In the above MATLAB program, a function, x = gauss_siedel( A ,B ), is initially defined. Here, A and B are the matrices generated with the coefficients used in the linear system of equations. The elements of A and B are input into the program following the basic syntax of MATLAB programming.

A and B are to be checked: A should be a square matrix and B must be a column matrix to satisfy the criteria of Gauss-Seidel method. Then, as explained in the theory, matrix A is split into its upper triangular and lower triangular parts to get the value of first iteration.

The value of variables obtained from the first iteration are used to start the second iteration, and the program keeps on iterating till the solution are in the desired limit of tolerance as provided by the user.

Here’s a sample output screen of the MATLAB program:

Gauss-Seidel method in MATLAB - Output

Gauss-Seidel Method Example:

The above MATLAB program of Gauss-Seidel method in MATLAB is now solved here mathematically. The equations given are:

4x1 – x2 –x3 = 3
-2x1 + 6x2 + x3 = 9
-x1 + x2 – 7x3 = -6

In order to get the value of first iteration, express the given equations as follows:

4x1 – 0 –0 = 3
-2x1 + 6x2 + 0 = 9
-x1 + x2 – 7x3 = -6

From the first equation: x1 = 3/4 = 0.750
Substitute the value of x1 in the second equation : x2 = [9 + 2(0.750)] / 6 = 1.750
Substitute the values of x1 and x2 in the third equation: x3 = [-6 + 0.750 − 1.750] / 7 = − 1.000
Thus, the result of first iteration is: ( 0.750, 1.750, -1.000 )

The whole iteration procedure that goes on in Gauss-Seidel method (and the above MATLAB program) is presented below:

Gauss Seidel method in Matlab - Iteration

where, k is the number of iteration.

The final solution obtained is (1.000, 2.000, -1.000).

If you have any questions regarding Gauss-Seidel method, its theory, or MATLAB program, drop them in the comments. You can find more Numerical methods tutorial using MATLAB here.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here