<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264798</link>
    <title>MATLAB Central Newsreader - SOR Function Error</title>
    <description>Feed for thread: SOR Function Error</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Tue, 03 Nov 2009 02:38:03 -0500</pubDate>
      <title>SOR Function Error</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264798#691621</link>
      <author>CJ </author>
      <description>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:&lt;br&gt;
??? Attempted to access xold(2); index out of bounds because numel(xold)=1.&lt;br&gt;
&lt;br&gt;
Error in ==&amp;gt; SOR at 17&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x = (1-w)*xold(j) + w*(C(j, :) * x + r(j));&lt;br&gt;
&lt;br&gt;
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:&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
function x = SOR(A, b, x0, w, tol, max_it)&lt;br&gt;
[ n, m ] = size(A);   x = x0;   C = -A;&lt;br&gt;
for i = 1:n&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;C(i, i) = 0;&lt;br&gt;
end&lt;br&gt;
for i = 1:n&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;C(i,1:n) = C(i,1:n)/A(i,i);&lt;br&gt;
end&lt;br&gt;
for i = 1:n&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r(i,1) = b(i)/A(i,i);&lt;br&gt;
end&lt;br&gt;
i = 1;&lt;br&gt;
disp('     i       x1       x2       x3       ...')&lt;br&gt;
while (i &amp;lt;= max_it)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;xold = x;         % save old solution from previous step&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for j = 1: n&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x(j) = (1-w)*xold(j) + w*(C(j, :) * x + r(j));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if norm(xold -x) &amp;lt;= tol,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disp('SOR method converged'); return;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disp([i       x' ])&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;i = i +1;&lt;br&gt;
end&lt;br&gt;
disp('SOR method did not converge');</description>
    </item>
    <item>
      <pubDate>Tue, 03 Nov 2009 15:00:54 -0500</pubDate>
      <title>Re: SOR Function Error</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264798#691733</link>
      <author>Steven Lord</author>
      <description>&lt;br&gt;
&quot;CJ &quot; &amp;lt;Jinkae@gmail.com&amp;gt; wrote in message &lt;br&gt;
news:hco52b$35a$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; Hey guys I was trying to check some Successive Over Relaxation problems I &lt;br&gt;
&amp;gt; did by hand using a function given in my numerical methods text and &lt;br&gt;
&amp;gt; although I used the exact SAME code given in the text (I've checked and &lt;br&gt;
&amp;gt; rechecked) I get the following error any time I try to use it:&lt;br&gt;
&amp;gt; ??? Attempted to access xold(2); index out of bounds because &lt;br&gt;
&amp;gt; numel(xold)=1.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Error in ==&amp;gt; SOR at 17&lt;br&gt;
&amp;gt;        x = (1-w)*xold(j) + w*(C(j, :) * x + r(j));&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I've only been using MatLab for a month, so I'm not sure what this means &lt;br&gt;
&amp;gt; exactly, but any advice would be greatly appreciated. Here is the book's &lt;br&gt;
&amp;gt; code:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; function x = SOR(A, b, x0, w, tol, max_it)&lt;br&gt;
&amp;gt; [ n, m ] = size(A);   x = x0;   C = -A;&lt;br&gt;
&amp;gt; for i = 1:n&lt;br&gt;
&amp;gt;    C(i, i) = 0;&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; for i = 1:n&lt;br&gt;
&amp;gt;    C(i,1:n) = C(i,1:n)/A(i,i);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; for i = 1:n&lt;br&gt;
&amp;gt;    r(i,1) = b(i)/A(i,i);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; i = 1;&lt;br&gt;
&amp;gt; disp('     i       x1       x2       x3       ...')&lt;br&gt;
&amp;gt; while (i &amp;lt;= max_it)&lt;br&gt;
&amp;gt;    xold = x;         % save old solution from previous step&lt;br&gt;
&amp;gt;    for j = 1: n&lt;br&gt;
&amp;gt;        x(j) = (1-w)*xold(j) + w*(C(j, :) * x + r(j));&lt;br&gt;
&lt;br&gt;
Apparently when you called this function, you passed in a scalar (a 1-by-1 &lt;br&gt;
array) for the x0 input.  The first line of code assigned that value into &lt;br&gt;
the variable x, and the &quot;save old solution&quot; link just before this loop &lt;br&gt;
assigned that value into the variable xold.&lt;br&gt;
&lt;br&gt;
Now the FOR loop executes.  When j = 1, the line of code that's executed is &lt;br&gt;
essentially:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x(1) = (1-w)*xold(1) + w*(C(1, :) * x + r(1));&lt;br&gt;
&lt;br&gt;
and everything's fine, since xold and r have at least 1 element and C has at &lt;br&gt;
least 1 column.&lt;br&gt;
&lt;br&gt;
Now when j = 2, the line of code that's executed is:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x(2) = (1-w)*xold(2) + w*(C(2, :) * x + r(2));&lt;br&gt;
&lt;br&gt;
Uh-oh.  Now the code is asking for the second element of xold, but xold only &lt;br&gt;
has one element.  That's when you receive the error message you wrote above.&lt;br&gt;
&lt;br&gt;
Either you need to update xold in the loop, or you need to specify a larger &lt;br&gt;
x0, or you need to make some other fix.  That's left as an exercise for you &lt;br&gt;
to determine.  When you determine what you need to do, you may want to add &lt;br&gt;
some error checking at the top of the function to make sure that everything &lt;br&gt;
has the sizes, shapes, etc. you expect them to have before you actually &lt;br&gt;
start computing.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Steve Lord&lt;br&gt;
slord@mathworks.com&lt;br&gt;
comp.soft-sys.matlab (CSSM) FAQ: &lt;a href=&quot;http://matlabwiki.mathworks.com/MATLAB_FAQ&quot;&gt;http://matlabwiki.mathworks.com/MATLAB_FAQ&lt;/a&gt; </description>
    </item>
  </channel>
</rss>

