Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to understand the reasons and find a way to avoid such behaviour of the Solve function in Mathematica 8.

Solve[x + y + z == 5 && y == 3 , {x, y}]
(* Out[1]= {{x -> 2 - z, y -> 3}} *)

Solve[x + y + z == 5 && y == 3 , {x}]
(* Out[2]= {} (why empty?) *)

I need to get an answer in the second case. Any suggestions on how to persuade the program to calculate an answer that is even simpler than the first?

share|improve this question

migrated from May 30 '12 at 15:31

This question came from our site for professional and enthusiast programmers.

up vote 7 down vote accepted

In general, when the system of equations is overdetermined, you have an optimization problem and would therefore not expect Solve or Reduce to be the right tools because the equations are likely not solvable "exactly" but only in some "best possible" way.

You would then instead formulate the optimization problem in terms of a merit function that you attempt to extremize. For example, in a system of linear equations you would want to minimize the sum of squares of the error for each equation.

A simple implementation of this would be to use Minimize, and for more specialized solutions one can use PseudoInverse or SingularValueDecomposition. Since your example is very simple, I'll just try to illustrate how to treat it with Minimize:

Last@Minimize[{(x + y + z - 5)^2, y == 3}, {x}]

Here the y == 3 is entered as a constraint whereas the other equation is converted to a squared "merit function" that has to be minimized. That last function would in general be a sum of several squares if you have more equations.

The result is a Piecewise expression of which we only need the part that actually satisfies y == 3:

$ \left\{ x\to\begin{cases} 2-z & y=3\\ \text{Indeterminate} & \text{True} \end{cases}\right\} $

An alternative would be:

Last@Minimize[{(x + y + z - 5)^2 + (y - 3)^2}, {x}]

$ \{x \to 5 - y - z\}$

share|improve this answer

You could use Reduce in this case:

Reduce[x + y + z == 5 && y == 3, {x}]

(* output: y == 3 && x == 2 - z *)

ToRules will transform this expression to a list of rules, e.g.

ToRules[Reduce[x + y + z == 5 && y == 3, {x}]]

(* output: {y -> 3, x -> 2 - z} *)
share|improve this answer

You'll find the explanation in the documentation page of Solve under Options -> MaxExtraConditions, as well as under Possible Issues (in a less obvious form).

You can coerce Solve to give you a solution like this:

In[1]:= Solve[x + y + z == 5 && y == 3, {x}, MaxExtraConditions -> All]
Out[1]= {{x -> ConditionalExpression[2 - z, y == 3]}}

The MaxExtraConditions -> All setting will allow Solve to generate extra conditions, and return a solution that is valid only under these special conditions. By default Solve does not attempt this (usually computationally expensive) manoeuvre. In your specific example it tells you that there are no solutions because there are indeed no solutions unless the very specific condition that y is equal to 3 is true.

I realize that this interpretation of the equations is counterintuitive at first.

The other solution is using Reduce which always attempts to generate all conditions that are necessary for the solution to be valid, even very special ones (e.g. that a must not be zero for a x == 1 to have a solution).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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