You are on page 1of 5

Successive Over-Relaxation Method, also known as SOR method, is popular iterative method

of linear algebra to solve linear system of equations. This method is the generalization of
improvement on Gauss Seidel Method. Being extrapolated from Gauss Seidel Method, this
method converges the solution faster than other iterative methods.

In this tutorial, were going to write a program for Successive Over-Relaxation SoR method in
MATLAB, and go through its mathematical derivation and theoretical background.

Derivation of SOR Method:


Consider the following sets of liner equations:

a11x1 + a12x2 + a13x3 + a14x4 . . . . . . . . . . .. . . . + a1nxn = b1

a21x1 + a22x2 + a23x3 + a24x4 . . . . . . . . . . .. . . . + a2nxn = b2

a31x1 + a32x2 + a33x3 + a34x4 . . . . . . . . . . .. . . . + a3nxn = b3

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

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

an1x1 + an2x2 + an3x3 + an4x4 . . . . . . . . . . .. . . . + annxn = bn

Express these equations in the form: Ax = b

where,

Now, decompose matrix A into Diagonal component matrix D, Lower triangular matrix L, and
upper triangular matrix U, such that:

A=D+L+U

where,
Rewrite the system linear equation as:

( D + L ) x = b [ U + ( 1 ) D ] x

Where, is the relaxation factor and > 1

During the iteration process, the left hand side of the equation is solved by using the previous
value for x on right hand side.

The iteration equation is expressed analytically as:

x(k+1) = ( D + L ) -1 (b [U + ( 1 ) D ] x (k) ) = L (k) + c

Where, x(k) is the kth approximation for x and x(k+1) is (k+1)th approximation for x.

Taking the advantage of triangular matrix form of ( D + L), the (k+1)th can be evaluated
sequentially by using forward substitution. The expression obtained for x(k+1) is:

SOR Method in MATLAB Code:


SORk =Method
k + 1; MATLAB Program
end

fprintf (' The final ans obtaibed

1 %for SOR method


2
3 function x = SOR( A ,B )
4 disp ( ' Enter system of equations in the form of AX=B')
5 % Calling matrix A
6
7 A = input ( ' Enter matrix A : \n')
8 % check for matrix A
9
10 % it should be a square matrix
11
12 [na , ma ] = size (A);
13 if na ~= ma
14 disp('ERROR:matrix A should be a square matrix')
15 return
16 end
17 % calling matrix B
18
19 B = input ( ' Enter matrix B : ')
20 % check for matrix B
21
22 [nb , mb ] = size (B);
23 if nb ~= na || mb~=1
24 disp( 'ERROR:input error..pls re-enter data')
25 return
26 end
27
28 w=input('Enter the relaxation parameter ');
29 % A= D + L+ U
30
31 D = diag(diag(A));
32 L = tril(A)- D;
33 U = triu(A)- D;
34
35 % check for convergence of system
36
37 e= max(eig(inv( D+w*L) * ( D*(1-w) - w*U)));
38
39 if abs(e) >= 1
40 disp ('Since the modulus of largest eigen value of iterative matrix is not less than 1')
41 disp ('This process is not convergent. Please try some other process.')
42 return
43 end
44
45 % initial guess for X..?
46
47 % default guess is [ 1 1 .... 1]'
48
49 r = input ( 'Any initial guess for X? (y/n): ','s');
50 switch r
51 case 'y'
% asking for initial guess
52
X0 = input('pls enter initial guess for X :\n')
53
% check for intial guess
54
[nx, mx] = size(X0);
55
if nx ~= na || mx ~= 1
56
disp( 'ERROR: pls check input')
57
return
58
end
59
otherwise
60
X0 = ones(na,1);
61
end
62
63
%allowed error in final answer
64
65
t = input ( 'enter the error allowed in final ans: ');
66
tol = t*ones(na,1);
67
k= 1;
68
69
X( : , 1 ) = X0;
70
err= 1000000000*rand(na,1); %intial error assumtion for looping
71
while sum(abs(err) >= tol) ~= zeros(na,1)
72
X ( : ,k+ 1 ) = inv(D+w*L) * ( D*(1-w) - w*U)*X( : ,k) + inv(D+ w*L)*B;% SOR
73
formula
74
err = X( :,k+1) - X( :, k);% finding error
75
k = k + 1;
76
77
end
78
79
fprintf (' The final ans obtaibed after %d itaration which is \n', k)
80
X( : ,k)

The above code for Successive Over-Relaxation method in Matlab for solving linear system of
equation is a three input program. Here, matrix A, matrix B, and relaxation parameter are the
input to the program. The user defined function in the program proceeds with input arguments A
and B and gives output X.

When the program is executed in MATLAB workspace, it asks for the value of coefficient matrix
A and checks if it is square matrix or not. After getting valid input for A, another input matrix B
is required to be entered to the program which must be a column matrix. Finally, the relaxation
parameter is given as input and solution of linear equation comes as output.

While giving input to the program, the standard MATLAB syntaxes must be obeyed. Heres a
sample output of the MATLAB program for SOR method:
In this output of the Matlab program, 2x + y z = 8, -3x y + 2z = -11, and -2x + y +2z = -3
have been tried to be solved. But, the largest Eigen value of iterative matrix is not less than 1. As
a result of which, the problem here is not suitable for SOR method.

You might also like