Message Boards Message Boards

GROUPS:

Avoid problem using "Nested" NMaximize?

Posted 8 days ago
73 Views
|
3 Replies
|
2 Total Likes
|

I'm having trouble with NMaximize in the following code (this is an example of my real problem, I know that this example has an exact analytical solution).

This part works just fine, is the clasical consumer maximization problem :

Util[x_, y_, a_] := x^a* y^(1 - a)
UtilMax[a_, px_, py_, P_] := Module[{temp, UtilMax, XYOptim},
  temp = NMaximize[{Util[x, y, a], px*x + py*y <= P, x > 0, 
     y > 0}, {x, y}];
  UtilMax = temp[[1]];
  XYOptim = {x, y} /. Last[temp];
  Flatten[{XYOptim, UtilMax}]]
Sales[vecAlfa_, vecP_, px_, py_] := Module[{temp},
        temp = Table[
          UtilMax [vecAlfa[[i]], px, py, vecP[[i]]], {i, 1, 
           Length[vecAlfa]}];
        Sum[temp[[i, 1]] + temp[[i, 2]], {i, 1, Length[vecAlfa]}]
        ]
vecAlfa = {0.1, 0.9};
vecP = {10, 20};

Test that the functions are working ok:

In[6]:= UtilMax[vecAlfa[[1]], 5, 5, vecP[[1]]]
UtilMax[vecAlfa[[2]], 5, 5, vecP[[2]]]
Sales[vecAlfa, vecP, 5, 5]

Out[6]= {0.2, 1.8, 1.44493}

Out[7]= {3.6, 0.4, 2.88987}

Out[8]= 6.

The problem arises with the followin part:

NMaximize[{Sales[vecAlfa, vecP, px, py], 0 < px < 100, 0 < py < 50}, {px, py}]

During evaluation of In[10]:= NMaximize::bcons: The following constraints are not valid: {x>0,y>0,px x+py y<=10}. Constraints should be equalities, inequalities, or domain specifications involving the variables. >>

During evaluation of In[10]:= ReplaceAll::reps: {x,y} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>

During evaluation of In[10]:= NMaximize::bcons: The following constraints are not valid: {x>0,y>0,px x+py y<=20}. Constraints should be equalities, inequalities, or domain specifications involving the variables. >>

During evaluation of In[10]:= ReplaceAll::reps: {x,y} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>

During evaluation of In[10]:= NMaximize::nnum: The function value -x^0.9 y^0.1-x^0.1 y^0.9-2 ({x,y}/. {x,y}) is not a number at {px,py} = {1.91862,1.66351}. >>

Out[10]= NMaximize[{x^0.9 y^0.1 + x^0.1 y^0.9 + 2 ({x, y} /. {x, y}), 
0 < px < 100, 0 < py < 50}, {px, py}]

Any idea what could I've been doing wrong?

3 Replies

Augusto,

You never define px and py in the second case. They are both 5 in the working example. They need to be set or in the list of variables to solve.

Regards

Neil

Augusto,

The problem is correct that px and py are not defined but I was not clear on the reason because I misread your post.

the reason that you get errors is because NMaximize first tries to analytically solve to problem and your equations can't be evaluated without px and py being defined.

The solution is to redefine Sales slightly:

Sales[vecAlfa_, vecP_, px_ /; NumberQ[px], py_ /; NumberQ[py]] :=

or Sales[vecAlfa, vecP, pxReal, pyReal] :=

If you only plan on using reals (and not integers).

With either version, NMaximize will use only numeric values in Sales.

Regards,

Neil

Thank you Neil,

That worked perfectly. Thumbs up!!.

Augusto

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract