# Using NL solver to find zeros

**URL:** https://discourse.julialang.org/t/using-nl-solver-to-find-zeros/50323
**Category:** General Usage
**Tags:** question, optimization, math, calculus, nlsolve
**Created:** [November 17, 2020, 7:45pm UTC](https://discourse.julialang.org/t/using-nl-solver-to-find-zeros/50323 "2020-11-17T19:45:43Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [November 17, 2020, 7:45pm UTC](https://discourse.julialang.org/t/using-nl-solver-to-find-zeros/50323/1 "2020-11-17T19:45:43Z")

</div>

I was using Roots.jl for this sort of problem, and wanted to use ModelingTookit.jl or NLsolver.jl (the former uses the later).

```julia
using ModelingToolkit
∂xf(x)=(2 * x) * inv(2 * sqrt(3 + (x ^ 2)))
nl_f = @eval eval(∂xf[.01])

f2 = (du,u) -> nl_f(du,u,(-100,100))

using NLsolve
nlsolve(f2,ones(1))

```

I’m trying to find x when:  
∂xf(x)=.01

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [November 19, 2020, 1:52am UTC](https://discourse.julialang.org/t/using-nl-solver-to-find-zeros/50323/2 "2020-11-19T01:52:42Z")

</div>

```julia
using ModelingToolkit, NLsolve

# define some symbols to be used for the symbolic part of the evaluation
@variables x
@parameters slope
@derivitaves D'~x

# The interesting function
f(x) = √(3 + x^2)

# A symbolic expression for the derivative of the function
∂xf_sym = expand_derivatives(D(f(x)))

# Build the system we would like solved
nl_sys = NonlinearSystem([0 ~ ∂xf_sym - slope], [x], [slope])

# generate an in-place evaluator and jacobian for the system
# These are julia functions that implement the symbolic calculation built above.
# https://mtk.sciml.ai/stable/tutorials/nonlinear/
f! = generate_function(nlsys, [x], [slope], expression=Val{false})[2]

j! = generate_jacobian(nlsys, expression=Val{false})[2]

# the desired value for slope
params = [0.01]

# pass the generated function and jacobian to the solver, with an initial guess
soln = nlsolve((out, x)->f!(out, x, params), (out, x)->j!(out, x, params), [0.0])

# build a Julia version of the symbolic derivative
∂xf = eval(build_function([∂xf_sym], [x])[1])

# check that the system is really solved
∂xf(soln.zero) ≈ params

```

---

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [November 19, 2020, 3:06am UTC](https://discourse.julialang.org/t/using-nl-solver-to-find-zeros/50323/3 "2020-11-19T03:06:41Z")

</div>

> [@contradict](#):
>
> `params = [0.01]`

It works, the others are simpler. I was not wanting to use IntervalRootFinder, because it was writen in Python (as though I’m doing anything close to where speed would begin to matter), but instead I think I’ll use roots.jl for this. This works good though, so I will consider it for other things.

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [November 19, 2020, 3:37am UTC](https://discourse.julialang.org/t/using-nl-solver-to-find-zeros/50323/4 "2020-11-19T03:37:45Z")

</div>

Err… I am the author of [IntervalRootFinding.jl](https://github.com/JuliaIntervals/IntervalRootFinding.jl) and I can guarantee that it is written in pure Julia. I’m not sure why you think otherwise.

---

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [November 19, 2020, 4:03am UTC](https://discourse.julialang.org/t/using-nl-solver-to-find-zeros/50323/5 "2020-11-19T04:03:45Z")

</div>

I thought from the documentation. It definatly works fine, except for the type of variable. NLsolve is cool, that it shares macros with ModelingToolkit.jl . I’ll keep using each, and see my preference.

---

<div class="post-metadata">

### Author: ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)
#### Post date: [November 19, 2020, 10:16am UTC](https://discourse.julialang.org/t/using-nl-solver-to-find-zeros/50323/6 "2020-11-19T10:16:37Z")

</div>

I wonder why you want to use root finding methods for what is essentially an optimization problem. Typically, specialized optimization algorithms should be more efficient and easier to use (because they take derivatives automatically, for example)

---

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [November 19, 2020, 3:29pm UTC](https://discourse.julialang.org/t/using-nl-solver-to-find-zeros/50323/7 "2020-11-19T15:29:35Z")

</div>

I agree, I was using the Mean Value Theorem and the Interval Value theorem, for demonstration, but for practical things I would use optimization. What packages would I use for optimization problems?

---

<div class="post-metadata">

### Author: ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)
#### Post date: [November 19, 2020, 10:10pm UTC](https://discourse.julialang.org/t/using-nl-solver-to-find-zeros/50323/8 "2020-11-19T22:10:23Z")

</div>

So far, I have mostly used Optim.jl which is very straight-forward. If you know that your function is convex, Convex.jl is great. Many swear by Jump.jl – I haven’t tried it yet because as far as I can tell it requires me to rewrite my functions using their domain specific language.

---

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [November 20, 2020, 3:55pm UTC](https://discourse.julialang.org/t/using-nl-solver-to-find-zeros/50323/9 "2020-11-20T15:55:57Z")

</div>

I’ll look at Optim.jl . I’ve heard of JuMP, but never used it.

---

<div class="post-metadata">

### Author: ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)
#### Post date: [November 20, 2020, 5:17pm UTC](https://discourse.julialang.org/t/using-nl-solver-to-find-zeros/50323/10 "2020-11-20T17:17:51Z")

</div>

Autodiff can be done for equations as well. The larger difference between minimizing f and  
solving grad f = 0 with a nonlinear solver is that the nonlinear solver is not aware that the goal is minimization. Optimization methods use a model Hessian that is symmetric at least and often positive definite. Optimization methods also guide the search with f, rather than norm(grad f) which is what nonlinear solvers do.
