NMaximize is not substituting parameters of its objective function. Is there a way to fix that? I have a three line program that exhibits the problem.
f[r_, x_] := Exp[-(x + r*Cos[x])^2]
obj[r_] := NIntegrate[f[r, x], {x, -1, 1}]
NMaximize[obj[r], r]
The objective function is defined for all r, and it can be easily computed.
In[47]:= obj[-1.34]
Out[47]= 0.698409
In[48]:= obj[0]
Out[48]= 1.49365
In[49]:= obj[0.54]
Out[49]= 1.29793
But when it is passed to NMaximize, it produces a bunch of warnings that look as follows:
"The integrand E^-(x+r Cos[x])^2 has evaluated to non-numerical values for all sampling points in the region with boundaries {{-1,1}}"
Notice the explicit 'r' in the expression in the warning message, where r is the free variable in the optimization. So it appears that NMaximize is not substituting for r. It eventually comes up with the right answer, but only after a bunch of warnings as above. Is there any way to fix that?
obj[r_?NumericQ] :=– ybeltukov Sep 18 '14 at 17:22