捕获 Mathematica 警告/错误而不显示它们

发布于 2024-10-22 00:59:14 字数 478 浏览 2 评论 0原文

我在 Mathematica 中遇到涉及 NDSolve 的问题,我使用不同的参数值运行了多次。对于其中一些值,解会导致奇点,并且 NDSolve 会发出 NDSolve::ndsz 警告或其他相关警告。

我只想捕捉这些警告,抑制它们的显示,并跟踪这些特定参数值出现问题的事实。我想到了以下选项(这两个选项都没有真正起作用):

  1. 我知道我可以使用 Check 确定命令是否导致警告或错误。但是,仍然会显示警告。如果我使用 Off 将其关闭,Check 也无法报告警告。
  2. 可以使用 EventLocator 方法停止 NDSolve,因此我可以检查函数或其导数的值是否非常大,并在这种情况下停止计算。然而,在实践中,这仍然会时不时地产生警告,大概是因为步长有时可能太大,以至于 NDSolve 警告在我的 Event 发生之前触发。

还有其他建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屋檐 2024-10-29 00:59:14

如果您将 CheckQuiet 包装在一起,那么我相信一切都应该按您的意愿进行。例如,您可以抑制特定消息 Power::indet

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

但仍显示其他消息

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]

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]
顾挽 2024-10-29 00:59:14

一起使用 Quiet 和 Check 可以发挥作用:

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

Using Quiet and Check together works:

Quiet[Check[Table[1/Sin[x], {x, 0, \[Pi], \[Pi]}], $Failed]]
苏佲洛 2024-10-29 00:59:14

也许您希望重定向消息?这几乎是从该页面逐字复制的。

stream = OpenWrite["msgtemp.txt"];

$Messages = {stream};

1/0

FilePrint["msgtemp.txt"]

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"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文