-
Notifications
You must be signed in to change notification settings - Fork 0
/
gz.m
59 lines (41 loc) · 1.08 KB
/
gz.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
function [x, flag, it] = gz(A, b, x0, itMax, errMax)
% Gaus Zajdelov postupak za resavanje sistema
% linearnih algebarskih jednacina
% A ulazna matrica
% b ulazni vektor
% x0 pocetno resenje
% itMax maksimalan broj iteracija
% errMax maksimalna greska
% x rezultat
% flag = 0 sve je u redu
% flag = 1 greska
flag = 1;
[n, c] = size(A);
if(n~=c)
return;
end
x = zeros(n,1);
for it = 1:itMax
for i=1:n
s = 0;
for j=1:i-1
s = s + A(i,j)*x(j);
end
for j =i+1:n
s = s + A(i,j)*x0(j);
end
x(i) = (b(i)-s)/A(i,i);
end
err = 0;
for i=1:n
err = err+(x(i)-x0(i))^2;
end
if (err<errMax^2)
flag = 0;
return;
end
for i=1:n
x0(i) = x(i);
end
end
end