# Help finding multiple roots to nonlinear equation - IntervalRootFinding not working

**URL:** <https://discourse.julialang.org/t/help-finding-multiple-roots-to-nonlinear-equation-intervalrootfinding-not-working/119575>\
**Category:** Numerics\
**Created:** [September 18, 2024, 10:36pm UTC](https://discourse.julialang.org/t/help-finding-multiple-roots-to-nonlinear-equation-intervalrootfinding-not-working/119575 "2024-09-18T22:36:26Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![lpz18](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@lpz18](https://discourse.julialang.org/u/lpz18)\
**Post date:** [September 18, 2024, 10:36pm UTC](https://discourse.julialang.org/t/help-finding-multiple-roots-to-nonlinear-equation-intervalrootfinding-not-working/119575/1 "2024-09-18T22:36:26Z")

</div>

I have a set of nonlinear equations (which happen to be steady-state DEs) that I am trying to solve that have multiple real roots and 3 variables (x, y, & z in the code below)

In Mathematica I can get the multiple roots as I’d expect:

 ![Screenshot 2024-09-18 at 3.04.52 PM](https://global.discourse-cdn.com/julialang/original/3X/3/7/37dfe0d94a065a7993a2a07417447e5148db1b5e.png)

In Julia, using JuMP I cannot:  
 ![Screenshot 2024-09-18 at 3.08.30 PM](https://global.discourse-cdn.com/julialang/original/3X/6/2/621cf079c45daea268ac996b18b66a1c99bfada2.png)

(the difference I care about in comparing the two graphs is that the curves go jagged though they shouldn’t. the axes are slightly different since I re-scaled in the Julia figure, and the points are data which can be neglected.)

I would like to find the multiple solutions I have in Mathematica. Naturally, JuMP finds the single optimal according to the specified objective, so this is as expected but not as desired.

I tried IntervalRootFinding.jl:

```julia
function core_eqns(u)
	x, y, z = u

	φ_R = 2 * 0.30 * x / (x + 0.8) + 0.05
	φ_F = 0.35 - φ_R
	φ_M = φ_F * 0.99
	φ_AR = φ_F * 0.01
	C_Rf = 63.7e-6 * φ_R - y
	φ_Rf = φ_R - y / 63.7e-6
	λ = 10 * x / (x + 0.03) * φ_Rf

	# steady-state equations
	A = 5 * φ_M - (1 + x) * λ
	B = 2.25e6 * z * C_Rf - 3.78 * y - λ * y
	C = 800 * (5e-4 - z) - 35 * 60^2 * φ_AR * 536e-6 * z / (z + 15e-6) + 3.78 * y - 2.25e6 * C_Rf * z - λ * z
	
	return [A, B, C]
end

function SS_solver(p)
	A_range = 0..15
	B_range = 0..35e-6
	C_range = 0..5e-4
	interval = A_range × B_range × C_range

	function f(x)
		out = core_eqns(x, p)
		return SVector(
			out[1],
			out[2],
			out[3]
		)
	end
	return roots(f, interval)
end

```

this gave me ~2000 intervals all with the flag :unknown, and it took 455s to run – which is quite undesirable since each the curves above are generated from computing the roots 100 times (and for comparison it takes \<4s for all of the solutions to run in JuMP)

 ![Screenshot 2024-09-18 at 3.33.35 PM](https://global.discourse-cdn.com/julialang/original/3X/1/5/15c274e601c0df33d370121bbf673069fcde557d.png)

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [September 19, 2024, 9:20pm UTC](https://discourse.julialang.org/t/help-finding-multiple-roots-to-nonlinear-equation-intervalrootfinding-not-working/119575/2 "2024-09-19T21:20:24Z")

</div>

What’s the result from Roots.jl?

---

<div class="post-metadata">

**Author:** ![lpz18](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@lpz18](https://discourse.julialang.org/u/lpz18)\
**Post date:** [September 19, 2024, 9:30pm UTC](https://discourse.julialang.org/t/help-finding-multiple-roots-to-nonlinear-equation-intervalrootfinding-not-working/119575/3 "2024-09-19T21:30:59Z")

</div>

from the docs:

> This package contains simple routines for finding roots, or zeros, of scalar functions of a single real variable

I didn’t try because I have 3 variables. Is there a way of using Roots.jl for multivariate root-finding?

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [September 19, 2024, 9:46pm UTC](https://discourse.julialang.org/t/help-finding-multiple-roots-to-nonlinear-equation-intervalrootfinding-not-working/119575/4 "2024-09-19T21:46:20Z")

</div>

Ah I guess not then.  
Sorry, I’m on mobile and just messaged a quick suggestion.

---

<div class="post-metadata">

**Author:** ![lpz18](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@lpz18](https://discourse.julialang.org/u/lpz18)\
**Post date:** [September 19, 2024, 9:47pm UTC](https://discourse.julialang.org/t/help-finding-multiple-roots-to-nonlinear-equation-intervalrootfinding-not-working/119575/5 "2024-09-19T21:47:35Z")

</div>

thanks anyways 🙂

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [September 19, 2024, 9:53pm UTC](https://discourse.julialang.org/t/help-finding-multiple-roots-to-nonlinear-equation-intervalrootfinding-not-working/119575/6 "2024-09-19T21:53:17Z")

</div>

How about this?

> **[Steady State Callbacks · DiffEqCallbacks.jl](https://docs.sciml.ai/DiffEqCallbacks/stable/steady_state/)**
>
> Documentation for DiffEqCallbacks.jl.

---

<div class="post-metadata">

**Author:** ![lpz18](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@lpz18](https://discourse.julialang.org/u/lpz18)\
**Post date:** [September 19, 2024, 10:08pm UTC](https://discourse.julialang.org/t/help-finding-multiple-roots-to-nonlinear-equation-intervalrootfinding-not-working/119575/7 "2024-09-19T22:08:57Z")

</div>

the DE steady solvers are the best combination of speed and robustness that I’ve found, but haven’t been able to get the multiple real solutions, I might just need to more systematically sweep different initial conditions

---

<div class="post-metadata">

**Author:** ![orebas](https://avatars.discourse-cdn.com/v4/letter/o/bbe5ce/32.png) [@orebas](https://discourse.julialang.org/u/orebas)\
**Post date:** [September 19, 2024, 10:31pm UTC](https://discourse.julialang.org/t/help-finding-multiple-roots-to-nonlinear-equation-intervalrootfinding-not-working/119575/8 "2024-09-19T22:31:50Z")

</div>

If these are rational, you might try clearing denominators and then HomotopyContinuation.jl.

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [September 20, 2024, 1:55am UTC](https://discourse.julialang.org/t/help-finding-multiple-roots-to-nonlinear-equation-intervalrootfinding-not-working/119575/9 "2024-09-20T01:55:28Z")

</div>

@ChrisRackauckas any ideas?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [September 20, 2024, 5:55am UTC](https://discourse.julialang.org/t/help-finding-multiple-roots-to-nonlinear-equation-intervalrootfinding-not-working/119575/10 "2024-09-20T05:55:58Z")

</div>

Isn’t this just a bifurcation plot? Why not use BifurcationKit.jl?

---

<div class="post-metadata">

**Author:** ![lpz18](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@lpz18](https://discourse.julialang.org/u/lpz18)\
**Post date:** [September 20, 2024, 4:46pm UTC](https://discourse.julialang.org/t/help-finding-multiple-roots-to-nonlinear-equation-intervalrootfinding-not-working/119575/11 "2024-09-20T16:46:29Z")

</div>

HomotopyContinuation.jl and BifurcationKit.jl both look promising! I’ll check them out and report back, thanks for the help!

---

<div class="post-metadata">

**Author:** ![lpz18](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@lpz18](https://discourse.julialang.org/u/lpz18)\
**Post date:** [September 24, 2024, 7:00am UTC](https://discourse.julialang.org/t/help-finding-multiple-roots-to-nonlinear-equation-intervalrootfinding-not-working/119575/12 "2024-09-24T07:00:24Z")

</div>

With BifurcationKit.jl I have not managed to find a bifurcation, this is my code:

```julia
	φ_F = 1 - 0.65 - φ_R
	φ_M = φ_F * 0.99
	φ_AR = φ_F * 0.01
	C_Rf = 63.7e-6 * φ_R - C_bnd
	φ_Rf = φ_R - C_bnd / 63.7e-6
	λ = 9.65 * c_pc / (c_pc + 0.03) * φ_Rf

	# steady-state equations
	cpc_ss = 5 * φ_M - (1 + c_pc) * λ
	C_bnd_ss = 2.25e6 * C_in * C_Rf - 3.78 * C_bnd - λ * C_bnd
	C_in_ss = 760 * (abx_conc[1] - C_in) - 35*60^2 * φ_AR * 536e6 * C_in / (C_in + 15e-6) + 3.78 * C_bnd - 2.25e6 * C_Rf * C_in - λ * C_in
	
	return [cpc_ss, C_bnd_ss, C_in_ss]
end

prob = BifurcationProblem(core_eqns, zeros(3), [1e-5], 1)
br = continuation(prob, PALC(), ContinuationPar(
	p_min = 0.0, p_max = 1e-3, detect_bifurcation = 2, max_steps = 1000
))

```

and the output I get is:

```julia
 ┌─ Curve type: EquilibriumCont
 ├─ Number of points: 2
 ├─ Type of vectors: Vector{Float64}
 ├─ Parameter p starts at 1.0e-5, ends at 0.001
 ├─ Algo: PALC
 └─ Special points:

- # 1, endpoint at p ≈ +0.00100000,   

```

---

<div class="post-metadata">

**Author:** ![lpz18](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@lpz18](https://discourse.julialang.org/u/lpz18)\
**Post date:** [September 24, 2024, 7:04am UTC](https://discourse.julialang.org/t/help-finding-multiple-roots-to-nonlinear-equation-intervalrootfinding-not-working/119575/13 "2024-09-24T07:04:17Z")

</div>

HomotopyContinuation.jl is working for single solutions but I am still working out how to run the loop so I can plot the multiple solutions nicely like in the original question. Strictly speaking, HomotopyContinuation.jl has satisfied my question as originally stated, but I still need to get the analogous plot which requires looping through the solution multiple times (like is intended in the bifurcation analysis)

---

<div class="post-metadata">

**Author:** ![lpz18](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@lpz18](https://discourse.julialang.org/u/lpz18)\
**Post date:** [September 25, 2024, 8:42pm UTC](https://discourse.julialang.org/t/help-finding-multiple-roots-to-nonlinear-equation-intervalrootfinding-not-working/119575/14 "2024-09-25T20:42:44Z")

</div>

![Screenshot 2024-09-25 at 1.41.44 PM](https://global.discourse-cdn.com/julialang/original/3X/4/a/4a3fc31068af8afe41edc775f27c56cce150ed0e.png)  
got it working nicely with BifurcationKit.jl, thanks for the pointers
