Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'd like to know how to use the results of Solve in later calculations. Here's what I'm doing now:

  • This is my expression

    g == Solve[g a - b d - f == g c - b e, g]
    

    which gives the result {{g -> (b d - b e + f)/(a - c)}}

  • Next, using the above result, I do

    Solve[{g == (b d + f - e b)/(a - c), b == 0}, {g, b}]
    

    which yields the final result: {{g -> f/(a - c), b -> 0}}

I want to do the same calculation without putting the first result into the second calculation "manually". There must be way to tell Mathematica that it has to take g and insert it into the next calculation by itself. However, I failed. I tried things like % or /. but it didn't work.

share|improve this question
    
just Solve[{g == (b d + f - e b)/(a - c), b == 0}, {g, b}] gives your final result –  belisarius Jun 14 '12 at 11:53
2  
I know, see above. But my question was how to use the previous result (I need this for longer calculations). As the result to be used is not just g=5, I don't know how to do this. If it was g=5, eg, I could do it using %.. –  Stefanie Schubert Jun 14 '12 at 12:01
    
In this particular example you could do sol=Solve[g a - b d - f == g c - b e, g], sol /. b -> 0 –  Heike Jun 14 '12 at 12:20

4 Answers 4

up vote 3 down vote accepted

Are you looking for something like this:

g1 = Solve[g a - b d - f == g c - b e, g][[1, 1]] /. Rule -> Equal
Solve[{g1, b == 0}, {g, b}]

g1 is the solution of your first equation, made from a Rule into an Equal. You can even nest the two steps into one:

Solve[{Solve[g a - b d - f == g c - b e, g][[1, 1]] /. Rule -> Equal, b == 0}, {g, b}]

Out= {{g -> f/(a - c), b -> 0}}

share|improve this answer
result = Solve[g a - b d - f == g c - b e, g]
Solve[{g==(g/.First@result),b==0},{b,g}]
share|improve this answer
    
Yes, that'it, thanks to you both, Peter and celtschk!! –  Stefanie Schubert Jun 14 '12 at 12:11

First it is resonable to do a simplification, adding the second argument to Simplify or to Refine i.e. an assumption :

Simplify[g a - b d - f == g c - b e, b == 0]
a g == f + c g    

So to make the task in one step try this :

Solve[ Simplify[ g a - b d - f == g c - b e, b == 0], g]
{{g -> f/(a - c)}}

or directly solve a system, e.g.

Solve[{g a - b d - f == g c - b e, b == 0}, {g, b}]
share|improve this answer

You might use a new function toEquals in (inverse) analogy to the built-in ToRules:

toEquals[x : {{__Rule} ..}] := Or @@ And @@@ (x /. Rule -> Equal)
toEquals[x_] := x

This will convert the output of Solve into a form that may be used as input.

Then:

Solve[g a - b d - f == g c - b e, g]

Solve[toEquals[%] && b == 0, {g, b}]

You could potentially use Fold to automate this substitution:

FoldList[
  Solve[toEquals[#] && #2[[1]], #2[[2]]] &,
  True,
  {
   {g a - b d - f == g c - b e, g},
   {b == 0, {g, b}}
  }
] // Rest
{
 {{g -> (b d - b e + f)/(a - c)}},
 {{g -> f/(a - c), b -> 0}}
}

That's a lot of code for a simple operation but if you need many layers of substitution it may help.

share|improve this answer

Your Answer

 
discard

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.