# Avoid nnum error associated with NMaximize?

Posted 3 months ago
442 Views
|
2 Replies
|
1 Total Likes
|
 I would appreciate any suggestions to fix the nnum error associated with NMaximize at the end of this script. Adding ?NumericQ does not help. Maybe I am not using it correctly. Thank you. In[1]:= f[k_, m_] := 4 k^(2/5) m^(2/5) In[2]:= h[x1_] := x1^(1/2) In[3]:= mu[c1_] := (3/2) c1^(-1/2) In[4]:= e[x1_, k_, m_] := f[k, m] - x1 In[5]:= a[k_, m_] := (k^(3/5) m^(-2/5))/(8/5) In[6]:= q[x1_, K1_, k_, m_] := If[e[x1, k, m] >= 0, a[k, m], a[K1 - k, 1 - m]] In[11]:= prof[p1_, p1s_, k_, K1_, m_, x1_, g_, gs_, t_, ts_] := (1 - t) p1 h[x1] - g k + (ts - t) q[x1, K1, k, m] e[x1, k, m] + (1 - ts) p1s h[ f[k, m] + f[K1 - k, 1 - m] - x1] - gs (K1 - k) In[12]:= prof[p1, p1s, 0.18217378897121703, 0.9793049816792121, \ 0.1549715222880414, 2.3964762912739057, .4, .601, .6, .399] Out[12]= -0.383489 + 0.619222 p1 + 0.845456 p1s In[13]:= MNEBest[p1_, p1s_, g_, gs_, t_, ts_] := NMaximize[{prof[p1, p1s, k, K1, m, x1, g, gs, t, ts], k >= 0, k <= K1, K1 >= 0, m >= 0, m <= 1, x1 >= 0, x1 <= f[k, m] + f[K1 - k, 1 - m]}, {{k, .1, .2}, {K1, .9, 1}, {m, .1, .2}, {x1, 2.3, 2.4}}] In[14]:= (*Correct answer for p1 and \ p1s.*)MNEBest[1.207759716810693, 1.2751220667322407, .4, .601, .6, \ .399] Out[14]= {1.44453, {k -> 0.118312, K1 -> 0.96547, m -> 0.131045, x1 -> 2.37927}} In[15]:= MNEx1[p1_, p1s_, g_, gs_, t_, ts_] := MNEBest[p1, p1s, g, gs, t, ts][[2]][[4]][[2]] In[16]:= ka[p1_, p1s_, g_, gs_, t_, ts_] := MNEBest[p1, p1s, g, gs, t, ts][[2]][[1]][[2]] In[17]:= K1a[p1_, p1s_, g_, gs_, t_, ts_] := MNEBest[p1, p1s, g, gs, t, ts][[2]][[2]][[2]] In[18]:= ma[p1_, p1s_, g_, gs_, t_, ts_] := MNEBest[p1, p1s, g, gs, t, ts][[2]][[3]][[2]] In[19]:= MNEx1s[p1_, p1s_, g_, gs_, t_, ts_] := f[ka[p1, p1s, g, gs, t, ts], ma[p1, p1s, g, gs, t, ts]] + f[K1a[p1, p1s, g, gs, t, ts] - ka[p1, p1s, g, gs, t, ts], 1 - ma[p1, p1s, g, gs, t, ts]] - MNEx1[p1, p1s, g, gs, t, ts] In[20]:= Consx1[p1_] := (1.5/p1)^(4) In[23]:= FindRoot[{MNEx1[p1, p1s, .4, .601, .6, .399] == Consx1[p1], MNEx1s[p1, p1s, .4, .601, .6, .399] == Consx1[p1s]}, {{p1, 1.2}, {p1s, 1.3}}] During evaluation of In[23]:= NMaximize::nnum: The function value 0.383489 -0.619222 p1-0.845456 p1s is not a number at {k,K1,m,x1} = {0.182174,0.979305,0.154972,2.39648}. During evaluation of In[23]:= NMaximize::nnum: The function value 0.383489 -0.619222 p1-0.845456 p1s is not a number at {k,K1,m,x1} = {0.182174,0.979305,0.154972,2.39648}. During evaluation of In[23]:= NMaximize::nnum: The function value 0.383489 -0.619222 p1-0.845456 p1s is not a number at {k,K1,m,x1} = {0.182174,0.979305,0.154972,2.39648}. During evaluation of In[23]:= General::stop: Further output of NMaximize::nnum will be suppressed during this calculation. Out[23]= {p1 -> 1.21803, p1s -> 1.28767} In[24]:= NSolve[{MNEx1[p1, p1s, .4, .601, .6, .399] == Consx1[p1], MNEx1s[p1, p1s, .4, .601, .6, .399] == Consx1[p1s] && p1 > 0 && p1s > 0}, {p1, p1s}, Reals] During evaluation of In[24]:= NMaximize::nnum: The function value 0.383489 -0.619222 p1-0.845456 p1s is not a number at {k,K1,m,x1} = {0.182174,0.979305,0.154972,2.39648}. During evaluation of In[24]:= NMaximize::nnum: The function value 0.383489 -0.619222 p1-0.845456 p1s is not a number at {k,K1,m,x1} = {0.182174,0.979305,0.154972,2.39648}. During evaluation of In[24]:= NMaximize::nnum: The function value 0.383489 -0.619222 p1-0.845456 p1s is not a number at {k,K1,m,x1} = {0.182174,0.979305,0.154972,2.39648}. During evaluation of In[24]:= General::stop: Further output of NMaximize::nnum will be suppressed during this calculation. Out[24]= {{p1 -> 1.21803, p1s -> 1.28767}} 
2 Replies
Sort By:
Posted 3 months ago
 0.383489 -0.619222 p1-0.845456 p1sthe error message is telling you p1 and p1s are not numbers, thus giving them to Maximize[] causes an erroryou can rid the message by fixing your code to either provide p1 and p1s as numbers OR to insure Maximize is called on each of them not all of them (if you do not wish to provide p1 and p1s) FindRoot[{MNEx1[p1, p1s, .4, .601, .6, .399] == Consx1[p1], MNEx1s[p1, p1s, .4, .601, .6, .399] == Consx1[p1s]}, {{p1, 1.2}, {p1s, 1.3}}] With the about Input you didn't give the value for p1 p1s but MNEx1 always gives the two to Maximize. You can provide the values or adjust the function to not gives them to Maximize[] (not when left as Symbol). As for doing it without any effort no, you must write at least a little code to rid the problem.