7
$\begingroup$

For example, if I try to Solve this set of equations

Solve[y == 3 x + 5 && y == -x + 7, {x, y}]

Mathematica gives the right values for x and y. But if I try

Solve[y == 3 x + 5 && y == -x + 7, {x}]

it returns nothing. Why is that? There doesn't seem to be a mathematical reason for it.

$\endgroup$
1
  • 2
    $\begingroup$ Use MaxExtraConditions->Infinity (or even just 1). $\endgroup$ Jun 29, 2012 at 17:40

3 Answers 3

5
$\begingroup$

That syntax makes Mathematica assume that y can be anything, and if y is different from 13/2, then there's no x, so it can't solve it for the general case.

In other words, Solve must return results that satisfy the equalities. Replacing x by 1/2, the solution you're looking for, doesn't make the equalities be true. For them to be true, you need to solve for y also

EDIT

It seems Solve's third argument also serves as a list of variables to eliminate. So, you should do

Solve[y == 3 x + 5 && y == -x + 7, {x}, {y}]

{{x -> 1/2}}

$\endgroup$
13
  • $\begingroup$ I see. Thanks for your answer. But couldn't it return a conditional expression then: ConditionalExpression[1/2, y=13/2]? Too far-fetched? Also, it knows y isn't assigned. $\endgroup$
    – stevenvh
    Jun 29, 2012 at 16:34
  • $\begingroup$ @stevenvh ConditionalExpression doesn't fit here. If we replaced x->ConditionalExpression[1/2, y==13/2], the equality would turn into something like ConditionalExpression[y==13/2, y==13/2], which is not True. There's nothing I can think of that you can put in place of x (that doesn't assign y an actual value) that would render the equation True. However, I see your point and your intentions. What you want is perhaps more suited for Reduce's spirit I think. This is not my strong area so if you are not satisfied, be hopeful and wait for other answers $\endgroup$
    – Rojo
    Jun 29, 2012 at 16:57
  • $\begingroup$ ConditionalExpression[y==13/2, y==13/2] is not true?? $\endgroup$
    – stevenvh
    Jun 29, 2012 at 17:08
  • $\begingroup$ @stevenvh it's as much true as If[x == True, True]. The condition is a prerrequisite, not an assumption $\endgroup$
    – Rojo
    Jun 29, 2012 at 17:24
  • 1
    $\begingroup$ Pretty much what I said in the comment to Jens's answer... :D $\endgroup$ Jun 30, 2012 at 3:47
10
$\begingroup$

Solve works directly with MaxExtraConditions (new in Mathematica 8) option set All or 1 :

Solve[y == 3 x + 5 && y == -x + 7, x, MaxExtraConditions -> All]
{{x -> ConditionalExpression[1/2, y == 13/2]}}

or solving with respect to y :

Solve[ y == 3 x + 5 && y == -x + 7, {y}, MaxExtraConditions -> 1]
{{y -> ConditionalExpression[13/2, x == 1/2]}}

ConditionalExpression is also new in M8.

In another case one has to use Eliminate :

Eliminate[ y == 3 x + 5 && y == -x + 7, {#}] & /@ {x, y}
{2 y == 13, 2 x == 1}

However using Reduce (it is more universal) there is no need for elimination of variables or using any options :

Reduce[y == 3 x + 5 && y == -x + 7, x]
y == 13/2 && x == 1/2
$\endgroup$
7
  • $\begingroup$ Thanks for your answer. You might have guessed that the example wasn't my real problem :-), and eliminating y like in your Solve example is no option. It's a set of 3 equations with 7 variables in $\mathbb{C}$, so I'm not going to eliminate by hand :-). $\endgroup$
    – stevenvh
    Jun 29, 2012 at 17:58
  • $\begingroup$ @stevenvh Anyway MaxExtraConditions in Solve or Reduce without any option satisfies your needs I hope, as you can read from the answer. $\endgroup$
    – Artes
    Jun 29, 2012 at 18:00
  • $\begingroup$ Nice one! I had no idea of that option +1 $\endgroup$
    – Rojo
    Jun 29, 2012 at 18:33
  • $\begingroup$ Very useful (+1) $\endgroup$
    – Jens
    Jun 29, 2012 at 18:50
  • $\begingroup$ Rojo and Jens, Thank You ! $\endgroup$
    – Artes
    Jun 29, 2012 at 20:34
6
$\begingroup$

Maybe this is what you want:

Solve[Eliminate[y == 3 x + 5 && y == -x + 7, y], x]

(* ==> {{x -> 1/2}} *)

I first tell Mathematica to reduce the two equations to one by eliminating the variable I don't want to solve for (y), and then solve for the remaining one, x.

$\endgroup$
1
  • 5
    $\begingroup$ More compactly: Solve[y == 3 x + 5 && y == -x + 7, {x}, {y}]. $\endgroup$ Jun 29, 2012 at 17:35

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