2009-10-05 79 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]]