# FindRoot, How to Return Null After x Number of Iterations

I am using FindRoot to find the root of a function, say f(x), and going through a loop for different values of x. The function has no root for certain values of x, so there is an error which is thrown saying

"Failed to converge to the requested accuracy or precision within 100 iterations."

I read that when this happens, Mathematica gives x the value at which it was at when the maximum iterations was achieved. Is there a way to make x equal to Null when the maximum number of iterations is achieved?

• Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 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, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. – bbgodfrey Oct 23 '15 at 19:47

Check[FindRoot[f[x], {x, x0}], {x -> Null}]


will yield {x -> Null} when any error is encountered. If you wish Null only for specific errors, list them in the third argument of Check; for instance,

Check[FindRoot[f[x], {x, x0}], {x -> Null}, {FindRoot::cvmit}]


To silence all error messages too, prepend Quiet.

Quiet@Check[FindRoot[f[x], {x, x0}], {x -> Null}]


I believe the best approach is to test the root.

You have

x0 = x /. FindRoot[f[x], {x, 1}]


or similar. Then see how close f[x0] is to zero and make your judgement on whether to accept or discard the result. FindRoot itself can't do better than that either. When it says that it can't converge to the desired accuracy, it means that it thinks that f[x0] is not close enough to zero.

Providing the result and letting the user decide about it is better than discarding it automatically and preventing us from even seeing it.

• Thank you for your answer. I have tried doing what you suggested, but I was having issues implementing it in my code, which is why I was looking to see if there was a better way of doing it. I will try to fix it. Thanks again! – Phys Mate Oct 23 '15 at 20:01