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...

  • 3
    $\begingroup$ 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 $\endgroup$
    – rm -rf
    Mar 21 '12 at 16:56
  • 8
    $\begingroup$ @R.M Hmmm, I'm not seeing a Duck function... $\endgroup$ Mar 21 '12 at 19:43
  • $\begingroup$ @Brett Champion: the best (only) Mathematica humor I've ever read? $\endgroup$
    – murray
    Mar 21 '12 at 19:53
  • 3
    $\begingroup$ @Brett Champion A very clear Duck function is in the responses to mathematica.stackexchange.com/questions/2897/… $\endgroup$ Mar 29 '12 at 22:24
  • 1
    $\begingroup$ 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. $\endgroup$
    – 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".


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.