# Different results from Solve depending upon the solution variables listed

Here is a very simple set of equations to solve:

Solve[{x == 2,
y == 0.5,
z == 0.0,
r == 0.4,
dbtot == Sqrt[x^2 + y^2 + z^2],
1/dib == -2/r - 1/(dbtot - r)},
{dbtot, dib}]


{{dbtot -> 2.06155, dib -> -0.178513}}

However, if I merely need dib and list it as the solution variable...

Solve[{x == 2,
y == 0.5,
z == 0.0,
r == 0.4,
dbtot == Sqrt[x^2 + y^2 + z^2],
1/dib == -2/r - 1/(dbtot - r)} , {dib}]


{{dib -> -0.217687}, {dib -> -0.178513}}

This contains the correct solution (dib -> -0.178513) as well as an incorrect answer (based on an impossible negative value of dbtot).

I've tried Assuming and With and incorporating constraints such as dbtot > 0. All are (I think) unnecessary and none of them work.

How do I use Solve to get the single correct value for dib?

An even more elementary illustration of the problem is here:

Solve[{r == .4,
1/dib == -2/r - 1/(2.12132 - r)},
{dib}]


{}

What could be simpler than that?!

• It is weird at first blush. I think I am missing something here. Anyway, you can ask Solve to explicitly eliminate dbtot from the system of equations, and then it will return your expected result: Solve[eq, dib, {dbtot}], where eq is your system of equations, returns {{dib -> -0.178513}}. – MarcoB Sep 21 '15 at 22:33
• @MarcoB Thanks, but I'd really like to understand why the straightforward approach simply does not work for a set of very elementary equations and variable assignments. There should be no need to be smart about eliminating variables in such a situation. – David G. Stork Sep 21 '15 at 22:39
• Replacing Solve with Reduce handles both your cases. I haven't the foggiest as to why Solve is struggling. – Jack LaVigne Sep 21 '15 at 23:33
• @JackLaVigne Thanks... that is a help, but still a bit unsatisfactory. After all, I would like to extract the value of dib using dib /. Reduce[...] but that will not work elegantly. – David G. Stork Sep 21 '15 at 23:41
• Use ToRules with Reduce to get rules rather than equations as results. – Bob Hanlon Sep 21 '15 at 23:51

Solve seems to work better if you separate definitions from equations.

Off[Solve::ratnz]

With[{r = .4, dbtot = 2.06155},
Solve[1/dib == -2/r - 1/(dbtot - r), dib]]

(*  {{dib -> -0.178513}}  *)


In this next example one of the definitions (= vice ==) is embedded in the Solve since it cannot be included with the other definitions since it depends on them.

With[{x = 2, y = 0.5, z = 0.0, r = 0.4},
Solve[dbtot = Sqrt[x^2 + y^2 + z^2];
1/dib == -2/r - 1/(dbtot - r), dib]]

(*  {{dib -> -0.178513}}  *)


Or it can be pulled out

With[{x = 2, y = 0.5, z = 0.0, r = 0.4},
dbtot = Sqrt[x^2 + y^2 + z^2];
Solve[1/dib == -2/r - 1/(dbtot - r), dib]]

(*  {{dib -> -0.178513}}  *)

Clear[dbtot]

With[{x = 2, y = 0.5, z = 0.0, r = 0.4},
Solve[{dbtot == Sqrt[x^2 + y^2 + z^2],
1/dib == -2/r - 1/(dbtot - r)}, {dbtot, dib}]]

(*  {{dbtot -> 2.06155, dib -> -0.178513}}  *)


From

Eliminate[{
x == 2, y == 0.5, z == 0.0, r == 0.4,
dbtot == Sqrt[x^2 + y^2 + z^2],
1/dib == -2/r - 1/(dbtot - r)},
{dbtot}]
(* 4170. dib + 10525. dib^2 == -409. && 5. r == 2. && x == 2. && 2. y == 1. && z == 0. *)


I see that Solve[] (incorrectly) believes dib satisfies a quadratic. This is incorrect squaring of the dib equation after replacement of dbtot with Sqrt[...].

Reduce[], however, does not make this error.

Reduce[{x == 2, y == 0.5, z == 0.0, r == 0.4,
dbtot == Sqrt[x^2 + y^2 + z^2],
1/dib == -2/r - 1/(dbtot - r)},
{dib}]
(* z == 0 && y == 0.5 && x == 2. && r == 0.4 && dbtot == 2.06155 && dib == -0.178513 &*)

Reduce[{x == 2, y == 0.5, z == 0.0, r == 0.4,
dbtot == Sqrt[x^2 + y^2 + z^2],
1/dib == -2/r - 1/(dbtot - r)},
{dbtot}]
(* z == 0 && y == 0.5 && x == 2. && r == 0.4 && dib == -0.178513 && dbtot == 2.06155 *)


We also get a message, Reduce::ratnz: suggesting how Solve[] confused itself: it replaced the system with a symbolic representation and picked up a spurious root doing so.