Re: check inside a loop?

*To*: mathgroup at smc.vnet.net*Subject*: [mg80777] Re: check inside a loop?*From*: David Bailey <dave at Remove_Thisdbailey.co.uk>*Date*: Sat, 1 Sep 2007 00:31:45 -0400 (EDT)*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. > > 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? > > The message generation mechanism in Mathematica has always seemed rather clumsy to me - as the example in the question shows. Here is the best I could come up with: nmessages=Length[$MessageList]; k=100; While[k>0, If[Mod[k,20]==0,tmp=Sin[k,2]]; If[nmessages<Length[$MessageList],Print["k=",k]]; nmessages=Length[$MessageList]; k--; ]; Sin::argx: Sin called with 2 arguments; 1 argument is expected. >> k=100 Sin::argx: Sin called with 2 arguments; 1 argument is expected. >> k=80 Sin::argx: Sin called with 2 arguments; 1 argument is expected. >> General::stop: Further output of Sin::argx will be suppressed during this calculation. >> k=60 The While loop has been set up to generate errors at 100,80,60,40,20 - except that errors get suppressed after a few repetitions. At the bottom of the loop, I test to see if the list of all the messages has increased, and print out the loop variable if it has. This does not get the answer into the message as such, but it does give you the information you need. Perhaps someone from Wolfram can suggest a better method - or acknowledge that some extra functionality is needed here. David Bailey http://www.dbaileyconsultancy.co.uk