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.
-
$\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 KApr 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 KApr 15, 2020 at 3:10
1 Answer
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.
-
$\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$ 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$– NasserApr 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]` orReturn[root, Do]
exits the (nearest) enclosingDo[]
. $\endgroup$ Jan 10, 2021 at 14:30