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?