Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

solve gives 1, 2, or 3 answers depending if one value in my equation is a real, rational, or integer

Any idea why solve() is giving me different answers depending on what the value of 'g' is in my equations (see code below)? If g is a variable, an integer, or 1.0 then I get the two correct solutions (i.e. +&- a sqrt). If g is 1/4 or 1.1 I get an incomplete answer which I can get the two correct solutions by doing a solve on the solved solution. If g is a real whole number like 2,3 then I get 3 solutions: the two correct ones and a spurious one. I only discovered this behaviour when I tried eqs=[0==f(x).diff(x)(x=0),0==f(x)(x=0)]; solve(eqs,T_1,x_0) and sage hanged just calculating forever. Is there any general rules about how I should enter variables in equations for use with solve()? I want to know beforehand if something like /4 will cause problems in which case I could use v* instead and later, after using solve(), substitute with .subs(v=1/4)?

reset()
forget()
var('T_1,T_2,x_0,h,d,g')
def f(x):
    return B_0 + B_1 * (x - x_0) + B_2 * sqrt((x - x_0) ^ 2 + g*d)

B_0 = T_2 * (x_0 - h)
B_1 = (T_1 + T_2) / 2
B_2 = (T_2 - T_1) / 2

def check_solutions(gval):
    print("g = {0}".format(gval)) 
    eqs=[0==f(x).diff(x)(x=0,g=gval),0==f(x)(x=0,g=gval)]
    print("******eqs:")
    print(eqs)
    sol1 = solve(eqs[0],T_1)
    print("******Solution for T_1:")
    print(sol1)

    print("******sub T_1 solution into 2nd equation:")
    print(eqs[1].subs(sol1[0]))
    assume(h>0)
    sol2 = solve(eqs[1].subs(sol1[0]),x_0)
    print("******Solutions for x_0")
    print(sol2)
    print("******Solve again for x_0")
    print([solve(sol2[i],x_0) for i in range(len(sol2))])
    print("*****************************************")

gvals=[g,1,1.0,2.0,2,1/4,1.1]
for i in range(len(gvals)):
    check_solutions(gvals[i])

running this I get:

g = g
******eqs:
[0 == 1/2*(T_1 - T_2)*x_0/sqrt(d*g + x_0^2) + 1/2*T_1 + 1/2*T_2, 0 == -(h - x_0)*T_2 - 1/2*(T_1 + T_2)*x_0 - 1/2*(T_1 - T_2)*sqrt(d*g + x_0^2)]
******Solution for T_1:
[
T_1 == (x_0 - sqrt(d*g + x_0^2))*T_2/(x_0 + sqrt(d*g + x_0^2))
]
******sub T_1 solution into 2nd equation:
0 == -(h - x_0)*T_2 - 1/2*((x_0 - sqrt(d*g + x_0^2))*T_2/(x_0 + sqrt(d*g + x_0^2)) + T_2)*x_0 - 1/2*((x_0 - sqrt(d*g + x_0^2))*T_2/(x_0 + sqrt(d*g + x_0^2)) - T_2)*sqrt(d*g + x_0^2)
******Solutions for x_0
[
x_0 == -sqrt(-d*g + h^2),
x_0 == sqrt(-d*g + h^2)
]
******Solve again for x_0
[[x_0 == -sqrt(-d*g + h^2)], [x_0 == sqrt(-d*g + h^2)]]
*****************************************
g = 1
******eqs:
[0 == 1/2*(T_1 - T_2)*x_0/sqrt(x_0^2 + d) + 1/2*T_1 + 1/2*T_2, 0 == -(h - x_0)*T_2 - 1/2*(T_1 + T_2)*x_0 - 1/2*(T_1 - T_2)*sqrt(x_0^2 + d)]
******Solution for T_1:
[
T_1 == (x_0 - sqrt(x_0^2 + d))*T_2/(x_0 + sqrt(x_0^2 + d))
]
******sub T_1 solution into 2nd equation:
0 == -(h - x_0)*T_2 - 1/2*((x_0 - sqrt(x_0^2 + d))*T_2/(x_0 + sqrt(x_0^2 + d)) + T_2)*x_0 - 1/2*((x_0 - sqrt(x_0^2 + d))*T_2/(x_0 + sqrt(x_0^2 + d)) - T_2)*sqrt(x_0^2 + d)
******Solutions for x_0
[
x_0 == -sqrt(h^2 - d),
x_0 == sqrt(h^2 - d)
]
******Solve again for x_0
[[x_0 == -sqrt(h^2 - d)], [x_0 == sqrt(h^2 - d)]]
*****************************************
g = 1.00000000000000
******eqs:
[0 == 1/2*(T_1 - T_2)*x_0/sqrt(x_0^2 + d) + 1/2*T_1 + 1/2*T_2, 0 == -(h - x_0)*T_2 - 1/2*(T_1 + T_2)*x_0 - 1/2*(T_1 - T_2)*sqrt(x_0^2 + d)]
******Solution for T_1:
[
T_1 == (x_0 - sqrt(x_0^2 + d))*T_2/(x_0 + sqrt(x_0^2 + d))
]
******sub T_1 solution into 2nd equation:
0 == -(h - x_0)*T_2 - 1/2*((x_0 - sqrt(x_0^2 + d))*T_2/(x_0 + sqrt(x_0^2 + d)) + T_2)*x_0 - 1/2*((x_0 - sqrt(x_0^2 + d))*T_2/(x_0 + sqrt(x_0^2 + d)) - T_2)*sqrt(x_0^2 + d)
******Solutions for x_0
[
x_0 == -sqrt(h^2 - d),
x_0 == sqrt(h^2 - d)
]
******Solve again for x_0
[[x_0 == -sqrt(h^2 - d)], [x_0 == sqrt(h^2 - d)]]
*****************************************
g = 2.00000000000000
******eqs:
[0 == 1/2*(T_1 - T_2)*x_0/sqrt(x_0^2 + 2.00000000000000*d) + 1/2*T_1 + 1/2*T_2, 0 == -(h - x_0)*T_2 - 1/2*(T_1 + T_2)*x_0 - 1/2*(T_1 - T_2)*sqrt(x_0^2 + 2.00000000000000*d)]
******Solution for T_1:
[
T_1 == (x_0 - sqrt(x_0^2 + 2*d))*T_2/(x_0 + sqrt(x_0^2 + 2*d))
]
******sub T_1 solution into 2nd equation:
0 == -(h - x_0)*T_2 - 1/2*((x_0 - sqrt(x_0^2 + 2*d))*T_2/(x_0 + sqrt(x_0^2 + 2*d)) + T_2)*x_0 - 1/2*((x_0 - sqrt(x_0^2 + 2*d))*T_2/(x_0 + sqrt(x_0^2 + 2*d)) - T_2)*sqrt(x_0^2 + 2.00000000000000*d)
******Solutions for x_0
[
x_0 == -sqrt(h^2 - 2*d),
x_0 == sqrt(h^2 - 2*d),
x_0 == -sqrt(x_0^2 + 2*d)
]
******Solve again for x_0
[[x_0 == -sqrt(h^2 - 2*d)], [x_0 == sqrt(h^2 - 2*d)], [x_0 == -sqrt(x_0^2 + 2*d)]]
*****************************************
g = 2
******eqs:
[0 == 1/2*(T_1 - T_2)*x_0/sqrt(x_0^2 + 2*d) + 1/2*T_1 + 1/2*T_2, 0 == -(h - x_0)*T_2 - 1/2*(T_1 + T_2)*x_0 - 1/2*(T_1 - T_2)*sqrt(x_0^2 + 2*d)]
******Solution for T_1:
[
T_1 == (x_0 - sqrt(x_0^2 + 2*d))*T_2/(x_0 + sqrt(x_0^2 + 2*d))
]
******sub T_1 solution into 2nd equation:
0 == -(h - x_0)*T_2 - 1/2*((x_0 - sqrt(x_0^2 + 2*d))*T_2/(x_0 + sqrt(x_0^2 + 2*d)) + T_2)*x_0 - 1/2*((x_0 - sqrt(x_0^2 + 2*d))*T_2/(x_0 + sqrt(x_0^2 + 2*d)) - T_2)*sqrt(x_0^2 + 2*d)
******Solutions for x_0
[
x_0 == -sqrt(h^2 - 2*d),
x_0 == sqrt(h^2 - 2*d)
]
******Solve again for x_0
[[x_0 == -sqrt(h^2 - 2*d)], [x_0 == sqrt(h^2 - 2*d)]]
*****************************************
g = 1/4
******eqs:
[0 == 1/2*(T_1 - T_2)*x_0/sqrt(x_0^2 + 1/4*d) + 1/2*T_1 + 1/2*T_2, 0 == -(h - x_0)*T_2 - 1/2*(T_1 + T_2)*x_0 - 1/2*(T_1 - T_2)*sqrt(x_0^2 + 1/4*d)]
******Solution for T_1:
[
T_1 == (x_0 - sqrt(x_0^2 + 1/4*d))*T_2/(x_0 + sqrt(x_0^2 + 1/4*d))
]
******sub T_1 solution into 2nd equation:
0 == -(h - x_0)*T_2 - 1/2*((x_0 - sqrt(x_0^2 + 1/4*d))*T_2/(x_0 + sqrt(x_0^2 + 1/4*d)) + T_2)*x_0 - 1/2*((x_0 - sqrt(x_0^2 + 1/4*d))*T_2/(x_0 + sqrt(x_0^2 + 1/4*d)) - T_2)*sqrt(x_0^2 + 1/4*d)
******Solutions for x_0
[
1/2*sqrt(4*x_0^2 + d) == h
]
******Solve again for x_0
[[x_0 == -1/2*sqrt(4*h^2 - d), x_0 == 1/2*sqrt(4*h^2 - d)]]
*****************************************
g = 1.10000000000000
******eqs:
[0 == 1/2*(T_1 - T_2)*x_0/sqrt(x_0^2 + 1.10000000000000*d) + 1/2*T_1 + 1/2*T_2, 0 == -(h - x_0)*T_2 - 1/2*(T_1 + T_2)*x_0 - 1/2*(T_1 - T_2)*sqrt(x_0^2 + 1.10000000000000*d)]
******Solution for T_1:
[
T_1 == (x_0 - sqrt(x_0^2 + 11/10*d))*T_2/(x_0 + sqrt(x_0^2 + 11/10*d))
]
******sub T_1 solution into 2nd equation:
0 == -(h - x_0)*T_2 - 1/2*((x_0 - sqrt(x_0^2 + 11/10*d))*T_2/(x_0 + sqrt(x_0^2 + 11/10*d)) + T_2)*x_0 - 1/2*((x_0 - sqrt(x_0^2 + 11/10*d))*T_2/(x_0 + sqrt(x_0^2 + 11/10*d)) - T_2)*sqrt(x_0^2 + 1.10000000000000*d)
******Solutions for x_0
[
1/10*sqrt(10*x_0^2 + 11*d)*sqrt(10) == h
]
******Solve again for x_0
[[x_0 == -1/10*sqrt(10*h^2 - 11*d)*sqrt(10), x_0 == 1/10*sqrt(10*h^2 - 11*d)*sqrt(10)]]
*****************************************