Thread Subject: while loop problem

Subject: while loop problem

From: Steve

Date: 7 Mar, 2008 23:49:02

Message: 1 of 11

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

------------------------------

Subject: while loop problem

From: us

Date: 8 Mar, 2008 00:14:03

Message: 2 of 11

"Steve ":
<SNIP obviously a CSSM newbie...

> 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

this is NOT the way to ask for help in this lovely NG...

1) nobody will ever be able to guess the contents of your
(fancily - or shall we call it: tediously - christened)
variables...
2) your snipplet is badly wrapped, which means: CSSMers
cannot easily copy/paste it for further examinations... and
NOBODY has/will ever spend the time to re-type your
particular stuff into an m-scriplet as all of us have to
finish some nobel-prize winning nature submissions and - by
nature - are not REALLY interested in your problem...

conclusion:
come up with the most frugal(!) code skeleton that
reproduces your problem/error - and (most likely) thou
shallst (sic!) be enlightened...
otherwise, all you get is guess-work - and corrections on
your part - and more guess-work - and people getting
(slightly) annoyed...

sorry...
us

Subject: while loop problem

From: Roger Stafford

Date: 8 Mar, 2008 00:33:01

Message: 3 of 11

"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

Subject: while loop problem

From: us

Date: 8 Mar, 2008 00:45:09

Message: 4 of 11

"Roger Stafford":
<SNIP verification of a previous post...

> Here is my best guess...

as i said: guess-work...

us

Subject: while loop problem

From: Steve

Date: 8 Mar, 2008 01:00:19

Message: 5 of 11

hmm, I thought that also, but somewhere along the way
convinced myself that it is not the problem. But seeing as
how I can't recall how I managed to convince myself of that,
I'll have to go back and ensure.

As for preallocating, is it best just to preallocate as more
zeros than I think I'll need?

Thanks!

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
wrote in message <fqsmrt$s9p$1@fred.mathworks.com>...
> "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
>

Subject: while loop problem

From: Steve

Date: 8 Mar, 2008 01:03:02

Message: 6 of 11

I know, I know, I was in a rush finishing MY Nobel
prize-winning Nature submission and wanted to get it up just
in case. I'll re-post. Thanks!

"us " <us@neurol.unizh.ch> wrote in message
<fqslob$jgb$1@fred.mathworks.com>...
> "Steve ":
> <SNIP obviously a CSSM newbie...
>
> > 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
>
> this is NOT the way to ask for help in this lovely NG...
>
> 1) nobody will ever be able to guess the contents of your
> (fancily - or shall we call it: tediously - christened)
> variables...
> 2) your snipplet is badly wrapped, which means: CSSMers
> cannot easily copy/paste it for further examinations... and
> NOBODY has/will ever spend the time to re-type your
> particular stuff into an m-scriplet as all of us have to
> finish some nobel-prize winning nature submissions and - by
> nature - are not REALLY interested in your problem...
>
> conclusion:
> come up with the most frugal(!) code skeleton that
> reproduces your problem/error - and (most likely) thou
> shallst (sic!) be enlightened...
> otherwise, all you get is guess-work - and corrections on
> your part - and more guess-work - and people getting
> (slightly) annoyed...
>
> sorry...
> us

Subject: while loop problem

From: Roger Stafford

Date: 8 Mar, 2008 01:21:02

Message: 7 of 11

"us " <us@neurol.unizh.ch> wrote in message <fqsnik$6kj
$1@fred.mathworks.com>...
> as i said: guess-work...
> us
----------
  I wouldn't be too hard on him, Urs. It's the kind of matlab mistake I've made
myself more times than I'd like to admit. Moreover, it it clear that he had no
idea what he had forgotten to do, so his presentation was perhaps the best that
could be made under the circumstances. He unknowingly revealed in his
description and code, the very clues that (hopefully) allow the mystery to be
cleared up.

Roger Stafford

Subject: while loop problem

From: Roger Stafford

Date: 8 Mar, 2008 01:39:03

Message: 8 of 11

"Steve " <steveDEL.bachmeierDEL@yahoo.com> wrote in message <fqsof3
$gkl$1@fred.mathworks.com>...
> hmm, I thought that also, but somewhere along the way
> convinced myself that it is not the problem. But seeing as
> how I can't recall how I managed to convince myself of that,
> I'll have to go back and ensure.
>
> As for preallocating, is it best just to preallocate as more
> zeros than I think I'll need?

 Yes, Steve, I would allocate an all zero vector long enough to exceed any
conceivable set of circumstances, then proceed with the while-loop one entry
at a time (provided there isn't some vectorized method of getting around the
loop), and when you emerge from the loop, finally lop off the unused portion
of the vector, making use of the 'i' index. That way you will not be misled by
a plot of the vector. If it happens that you did after all underestimate the
needed length, the computation can still proceed successfully though of
course a lot slower.

Roger Stafford

Subject: while loop problem

From: us

Date: 8 Mar, 2008 01:40:24

Message: 9 of 11

"Roger Stafford":
<SNIP he that is without sin among you, let him first cast
a stone at her - or was it a CSSM newbie...

> I wouldn't be too hard on him, Urs. It's the kind of
matlab mistake I've made myself more times than I'd like to
admit...

but then, dear <roger>, you (most obviously) have improved
grandly(!) over the past forty years!...

urs with a :-) and greetings from zurich

Subject: while loop problem

From: Roger Stafford

Date: 8 Mar, 2008 02:16:02

Message: 10 of 11

"us " <us@neurol.unizh.ch> wrote in message <fqsqq8$cps
$1@fred.mathworks.com>...
> "Roger Stafford":
> <SNIP he that is without sin among you, let him first cast
> a stone at her - or was it a CSSM newbie...
>
> > I wouldn't be too hard on him, Urs. It's the kind of
> matlab mistake I've made myself more times than I'd like to
> admit...
>
> but then, dear <roger>, you (most obviously) have improved
> grandly(!) over the past forty years!...
>
> urs with a :-) and greetings from zurich
----------
  I thank you for that Urs. However, you are overly kind. As an octogenarian of
82, I'm afraid my days of improvement are long over, and it is becoming more
and more difficult to keep up with all you young guys.

Roger Stafford (with greetings from Orange, California)

Subject: while loop problem

From: suren

Date: 28 Oct, 2011 13:52:14

Message: 11 of 11

post the full code..then it can be done..need more details.or if you dont want to post it in public domain fix it yourself

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
scolding us 7 Mar, 2008 19:15:02
if Steve 7 Mar, 2008 18:50:24
loop Steve 7 Mar, 2008 18:50:24
while Steve 7 Mar, 2008 18:50:24
rssFeed for this Thread

Contact us at files@mathworks.com