# Handling failed FindRoot calls

I want to handle FindRoot calls which did not converge (e.g "thrown" error message FindRoot::cvmit)

sols = Table[FindRoot[some equations dependant in a, startSolutions],
{a, minA, maxA, stepSize}]


For some, FindRoot will not converge (3000 iterations). Is there a way to define the entry of sol in this case to e.g. NAN?

EDIT: Is there a possibility to break further evaluation of the Table call, if Check[] returns a defined value?

Break[] is only allowed in For[], While[] and Do[] - so do I have to encapsulate my FindRoot call in an extra loop and construct the table by hand?

e.g.: (** apply solver and Check[] if jacobian gets singular **)

sols = {};

For[Alpha = minAlpha, Alpha <= maxAlpha, Alpha += stepSize, Append[sols, Check[solver, {Alpha, Break[]}, {FindRoot::jsing}]]];

Unfortunately this does not work...

-
Well, the wording of your question has part of a potential solution... what do you do when someone Throws something at you? You ... ;). Perhaps you could try working with this new information and try to update your question with what you've tried and where you're getting stuck? You might also want to take a look at Check – R. M. Mar 21 '12 at 16:56
@R.M Hmmm, I'm not seeing a Duck function... – Brett Champion Mar 21 '12 at 19:43
@Brett Champion: the best (only) Mathematica humor I've ever read? – murray Mar 21 '12 at 19:53
@Brett Champion A very clear Duck function is in the responses to mathematica.stackexchange.com/questions/2897/… – Daniel Lichtblau Mar 29 '12 at 22:24
Please ask a separate question on breaking from a Table instead of editing this old question. It's not possible to break from a Table and preserve all results comuted so far (it's possible to break using Throw and discard results). I'd recommend using Sow/Reap in a Do loop instead. – Szabolcs Apr 19 '12 at 15:23

You can use Check to return an alternate result and use Quiet to avoid the messages that you are expecting anyway. Here's an example:

Quiet[Table[Check[x /. FindRoot[x^2 - c, {x, 1}],
"NaN", {FindRoot::lstol, FindRoot::jsing}], {c, -5, 5}],
{FindRoot::lstol, FindRoot::jsing}]

(* Out:
{"NaN", "NaN", "NaN", "NaN", "NaN", 7.45058*10^-9,
1., 1.41421, 1.73205, 2., 2.23607}
*)


One important note is to make sure that the Check is inside the Quiet, and not the other way around. If Quiet is inside Check the message will be "quieted" before it is seen by Check and so you won't get "NaN".

-