Path: news.mathworks.com!not-for-mail
From: "CJ " <Jinkae@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: SOR Function Error
Date: Tue, 3 Nov 2009 02:38:03 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 34
Message-ID: <hco52b$35a$1@fred.mathworks.com>
Reply-To: "CJ " <Jinkae@gmail.com>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1257215883 3242 172.30.248.35 (3 Nov 2009 02:38:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 3 Nov 2009 02:38:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2073857
Xref: news.mathworks.com comp.soft-sys.matlab:581949


Hey guys I was trying to check some Successive Over Relaxation problems I did by hand using a function given in my numerical methods text and although I used the exact SAME code given in the text (I've checked and rechecked) I get the following error any time I try to use it:
??? Attempted to access xold(2); index out of bounds because numel(xold)=1.

Error in ==> SOR at 17
        x = (1-w)*xold(j) + w*(C(j, :) * x + r(j));

I've only been using MatLab for a month, so I'm not sure what this means exactly, but any advice would be greatly appreciated. Here is the book's code:


function x = SOR(A, b, x0, w, tol, max_it)
[ n, m ] = size(A);   x = x0;   C = -A;
for i = 1:n
    C(i, i) = 0;
end
for i = 1:n
    C(i,1:n) = C(i,1:n)/A(i,i);
end
for i = 1:n
    r(i,1) = b(i)/A(i,i);
end
i = 1;
disp('     i       x1       x2       x3       ...')
while (i <= max_it)
    xold = x;         % save old solution from previous step
    for j = 1: n
        x(j) = (1-w)*xold(j) + w*(C(j, :) * x + r(j));
    end
    if norm(xold -x) <= tol,
        disp('SOR method converged'); return;
    end
    disp([i       x' ])
    i = i +1;
end
disp('SOR method did not converge');