Introspection of messages generated in Mathematica
Is there any way to get at the actual messages generated during the evaluation of an expression in Mathematica? Say I'm numerically solving an ODE and it blows up, like so
In[1] := sol = ND开发者_运维知识库Solve[{x'[t] == -15 x[t], x[0] == 1}, x, {t, 0, 1},
Method -> "ExplicitEuler"];
In this case, I'll get the NDSolve::mxst
error, telling me the maximum number of 10000 steps was reached at t == 0.08671962566152185
. Now, if I look at the $MessageList
variable, I only receive the message name; in particular, the information about the value of t
where NDSolve
decided to quit has been lost.
Now, I can always get that information from sol
using the InterpolatingFunctionDomain
function from one of the standard add-on packages, but if I can somehow pull it out of the message, it would be quite helpful.
You might be able to use $MessagePrePrint to set up a function which would store away each of the messages for later retrieval.
I don't know if this will work, but if the only thing you want to know are the values of specific parameters at the point of error then a kludgy way of getting them would be to define those variables with dummy values globally. This works with loop counters, but I don't know if it works from within NDSolve
. Another kludge would be to make t Dynamic and have an evaluated cell with t.
A more elegant (and probably the correct) approach would be to use Reap and Sow.
精彩评论