2009-10-05 43 views
5

使用Mathematica,我需要优化是在BinCounts来定义一个函数; 我想要最大化的参数定义了bin分割点。优化使用Mathematica:使用BinCounts目标函数

我认为这个问题是数学扩展目标函数 在参数方面被赋予数值 值之前他们,所以BinCounts抱怨垃圾桶规格不包含真正的价值,无限“一个 列表, -无穷”。

我认为下面是那种东西的小例子,我 试图做的和正在发生的事情。我非常感谢关于 如何解决这个问题的建议。

In[1]:= data = RandomReal[1, 30]; (* Make some test data. *) 

In[2]:= f[a_, b_, c_] := BinCounts[data, {{0, a, b, c, 1}}] (* Shorthand to use below… *) 

In[12]:= g[a_, b_, c_] := Max[f[a, b, c]] - Min[f[a, b, c]] (* Objective function. *) 

In[13]:= NMaximize[{g[a, b, c], 0 < a < b < c < 1}, {a, b, c}] (* Try to oprimize. *) 

During evaluation of In[13]:= BinCounts::cvals: The bin specification {{0,a,b,c,1}} is not a list containing real values, Infinity, and -Infinity. >> 

During evaluation of In[13]:= BinCounts::cvals: The bin specification {{0,a,b,c,1}} is not a list containing real values, Infinity, and -Infinity. >> 

During evaluation of In[13]:= BinCounts::cvals: The bin specification {{0,a,b,c,1}} is not a list containing real values, Infinity, and -Infinity. >> 

During evaluation of In[13]:= General::stop: Further output of BinCounts::cvals will be suppressed during this calculation. >> 

Out[13]= {0., {a -> 0., b -> 0., c -> 1.}} 

回答

3

的解决方案是简单地指定该目标函数仅定义在数值参数而言,像这样:

g[a_?NumericQ, b_?NumericQ, c_?NumericQ] := Max[f[a, b, c]] - Min[f[a, b, c]]