# Roots::find\_zero fails depending on small differences in initial condition

**URL:** <https://discourse.julialang.org/t/roots-find-zero-fails-depending-on-small-differences-in-initial-condition/95202>\
**Category:** General Usage\
**Tags:** roots\
**Created:** [February 25, 2023, 8:10pm UTC](https://discourse.julialang.org/t/roots-find-zero-fails-depending-on-small-differences-in-initial-condition/95202 "2023-02-25T20:10:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nvonturk](https://avatars.discourse-cdn.com/v4/letter/n/b487fb/32.png) [@nvonturk](https://discourse.julialang.org/u/nvonturk)\
**Post date:** [February 25, 2023, 8:10pm UTC](https://discourse.julialang.org/t/roots-find-zero-fails-depending-on-small-differences-in-initial-condition/95202/1 "2023-02-25T20:10:34Z")

</div>

Hi there,

I’m having trouble with the Roots package. I have an MVE below (base of post) which is setting up a simple economics model.

The last two lines showcase the issue. The first line works and returns the correct value of about 0.9. When I change the initial condition slightly, it fails with the following error message:

```julia
ERROR: MethodError: no method matching Roots.UnivariateZeroState(::Float64, ::Float64, ::Int64, ::Float64)
Closest candidates are:
  Roots.UnivariateZeroState(::T, ::T, ::S, ::S) where {T, S} at C:\Users\nvonturk\.julia\packages\Roots\Ez3Om\src\state.jl:3

```

I’m making the transition over to Julia from Matlab and “fzero” in Matlab doesn’t exhibit this issue. I’m repeating this calculation for different parameter values so simply changing the initial guess would not be a robust approach.

Also, any feedback on how to ask for help is appreciated. First time posting.

Nick

MVE

```julia
# Load libraries

using Parameters, QuantEcon, Roots

# Define utility function

u(c;p) = (1/(1-p.σ)) * c^(1-p.σ) - p.ψ * (n^(1+p.ε)/(1+p.ε))

# Define production function

f(k,n,z;p) = exp(z)*k^(p.α)*n^(1-p.α)

# Define optimal labor choice as a function of k, k prime, z, and a choice for consumption

n_star(c, k, kp, z;p) = ((1-p.α)/p.ψ * exp(z) * k^p.α * c^(-p.σ))^(1/(p.ε + p.α))

# Taking the optimal choice of labor as given, return how much the resource constraint is violated for particular value of c

resource_constraint(c, k, kp, z;p) = c>0 ? (f(k,n_star(c, k, kp, z;p),z;p) - c - kp + (1-p.δ)*k) : -100

# Define model parameters

RBCModel = @with_kw (α = 0.333, δ = 0.025, β = 0.984, σ = 1, ψ = 0.8356, ε = 0.25, z0 = 1, ρ = 0.9, σ_z = 0.05, μ = 0, n_std = 1.2, u = u, f = f, Kn = 500, mc = tauchen(10, ρ, σ_z, μ, n_std), tol = 1e-7)

p = RBCModel()

# Compute steady state capital and consumption

kss = ((1/p.β − 1 + p.δ)/p.α)^(1/(p.α−1))

css = kss^(p.α)-p.δ*kss

# Compute range for potential capital levels

k_grid = range(0.5*kss, 2*kss, p.Kn-1)

kss_id = findlast(k_grid .< kss)

k_grid = [k_grid[1:kss_id]; kss; k_grid[kss_id+1:end]]

# Benchmarking exercise

resource_constraint_anon(c) = resource_constraint(c, k_grid[1], k_grid[37], p.mc.state_values[1]; p)

# This will work

cs = find_zero(resource_constraint_anon, css/2)

# This will fail

cs = find_zero(resource_constraint_anon, css)

```

---

<div class="post-metadata">

**Author:** ![nvonturk](https://avatars.discourse-cdn.com/v4/letter/n/b487fb/32.png) [@nvonturk](https://discourse.julialang.org/u/nvonturk)\
**Post date:** [February 25, 2023, 8:18pm UTC](https://discourse.julialang.org/t/roots-find-zero-fails-depending-on-small-differences-in-initial-condition/95202/2 "2023-02-25T20:18:55Z")

</div>

Also, I tried to simply attach the .jl file with the code but as a new poster I’m not allowed to. Any advice on the best way to share code examples is appreciated.

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [February 25, 2023, 8:22pm UTC](https://discourse.julialang.org/t/roots-find-zero-fails-depending-on-small-differences-in-initial-condition/95202/3 "2023-02-25T20:22:33Z")

</div>

From the error message, it looks like a missing promotion. I’ll have a look.

---

<div class="post-metadata">

**Author:** ![nvonturk](https://avatars.discourse-cdn.com/v4/letter/n/b487fb/32.png) [@nvonturk](https://discourse.julialang.org/u/nvonturk)\
**Post date:** [February 25, 2023, 8:29pm UTC](https://discourse.julialang.org/t/roots-find-zero-fails-depending-on-small-differences-in-initial-condition/95202/4 "2023-02-25T20:29:02Z")

</div>

Very appreciated - thanks for the quick response. Let me know if the code example I sent is not sufficient and I’ll update

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [February 25, 2023, 9:15pm UTC](https://discourse.julialang.org/t/roots-find-zero-fails-depending-on-small-differences-in-initial-condition/95202/5 "2023-02-25T21:15:31Z")

</div>

> [@nvonturk](#):
>
> resource\_constraint(c, k, kp, z;p) = c\>0 ? (f(k,n\_star(c, k, kp, z;p),z;p) - c - kp + (1-p.δ)\*k) : -100

The quick fix is to make -100 equal to 100.0 above, as then the function will s always return Floats. This should be automated in the next tagged release.

---

<div class="post-metadata">

**Author:** ![nvonturk](https://avatars.discourse-cdn.com/v4/letter/n/b487fb/32.png) [@nvonturk](https://discourse.julialang.org/u/nvonturk)\
**Post date:** [February 25, 2023, 9:19pm UTC](https://discourse.julialang.org/t/roots-find-zero-fails-depending-on-small-differences-in-initial-condition/95202/6 "2023-02-25T21:19:22Z")

</div>

Fantastic - thank you for the help. I’m still getting used to the way in which Julia handles types. This was very helpful.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 26, 2023, 11:20am UTC](https://discourse.julialang.org/t/roots-find-zero-fails-depending-on-small-differences-in-initial-condition/95202/7 "2023-02-26T11:20:15Z")

</div>

> [@nvonturk](#):
>
> Any advice on the best way to share code examples is appreciated.

In markdown,

You can insert inline code with `backticks` like this

```julia
You can insert inline code with `backticks` like this

```

For code blocks,

```julia
you can use triple-backticks

```

````julia
```
you can use triple-backticks
```

````
