| 1 |
% function [sol,resnrm]=jacobi(A,x0,b,omega,nits,[varargin]) |
| 2 |
% |
| 3 |
% Runs SOR-Jacobi iteration on the matrix A to solve |
| 4 |
% the equation Ax=b. Assumes A is sparse. Uses |
| 5 |
% initial guess x0. |
| 6 |
% -------------- Parameters --------------- |
| 7 |
% A = Matrix of the system. |
| 8 |
% x0 = Initial guess |
| 9 |
% b = RHS of system |
| 10 |
% D = Matrix diagonal |
| 11 |
% omega = Over-relaxation parameter |
| 12 |
% nits = Iteration cap |
| 13 |
% -------------- Outputs ------------------ |
| 14 |
% sol = Solution vector from SOR-Jacobi |
| 15 |
% resrnm= Norm of the residual from the solution sol |
| 16 |
% |
| 17 |
|
| 18 |
%Use jacobi() as "solve" function for MatlabSmoother |
| 19 |
% CMS: First time through |
| 20 |
function [sol] = jacobi(A, x0, b, D) |
| 21 |
omega = 0.9; |
| 22 |
nits = 5; |
| 23 |
% CMS: Later interface |
| 24 |
%function [sol]=jacobi2(A,x0,b,D,omega,nits) |
| 25 |
|
| 26 |
% Initializations |
| 27 |
sol = x0; |
| 28 |
n = size(A, 1); |
| 29 |
|
| 30 |
% Compute Initial Residual Norm |
| 31 |
resnrm(1) = norm(b - A * x0); |
| 32 |
|
| 33 |
for I = 1:nits, |
| 34 |
%disp(['MATLAB: Running Jacobi iteration #', I]); |
| 35 |
% Next iterate |
| 36 |
sol = sol + omega * ((b - A * sol) ./ D); |
| 37 |
end |
| 38 |
end |