13
$\begingroup$

In certain problems, we need to solve systems of equations and get results in terms of just selected variables. For example, how could we solve eqn==0 below for c3 and c4 expressed in terms of c1 and c2 only, without a1 or a2?

eqn = {{c1, c2}, {c1, c3}, {c1, c4}, {c2, c3}}.{a1, a2} - {5, 2, -4, -3}

We can select two equations from the system and solve them for a1 and a2, then substitute those results back in...

asoln = Solve[eqn[[{1, 2}]] == 0, {a1, a2}];
b = eqn /. asoln;
Solve[b == 0, {c3, c4}]

(*  {{c3 -> 1/5 (3 c1 + 2 c2), c4 -> 1/5 (9 c1 - 4 c2)}} *)

This approach works but it requires that we find a subset of equations from which a1 and a2 can be solved for unambiguously, which might be difficult. Is it possible to make Solve[] eliminate a1 and a2 for us?

$\endgroup$
7
  • 3
    $\begingroup$ Why not Solve[Eliminate[eqn == 0, {a1, a2}], {c3, c4}]? $\endgroup$
    – Kuba
    Commented May 20, 2015 at 8:56
  • $\begingroup$ Yep, I saw it, nice find. Could you show a minimal example that fails? $\endgroup$
    – Kuba
    Commented May 20, 2015 at 9:02
  • 2
    $\begingroup$ Although this question should remain to improve searches I think it can be marked as a duplicate of (41247) unless a better solution is proposed here. (Despite this you've got my vote on both question and answer for raising awareness of this.) $\endgroup$
    – Mr.Wizard
    Commented May 20, 2015 at 9:34
  • $\begingroup$ @Kuba On rechecking my results, I think the "failures" I experienced happened for other reasons, because I couldn't re-generate any. Maybe what you proposed above is exactly what Solve[] does "under the hood" when when given an elim argument? $\endgroup$ Commented May 20, 2015 at 20:25
  • $\begingroup$ @Kuba What kind of answer do you have? If it is about the elims parameter of Solve or Reduce I think it should go in the older Q&A; or is it a different approach to the same problem? $\endgroup$
    – Mr.Wizard
    Commented May 21, 2015 at 6:38

4 Answers 4

19
$\begingroup$

It turns out Solve[] has a feature that doesn't appear in the online documentation that I could find. A third argument can be added, a list of variables to be eliminated from the solution:

Solve[eqns == 0, {c3, c4}, {a1, a2}]

This yield the same output as above. And I have tested it on problems where solving for a1 and a2 (in order to eliminate them from the system) requires a careful choice of equation subset.

Reduce[] has an analogous third argument discussed here: Behavior of Reduce with variables as domain

$\endgroup$
6
  • 4
    $\begingroup$ It was documented, once upon a time. A pity that it isn't anymore. $\endgroup$ Commented May 20, 2015 at 11:30
  • 3
    $\begingroup$ It is no longer documented because Solve and Reduce now have a third argument indicating domain over which to solve, and this is a very different beast than the original third arg. That earlier third arg is still supported though (as of this time at least). $\endgroup$ Commented May 20, 2015 at 19:26
  • 3
    $\begingroup$ @J. M. It comes as a surprise that the syntax has been changed. Version 7 documentation shows the "old" form. I felt this was worthy of an answer of itself, assuming that this question is not closed, so I posted one below. $\endgroup$
    – Mr.Wizard
    Commented May 21, 2015 at 10:23
  • 1
    $\begingroup$ Finally, a clear case of something not easily found in the documentation. :) $\endgroup$
    – Michael E2
    Commented May 21, 2015 at 14:28
  • 1
    $\begingroup$ As I had posted with another question for Reduce (cf. Mr.Wizard's entry below). The feature is still documented and can be found here. $\endgroup$
    – gwr
    Commented May 21, 2015 at 15:11
5
$\begingroup$

Just to complement the topic:

Solve[ Eliminate[eqn == 0, {a1, a2}], {c3, c4}]
$\endgroup$
1
  • $\begingroup$ I have discovered that Eliminate returns some helpful error messages that Solve (with 3rd argument) does not. Could this be why Wolfram took that 3rd argument out of their Solve[] documentation? mathematica.stackexchange.com/questions/84032/… $\endgroup$ Commented May 21, 2015 at 17:06
4
$\begingroup$

This question is nearly a duplicate of Behavior of Reduce with variables as domain but since it is being addressed separately I shall answer here as well. In the documentation for version 7 (which I used for an extended time) it starts with:

enter image description here

In version 8 this was changed to a domain specification, but where distinguishable the older syntax still works. For now.

$\endgroup$
2
$\begingroup$

Yes, if you include the intermediate variables in the Solve list then Mathematica will try and find solutions for those as well:

sys = {{c1, c2}, {c1, c3}, {c1, c4}, {c2, c3}}.{a1, a2} == {5, 2, -4, -3};
Solve[sys, {c3, c4, a1, a2}]

Gives: {{c3 -> 1/5 (3 c1 + 2 c2), c4 -> 1/5 (9 c1 - 4 c2), a1 -> 5/(c1 - c2), a2 -> -(5/(c1 - c2))}}

$\endgroup$
1
  • 1
    $\begingroup$ My one reservation is that on very large, complicated, non-linear calculations, this method makes MMa grind out results that weren't needed. $\endgroup$ Commented May 20, 2015 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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