MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: check inside a loop?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg80828] Re: [mg80736] check inside a loop?
  • From: bsyehuda at gmail.com
  • Date: Mon, 3 Sep 2007 06:15:39 -0400 (EDT)
  • References: <200708310341.XAA07769@smc.twtelecom.net>

Hi,
For the specific example you posted, first using f[x_]=  rather then f[x_]:=
forces Mathematica to evaluate the right hand side of the definition at the
time of definition of f, and it is replaced by Csc[x] which does not return
an error message. It returns ComplexInfinity without the message. In
addition, use Pi with capital P
Next, For and Do loops DO NOT return values. you need to use some sort of
breaking mechanism or collect the values of i when the error is generated,
for example, Catch and Throw (breaks at the first error) or Reap and Sow
(collect all errors)

Reap[Do[If[Check[f[i], "zzz"] == "zzz", Sow[i]], {i, 0, 2,
    1/4}]] // Rest

or
Catch[Do[If[Check[f[i], "zzz"] == "zzz", Throw[i]], {i, 0, 2, 1/4}]]

yehuda

On 8/31/07, Jeremy Price < cfgauss at u.washington.edu> wrote:
>
> I have a large loop that is ndsolving/nintegrating a bunch of things, and
> a
> lot of the results give me various errors due to the equations not being
> very nice.  I'd like to have a way to check what values of my paramaters
> are
> causing the errors, but I can't find a way to do that inside a loop.
>
> For example, if I have something like,
> f[x_] = 1/Sin[2 pi x]
>
> For[ i=1, i < 1000, i++,
> f[i]
> ]
>
> I'm going to get a lot of "Power::infy: Infinite expression 1/0
> encountered." errors.  I'd like to see what values of i these occur at.
>
> I've tried something like
>
> For[ i=1, i<1000, i++,
> Check[ f[i] , i]
> ]
>
> But this just returns "Power::infy: Infinite expression 1/0 encountered."
> errors without the i vaule, which is different than I get by evaluating
> something like
> Check[ f[0],0]
> Which returns:
> Power::infy: Infinite expression 1/0 encountered.
> 0
>
> Is there any way I can get it to return the index that the error occured
> at
> for every error that occurs inside of the loop?
>
>
>



  • Prev by Date: Re: Re: Rule-based programming: declare variables to be a number
  • Next by Date: Re: Re: rotating rings illusion
  • Previous by thread: Re: check inside a loop?
  • Next by thread: Mouse changes when Tab label is Text[] vs normal text. Why?