2
$\begingroup$

I'm new in Mathematica 10.

So far, I have a two variable-function and I've use Minimize to find the optimal solution.

Nevertheless, I'd like to make a sensibility analysis and study how this optimal solution behaves, doing a "For" cycle changing some coefficient of this function and keeping all the optimal solution in a table or matrix.

Is that possible to do? I've tried using a For cycle but I don't know how to save the optimal value in an auxiliary variable.

For instance :

Table[NMinimize[{a x + b y, 0.2 x + 0.1 y >= 14, 0.25 x + 0.6 y >= 30, 
    0.1 x + 0.15 y >= 10, x >= 0, y >= 0}, {x, y}][[2]], {a, 0, 3, 1}, {b, 0, 3, 1}]

Does anyone one how to save all of the X and Y Values in only 2 columns? I'd like to associate those columns to de corresponding value of a and b

$\endgroup$
8
  • 1
    $\begingroup$ Please read the documentation for Table, then if necessary edit your question with what you tried and in what way it did not work. (I will reopen the question at that time.) $\endgroup$
    – Mr.Wizard
    Jul 21, 2014 at 7:14
  • $\begingroup$ Perhaps this: TableForm[ Table[NMinimize[{a x+b y,0.2 x+0.1 y>=14,0.25 x+0.6 y>=30,0.1 x+0.15 y>=10,x>=0,y>=0},{x,y}][[1]],{a,0,2,0.2},{b,0,3,0.3}], TableHeadings->{"a = "<>ToString[#]&/@Range[0,2,0.2],"b = "<>ToString[#]&/@Range[0,3,0.3]}]? $\endgroup$
    – seismatica
    Jul 21, 2014 at 7:47
  • $\begingroup$ Thank you! @seismatica I'd like to save the x and y value too. I've thought in 5 columns, a,b,x,y and CT. I'm gonna review the table information of this software. thanks a lot! $\endgroup$
    – YoJesseP
    Jul 21, 2014 at 21:05
  • $\begingroup$ @Mr.Wizard I've already modified the post. Is that enough to modify the condition of this post? $\endgroup$
    – YoJesseP
    Jul 22, 2014 at 3:26
  • $\begingroup$ @YoJesseP Done. $\endgroup$
    – Mr.Wizard
    Jul 22, 2014 at 3:47

2 Answers 2

2
$\begingroup$

How about this?

Grid[Prepend[
  Flatten[Table[{a, b, 
      Reverse@NMinimize[{a x + b y, 0.2 x + 0.1 y >= 14, 
         0.25 x + 0.6 y >= 30, 0.1 x + 0.15 y >= 10, x >= 0, 
         y >= 0}, {x, y}]}, {a, 0, 3, 1}, {b, 0, 3, 1}] /. {ap_, 
      bp_, {{x -> xp_, y -> yp_}, axbyp_}} :> {ap, bp, xp, yp, axbyp},
    1], {"a", "b", "x", "y", "CT"}], Frame -> All]

Mathematica graphics

$\endgroup$
5
  • 1
    $\begingroup$ This strikes me as a bit more complicated that necessary but it gets the job done. +1 $\endgroup$
    – Mr.Wizard
    Jul 22, 2014 at 7:00
  • $\begingroup$ I just realized that I didn't need Reverse since I'm pattern-matching the output anyway. I really don't know if post-processing this way is a bad habit or not. $\endgroup$
    – seismatica
    Jul 22, 2014 at 7:04
  • $\begingroup$ Thank you! this is what I'm looking for. $\endgroup$
    – YoJesseP
    Jul 22, 2014 at 7:08
  • $\begingroup$ Generally I don't think there is anything wrong with it unless performance is critical (in which case Part will be faster if you can find a way to apply it) but NMinimize is returning Rules specifically to make their substitution easier so you might as well use them. $\endgroup$
    – Mr.Wizard
    Jul 22, 2014 at 7:08
  • $\begingroup$ True. Thank you for your suggestion. $\endgroup$
    – seismatica
    Jul 22, 2014 at 7:11
4
$\begingroup$

I'm not sure exactly what you want, but in an effort to demonstrate some possibilities:

Join @@ Table[{{a, b}, 
    NMinimize[{a x + b y, 0.2 x + 0.1 y >= 14, 0.25 x + 0.6 y >= 30, 0.1 x + 0.15 y >= 10,
        x >= 0, y >= 0}, {x, y}][[2]]}, {a, 0, 3, 1}, {b, 0, 3, 1}] // Column

Or:

Join @@ Table[{{HoldForm[a] -> a, HoldForm[b] -> b}, 
    NMinimize[{a x + b y, 0.2 x + 0.1 y >= 14, 0.25 x + 0.6 y >= 30, 0.1 x + 0.15 y >= 10,
        x >= 0, y >= 0}, {x, y}][[2]]}, {a, 0, 3, 1}, {b, 0, 3, 1}] // MatrixForm

Based on your comment perhaps:

expr =
  {a x + b y, 0.2 x + 0.1 y >= 14, 0.25 x + 0.6 y >= 30, 0.1 x + 0.15 y >= 10, x >= 0, y >= 0};

tab =
  Table[
    {a, b, x, y, #} /. #2 & @@ NMinimize[expr, {x, y}],
    {a, 0, 3}, {b, 0, 3}
  ] ~Flatten~ 1;

TableForm[tab, TableHeadings -> {None, {"a", "b", "x", "y", "value"}}]

enter image description here

If wish to use specific values rather than a range you merely need this syntax for Table:

enter image description here

For example:

aVals = RandomInteger[{0, 20}, 7]
{3, 9, 4, 5, 18, 13, 3}
expr =
  {a x + b y, 0.2 x + 0.1 y >= 14, 0.25 x + 0.6 y >= 30, 0.1 x + 0.15 y >= 10, x >= 0, y >= 0};

tab =
  Table[
    {a, b, x, y, #} /. #2 & @@ NMinimize[expr, {x, y}],
    {a, aVals}, {b, 0, 3}
  ] ~Flatten~ 1;

TableForm[tab, TableHeadings -> {None, {"a", "b", "x", "y", "value"}}]

enter image description here


Explanation

brama requested an explanation of this code:

I have trouble understanding the role of #2 and ~Flatten~ 1. Also, How does it transfer the optimal x and y values from the NMinimize to the table?

The documentation for NMinimize states:

enter image description here

The first part is the minimum value found, and the second part is a list of replacement rules.

I used the combination of Function, Slot, and Apply to handle these two parts. An independent example:

{a, b, x, y, #} /. #2 & @@ {"part1", "part2"} // Quiet
{a, b, x, y, "part1"} /. "part2"

(I used Quiet to suppress the message informing us that "part2" is not a list of replacement rules.)

With actual output from NMimimize: {0., {x -> 55., y -> 30.}} this becomes:

{a, b, x, y, 0.} /. {x -> 55., y -> 30.}

Then after the ReplaceAll replacements and evaluation of a and b within Table:

{0, 0, 55., 30., 0.}

See this answer for other ways to work with the output of NMinimze.

The final piece of the code is ~Flatten~ 1. First: a ~op~ b is infix notation for op[a, b]. Flatten is used to combine expressions with the same head (by default List) and different levels. Here it is used to combine the lists of solutions for each a value into a single list of solutions. An independent example:

x1 = Array[Plus, {3, 4, 5}]
{{{3, 4, 5, 6, 7}, {4, 5, 6, 7, 8}, {5, 6, 7, 8, 9}, {6, 7, 8, 9, 10}},
 {{4, 5, 6, 7, 8}, {5, 6, 7, 8, 9}, {6, 7, 8, 9, 10}, {7, 8, 9, 10, 11}},
 {{5, 6, 7, 8, 9}, {6, 7, 8, 9, 10}, {7, 8, 9, 10, 11}, {8, 9, 10, 11, 12}}}
x1 ~Flatten~ 1
{{3, 4, 5, 6, 7},
 {4, 5, 6, 7, 8},
 {5, 6, 7, 8, 9},
 {6, 7, 8, 9, 10},
 {4, 5, 6, 7, 8},
 {5,6, 7, 8, 9},
 {6, 7, 8, 9, 10},
 {7, 8, 9, 10, 11},
 {5, 6, 7, 8, 9},
 {6, 7, 8, 9, 10},
 {7, 8, 9, 10, 11},
 {8, 9, 10, 11, 12}}
$\endgroup$
13
  • $\begingroup$ The first one is closer to what I want. I'd like to make a table with the next columns: a|b|x|y|Optimal Value| $\endgroup$
    – YoJesseP
    Jul 22, 2014 at 6:39
  • $\begingroup$ @YoJesseP Please see the update. $\endgroup$
    – Mr.Wizard
    Jul 22, 2014 at 7:00
  • $\begingroup$ Yeah!!! this is exactly what I want! thanks a lot!!! $\endgroup$
    – YoJesseP
    Jul 22, 2014 at 7:04
  • $\begingroup$ @YoJesseP I am glad I could help. If this is a fully satisfying answer please consider Accepting it, though also consider waiting a while to give others a chance to answer too before the question appears concluded. $\endgroup$
    – Mr.Wizard
    Jul 22, 2014 at 7:07
  • $\begingroup$ @YoJesseP I modified the code of my final method to hopefully make it easier to read and reuse. $\endgroup$
    – Mr.Wizard
    Jul 22, 2014 at 7:12

Your Answer

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

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