4
$\begingroup$

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}}

This is the correct answer.

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?!

$\endgroup$
  • $\begingroup$ 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}}. $\endgroup$ – MarcoB Sep 21 '15 at 22:33
  • $\begingroup$ @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. $\endgroup$ – David G. Stork Sep 21 '15 at 22:39
  • $\begingroup$ Replacing Solve with Reduce handles both your cases. I haven't the foggiest as to why Solve is struggling. $\endgroup$ – Jack LaVigne Sep 21 '15 at 23:33
  • $\begingroup$ @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. $\endgroup$ – David G. Stork Sep 21 '15 at 23:41
  • $\begingroup$ Use ToRules with Reduce to get rules rather than equations as results. $\endgroup$ – Bob Hanlon Sep 21 '15 at 23:51
5
$\begingroup$

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}}  *)
| improve this answer | |
$\endgroup$
2
$\begingroup$

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.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.