# How to terminate a Do Loop when encounter FindRoot::lstol warning

While finding the roots of a system of non-linear equations, I am encountering the warning "FindRoot::lstol: The line search decreased the step size to within tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may need more than MachinePrecision digits of working precision to meet these tolerances.". I want to terminate/break the loop when this warning comes. Any example to explain the code will be fine. I cannot post my code here because the Do loop is very convoluted and posting 5 lines of code will not make sense. Thank You for your help.

• Welcome to Mathematica.SE, sslucifer! I suggest the following: 1) Take the tour and check the faqs. 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. Apr 15, 2020 at 3:09
• It's usually easier to help with a concrete example. See if you can come up with a minimal working example. Apr 15, 2020 at 3:10

at = {I, 3, -I};
Do[
Print["I am working on root ", n];
Quiet@Check[root = FindRoot[x^2 + x + 1, {x, at[[n]]}], Return[],FindRoot::lstol]
, {n, 1, Length@at}
]


Print only 2 iterations

Because second one FindRoot[x^2 + x + 1, {x, at[[2]]}] gives FindRoot::lstol and then Return[] returns from the closest surrounding loop.

• Thanks, that will answer my question. Lets say now, instead of terminating, I want to make the second root from 3 to 3+∆ (∆ is some small value) so that it will satisfy the root (in this case it will not, but just for the sake of argument assume that it does), how can I change the code? Apr 15, 2020 at 14:13
• @sslucifer it will best to post this as separate question in order to keep each question having one answer. Apr 15, 2020 at 16:49
• @sslucifer The undocumented second argument of Return can be used to specify where control returns to. E.g. Return[Null, Do] or Return[root, Do] exits the (nearest) enclosing Do[]`. Jan 10, 2021 at 14:30