# Strange behaviour of find\_zero() from Roots.jl

**URL:** <https://discourse.julialang.org/t/strange-behaviour-of-find-zero-from-roots-jl/31304>\
**Category:** General Usage\
**Created:** [November 20, 2019, 8:49am UTC](https://discourse.julialang.org/t/strange-behaviour-of-find-zero-from-roots-jl/31304 "2019-11-20T08:49:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)\
**Post date:** [November 20, 2019, 8:49am UTC](https://discourse.julialang.org/t/strange-behaviour-of-find-zero-from-roots-jl/31304/1 "2019-11-20T08:49:39Z")

</div>

Hi everyone,

I recently updated the version of `Roots.jl` I use in my package. I have some simple code that looks like this

```julia
    f = β -> begin
                    diff = mb(β, β, par, bpar = ConstructBPar(β, par)) - infocostdiff(β,par)
                    @show diff
                    @show β
                    return diff
                end
        find_zero(f, (0.00000001, 2), Bisection())
    end

```

This is what the last iterations look like

```julia
diff = -1.6653345369377348e-16
β = 0.06802616450595264
diff = 3.3306690738754696e-16
β = 0.06802616450595263
diff = 3.3306690738754696e-16
β = 0.06802616450595263
diff = -2.220446049250313e-16
β = 0.06802616450595265
diff = -5.0e-8
β = 1.0e-8
diff = -10.0
β = 2.0

```

and I get the following error

```julia
ArgumentError: The interval [a,b] is not a bracketing interval.
You need f(a) and f(b) to have different signs (f(a) * f(b) < 0).
Consider a different bracket or try fzero(f, c) with an initial guess c.

init_state(::Bisection, ::Roots.DerivativeFree{getfield(AlbagliInfo, Symbol("##238#239")){Pars}}, ::Tuple{Float64,Float64}) at bracketing.jl:81
#find_zero#8(::Roots.NullTracks, ::Bool, ::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::typeof(find_zero), ::Function, ::Tuple{Float64,Int64}, ::Bisection) at bracketing.jl:310
find_zero at bracketing.jl:307 [inlined]
FindFixedPoint at Functions.jl:636 [inlined]
ChooseBeta at GrowthFuncs.jl:236 [inlined]
#Sim#186(::Array{Float64,2}, ::Type, ::Int64, ::Type{Sim}, ::Pars, ::Int64, ::Symbol) at GrowthFuncs.jl:159
Sim(::Pars, ::Int64, ::Symbol) at GrowthFuncs.jl:137
top-level scope at Growth.jl:65

```

It seems to me that it found a zero, but then decided that it actually didn’t and that an error would a better outcome.

What can I do? When I run status inside my package, this happens, but I don’t understand why

```julia
(MyPackage) pkg> status
Project MyPackage v0.1.0
    Status `C:\Path\MyPackage\Project.toml`
  [1375f997] + CompEcon v0.4.0
  [a93c6f00] ↑ DataFrames v0.17.1 ⇒ v0.19.4
  [31c24e10] ↑ Distributions v0.16.4 ⇒ v0.21.8
  [2fe49d83] ↑ Expectations v1.0.2 ⇒ v1.1.1
  [442a2c76] ↑ FastGaussQuadrature v0.3.2 ⇒ v0.4.0
  [a98d9a8b] ↑ Interpolations v0.11.2 ⇒ v0.12.5
  [429524aa] ↑ Optim v0.17.2 ⇒ v0.19.4
  [d96e819e] ↑ Parameters v0.10.3 ⇒ v0.12.0
  [fcd29c91] ↑ QuantEcon v0.15.0 ⇒ v0.16.2
  [f2b01f46] ↑ Roots v0.7.4 ⇒ v0.8.3
  [37e2e46d] LinearAlgebra
  [9a3f8284] Random
  [10745b16] Statistics
    Status `C:\Path\MyPackage\Project.toml`
  [1375f997] + CompEcon v0.4.0
  [a93c6f00] ↑ DataFrames v0.17.1 ⇒ v0.19.4
  [31c24e10] ↑ Distributions v0.16.4 ⇒ v0.21.8
  [2fe49d83] ↑ Expectations v1.0.2 ⇒ v1.1.1
  [442a2c76] ↑ FastGaussQuadrature v0.3.2 ⇒ v0.4.0
  [a98d9a8b] ↑ Interpolations v0.11.2 ⇒ v0.12.5
  [429524aa] ↑ Optim v0.17.2 ⇒ v0.19.4
  [d96e819e] ↑ Parameters v0.10.3 ⇒ v0.12.0
  [fcd29c91] ↑ QuantEcon v0.15.0 ⇒ v0.16.2
  [f2b01f46] ↑ Roots v0.7.4 ⇒ v0.8.3

```

---

<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:** [November 20, 2019, 9:21am UTC](https://discourse.julialang.org/t/strange-behaviour-of-find-zero-from-roots-jl/31304/2 "2019-11-20T09:21:53Z")

</div>

You could make an MWE and open an issue for Roots.jl.

I am not sure what the problem is with the `pkg> status` output.

---

<div class="post-metadata">

**Author:** ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)\
**Post date:** [November 20, 2019, 9:28am UTC](https://discourse.julialang.org/t/strange-behaviour-of-find-zero-from-roots-jl/31304/3 "2019-11-20T09:28:14Z")

</div>

I am just confused by all the updating statements, I thought it should just give you a list of installed packages, as it just in the general environment.

Sure, I’ll open an issue with a MWE this week.

Is there another package you can recommend in the meantime that has a similar functionality (rootfinding)? I kind of need this to work now 😅

---

<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:** [November 20, 2019, 9:31am UTC](https://discourse.julialang.org/t/strange-behaviour-of-find-zero-from-roots-jl/31304/4 "2019-11-20T09:31:31Z")

</div>

Bisection is really simple to implement. Eg

> [@Is there a faster bisection root solver (that uses atol)?](https://discourse.julialang.org/t/is-there-a-faster-bisection-root-solver-that-uses-atol/12658/15):
>
> This should be type stable (but didn’t test it thoroughly): using Statistics: middle """ bisection(f, a, b; fa = f(a), fb = f(b), ftol, wtol) Bisection algorithm for finding the root ``f(x) ≈ 0`` within the initial bracket `[a,b]`. Returns a named tuple `(x = x, fx = f(x), isroot = ::Bool, iter = ::Int, ismaxiter = ::Bool)`. Terminates when either 1. `abs(f(x)) \< ftol` (`isroot = true`), 2. the width of the bracket is `≤wtol` (`isroot = false`), 3. `maxiter` number of iterations is re…

---

<div class="post-metadata">

**Author:** ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)\
**Post date:** [November 20, 2019, 9:44am UTC](https://discourse.julialang.org/t/strange-behaviour-of-find-zero-from-roots-jl/31304/5 "2019-11-20T09:44:44Z")

</div>

And of course the problem was not at all with Roots.jl, but somewhere else in my code \>.\<

Sorry for the inconvenience and your help nonetheless Tamas\_Papp! You have helped already a few times and you are in general very helpful in this forum! 😄

Is it possible to delete this topic?

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [November 21, 2019, 6:25am UTC](https://discourse.julialang.org/t/strange-behaviour-of-find-zero-from-roots-jl/31304/6 "2019-11-21T06:25:47Z")

</div>

Haahah, don’t delete it, i had this same problem, now i make sure that my intervals are of opposite signs before passing those to `find_zero`
