-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsor.m
56 lines (44 loc) · 1.36 KB
/
sor.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
% SOR method for solving linear equations
% --- Parameters ---
% A = N x N non-singular matrix
% b = N x 1 vector
% x0 = N x 1 vector
% w = convergence factor
% --- Return Value ---
% xks = every xk value found along the way is stored in a list
function xks = sor(A, b, x0, w)
% Initializing variables
xks = [];
xks = [xks; x0.'];
xk = x0;
xk_recent = x0;
n = size(x0, 1);
xk_new = zeros(n, 1);
% Iteratively calculating a new x value as a solution to the
% equation Ax = b
while true
% Calculating next x value
for i = 1:n
temp = b(i);
for j = 1:n
if (j ~= i)
temp = temp - A(i,j)*xk_recent(j);
end
end
xk_new(i) = (w/A(i,i))*temp + (1-w)*xk(i);
% Updating most recent x value found since we just
% calculated a new one
xk_recent(i) = xk_new(i);
end
% Appendng current xk value to list
xks = [xks; xk_new.'];
% Calculating error
error = norm(xk_new - xk);
% If the algorithm has converged, exit
if (error < 1.0e-7)
return;
end
% Updating xk for the next step
xk = xk_new;
end
end