tags:

views:

81

answers:

3

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 = NDSolve[{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.

A: 

Hi

Not really an answer to your question, but: you have assigned the result to your variable sol. When I run it I find that:

In[2]:= sol

Out[2]= {{x->InterpolatingFunction[{{0.,0.0867196}},<>]}}

and there's the value of t when evaluation blows up.

Is that any use to you ?

Supplement

Ok, then have a look at the online documentation for $Messages. This shows you how to redirect messages to a file of your choice.

High Performance Mark
Not really. I'll clarify the question.
Pillsy
+3  A: 

You might be able to use $MessagePrePrint to set up a function which would store away each of the messages for later retrieval.

ragfield
So far this looks like the best bet. The documentation for `$MessagePrePrint` suggests using `Sow`/`Reap` for that purpose.
Pillsy
I tried the `Sow`/`Reap` solution with `$MessagePrePrint`. It worked OK, but ultimately I found introspecting the returned `InterpolatingFunction` objects was considerably less cumbersome.
Pillsy
+1  A: 

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.

Timo