7

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.

stevenvh
  • 6,866
  • 5
  • 41
  • 64

3 Answers3

10

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
Artes
  • 57,212
  • 12
  • 157
  • 245
  • 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 :-). – stevenvh Jun 29 '12 at 17:58
  • @stevenvh Anyway MaxExtraConditions in Solve or Reduce without any option satisfies your needs I hope, as you can read from the answer. – Artes Jun 29 '12 at 18:00
  • Nice one! I had no idea of that option +1 – Rojo Jun 29 '12 at 18:33
  • Very useful (+1) – Jens Jun 29 '12 at 18:50
  • Rojo and Jens, Thank You ! – Artes Jun 29 '12 at 20:34
  • @stevenvh A related problem you can find here : http://mathematica.stackexchange.com/questions/7796/solving-for-one-solution-to-a-system-of-polynomials/7798#7798 – Artes Jul 02 '12 at 19:25
  • @Artes - Yes, I saw it too. Thanks for the hint. – stevenvh Jul 03 '12 at 01:42
6

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.

Jens
  • 97,245
  • 7
  • 213
  • 499
5

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

Rojo
  • 42,601
  • 7
  • 96
  • 188
  • 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. – stevenvh Jun 29 '12 at 16:34
  • @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 – Rojo Jun 29 '12 at 16:57
  • ConditionalExpression[y==13/2, y==13/2] is not true?? – stevenvh Jun 29 '12 at 17:08
  • @stevenvh it's as much true as If[x == True, True]. The condition is a prerrequisite, not an assumption – Rojo Jun 29 '12 at 17:24
  • @stevenvh You'll get {x -> ConditionalExpression[1/2, y == 13/2]} using this MaxExtraConditions->All option in Solve. – Artes Jun 29 '12 at 18:25
  • @stevenvh, check the edit – Rojo Jun 30 '12 at 03:44
  • 1
    Pretty much what I said in the comment to Jens's answer... :D – J. M.'s missing motivation Jun 30 '12 at 03:47
  • @J.M. - Your own fault, you should have posted as an answer :-). I never thought of using {y} as domain argument for Solve, but I like it better than nesting an Eliminate inside a Solve. Thanks a bunch! – stevenvh Jun 30 '12 at 05:51
  • @steven: I'd have posted it if Jens hadn't posted his longer version. I'll note that that form for Solve[] has always been supported in Mathematica: Solve[eqns, vars, elims], that is, solve for variables in vars in eqns, while eliminating variables in elims. – J. M.'s missing motivation Jun 30 '12 at 06:34
  • @J.M would you believe me I just read it in the Solve tutorial? :P – Rojo Jun 30 '12 at 11:21
  • I could, Rojo. I could. ;P – J. M.'s missing motivation Jun 30 '12 at 12:53
  • @Rojo, J.M.: I can't find any documentation on using {y} as domain argument for Solve nor on the version Solve[eqns, vars, elims]. Can you tell me where to find it? – balu Sep 19 '14 at 08:41
  • I can't remember now nor have time to look but, it seems the Rojo of the past (look up a couple of comments) saw it in the Solve tutorial in the docs – Rojo Sep 20 '14 at 05:45