You are on page 1of 3

Islamic University of Technology (IUT)

Organization of Islamic Cooperation (OIC)


Department of Electrical and Electronic Engineering (EEE)

Course No : Math 4522


Course Name : Numerical Methods Lab
Experiment No : 07
Experiment Name : Introduction to Gauss Seidel Method

Gauss Seidel Method:

Iterative or approximate methods provide an alternative to the elimination methods


described to this point. Such approaches are similar to the techniques we developed
to obtain the roots of a single equation in earlier labs. Those approaches consisted of
guessing a value and then using a systematic method to obtain a refined estimate of
the root. Because the present part of the book deals with a similar problem—
obtaining the values that simultaneously satisfy a set of equations—we might
suspect that such approximate methods could be useful in this context.

The Gauss-Seidel method is the most commonly used iterative method. Assume that
we are given a set of n equations:

[A][X] = [B]
Suppose that for conciseness we limit ourselves to a 3 × 3 set of equations. If the
diagonal elements are all nonzero, the first equation can be solved for x1, the second
for x2, and the third for x3 to yield

𝑏1 −𝑎12 𝑥2 −𝑎13 𝑥3
𝑥1 = ………………….(1)
𝑎11
𝑏2 −𝑎21 𝑥1 −𝑎23 𝑥3
𝑥2 = ………………….(2)
𝑎22
𝑏3 −𝑎31 𝑥1 −𝑎32 𝑥2
𝑥3 = ………………….(3)
𝑎33
Now, we can start the solution process by choosing guesses for the x’s. A simple
way to obtain initial guesses is to assume that they are all zero. These zeros can be
substituted into Eq. (1), which can be used to calculate a new value for x1 = b1/a11.
Then, we substitute this new value of x1 along with the previous guess of zero for
x3 into Eq. (2) to compute a new value for x2. The process is repeated for Eq. (3) to
calculate a new estimate for x3. Then we return to the first equation and repeat the
entire procedure until our solution converges closely enough to the true values.
𝑥𝑖 −𝑥𝑖−1
|𝑒𝑎 | = | | * 100%
𝑥𝑖

Code for implementing Gauss Seidel Method:


clc
clear all
close all

A=input('Enter matrix [A]: ');


b=input('Enter matrix [b]: ');
tol=input('maximum approximated error :');
n=size(A,1);
x=rand(n,1);
normVal=Inf;
GaussItr=0;

%Algorithm: Gauss Seidel Method


while normVal>tol

x_old=x;

for i=1:n

sigma=0;

for j=1:i-1
sigma=sigma+A(i,j)*x(j);
end

for j=i+1:n
sigma=sigma+A(i,j)*x_old(j);
end

x(i)=(1/A(i,i))*(b(i)-sigma);
end

GaussItr=GaussItr+1;
normVal=norm(x_old-x);

end
fprintf('Solution of the system is : \n%f\n%f\n%f in %d iterations ',x,GaussItr);
Task:
Write a Matlab code to employ LU decomposition method to compute inverse of any
3x3 matrix.
Hints:
1. First find L and U matrix of the input matrix.
2. Use [L][D]=[1 0 0]’ to compute [D].
3. Use [U][X]=[D] to compute 1st column of inverse matrix.
4. Repeat the above steps to find 2nd and 3rd column of the inverse matrix using
[L][D]= [0 1 0]’ and [L][D]=[0 0 1]’ respectively.
[N.B : Nowhere in your solution can you use matlab function “inv” to compute any
kind of value]
To verify your code use

to get

Assignment:
Although, the code given in the lab sheet for implementing Gauss Seidel method, is
written for solving any nxn type of linear equations, it may not work for a specific type
of input sets. In which case this code will not give you proper results?
Rewrite the code in a way which will fix this problem.

You might also like