# How to avoid gsl root finder evaluate function outside its domain

When I use the newton's method or hybrid solver in the GSL package to deal with 1-D or multidimensional root solving problems, the code frequently crashes when the solver requests function value outside its domain of definition. For example, to solve $$f(x)=0$$ where $$f(x)$$ is only defined at $$x\geq0$$, even when I start with an initial guess very close to the root, the solver may still ask to evaluate the $$f(x)$$ at a negative $$x$$. Bisection method may be helpful in the 1-D problem, but it won't work for a multidimensional problem.

I typically try to solve the problem by arbitrarily define the $$f(x)$$ in the whole domain. But in some situation, especially in the case of complicated multidimensional root solving, I feel it's hard to extend the domain of the function and make sure it roughly maintain the general trend of the original function.

So I'm wondering if there is a method to restrict the region that solver would evaluate the function.

• Possibly related question. Can you take a look at the tricks described there and evaluate them for the root-finding usage? Sep 20 '19 at 9:00
• @AntonMenshov Do you suggest defining the function to NaN outside the domain? I believe the root solver will stop when the function doesn't return a real number. Or do you suggest to construct a fake function outside the domain? If so, do you have any trick on how to construct such a function? Sep 20 '19 at 9:30
• Can you provide an example where Newton's method is evaluating your function outside of the domain? Sep 21 '19 at 14:00
• @nicoguaro I think I figured out the reason. I tried to reproduce my problem with a simple function, but I failed. My multidimensional function has some discontinuity in one dimension. The problem happens when the solution is very close to a discontinuity point. I temporarily solve the problem by checking the location of the discontinuity point before the root solving. If it is too close to the potential solution, I modify the function to avoid that. Sep 25 '19 at 5:45
• Newton method works for continuous functions, so I would say that what you mention makes sense. If you provide your function (or simplified one that reproduces the problem) you could write an answer on how you solved it. Sep 25 '19 at 12:10