# How to eliminate variables when using Solve[]

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?

• Why not Solve[Eliminate[eqn == 0, {a1, a2}], {c3, c4}]? – Kuba May 20 '15 at 8:56
• Yep, I saw it, nice find. Could you show a minimal example that fails? – Kuba May 20 '15 at 9:02
• 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 9: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 6:38

## 4 Answers

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

• It was documented, once upon a time. A pity that it isn't anymore. – J. M. is away May 20 '15 at 11:30
• 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
• @Guess 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
• Finally, a clear case of something not easily found in the documentation. :) – Michael E2 May 21 '15 at 14:28
• 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

Just to complement the topic:

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

• 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/… – Jerry Guern May 21 '15 at 17:06

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:

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

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

• 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