# Non-monotonic equations and \`mcpsolver\` from \`NLsolve\`

**URL:** <https://discourse.julialang.org/t/non-monotonic-equations-and-mcpsolver-from-nlsolve/3493>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [May 3, 2017, 9:03am UTC](https://discourse.julialang.org/t/non-monotonic-equations-and-mcpsolver-from-nlsolve/3493 "2017-05-03T09:03:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HugoLhuillier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hugolhuillier/32/976_2.png) [@HugoLhuillier](https://discourse.julialang.org/u/HugoLhuillier)\
**Post date:** [May 3, 2017, 9:03am UTC](https://discourse.julialang.org/t/non-monotonic-equations-and-mcpsolver-from-nlsolve/3493/1 "2017-05-03T09:03:49Z")

</div>

Dear all,

Working on some related topics, I’ve realized that the `mcpsolver` provided by the `NLsolve` package is struggling to find the solution(s) to non-monotonic functions.

As an example, say we are looking for the `x`’s which solve `(x^3 + x^2 - 20x - 8)/6 - 3 = 0`, subject to the constraint `x >= 0`. Graphically, this looks like

 ![](https://global.discourse-cdn.com/julialang/original/3X/7/a/7a47fcc2eb84b75a46e6318c71af88017fd60839.png)

The three roots are complex-valued. However, one of them has a real part greater than 0, `x = 4.59056`, at which point the function evaluates at 0. Tackling the problem with the `mcpsolver`, we have

```julia
function f!(x,fvec) 
  fvec[1] = (x[1]^3 + x[1]^2 - 20x[1] - 8)/6 - 3
end

NLsolve.mcpsolve(f!, [0.], [Inf], [0.], reformulation = :smooth, autodiff = true) # returns -1.48382
NLsolve.mcpsolve(f!, [0.], [Inf], [0.], reformulation = :minmax, autodiff = true) # returns -1.0
NLsolve.mcpsolve(f!, [0.], [Inf], [0.], reformulation = :smooth, autodiff = true, method = :newton) # returns 6.25915e8 

```

First, the solver returns negative value, even though the constraint indicates that `x` needs to be positive. Second, it does not find the root at 4.6.

I was wondering whether this is because the root is actually complex-valued. And if not, whether there would be a way to find this root, without changing the initial condition.

Thanks a lot for the help.

---

<div class="post-metadata">

**Author:** ![jkbest2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jkbest2/32/7350_2.png) [@jkbest2](https://discourse.julialang.org/u/jkbest2)\
**Post date:** [May 9, 2017, 4:52pm UTC](https://discourse.julialang.org/t/non-monotonic-equations-and-mcpsolver-from-nlsolve/3493/2 "2017-05-09T16:52:13Z")

</div>

Have you looked at [Roots.jl](https://github.com/JuliaMath/Roots.jl)?

---

<div class="post-metadata">

**Author:** ![HugoLhuillier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hugolhuillier/32/976_2.png) [@HugoLhuillier](https://discourse.julialang.org/u/HugoLhuillier)\
**Post date:** [May 11, 2017, 8:17am UTC](https://discourse.julialang.org/t/non-monotonic-equations-and-mcpsolver-from-nlsolve/3493/3 "2017-05-11T08:17:02Z")

</div>

I had not because I was first working on multidimensional problems. But for functions with a single variable, like this one, `Roots` works indeed nicely. Thanks!
