13

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?

xyz
  • 605
  • 4
  • 38
  • 117
Jerry Guern
  • 4,602
  • 18
  • 47
  • 3
    Why not Solve[Eliminate[eqn == 0, {a1, a2}], {c3, c4}]? – Kuba May 20 '15 at 08:56
  • Yep, I saw it, nice find. Could you show a minimal example that fails? – Kuba May 20 '15 at 09:02
  • 2
    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.) – Mr.Wizard May 20 '15 at 09:34
  • @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? – Jerry Guern May 20 '15 at 20:25
  • @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? – Mr.Wizard May 21 '15 at 06:38
  • @Kuba Post it here. – Mr.Wizard May 21 '15 at 06:47
  • @Mr.Wizard ok, thanks. – Kuba May 21 '15 at 06:53

4 Answers4

18

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

Jerry Guern
  • 4,602
  • 18
  • 47
  • 4
    It was documented, once upon a time. A pity that it isn't anymore. – J. M.'s missing motivation May 20 '15 at 11:30
  • @J.M. Yeeeah... Why is that? I've been using MMa less than a year, so a lot of conspicuous absences in the documentation and features are bewildering to me. Was there some Dilbert-esque political battle at WRC? – Jerry Guern May 20 '15 at 13:32
  • 3
    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). – Daniel Lichtblau May 20 '15 at 19:26
  • 2
    @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. – Mr.Wizard May 21 '15 at 10:23
  • 1
    Finally, a clear case of something not easily found in the documentation. :) – Michael E2 May 21 '15 at 14:28
  • 1
    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. – gwr May 21 '15 at 15:11
5

Just to complement the topic:

Solve[ Eliminate[eqn == 0, {a1, a2}], {c3, c4}]
Kuba
  • 136,707
  • 13
  • 279
  • 740
  • 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? http://mathematica.stackexchange.com/questions/84032/how-to-speed-or-help-solve-when-it-is-stuck – Jerry Guern May 21 '15 at 17:06
3

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.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
2

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

SPPearce
  • 5,653
  • 2
  • 18
  • 40
  • 1
    My one reservation is that on very large, complicated, non-linear calculations, this method makes MMa grind out results that weren't needed. – Jerry Guern May 20 '15 at 19:14