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.

The following expression allows me to extract the value of x and y:

{x, y, u, v} /. #2 & @@ 
 List[Minimize[{x - 2 y^2, 1500 >= x >= y >= 0}, {x, y}], 
   List[FindRoot[{v - Sin[u] == 0, v + u == 30}, {u, 0}, {v, 20}]]]~
  Flatten~1

On the other hand, this expression allows me to extract the value of u and v

{x, y, u, v} /. #3 & @@ 
 List[Minimize[{x - 2 y^2, 1500 >= x >= y >= 0}, {x, y}], 
   List[FindRoot[{v - Sin[u] == 0, v + u == 30}, {u, 0}, {v, 20}]]]~
  Flatten~1

I want to extract the 4 values at once modifing the code, you know {x,y,u,v} = {1500, 1500, 30.675, -0.674984}

Does anyone know how to do it? thanks!

share|improve this question
    
FYI: you can only Accept one answer. I think I just saw you attempt to Accept all three. –  Mr.Wizard Jul 25 at 7:42
    
Sorry I didn't know that, thanks Mr. Wizard. –  YoJesseP Jul 25 at 7:43

4 Answers 4

up vote 1 down vote accepted
input = Flatten[List[Minimize[{x - 2 y^2, 1500 >= x >= y >= 0}, {x, y}], 
                     List[FindRoot[{v - Sin[u] == 0, v + u == 30}, {u, 0}, {v, 20}]]], 
                1];

{x, y, u, v} /. Join @@ Rest @ input
(* {1500, 1500, 30.675, -0.674984} *)

or

{x, y, u, v} /. Join@##2 & @@ input
(* {1500, 1500, 30.675, -0.674984} *)

or

{x, y, u, v} /. #2 /. #3 & @@ input
(* {1500, 1500, 30.675, -0.674984} *)
share|improve this answer
    
I like the 3rd one! thank you! –  YoJesseP Jul 25 at 7:42

If you don't need the value of the minimization, just throw it away and use a flattened list of rules:

{x, y, u, v} /. 
 Flatten[{Last@Minimize[{x - 2 y^2, Q >= x >= y >= 0}, {x, y}], 
   FindRoot[{v - Sin[u] == 0, v + u == 30}, {u, 0}, {v, 20}]}]
share|improve this answer
    
Thank you. It worked. But, I could need the value of the minimization eventually :/ –  YoJesseP Jul 25 at 6:41

That code looks familiar. ;-)

There are many different ways to approach this, as you can see from the answers you're getting. Fundamentally it is important to learn how to manipulate arbitrary expressions rather than carrying around specific bits of code or you will end up in the trap of cargo cult programming.

You have output that looks like this:

r1 = Minimize[{x - 2 y^2, 1500 >= x >= y >= 0}, {x, y}]
r2 = FindRoot[{v - Sin[u] == 0, v + u == 30}, {u, 0}, {v, 20}]
{-4498500, {x -> 1500, y -> 1500}}

{u -> 30.675, v -> -0.674984}

The first output is a list of two elements: a numeric value and a list of replacement rules.
The second output is simply a list of rules.

My original method was used to extract both the numeric value and the list of rules from the first result, i.e.:

{x, y, #} /. #2 & @@ r1

Assuming no conflict between the replacements you can simply string together additional ReplaceAll operations:

{x, y, u, v, #} /. #2 & @@ r1 /. r2
{1500, 1500, 30.675, -0.674984, -4498500}

Note that because of the order of operations the code above parses as:

({x, y, u, v, #} /. #2 & @@ r1) /. r2

(r1 /. r2) would not produce the result you intend. You could also write:

{x, y, u, v, #} /. #2 /. r2 & @@ r1

These methods work fine for a one-off operation but there is another format you may wish to use: convert all results to a standard list of replacement rules. We need to convert the output of Minimize into this form, so I will use the label "min" for the first value:

r1 = Join[{"min" -> #}, #2] & @@ r1
{"min" -> -4498500, x -> 1500, y -> 1500}

From here you have many options. You can simply apply both sets of rules in sequence:

{x, y, u, v, "min"} /. r1 /. r2
{1500, 1500, 30.675, -0.674984, -4498500}

Or you could join the rule lists for ease of later application:

rules = Join[r1, r2]
{"min" -> -4498500, x -> 1500, y -> 1500, u -> 30.675, v -> -0.674984}

You can even use some more exotic methods such as OptionValue which can be useful in certain cases such as handling defaults or issuing a message when a value is missing:

OptionValue[{r1, r2}, {x, y, u, v, "min"}]
{1500, 1500, 30.675, -0.674984, -4498500}
share|improve this answer

Try this:

Cases[List[Minimize[{x - 2 y^2, 1500 >= x >= y >= 0}, {x, y}], 
  List[FindRoot[{v - Sin[u] == 0, v + u == 30}, {u, 0}, {v, 20}]]], 
 Rule[x_, y_] :> y, {0, -1}]

or

{x, y, u, v} /. 
 Level[List[Minimize[{x - 2 y^2, 1500 >= x >= y >= 0}, {x, y}], 
   List[FindRoot[{v - Sin[u] == 0, v + u == 30}, {u, 0}, {v, 
      20}]]], {-2}]
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.