12

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?

3 Answers 3

11

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]
Sign up to request clarification or add additional context in comments.

2 Comments

While Check does not seem to catch NDSolve::ndsz warnings, the hint about Quiet was very helpful.
@Kasper: Glad I could help. Can you give an example where Check fails to catch the NDSolve warning?
5

Using Quiet and Check together works:

Quiet[Check[Table[1/Sin[x], {x, 0, \[Pi], \[Pi]}], $Failed]]

1 Comment

Sorry, didn't see Simon's earlier answer coming in. Reminder to self: refresh before posting
3

Perhaps you wish to redirect messages? This is copied almost verbatim from that page.

stream = OpenWrite["msgtemp.txt"];

$Messages = {stream};

1/0

FilePrint["msgtemp.txt"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.