# Find\_zero to find a solution x as a function of y?

**URL:** https://discourse.julialang.org/t/find-zero-to-find-a-solution-x-as-a-function-of-y/91628
**Category:** General Usage
**Tags:** question, roots
**Created:** [December 14, 2022, 3:02am UTC](https://discourse.julialang.org/t/find-zero-to-find-a-solution-x-as-a-function-of-y/91628 "2022-12-14T03:02:21Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![kwyyoo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kwyyoo/32/37181_2.png) [@kwyyoo](https://discourse.julialang.org/u/kwyyoo)
#### Post date: [December 14, 2022, 3:02am UTC](https://discourse.julialang.org/t/find-zero-to-find-a-solution-x-as-a-function-of-y/91628/1 "2022-12-14T03:02:21Z")

</div>

Hi. This is my first thread topic and question ever posting here.

I have the following function and the my goal is to find a solution of x to make it zero.

```julia
f(x, y) = (x^0.5 - y)/(1-x) - x^(-0.5);

```

Can I find the solution x^\star as a function of y such that x^\star = g(y)? I need this to plug x^\star = g(y) into another equation of y to maximize so that the final solution x^\star can be found afterwards.

I guess “find\_zero” function from [Roots.jl](https://juliamath.github.io/Roots.jl/dev/reference/#The-find_zero-and-find_zeros-functions) cannot work in this case since the function is not univariate.

---

<div class="post-metadata">

### Author: ![cvanaret](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cvanaret/32/11594_2.png) [@cvanaret](https://discourse.julialang.org/u/cvanaret)
#### Post date: [December 14, 2022, 3:34am UTC](https://discourse.julialang.org/t/find-zero-to-find-a-solution-x-as-a-function-of-y/91628/2 "2022-12-14T03:34:24Z")

</div>

Set the equation f(x, y) = 0, multiply by 1-x, reorder, let u = \sqrt{x}, solve the quadratic equation in u and recover x \> 0. There’s probably some conditions on y for the solution to exist.

---

<div class="post-metadata">

### Author: ![czylabsonasa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/czylabsonasa/32/8663_2.png) [@czylabsonasa](https://discourse.julialang.org/u/czylabsonasa)
#### Post date: [December 14, 2022, 6:25am UTC](https://discourse.julialang.org/t/find-zero-to-find-a-solution-x-as-a-function-of-y/91628/3 "2022-12-14T06:25:28Z")

</div>

as @cvanaret said, by reordering you’ll get y=\frac{2x-1}{\sqrt{x}}, \ x\>0,\ x\neq 1

but it is not a julia related question… 🙂

if you are searching for a programmatic way, check out the [Symbolics.jl](https://symbolics.juliasymbolics.org/dev/)

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [December 14, 2022, 8:07am UTC](https://discourse.julialang.org/t/find-zero-to-find-a-solution-x-as-a-function-of-y/91628/4 "2022-12-14T08:07:58Z")

</div>

> [@kwyyoo](#):
>
> `f(x, y) = (x^0.5 - y)/(1-x) - x^(-0.5);`

Programming related: `sqrt(x)` is much, much, faster than `x^0.5`.

---

<div class="post-metadata">

### Author: ![cvanaret](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cvanaret/32/11594_2.png) [@cvanaret](https://discourse.julialang.org/u/cvanaret)
#### Post date: [December 14, 2022, 9:08am UTC](https://discourse.julialang.org/t/find-zero-to-find-a-solution-x-as-a-function-of-y/91628/5 "2022-12-14T09:08:54Z")

</div>

I actually suggested to solve for x, not for y 😉  
I found x = \frac{(y + \sqrt{y^2 + 8})^2}{16} (we keep only the positive root because x \> 0). We also have x \neq 1, therefore you can’t have y = 1 (it implies x = 1).

---

<div class="post-metadata">

### Author: ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)
#### Post date: [December 14, 2022, 9:24am UTC](https://discourse.julialang.org/t/find-zero-to-find-a-solution-x-as-a-function-of-y/91628/6 "2022-12-14T09:24:37Z")

</div>

> [@kwyyoo](#):
>
> I need this to plug x^\star = g(y) x⋆=g(y)x^\star = g(y) into another equation of y yy to maximize so that the final solution x^\star x⋆x^\star can be found afterwards.

This is a nonlinear programming (NLP), where `f(x,y) == 0` is a constraint. You might want to have a look at [JuMP](https://jump.dev/JuMP.jl/stable/manual/nlp/).

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [December 14, 2022, 10:24am UTC](https://discourse.julialang.org/t/find-zero-to-find-a-solution-x-as-a-function-of-y/91628/7 "2022-12-14T10:24:43Z")

</div>

Here is a way of using Roots to get a solution (perhaps not as performant as solving the equation algebraically):  
(using DNF’s suggestion to replace `^0.5` with `sqrt`)

```julia
using Roots

f(x,y) = (sqrt(abs(x)) - y)/(1-x) - 1/sqrt(abs(x));
G(y) = x -> f(x,y)
X(y) = find_zero(G(y),0.1)

```

Now we can:

```julia
julia> X(0.2)
0.5758872343937891

julia> f(X(0.2),0.2)
0.0

```

BTW here is a Unicode chart for a specific `y`:

```julia
julia> using UnicodePlots

julia> lineplot(G(0.2); name="f(x,0.2)", ylabel="G(0.2)")
             ┌────────────────────────────────────────┐         
           4 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ f(x,0.2)
             │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⢰⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│         
             │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│         
             │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│         
             │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│         
             │⠶⠶⠶⠶⠶⠶⠶⠶⠶⠶⠶⠶⠶⠶⢖⣒⣒⠒⠒⠒⡗⡗⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒│         
             │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⢢⡀⣿⠁⠀⠀⠀⠀⠀⢀⣀⣀⡤⠤⠤⠤⠔⠒⠒⠒⠒⠒│         
   G(0.2) │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⡿⠀⠀⠀⡠⠖⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│         
             │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⡇⠀⠀⡜⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│         
             │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⢠⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│         
             │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│         
             │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│         
             │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⡎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│         
             │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│         
          -7 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│         
             └────────────────────────────────────────┘         
             ⠀-10⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀10⠀         
             ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀x⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀         

```

Admittedly, there is a problem with the `0.1` initial guess to `find_zero`. One solution would be to use `find_zeros` from Roots package, with a known good interval. Or to use this interval in `find_zero` with a different root finding method (i.e. Bisection). Something like:

```julia
julia> X(y) = minimum((abs(x),x) for x in find_zeros(G(0.2),(-10.0,10.0)))[2]
X (generic function with 1 method)

julia> X(0.2)
0.5758872343937891

```

As for the power of Roots:

Richard Feynman said (Lectures on Physics):

> “All of the laws of physics can be contained in one equation. That equation is **U=0**.”

So root finding is pretty powerful 😉 (lots of small print here).

---

<div class="post-metadata">

### Author: ![czylabsonasa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/czylabsonasa/32/8663_2.png) [@czylabsonasa](https://discourse.julialang.org/u/czylabsonasa)
#### Post date: [December 14, 2022, 10:26am UTC](https://discourse.julialang.org/t/find-zero-to-find-a-solution-x-as-a-function-of-y/91628/8 "2022-12-14T10:26:17Z")

</div>

you’re right, i read it w/o too much care…and solved the easier direction.

---

<div class="post-metadata">

### Author: ![cvanaret](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cvanaret/32/11594_2.png) [@cvanaret](https://discourse.julialang.org/u/cvanaret)
#### Post date: [December 14, 2022, 10:31am UTC](https://discourse.julialang.org/t/find-zero-to-find-a-solution-x-as-a-function-of-y/91628/9 "2022-12-14T10:31:07Z")

</div>

> [@Dan](#):
>
> `f(x,y) = (sqrt(abs(x)) - y)/(1-x) - sqrt(abs(x));`

should be `f(x,y) = (sqrt(abs(x)) - y)/(1-x) - 1/sqrt(abs(x));` 🙂

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [December 14, 2022, 10:51am UTC](https://discourse.julialang.org/t/find-zero-to-find-a-solution-x-as-a-function-of-y/91628/10 "2022-12-14T10:51:40Z")

</div>

Thanks, fixed this by editing answer!

---

<div class="post-metadata">

### Author: ![kwyyoo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kwyyoo/32/37181_2.png) [@kwyyoo](https://discourse.julialang.org/u/kwyyoo)
#### Post date: [February 10, 2023, 5:45pm UTC](https://discourse.julialang.org/t/find-zero-to-find-a-solution-x-as-a-function-of-y/91628/11 "2023-02-10T17:45:18Z")

</div>

Thank you so much Dan! This worked well in my case. I wanted to solve it through the code, not algebraically. Appreciate it!
