4
$\begingroup$

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.

$\endgroup$
3
  • 1
    $\begingroup$ {a, b} = {a, b} /. First@Solve[...] ? $\endgroup$
    – Öskå
    Jul 8, 2013 at 16:00
  • $\begingroup$ Try First@Solve[{a == 3*c, b == 2*a}, {a, b}]; Reduce[(a < 7 b) /. %] $\endgroup$ Jul 8, 2013 at 16:05
  • $\begingroup$ @Öskå this would work but a and b are not local in this case. $\endgroup$
    – Sulli
    Jul 8, 2013 at 16:12

1 Answer 1

5
$\begingroup$

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.

$\endgroup$
13
  • 1
    $\begingroup$ 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 $\endgroup$
    – Sulli
    Jul 8, 2013 at 16:08
  • $\begingroup$ @su1 I think you want Reduce[a < 7 b /. sol] $\endgroup$
    – Mr.Wizard
    Jul 8, 2013 at 16:11
  • $\begingroup$ @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. $\endgroup$
    – halirutan
    Jul 8, 2013 at 16:14
  • $\begingroup$ @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. $\endgroup$
    – halirutan
    Jul 8, 2013 at 16:16
  • 1
    $\begingroup$ @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}]] $\endgroup$
    – Mr.Wizard
    Jul 8, 2013 at 16:38

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