Sie sind auf Seite 1von 5

This function solves linear equation systems such as Ax=b using SOR method.

sor(A, x, b, w, max_it, tol)


function [ x, err, iter, flag ] = sor(A, x, b, w, max_it, tol)
%
% [ x, err, iter, flag ] = sor(A, x, b, w, max_it, tol)
%
% SOR Successive Over-Relaxation Method
%
This function solves linear equation systems such as Ax=b using SOR
%
method (Successive Over-Relaxation).
%
When the relaxation scalar w=1, the method used is Gauss-Seidel.
%
% Input:
%
A - input matrix
%
x - inicial vector
%
b - vector b
%
w - relaxation scalar
%
max_it - maximum number of iterations
%
tol - tolerance
%
% Output:
%
x - solution vector
%
err - norm err estimate
%
iter - nu,ber of performed iterations
%
flag - 0 = a solution was found / 1 = no convergence for max_it
%
% Example:
%
[ x, err, iter, flag ] = sor( [.5 .125; .125 .25], zeros(2,1),
%
ones(2,1), .5, 1e4, 1e-4 )
%
% Author:
Tashi Ravach
% Version:
1.0
% Date:
08/06/2006
%
if nargin == 3
w = .5;
max_it = 1e4;
tol = 1e-4;
elseif nargin == 4
max_it = 1e4;
tol = 1e-4;
elseif nargin == 5
tol = 1e-4;
elseif nargin ~= 6
error('sor: invalid input parameters');
end
flag = 0;
iter = 0;
norma2_b = norm(b);
if (norma2_b == 0.0)
norma2_b = 1.0;
end

r = b - A * x;
err = norm(r) / norma2_b;
if (err < tol)
return
end
% separate A into several matrix for SOR/Gauss-Seidel
[ M, N, b ] = matsep(A, b, w, 2);
for iter = 1 : max_it
x_1 = x;
x = M \ (N * x + b); % adjust the aproximation
%err = norm(x - x_1) / norm(x); % compute error
err = norm(x_1 - x, 1); % compute error
if (err <= tol) % check for convergence
break
end
end
b = b / w; % vector b
if (err > tol) % no convergence
flag = 1;
end
end
function [ M, N, b ] = matsep(A, b, w, flag)
%
% [ M, N, b ] = matsep(A, b, w, flag)
%
% MATSEP Matrix Separation
%
Input matrix is splitted into several others in diferent ways depending
%
on the method to be used: Jacobi and SOR (Gauss-Seidel when w = 1)
%
% Input:
%
A - input matrix
%
x - inicial vector
%
b - vector b
%
flag - 1 = Jacobi / 2 = SOR
%
% Output:
%
M - matrix M
%
N - matrix N
%
b - vector b (modified for SOR)
%
% Author:
Tashi Ravach
% Version:
1.0
% Date:
08/06/2006
%
if nargin ~= 4
error('matsep: invalid input parameters');
end

[ m, n ] = size(A);
if m ~= n
error('matsep: input matrix A must have dimension nXn');
end
[ l, o ] = size(b);
if l ~= n && o ~= 1
error('matsep: input matrix b must have dimension nX1');
end
if (flag == 1) % separation for Jacobi
M = diag(diag(A));
N = diag(diag(A)) - A;
elseif (flag == 2) % separation for SOR/Gauss-Seidel
b = w * b;
M = w * tril(A, -1) + diag(diag(A));
N = -w * triu(A, 1) + (1.0 - w) * diag(diag(A));
end
end

0 down
vote
favorite

I need to code the Gauss Seidel and Successive over relaxation iterative methods in
Matlab. I have created the below code for each of them, however my final solution
vector does not return the correct answers and i'm really struggling to figure out why.
Could anyone please help me? In both cases, x is the final solution vector and i
returns the number of iterations.
Thanks in advance
Gauss Seidel Method:
function [x,i] = gaussSeidel(A,b,x0,tol)
x2 = x0;
count = 0;
D = diag(diag(A));
U = triu(A-D);
disp(U);
L = tril(A-D);
disp(L);
C = diag(diag(A));
disp(C);
Inv = inv(C+D);
error = inf;
while error>tol
x1 = x2;
x2 = Inv*(b-(U*x1));
error = max(abs(x2-x1)/abs(x1));
count = count + 1;
end
x = x2;
i = count;
end

SOR Method:
function [x,i] = sor(A,b,x0,tol,omega)
[m,n] = size(A);
D = diag(diag(A));
U = triu(A-D);
L = tril(A-D);
count = 1;
xtable = x0;
w = omega;
if size(b) ~= size(x0)
error('The given approximation vector does not match the x vector
size');
elseif m~=n
error('The given coefficient matrix is not a square');
else
xnew = (inv(D+w*L))*(((1-w)*D-w*U)*x0 +w*b);
RelError = (abs(xnew-x0))/(abs(xnew));
RelErrorCol = max(max(RelError));
while RelErrorCol>tol
xnew = (inv(D+w*L))*(((1-w)*D-w*U)*x0 +w*b);
RelError = (abs(xnew-x0))/(abs(xnew));
RelErrorCol = max(max(RelError));
x0 = xnew;
count = count+1;
xtable = [xtable, xnew];
end
disp(xtable);
x = xnew;
i = count;
end

hm
function sor(A, b, N)
%SOR(A, b, N) solve iteratively a system of linear equations whereby
%A is the coefficient matrix, and b is the right-hand side column vector.
%N is the maximum number of iterations.
%The method implemented is that of Successive Over Relaxation.
%The starting vector is the null vector, but can be adjusted to one's needs.
%The iterative form is based on the S.O.R. transition/iteration matrix
%Tw = inv(D-w*L)*((1-w)*D+w*U) and the constant vector cw = w*inv(D-w*L)*b.
%The optimal parameter w is calculated using the spectral raduis of the Jacobi
%transition matrix Tj = inv(D)*(L+U).
%The output is the solution vector x.
%This file follows the algorithmic guidelines given in the book
%Numerical Analysis, 7th Ed, by Burden & Faires
%Author: Alain G. Kapitho
%Date : Aug. 2007
n = size(A,1);
%splitting matrix A into the three matrices L, U and D

D = diag(diag(A));
L = tril(-A,-1);
U = triu(-A,1);
Tj = inv(D)*(L+U);
%Jacobi iteration matrix
rho_Tj = max(abs(eig(Tj)));
%spectral radius of Tj
w = 2./(1+sqrt(1-rho_Tj^2));
%optimal overrelaxation parameter
disp('w =');disp(w);
Tw = inv(D-w*L)*((1-w)*D+w*U); %SOR iteration matrix
cw = w*inv(D-w*L)*b;
%constant vector needed for iterations
tol = 1e-05;
k = 1;
x = zeros(n,1);

%starting vector

while k <= N
x(:,k+1) = Tw*x(:,k) + cw;
if norm(x(:,k+1)-x(:,k)) < tol
disp('The procedure was successful')
disp('Condition ||x^(k+1) - x^(k)|| < tol was met after k iterations')
disp(k); disp('x = ');disp(x(:,k+1));
break
end
k = k+1;
end
if norm(x(:,k+1)- x(:,k)) > tol || k > N
disp('Maximum number of iterations reached without satisfying condition:')
disp('||x^(k+1) - x^(k)|| < tol'); disp(tol);
disp('Please, examine the sequence of iterates')
disp('In case you observe convergence, then increase the maximum number of
iterations')
disp('In case of divergence, the matrix may not be diagonally dominant')
disp(x');
end

Das könnte Ihnen auch gefallen