Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: while loop problem
Date: Sat, 8 Mar 2008 00:33:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 56
Message-ID: <fqsmrt$s9p$1@fred.mathworks.com>
References: <fqsk9e$73s$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1204936381 28985 172.30.248.38 (8 Mar 2008 00:33:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 8 Mar 2008 00:33:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:456061



"Steve " <steveDEL.bachmeierDEL@yahoo.com> wrote in message <fqsk9e
$73s$1@fred.mathworks.com>...
> I'm trying to grow a vector using 'while' and there seems 
> to be a sort of instability somewhere.  Sometimes (but not 
> always), it does not stop looping when I think it should.  
> I have copied part of the code below.
> 
> On several equations, plotting Kmax_net shows that Kmax_net 
> does become larger than Kc.  There is always an immediate 
> drop off at this point, and Kmax_net starts growing again.  
> This may happen two or three times before it finally goes 
> above Kc and the loop ends.  Why isn't the loop terminating 
> the first time through?  Any suggestions?  Thanks!
> 
> ----------------------
> 
> while Kmax_net(i)<Kc
>     i=i+1;
>     % calculate crack propagation
>     Kmax_app(i)=1.12*Smax*sqrt(pi*a(i-1));
>     Kmin_app(i)=1.12*Smin*sqrt(pi*a(i-1));
>     Kresidual(i)=interp1(KresTable(:,1),KresTable(:,2),a(i-
> 1));
>     Kmax_net(i)=Kmax_app(i)+Kresidual(i);
>     if Kmax_net(i)<0
>         Kmax_net(i)=0;
>     else
>         Kmax_net(i)=Kmax_net(i);
>     end
>     Kmin_net(i)=Kmin_app(i)+Kresidual(i);
>     if Kmin_net(i)<0
>         Kmin_net(i)=0;
>     else
>         Kmin_net(i)=Kmin_net(i);
>     end
>     dKnet(i)=Kmax_net(i)-Kmin_net(i);
>     Rnet(i)=Kmin_net(i)/Kmax_net(i);
>     da(i)=C*(dKnet(i))^M/((1-Rnet(i))*Kc-dKnet(i));
>     a(i)=a(i-1)+da(i);
> end
> 
> ------------------------------

  Here is my best guess as to your difficulty, Steve.  The while-loop is actually 
stopping each time it detects a Kmex_net above Kc, just as you planned.  
However, I am guessing you haven't cleared out this vector from previous 
runs with it, so a plot gives you two or more pieces of Kmex_net's history all 
in the same plot.  That would also account for the abrupt jump down you see 
at each such transition from one run to a previous one.

  In any case, whenever it is at all possible you should pre-allocate a vector 
rather than letting it grow one element at a time, which greatly extends 
execution time.

Roger Stafford