Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch Mathematica warnings/errors without displaying them

I have a problem involving NDSolve in Mathematica, which I run multiple times with different values of the parameters. For some of these values, the solution results in singularities and NDSolve warns with NDSolve::ndsz or other related warnings.

I would simply like to catch these warnings, suppress their display, and just keep track of the fact that a problem occurred for these particular values of the parameters. I thought of the following options (neither of which really do the trick):

  1. I know I can determine whether a command has resulted in a warning or error by using Check. However, that will still display the warning. If I turn it off with Off the Check fails to report the warning too.
  2. It is possible to stop NDSolve using the EventLocator method, so I could check for very large values of the function or its derivatives and stop evaluation in that case. However, in practice, this still produces warnings from time to time, presumably because the step size can sometimes be so large that the NDSolve warning triggers before my Event has taken place.

Any other suggestions?

like image 501
Kasper Peeters Avatar asked Mar 15 '11 09:03

Kasper Peeters


2 Answers

Using Quiet and Check together works:

Quiet[Check[Table[1/Sin[x], {x, 0, \[Pi], \[Pi]}], $Failed]]
like image 128
Sjoerd C. de Vries Avatar answered Sep 17 '22 15:09

Sjoerd C. de Vries


If you wrap the Check with Quiet then I believe that everything should work as you want. For example, you can suppress the specific message Power::indet

In[1]:= Quiet[Check[0^0,err,Power::indet],Power::indet]
Out[1]= err

but other messages are still displayed

In[2]:= Quiet[Check[Sin[x,y],err,Power::indet],Power::indet]
During evaluation of In[2]:= Sin::argx: Sin called with 2 arguments; 1 argument is expected. >>
Out[2]= Sin[x,y]
like image 24
Simon Avatar answered Sep 18 '22 15:09

Simon