%
% SR.M Function to implement the Successive Relaxation (SR) Method
%
% This routine implements a simple version of the SR method -- that is,
% no partial pivoting is performed. Thus, the user must be sure to arrange
% the equations appropriately so that the diagonal elements do not have any zeros.
%
% Inputs: A = n by n coefficient matrix (nonzero diagonal elements)
% b = n by 1 right-hand side vector
% x = n by 1 vector containing initial guess
% alpha = relaxation parameter (alpha > 0)
% tol = error tolerance used to terminate search
% M = maximum number of iterations
%
% Outputs: x = n by 1 solution vector
% k = number of iterations performed
%
% Note: This file is a modified version of a similar routine from the
% text "Applied Numerical Methods for Engineers Using Matlab and C," by
% Schilling and Harris, Brooks Cole Publishing (2000).
%
% File generated by J. R. White, UMass-Lowell (Aug. 2003)
%
function [x,k] = sr(A,b,x,alpha,tol,M)
%
% set some iteration parameters
k = 0; rmax = 1;
%
% check for zeros along diagonal
n = length(x);
for i = 1:n
if abs(A(i,i)) < eps
disp(' WARNING: Check A matrix for a zero along the diagonal!!!')
disp(' ')
return;
end
end
%
% perform iteration
while (k < M & rmax >= tol)
for i = 1:n;
d = A(i,i);
x(i) = (1 - alpha)*x(i) + alpha*b(i)/d;
for j = 1:n
if j ~= i
x(i) = x(i) - alpha*A(i,j)*x(j)/d;
end
end
end
rmax = max(abs(b - A*x));
k = k + 1;
end
%
% end of function