gauss seidel abs rel compat

This commit is contained in:
2014-11-01 19:25:34 +01:00
parent 8c6af09b7c
commit 0aff089106
3 changed files with 132 additions and 1 deletions

View File

@@ -0,0 +1,66 @@
% gauss seidel algorithm
%input
%U= matrice di input compattata
%b=[1;2;17]; termini noti
%x0 vettore di partenza
%toll tolleranza assoluta , esempio 1e-4
%nmax numero massimo di passaggi, esempio 500
%Ooutput
%x0 soluzione computata
%err errore assoluto computato
%niter numero di interazioni eseguite
%ier 1 se il numero di iterazioni ha raggiunto nmax, 0 altrimenti
function [x0,err,niter,ier] = gauss_seidel_abs_compact (U,b,x0,toll,nmax)
n= length(b); % lunghezza del vettore dei termini noti o numero equazioni
niter = 0;
ier = 0;
err = inf;% err infinito per superare subito toll
%detect right and left
x=1;
y=1;
right= 0;
while U(x,y)==0
right = right + 1;
x=x+1;
end
dim= size(U);
dimx=dim(1);
left = dimx - right - 1;
%begin iterations
while ( niter < nmax ) && ( err >= toll)
x0_old = x0;
for i=1:n
partial_sum = 0;
partial_sum2 = 0;
kend = min (left, i - 1);
for k=1:kend
partial_sum = partial_sum + ( U(right +1 +k,i-k) * x0(i-k));
end
kend = min (right, n - i);
for k=1: kend
partial_sum2 = partial_sum2 + ( U(right+1-k,i+k) * x0(i+k));
end
x0(i) = (b(i) - partial_sum - partial_sum2)/U(right +1,i) ;
end
niter = niter + 1;
err = norm(x0_old - x0,inf);
mex = [' Iterazione ', num2str(niter),' : ',mat2str(x0), ' Errore assoluto : ', num2str(err)];
disp (mex)
end
if niter == nmax
disp('Warning: Massimo numero di step raggiunti')
ier = 1;
end
end

View File

@@ -0,0 +1,66 @@
% gauss seidel algorithm
%input
%U= matrice di input compattata
%b=[1;2;17]; termini noti
%x0 vettore di partenza
%toll tolleranza relativa , esempio 1e-4
%nmax numero massimo di passaggi, esempio 500
%Ooutput
%x0 soluzione computata
%err errore relativo computato
%niter numero di interazioni eseguite
%ier 1 se il numero di iterazioni ha raggiunto nmax, 0 altrimenti
function [x0,err,niter,ier] = gauss_seidel_abs_compact (U,b,x0,toll,nmax)
n= length(b); % lunghezza del vettore dei termini noti o numero equazioni
niter = 0;
ier = 0;
err = inf;% err infinito per superare subito toll
%detect right and left
x=1;
y=1;
right= 0;
while U(x,y)==0
right = right + 1;
x=x+1;
end
dim= size(U);
dimx=dim(1);
left = dimx - right - 1;
%begin iterations
while ( niter < nmax ) && ( err >= toll)
x0_old = x0;
for i=1:n
partial_sum = 0;
partial_sum2 = 0;
kend = min (left, i - 1);
for k=1:kend
partial_sum = partial_sum + ( U(right +1 +k,i-k) * x0(i-k));
end
kend = min (right, n - i);
for k=1: kend
partial_sum2 = partial_sum2 + ( U(right+1-k,i+k) * x0(i+k));
end
x0(i) = (b(i) - partial_sum - partial_sum2)/U(right +1,i) ;
end
niter = niter + 1;
err = norm(x0_old - x0,inf)/norm(x0,inf);
mex = [' Iterazione ', num2str(niter),' : ',mat2str(x0), ' Errore relativo : ', num2str(err)];
disp (mex)
end
if niter == nmax
disp('Warning: Massimo numero di step raggiunti')
ier = 1;
end
end

View File

@@ -26,7 +26,6 @@ function [x0,err,niter,ier] = jacobi_abs_compact (U,b,x0,toll,nmax)
%detect right and left
x=1;
y=1;
left= 0;
right= 0;
while U(x,y)==0
right = right + 1;