# Avoid problem using "Nested" NMaximize?

Posted 6 months ago
640 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[]; 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:= UtilMax[vecAlfa[], 5, 5, vecP[]] UtilMax[vecAlfa[], 5, 5, vecP[]] Sales[vecAlfa, vecP, 5, 5] Out= {0.2, 1.8, 1.44493} Out= {3.6, 0.4, 2.88987} Out= 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:= 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:= 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:= 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:= 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:= 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= 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? Answer
3 Replies
Sort By:
Posted 6 months ago
 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 Answer
Posted 6 months ago
 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 Answer
Posted 6 months ago
 Thank you Neil,That worked perfectly. Thumbs up!!.Augusto Answer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments