       Re: check inside a loop?

• To: mathgroup at smc.vnet.net
• Subject: [mg80767] Re: check inside a loop?
• From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
• Date: Sat, 1 Sep 2007 00:26:37 -0400 (EDT)
• Organization: The Open University, Milton Keynes, UK
• References: <fb82ov\$7ka\$1@smc.twtelecom.net>

```Jeremy Price 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.

It is possible that you are using a version prior 5.2, because none of
your examples, even with the typo for Pi fixed, succeed in producing an
error message.

In:= f[x_] = 1/Sin[2 pi x]
For[i = 1, i < 1000, i++, f[i]]

Out= Csc[2 pi x]

In:= f[x_] = 1/Sin[2 Pi x]
For[i = 1, i < 1000, i++, f[i]]

Out= Csc[2 \[Pi] x]

In:= f

Out= ComplexInfinity

> 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]
> 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?

You can use any function as second argument for Check. For instance,
Print or Sow will do it:

In:= g[n_] = 1/Mod[5, n]

Out= 1/Mod[5, n]

In:= For[i = 1, i < 10, i++,
Check[g[i], Print["Division by zero for i = ", i], Power::infy]]

During evaluation of In:= Power::infy: Infinite expression 1/0 \
encountered. >>

During evaluation of In:= Division by zero for i = 1

During evaluation of In:= Power::infy: Infinite expression 1/0 \
encountered. >>

During evaluation of In:= Division by zero for i = 5

In:= Reap[
For[i = 1, i < 10, i++, Check[g[i], Sow[i], Power::infy]]][]

During evaluation of In:= Power::infy: Infinite expression 1/0 \
encountered. >>

During evaluation of In:= Power::infy: Infinite expression 1/0 \
encountered. >>

Out= {{1, 5}}

--
Jean-Marc

```

• Prev by Date: Re: check inside a loop?
• Next by Date: Re: Animation of a rotating object. (Mathematica 6)
• Previous by thread: Re: check inside a loop?
• Next by thread: Re: check inside a loop?