-
Notifications
You must be signed in to change notification settings - Fork 0
/
gauss_siedel.m
executable file
·70 lines (60 loc) · 1.79 KB
/
gauss_siedel.m
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
% for gauss_siedel methos
function x = gauss_siedel( A ,B )
disp ( ' Enter system of equations in the form of AX=B')
%calling matrix A
A = input ( ' Enter matrix A : \n')
% a check for matrix A
% Matrix A should be a square matrix
[na , ma ] = size (A);
if na ~= ma
disp('ERROR:matrix A should be a square matrix')
return
end
%calling matrix B
B = input ( ' Enter matrix B : ')
% check for matrix B
[nb , mb ] = size (B);
if nb ~= na || mb~=1
disp( 'ERROR:input error..pls re-enter data')
return
end
% A = D + L + U
D = diag(diag(A));
L = tril(A)- D;
U = triu(A)- D;
% check for condition of convergence for the method
e= max(eig(-inv(D+L)*(U)));
if abs(e) >= 1
disp (' Since the modulus of largest eigen value of itarive matix is not less than 1')
disp ('This process is not concergent. Pls try some other process.')
return
end
% initial guess for X..?
% default guess is [ 1 1 .... 1]'
r = input ( 'Any initial guess for X? (y/n): ','s');
switch r
case 'y'
% asking for initial guess
X0 = input('pls enter initial guess for X :\n')
% check for intial guess
[nx, mx] = size(X0);
if nx ~= na || mx ~= 1
disp( 'ERROR: pls check input')
return
end
otherwise
X0 = ones(na,1);
end
% allowed error in final answer
t = input ( 'enter the error allowed in final ans: ');
tol = t*ones(na,1);
k= 1;
X( : , 1 ) = X0;
err= 1000000000*rand(na,1);% intial error assumtion for the sake of looping
while sum(abs(err) >= tol) ~= zeros(na,1)
X ( : ,k+ 1 ) = -inv(D+L)*(U)*X( : ,k) + inv(D+L)*B;% GAUSS-SIEDEL formula
err = X( :,k+1) - X( :, k);% findind error
k = k + 1;
end
fprintf (' The final ans obtaibed after %g itaration which is \n', k)
X( : ,k)