-
Notifications
You must be signed in to change notification settings - Fork 0
/
jacobi.m
37 lines (34 loc) · 993 Bytes
/
jacobi.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
%% H synarthsh ayth ylopoiei thn epanalhptikh me8odos Jacobi
%% Kaleite me [sol err it] = jacobi(a, x0, b, tol, maxiter), opou
%% a = pinakas, b = deksio melos,
%% x0 = arxikh proseggish ths lyshs,
%% tol = megisto apodekto sfalma sthn me8odo,
%% maxiter = megistos ari8mos epanalhpsewn
%% sol = h lysh toy systhmatos
%% err = sfalma ths me8odou
%% iter = plh8os epnalhpsewn pou eginan
%%
function [sol, err, it] = jacobi(a , x0, b, tol, maxiter)
xold = x0;
error = tol + 1;
iter = 0;
d = diag(a);
mat = - (tril(a, -1) + triu(a, 1) );
clf;
title('Jacobi iterations');
xlabel('iterations');
ylabel('log_{10} of error , ||.||_2');
hold on;
while (iter < maxiter & error > tol)
xnew = (mat*xold + b ) ./ d ;
error = norm(xnew - xold);
xold = xnew;
iter = iter + 1;
fprintf('Sthn %d epanalhpsh to diadoxiko sfalma %f12.8 . \n',...
iter,error );
plot([iter],[log10(error)], 'r*');
end
err = error;
it = iter;
sol = xnew;