# How to solve an overdetermined system in Mathematica

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?

-

## migrated from stackoverflow.comMay 30 '12 at 15:31

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

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

-

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

-

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

-