Path: news.mathworks.com!not-for-mail
From: "Ender " <jr147@msstate.edu>
Newsgroups: comp.soft-sys.matlab
Subject: Re: j not equal to i
Date: Fri, 22 Jan 2010 21:29:20 +0000 (UTC)
Organization: Mississippi State University
Lines: 112
Message-ID: <hjd5bg$e9s$1@fred.mathworks.com>
References: <hjck1s$25s$1@fred.mathworks.com> <2047bb41-198b-4a35-9003-bc68cc89418d@l11g2000yqb.googlegroups.com> <32ce2567-140e-46f3-93c8-88c6df54643e@l11g2000yqb.googlegroups.com>
Reply-To: "Ender " <jr147@msstate.edu>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1264195760 14652 172.30.248.37 (22 Jan 2010 21:29:20 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 22 Jan 2010 21:29:20 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1035766
Xref: news.mathworks.com comp.soft-sys.matlab:601020

How do I fix this? I had read in the MATLAB help that while adds 1 to the element when doing a while limit. (e.g. eps example) So I tried commenting out the k+1 but this put the code into an infinite loop. I don't think I am supposed to comment out the k+1, but after reading your post how could I tell MATLAB to not add elements to the k vector?

--Ender--


Nathan <ngreco32@gmail.com> wrote in message <32ce2567-140e-46f3-93c8-88c6df54643e@l11g2000yqb.googlegroups.com>...
> On Jan 22, 10:38 am, "Ender " <jr...@msstate.edu> wrote:
> > Thanks, I will try your example.
> >
> >  In my current code I cannot figure out why MATLAB is not continuing my while loop. At the end of the code and outside of the while loop I wrote a code to display that the maximum number of iterations have been exceeded. However, my code does this at k=2 instead of k=100. I think it is something simple that I am leaving out or forgetting, but I can not figure out what it is.
> >
> > Here is my code:
> >
> > % Set the number of iterations to perform
> > N=100;
> >
> > % Set the Tolerance
> > TOL=10e-3;
> >
> > % Initial Vector
> > XO=[0;0;0];
> >
> > % The Matrix A
> > A=[3,-1,1;3,6,2;3,3,7];
> >
> > % The Matrix b
> > b=[1;0;4];
> >
> > % % Returns the two integers in two separate variables.
> > [n,n]=size(A);
> >
> > % Step 1
> > k=1:N;
> >
> > % Step 2
> > while (k <= N)
> >       % initialize s1a + s1b
> >       s1a=0;
> >       s1b=0;
> >       % Step 3
> >       for i=1:n
> >
> >           % To account for not using the j=i in the algorithm
> >           for j=1:i-1
> >               s1a=s1a+(A(i,j))*XO(j)+b(i);
> >           end
> >           for j=i+1:n
> >               s1b=s1b+(A(i,j))*XO(j)+b(i);
> >           end
> >
> >           % Add the sections before & after j not equal to i
> >           sT=s1a+s1b;
> >
> >           x(i)=-sT/A(i,i);
> >
> >           % Step 4
> >           if abs(x(i)-XO)<TOL
> >              display(' The Procedure was successful')
> >              % Output x
> >              x(i);
> >              % Stop the Algorithm
> >              return
> >           end
> >       end      
> >          % Step 5
> >          k=k+1;
> >
> >       % Step 6
> >       % Update XO
> >       for i=1:n
> >           XO(i)=x(i);
> >       end
> > end  
> >
> > % Step 7
> > % Ouput to screen
> > display('Maximum number of iterations exceeded')
> >
> > ImageAnalyst <imageanal...@mailinator.com> wrote in message <2047bb41-198b-4a35-9003-bc68cc894...@l11g2000yqb.googlegroups.com>...
> > >  --Ender--
> > > I thought of these two ways off the top of my head.
> > > They're not necessarily better than splitting it up into two for loops
> > > which is I think what you were hinting at.
> >
> > > j=5;
> > > % Method 1
> > > for k = [1:(j-1), (j+1):10]
> > >    k % Show index in command window.
> > > end
> > > % Method 2
> > > for m = 1:10
> > >    if m ~= j
> > >            m % Show index in command window.
> > >    end
> > > end
> >
> >
> 
> Two lines of code make your loop end early:
> 
> k=1:N;
> AND
> k=k+1;
> 
> The first line is an array from 1 to 100.
> The second line shown adds 1 to every element of k.
> The while loop tests to see if EVERY element in k is less than or
> equal to N.
> When you add 1 to k, it ranges from 2 to 101. 101 is not less than
> 100, therefore it exits.
> 
> -Nathan