# Beginner question: How to get y value given x in an equation?

Hi, I have an equation like so (I put in some simplifying values for the constants):

var('h1 h2 x y n')
n = 4/3
h1 = 1
h2 = 1
g = sqrt(x^2 + y^2) + n * sqrt(x^2 + (h1 + h2 - y)^2) - (h1 + n * h2) == 0


I am able to get a beautiful implicit plot:

graph = implicit_plot(g, (x, -0.5, 0.5), (y, 0, 2.5))
show(graph)


But how do I find out the numerical values at say x = 0.2? This doesn't give numerical values (and I don't really understand its result either):

solve(g(x=0.2), y)


It gives

[sqrt(y^2 - 4*y + 101/25) == -3/4*sqrt(y^2 + 1/25) + 7/4]


Thanks

edit retag close merge delete

Note: no need to declare h1, h2, n as symbolic variables before assigning values to them.

Only declaring x and y is useful, as they will be used as x and y.

Sort by » oldest newest most voted

The exercise has two square roots and some extra stuff.

To solve by hand, one would

• isolate one square root on one side of the equation, to get something like a = b + c where a and b are square roots and c is not
• square both sides (this can lose information so we'll have to check, among all solutions of this new equation, which ones are solutions of the original equation)
• this gets rid of one of the square roots, the other one survives in the double product of (b + c)^2, as (2bc)
• isolate the square root one one side of the equation
• square both sides again
• finally solve the equation with no square roots (a polynomial equation of degree four)
• check which solutions satisfy the original equation

We can follow this method with Sage too, guiding it through the calculations.

It's a bit tedious but it saves us from doing the computations by hand.

Set constants:

sage: n = 4/3
sage: h1 = 1
sage: h2 = 1


Two variables in the symbolic ring and an equation:

sage: x, y = SR.var('x, y')
sage: g = sqrt(x^2 + y^2) + n * sqrt(x^2 + (h1 + h2 - y)^2) - (h1 + n * h2) == 0
sage: g
4/3*sqrt(x^2 + (y - 2)^2) + sqrt(x^2 + y^2) - 7/3 == 0


Solve does not help much:

sage: solve(g.subs({x: 1/5}), y)
[sqrt(25*y^2 - 100*y + 101) == -3/4*sqrt(25*y^2 + 1) + 35/4]


Substitute:

sage: h = g.subs({x: 1/5})
sage: h
4/15*sqrt(25*(y - 2)^2 + 1) + 1/5*sqrt(25*y^2 + 1) - 7/3 == 0


Get rid of denominators:

sage: hh = 15 * h
sage: hh
4*sqrt(25*(y - 2)^2 + 1) + 3*sqrt(25*y^2 + 1) - 35 == 0


Give names to the three summands:

sage: a, b, c = hh.lhs().operands()
sage: a, b, c
(4*sqrt(25*(y - 2)^2 + 1), 3*sqrt(25*y^2 + 1), -35)


Isolate one and square both sides:

sage: j = a^2 == (b + c)^2
sage: j
400*(y - 2)^2 + 16 == (3*sqrt(25*y^2 + 1) - 35)^2


Expand:

sage: jj = j.expand()
sage: jj
400*y^2 - 1600*y + 1616 == 225*y^2 - 210*sqrt(25*y^2 + 1) + 1234


Compute the difference of both sides (that should be zero):

sage: k = jj.lhs() - jj.rhs()
sage: k
175*y^2 - 1600*y + 210*sqrt(25*y^2 + 1) + 382


Give names to the four summands:

sage: aa, bb, cc, dd = k.operands()
sage: aa, bb, cc, dd
(175*y^2, -1600*y, 210*sqrt(25*y^2 + 1), 382)


If the sum is zero, then the difference of squares must be zero:

sage: m = (aa + bb + dd)^2 - (cc)^2
sage: m
(175*y^2 - 1600*y + 382)^2 - 1102500*y^2 - 44100


Expand:

sage: mm = m.expand()
sage: mm
30625*y^4 - 560000*y^3 + 1591200*y^2 - 1222400*y + 101824


Move from the symbolic ring to a proper polynomial ring:

sage: R.<y> = QQ[]
sage: p = R(mm)
sage: p
30625*y^4 - 560000*y^3 + 1591200*y^2 - 1222400*y + 101824


Find the roots as algebraic numbers:

sage: rr = p.roots(QQbar, multiplicities=False)
sage: rr
[0.09455004564983336?,
1.144286167075680?,
2.049032178063518?,
14.99784589492526?]


Substitute in the original equation to know which give zero:

sage: ss = [h.subs({y: r}).lhs() for r in rr]
sage: ss
[4/15*sqrt(91.76848821332630?) + 1/5*sqrt(1.223492778309640?) - 7/3,
1/5*sqrt(33.73477080401876?) + 4/15*sqrt(19.30615409645079?) - 7/3,
1/5*sqrt(105.9633216684931?) + 4/15*sqrt(1.060103862141313?) - 7/3,
1/5*sqrt(5624.384537198158?) + 4/15*sqrt(4224.599947705633?) - 7/3]


The symbolic ring keeps things too symbolic, move to algebraic numbers:

sage: tt = [QQbar(s) for s in ss]
sage: tt
[0.4424464312541602?, 0.?e-16, 0.?e-16, 29.99835872096514?]


Exactify to make sure things that look like zero are actually zero:

sage: _ = [t.exactify() for t in tt]
sage: tt
[0.4424464312541602?, 0, 0, 29.99835872096514?]


Select the roots of the transformed equation that are actually roots of the original equation:

sage: zz = [r for i, r in enumerate(rr) if tt[i].is_zero()]
sage: zz
[1.144286167075680?, 2.049032178063518?]


These are algebraic numbers:

sage: za, zb = zz
sage: za.minpoly()
x^4 - 128/7*x^3 + 63648/1225*x^2 - 48896/1225*x + 101824/30625
sage: zb.minpoly()
x^4 - 128/7*x^3 + 63648/1225*x^2 - 48896/1225*x + 101824/30625


Since their minimal polynomial is of degree four, they can be expressed with radicals, although the radical expression is not particularly illuminating.

sage: za.radical_expression()
-1/2450*sqrt((1500625*(12288/42875*I*sqrt(2776173) - 6128672768/1838265625)^(2/3) + 73460800*(12288/42875*I*sqrt(2776173) - 6128672768/1838265625)^(1/3) + 91681024)/(12288/42875*I*sqrt(2776173) - 6128672768/1838265625)^(1/3)) + 1/2*sqrt(-(12288/42875*I*sqrt(2776173) - 6128672768/1838265625)^(1/3) - 91681024/1500625/(12288/42875*I*sqrt(2776173) - 6128672768/1838265625)^(1/3) - 806400/sqrt((1500625*(12288/42875*I*sqrt(2776173) - 6128672768/1838265625)^(2/3) + 73460800*(12288/42875*I*sqrt(2776173) - 6128672768/1838265625)^(1/3) + 91681024)/(12288/42875*I*sqrt(2776173) - 6128672768/1838265625)^(1/3)) + 119936/1225) + 32/7
1/2450*sqrt((1500625*(12288/42875*I*sqrt(2776173) - 6128672768/1838265625)^(2/3) + 73460800*(12288/42875*I*sqrt(2776173) - 6128672768/1838265625)^(1/3) + 91681024)/(12288/42875*I*sqrt(2776173) - 6128672768/1838265625)^(1/3)) - 1/2*sqrt(-(12288/42875*I*sqrt(2776173) - 6128672768/1838265625)^(1/3) - 91681024/1500625/(12288/42875*I*sqrt(2776173) - 6128672768/1838265625)^(1/3) + 806400/sqrt((1500625*(12288/42875*I*sqrt(2776173) - 6128672768/1838265625)^(2/3) + 73460800*(12288/42875*I*sqrt(2776173) - 6128672768/1838265625)^(1/3) + 91681024)/(12288/42875*I*sqrt(2776173) - 6128672768/1838265625)^(1/3)) + 119936/1225) + 32/7

more

Bravo, bravissimo !

I didn't have the courage to write up and post this solution (a bit too high school). But you were absolutely right to post it.

Thank you thank you thank you. You not only solved it, but taught us the way to solve similar problems. Tremendously grateful...

I'll work each step out today. hints that for x=1/5, there may be two possible values of y satisfying g : one almost surely between 1 and 3/2, the other almost surely between 3/2 and 11/5.

We may seek numerical roots of the left-hand side of g:

sage: (g.rhs()-g.lhs()).subs(x=1/5).find_root(1,3/2)
1.1442861670756799
sage: (g.rhs()-g.lhs()).subs(x=1/5).find_root(3/2,11/5)
2.0490321780635146


Finding symbolic solutions is harder. Most algorithms available to sage fail miserably :

sage: solve(g.subs(x==1/5),y)
[sqrt(25*y^2 - 100*y + 101) == -3/4*sqrt(25*y^2 + 1) + 35/4]
sage: solve(g.subs(x==1/5),y, to_poly_solve=True)
[]
sage: solve(g.subs(x==1/5),y, algorithm="fricas")
[sqrt(25*y^2 - 100*y + 101) == -3/4*sqrt(25*y^2 + 1) + 35/4]
sage: solve(g.subs(x==1/5),y, algorithm="giac")
[sqrt(25*y^2 - 100*y + 101) == -3/4*sqrt(25*y^2 + 1) + 35/4]


However, sympy is able to find a solution :

[[y == -1/1225*sqrt(2)*sqrt(1500625*(24/42875*I*sqrt(2776173) - 11970064/1838265625)^(1/3) + 1432516/(24/42875*I*sqrt(2776173) - 11970064/1838265625)^(1/3) + 9182600) + sqrt(-2*(24/42875*I*sqrt(2776173) - 11970064/1838265625)^(1/3) - 50400*sqrt(2)/sqrt(1500625*(24/42875*I*sqrt(2776173) - 11970064/1838265625)^(1/3) + 1432516/(24/42875*I*sqrt(2776173) - 11970064/1838265625)^(1/3) + 9182600) - 2865032/1500625/(24/42875*I*sqrt(2776173) - 11970064/1838265625)^(1/3) + 29984/1225) + 32/7,
y == 1/1225*sqrt(2)*sqrt(1500625*(24/42875*I*sqrt(2776173) - 11970064/1838265625)^(1/3) + 1432516/(24/42875*I*sqrt(2776173) - 11970064/1838265625)^(1/3) + 9182600) - sqrt(-2*(24/42875*I*sqrt(2776173) - 11970064/1838265625)^(1/3) + 50400*sqrt(2)/sqrt(1500625*(24/42875*I*sqrt(2776173) - 11970064/1838265625)^(1/3) + 1432516/(24/42875*I*sqrt(2776173) - 11970064/1838265625)^(1/3) + 9182600) - 2865032/1500625/(24/42875*I*sqrt(2776173) - 11970064/1838265625)^(1/3) + 29984/1225) + 32/7],
ConditionSet(y, Eq(sqrt(25*y**2 + 1)/5 + 4*sqrt(25*(y - 2)**2 + 1)/15 - 7/3, 0), FiniteSet(32/7 + sqrt(29984/1225 - 2*(-11970064/1838265625 + 24*sqrt(2776173)*I/42875)**(1/3) + 576/(7*sqrt(14992/1225 + 2865032/(1500625*(-11970064/1838265625 + 24*sqrt(2776173)*I/42875)**(1/3)) + 2*(-11970064/1838265625 + 24*sqrt(2776173)*I/42875)**(1/3))) - 2865032/(1500625*(-11970064/1838265625 + 24*sqrt(2776173)*I/42875)**(1/3))) + sqrt(14992/1225 + 2865032/(1500625*(-11970064/1838265625 + 24*sqrt(2776173)*I/42875)**(1/3)) + 2*(-11970064/1838265625 + 24*sqrt(2776173)*I/42875)**(1/3)), 32/7 - sqrt(14992/1225 + 2865032/(1500625*(-11970064/1838265625 + 24*sqrt(2776173)*I/42875)**(1/3)) + 2*(-11970064/1838265625 + 24*sqrt(2776173)*I/42875)**(1/3)) - sqrt(29984/1225 - 2*(-11970064/1838265625 + 24*sqrt(2776173)*I/42875)**(1/3) - 576/(7*sqrt(14992/1225 + 2865032/(1500625*(-11970064/1838265625 + 24*sqrt(2776173)*I/42875)**(1/3)) + 2*(-11970064/1838265625 + 24*sqrt(2776173)*I/42875)**(1/3))) - 2865032/(1500625*(-11970064/1838265625 + 24*sqrt(2776173)*I/42875)**(1/3)))))]


The second part of this solution (which is a sympy object, not yet translatable to sage, BTW) is a set of conditions. We shall ignore this for the moment...

The first part gives us two solutions, apparently complex. However, one can easily show formally that those roots are indeed real:

sage: [u.rhs().imag_part().is_zero() for u in Sol]
[True, True]


Leaving the pleasure of finding a "nice" algebraic form of the real part of these roots to masochists, we can assess numerically the proximity of these answers to tjose obtained by numerical means:

sage: [u.rhs().real_part().n() for u in Sol]
[1.14428616707569, 2.04903217806352]


We may also attempt a formal check :

sage: %time [g.lhs().subs(x==1/5).subs(y==s.rhs().real_part()).is_zero() for s in Sol]


but this has no guarantee to even terminate and, anyway, exceeded my patience...

FWIW, "the competition" doesn't do much better :

sage: mathematica.Simplify(mathematica.Solve(g.subs(x==1/5),y))
{
{y -> (-2*(-80 + Sqrt[3748 + 358129/(-1496258 + (128625*I)*Sqrt)^
(1/3) + (-1496258 + (128625*I)*Sqrt)^(1/3)] -
Sqrt[7496 - 358129/(-1496258 + (128625*I)*Sqrt)^(1/3) -
(-1496258 + (128625*I)*Sqrt)^(1/3) -
441000/Sqrt[3748 + 358129/(-1496258 + (128625*I)*Sqrt)^
(1/3) + (-1496258 + (128625*I)*Sqrt)^(1/3)]]))/35},
{y -> (2*(80 + Sqrt[3748 + 358129/(-1496258 + (128625*I)*Sqrt)^
(1/3) + (-1496258 + (128625*I)*Sqrt)^(1/3)] -
Sqrt[7496 - 358129/(-1496258 + (128625*I)*Sqrt)^(1/3) -
(-1496258 + (128625*I)*Sqrt)^(1/3) +
441000/Sqrt[3748 + 358129/(-1496258 + (128625*I)*Sqrt)^
(1/3) + (-1496258 + (128625*I)*Sqrt)^(1/3)]]))/35}}
sage: mathematica.N(mathematica.Solve(g.subs(x==1/5),y))
{{y -> 1.1442861670756819 + 2.8197360586151467*^-16*I},
{y -> 2.049032178063519 - 1.3904088425248892*^-16*I}}


HTH,

more

Thanks a million.

Only think I don't understand is how to use sympy: "sympy is able to find a solution".

How do I install sympy? I am on Sage 8.1 running Ubuntu (actually ElementaryOS)

See I'm getting this:

solve(g.subs(x==1/5),y, algorithm="sympy")
[sqrt(y^2 - 4*y + 101/25) == -3/4*sqrt(y^2 + 1/25) + 7/4]


No need to install SymPy, as Sage either uses the SymPy on your system or ships it (depending how you installed Sage).

SymPy improves with time and SageMath improves with time in terms of what version of SymPy it uses and in terms of how well it interfaces with it.

Sage 8.1 uses SymPy 1.1, Sage 9.1 uses SymPy 1.5, Sage 9.2.beta8 uses SymPy 1.6.