# Using NLsolve to find multiple roots

**URL:** <https://discourse.julialang.org/t/using-nlsolve-to-find-multiple-roots/67637>\
**Category:** General Usage\
**Tags:** question, nlsolve\
**Created:** [September 3, 2021, 11:19am UTC](https://discourse.julialang.org/t/using-nlsolve-to-find-multiple-roots/67637 "2021-09-03T11:19:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)\
**Post date:** [September 3, 2021, 11:19am UTC](https://discourse.julialang.org/t/using-nlsolve-to-find-multiple-roots/67637/1 "2021-09-03T11:19:45Z")

</div>

I have two non-linear equations for which I want to find all roots.  
Using `nlsolve` only returns one root.  
How can I configure it to return all roots, say in some range of parameters?

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [September 3, 2021, 12:03pm UTC](https://discourse.julialang.org/t/using-nlsolve-to-find-multiple-roots/67637/2 "2021-09-03T12:03:50Z")

</div>

You may be interested in `IntervalRootFinding.jl`. Here’s a tutorial: [https://github.com/JuliaIntervals/IntervalRootFinding.jl](https://github.com/JuliaIntervals/IntervalRootFinding.jl).  
edit:  
The tutorial is actually here:  
[Interval root finding tutorial](https://juliaintervals.github.io/pages/tutorials/tutorialRootFinding/)

---

<div class="post-metadata">

**Author:** ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)\
**Post date:** [September 4, 2021, 7:34am UTC](https://discourse.julialang.org/t/using-nlsolve-to-find-multiple-roots/67637/3 "2021-09-04T07:34:06Z")

</div>

Thanks, this package indeed seems to do the job!

However, I am having a hard time to take the actual number out of the returned interval.  
The returned roots are put in `rts` and they are

```julia
3-element Vector{Root{IntervalBox{2, Float64}}}:
 Root([-0.261043, -0.261042] × [0.722688, 0.722689], :unique)
 Root([0.26962, 0.269621] × [0.26962, 0.269621], :unique)
 Root([0.722688, 0.722689] × [-0.261043, -0.261042], :unique)

```

So `rts[1].interval[1]` gives back the first interval `[-0.261043, -0.261042]`. I want to get one number from this so I can plot it or do other operations with it.  
When I run `first(rts[1].interval[1])` I still get the entire interval `[-0.261043, -0.261042]`.  
I looked in the documentation and didn’t find a specification on how to do that. If you happen to know, I would love the help:)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 4, 2021, 8:38am UTC](https://discourse.julialang.org/t/using-nlsolve-to-find-multiple-roots/67637/4 "2021-09-04T08:38:50Z")

</div>

> [@roi.holtzman](#):
>
> plot it or do other operations with it

For practical purposes, I would check that the interval is sufficiently narrow (below some \epsilon) and then just take a midpoint or something similar.

---

<div class="post-metadata">

**Author:** ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)\
**Post date:** [September 4, 2021, 8:40am UTC](https://discourse.julialang.org/t/using-nlsolve-to-find-multiple-roots/67637/5 "2021-09-04T08:40:10Z")

</div>

yes, this is exactly what I had in mind.  
The problem is that I don’t know how to get the bounds of the interval object.

---

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [September 4, 2021, 9:19am UTC](https://discourse.julialang.org/t/using-nlsolve-to-find-multiple-roots/67637/6 "2021-09-04T09:19:25Z")

</div>

You can extract the low and high value of an interval as

```julia
low, high = interval.lo, interval.hi

```

---

<div class="post-metadata">

**Author:** ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)\
**Post date:** [September 4, 2021, 11:30am UTC](https://discourse.julialang.org/t/using-nlsolve-to-find-multiple-roots/67637/7 "2021-09-04T11:30:22Z")

</div>

thanks! this works! perhaps you can point me to where in the docs I can find more useful operations?

---

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [September 5, 2021, 1:50am UTC](https://discourse.julialang.org/t/using-nlsolve-to-find-multiple-roots/67637/8 "2021-09-05T01:50:21Z")

</div>

Please refer to [Interval arithmetic tutorial](https://juliaintervals.github.io/pages/tutorials/tutorialArithmetic/) for more operations on the interval. However, there is no docs for `.lo` and `.hi`. I find these fields in the source code.

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [September 5, 2021, 1:55am UTC](https://discourse.julialang.org/t/using-nlsolve-to-find-multiple-roots/67637/9 "2021-09-05T01:55:29Z")

</div>

An easy way to learn about fields inside types is using `typeof` and `fieldnames`. Following the same setup in the tutorial, you can do:

```julia
julia> rts
2-element Vector{Root{Interval{Float64}}}:
 Root([1.99999, 2.00001], :unique)
 Root([-1.72982e-09, 1.38862e-09], :unique)

julia> rts |> typeof
Vector{Root{Interval{Float64}}} (alias for Array{Root{Interval{Float64}}, 1})

julia> rts[1] |> typeof |> fieldnames
(:interval, :status)

julia> rts[1].interval |> typeof |> fieldnames
(:lo, :hi)

```
