1

Assume that I am in Mathematica trying to use the Function Solve and the output is $\{r \rightarrow 2, r\rightarrow 3 , r\rightarrow 4\}$, and from this I want to pick the solution $r=4$ So I have tried using the command $[[3]]$ but what I get is $r\rightarrow 4$. Is there anyway I can something else to only get $4$?

Thanks in advance.

Someone
  • 175
  • 8
  • 1
    See https://mathematica.stackexchange.com/a/18706/4999. Also Values[rules] gives a list of the right-hand sides of the ->. – Michael E2 Aug 08 '20 at 17:27
  • 1
    Say your output is sol. Then r /. sol[[3]]. But you should read the link provided in the previous comment. – PaulCommentary Aug 08 '20 at 17:30
  • You might consider that if you're using r as a variable, r=4 is not so nice, as it defines a value that automatically replaces r with 4 whenever it appears. But having a Rule like r->4 is more useful, because now you have control over your replacement. Thus, an expression like 2r/.r->4 yields 8 without getting in the way of continuing to use r as a variable. – John Doty Aug 08 '20 at 18:24
  • List@@@{r->1, r->3,r->4}[[3,2]], maybe? – user1066 Aug 08 '20 at 19:46

1 Answers1

1

The command Solve gives a list of Rule objects. Rule Objects have the property of replace a symbol by a value. For example,

Solve[(r-2)(r-4)(r-3)==0,r]

gives the output

{{r -> 2}, {r -> 3}, {r -> 4}}

Each sublist in the list contains a Rule object. To Make the the replacement you have to use the ReplaceAll buit-in function :

ReplaceAll[r, Solve[(r - 2)(r - 3)(r - 4)==0,r]]
{2,3,4}

or its shorthand form

r/.Solve[(r - 2)(r -3)(r - 4)==0, r]
{2,3,4}

And so you can extract the desired solution.

DIEGO R.
  • 500
  • 2
  • 6