FindRoot, How to Return Null After x Number of Iterations
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.