4

Let's say I solve a system:

Solve[{a == 3* c, b == 2 *a}, {a, b}]

and then want to see if the values found for a and b satisfy an inequality:

Reduce[a < 7 b]

What I would usually do is copy and paste by hand the result of the Solve[] to make it available to Reduce:

Solve[{a == 3* c, b == 2 *a}, {a, b}]
(output) {{a -> 3 c, b -> 6 c}}

a = 3 c;
b = 6 c;
Reduce[a < 7 b]

but there must be a better way to do this? I would also like all those variables (a,b and c) to stay local because I will have to solve a lot of similar equations with the same variable names on the same notebook and I wouldn't want the values to mix.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Sulli
  • 2,185
  • 14
  • 28

1 Answers1

5

There is a reason why Solve returns a list of rules ;-)

sol = Solve[{a == 3*c, b == 2*a}, {a, b}]
Reduce[a < 7 b /. First[sol]]

To be a bit more verbose in my answer: a thing like a->b is called a Rule and it can be used to replace a with b in expressions. Hopefully, now it makes more sense to you why most solving or minimization routines return rules.

halirutan
  • 112,764
  • 7
  • 263
  • 474
  • 1
    but if I try that I get as output {c \[Element] Reals && 3 c < 42 c} which is not the same as the "normal" output c > 0 – Sulli Jul 08 '13 at 16:08
  • @su1 I think you want Reduce[a < 7 b /. sol] – Mr.Wizard Jul 08 '13 at 16:11
  • @su1 I was to fast, but updated my answer. Of course you first want to replace the solution and then you want to reduce it. – halirutan Jul 08 '13 at 16:14
  • @su1 Additionally, note that in this case it doesn't matter whether you leave out the First as MrWizard did in his comment. In general solve returns a list of solutions, thats why you get a double nested list. – halirutan Jul 08 '13 at 16:16
  • ok that would be the best solution except that sol is not a local variable. I tried Module[{sol}, sol = Solve[{a == 3*c, b == 2*a}, {a, b}] Reduce[a < 7 b /. sol] ] but this gives messages like "sol$69027 is neither a list of replacement rules nor a valid dispatch table" – Sulli Jul 08 '13 at 16:22
  • 1
    @su1 you are missing a ; in that line. Try: Module[{sol}, sol = Solve[{a == 3*c, b == 2*a}, {a, b}]; Reduce[a < 7 b /. sol]] or simply: Reduce[a < 7 b /. Solve[{a == 3*c, b == 2*a}, {a, b}]] – Mr.Wizard Jul 08 '13 at 16:38
  • @halirutan I think this question is a duplicate (see links above). Please let me know if you disagree. – Mr.Wizard Jul 08 '13 at 16:40
  • @Mr.Wizard I agree completely. I would have been surprised if this wasn't asked before. I would prefer to close it as dup of the newbie pitfalls post because users which are redirected to this will definitely learn something else too. What do you think? – halirutan Jul 08 '13 at 19:02
  • @Mr.Wizard OK thanks almost there... why if I add a condition in the Reduce, for example Module[{sol}, sol = Solve[{a == 3*c, b == 2*a}, {a, b}]; Reduce[{a < 7 b, c == 2} /. sol] ] I get the error message "Reduce::naqs: "{3\ c<42\ c,c==2} is not a quantified system of equations and inequalities""? How to reduce multiple equations but keeping the replacement rule? – Sulli Jul 08 '13 at 19:42
  • @su1 The issue here is that Reduce[{{. . .}}] is not valid input, while Reduce[{. . .}] is, and {a < 7 b, c == 2} /. sol produces {{3 c < 42 c, c == 2}}. If you only want the first solution you could use Reduce[{a < 7 b, c == 2} /. First[sol]] or if you want all (in the case of multiple solutions) you could use Map[Reduce, {a < 7 b, c == 2} /. sol]. – Mr.Wizard Jul 10 '13 at 00:10
  • @halirutan We got the best possible result, it being flagged as a duplicate of both due to close votes for each. Incidentally that's something a moderator cannot do by himself. – Mr.Wizard Jul 10 '13 at 00:11
  • @Mr.Wizard yes, I just saw it now and was surprised this is possible now. – halirutan Jul 10 '13 at 00:12
  • Yes, it's still possible during closure, but not afterward. I believe that each question that is marked as a duplicate during close voting will appear in the "already has an answer" header. – Mr.Wizard Jul 10 '13 at 00:14