# Find_zero to find a solution x as a function of y?

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.

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 cannot work in this case since the function is not univariate.

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.

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

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

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).

This is a nonlinear programming (NLP), where f(x,y) == 0 is a constraint. You might want to have a look at JuMP.

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)

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> 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> 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> X(y) = minimum((abs(x),x) for x in find_zeros(G(0.2),(-10.0,10.0)))
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).

1 Like

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

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

Thanks, fixed this by editing answer!

1 Like

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