3
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – Chris K
    Apr 15, 2020 at 3:09
  • 4
    $\begingroup$ It's usually easier to help with a concrete example. See if you can come up with a minimal working example. $\endgroup$
    – Chris K
    Apr 15, 2020 at 3:10

1 Answer 1

2
$\begingroup$
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

Mathematica graphics

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

$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$
    – sslucifer
    Apr 15, 2020 at 14:13
  • $\begingroup$ @sslucifer it will best to post this as separate question in order to keep each question having one answer. $\endgroup$
    – Nasser
    Apr 15, 2020 at 16:49
  • 1
    $\begingroup$ @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[]. $\endgroup$
    – Michael E2
    Jan 10, 2021 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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