4
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ just Solve[{g == (b d + f - e b)/(a - c), b == 0}, {g, b}] gives your final result $\endgroup$ Jun 14, 2012 at 11:53
  • 2
    $\begingroup$ 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 %.. $\endgroup$ Jun 14, 2012 at 12:01
  • $\begingroup$ In this particular example you could do sol=Solve[g a - b d - f == g c - b e, g], sol /. b -> 0 $\endgroup$
    – Heike
    Jun 14, 2012 at 12:20

4 Answers 4

3
$\begingroup$

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

$\endgroup$
5
$\begingroup$
result = Solve[g a - b d - f == g c - b e, g]
Solve[{g==(g/.First@result),b==0},{b,g}]
$\endgroup$
1
  • $\begingroup$ Yes, that'it, thanks to you both, Peter and celtschk!! $\endgroup$ Jun 14, 2012 at 12:11
4
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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