# Solving F(X)=0 using nlsolve

**URL:** <https://discourse.julialang.org/t/solving-f-x-0-using-nlsolve/105516>\
**Category:** Optimization (Mathematical)\
**Tags:** question, nlsolve\
**Created:** [October 28, 2023, 5:04pm UTC](https://discourse.julialang.org/t/solving-f-x-0-using-nlsolve/105516 "2023-10-28T17:04:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![havana](https://avatars.discourse-cdn.com/v4/letter/h/ecd19e/32.png) [@havana](https://discourse.julialang.org/u/havana)\
**Post date:** [October 28, 2023, 5:04pm UTC](https://discourse.julialang.org/t/solving-f-x-0-using-nlsolve/105516/1 "2023-10-28T17:04:34Z")

</div>

I wanna solve a root finding problem of the form F(X)=0 using NLsolve.  
However, I do not know the regularity of the function F, for example if it is not differentiable, so the Jacobian does not exists, how do the function nlsolve() work ? using a Newton method or a trust region method ?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 28, 2023, 5:41pm UTC](https://discourse.julialang.org/t/solving-f-x-0-using-nlsolve/105516/2 "2023-10-28T17:41:05Z")

</div>

You could use a bisection-style method, e.g. [IntervalRootFinding.jl](https://github.com/JuliaIntervals/IntervalRootFinding.jl). In 1d, [Roots.jl](https://github.com/JuliaMath/Roots.jl) provides several methods.

(It looks like the algorithms in NLsolve.jl all assume differentiability?)

---

<div class="post-metadata">

**Author:** ![havana](https://avatars.discourse-cdn.com/v4/letter/h/ecd19e/32.png) [@havana](https://discourse.julialang.org/u/havana)\
**Post date:** [October 28, 2023, 6:09pm UTC](https://discourse.julialang.org/t/solving-f-x-0-using-nlsolve/105516/4 "2023-10-28T18:09:40Z")

</div>

I tried for example f(x)=|x|, and it succeeded to converge even it is not differentiable at 0

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 28, 2023, 8:18pm UTC](https://discourse.julialang.org/t/solving-f-x-0-using-nlsolve/105516/5 "2023-10-28T20:18:02Z")

</div>

I wouldn’t count on it always working with a method that assumes differentiability.

|x| is a terrible function for root-finding with because it doesn’t change sign when it crosses the root, something most algorithms assume. A function like that is probalby more suited for optimization (using non-smooth algorithms).

Of course, if you _know_ the source of non-smoothness in your function, you should first try your hardest to remove the non-differentiability, e.g. by a change of variables or some other transformation. This will allow you to obtain faster convergence.

---

<div class="post-metadata">

**Author:** ![havana](https://avatars.discourse-cdn.com/v4/letter/h/ecd19e/32.png) [@havana](https://discourse.julialang.org/u/havana)\
**Post date:** [October 28, 2023, 8:38pm UTC](https://discourse.julialang.org/t/solving-f-x-0-using-nlsolve/105516/6 "2023-10-28T20:38:55Z")

</div>

Thank you for your reply. In fact, using  
nlsolve(F, [-5.0]; xtol=1e-8, method=:trust\_region)  
to determine the zero of F.

I m wondering if the nlsolve function have a regularization effet, or at least in the nondifferentiability point, it only uses finite differences instead of computing the Jacobian?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 28, 2023, 8:41pm UTC](https://discourse.julialang.org/t/solving-f-x-0-using-nlsolve/105516/7 "2023-10-28T20:41:53Z")

</div>

> [@havana](#):
>
> I’m wondering if the nlsolve function have a regularization effet, or at least in the nondifferentiability point, it only uses finite differences instead of computing the Jacobian?

If an algorithm “at its heart” assumes that the function is differentiable, even if it doesn’t compute derivatives explicitly (or approximates them in some way, e.g. by finite differences), then you should generally beware that it may not converge for non-differentiable functions.

Perhaps a better question is,why do you think your actual problem is non-smooth? (If you _know_ it is non-smooth, then you may know enough to be able to derive some way to rectify the problem.)

---

<div class="post-metadata">

**Author:** ![havana](https://avatars.discourse-cdn.com/v4/letter/h/ecd19e/32.png) [@havana](https://discourse.julialang.org/u/havana)\
**Post date:** [October 28, 2023, 9:29pm UTC](https://discourse.julialang.org/t/solving-f-x-0-using-nlsolve/105516/8 "2023-10-28T21:29:38Z")

</div>

Thank you for your insight, it’s better to consider the specific case of the function F and its properties (non-smoothness etc.)
