2012-12-13 38 views
-1

最小化功能我試圖儘量減少這樣在MATLAB機能缺失:如何同等價值

function [c, ceq] = const_slot(x) 
c = []; 
% Nonlinear equality constraints 
ceq = [sum(x)-1]; 
end 



[x,fval] = fmincon(@func_slot, x0,[],[],[],[],lb,ub,@const_slot,options) 

但是,我要珍惜fval這是指定的值,或正內。我怎樣才能做到這一點?

+0

您的問題不清楚:'fval'是輸出。你的意思是你無法找到'x0' - 優化的初始點? – Shai

回答

3

據我瞭解你的問題,你想對你的功能@func_slot(我假設是非線性的)的約束。

Matlab help for fmincon我們發現:

x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options) 

非線性約束可以使用nonlcon參數(在這個問題您使用@const_slot)進行設置。這些限制應該被定義爲:

function [c,ceq] = mycon(x) 
c = ...  % # Compute nonlinear inequalities at x. 
ceq = ... % # Compute nonlinear equalities at x. 

因此,例如,當你想你的函數@func_slot是大於零,您可以在@const_slot定義不等式約束c作爲函數的負。

編輯

如果我理解正確的話,你需要的函數值比指定的限制大於零,但少。在這種情況下,你可以試試這個。

function [c, ceq] = const_slot(x) 

% # Nonlinear inequality constraints 
upperLimit = 10; 
c = [-func_slot(x); 
    -upperLimit + func_slot(x)]; 

% # Nonlinear equality constraints 
ceq = [sum(x)-1]; 

end 
+0

我已經使用x值的約束。函數[c,ceq] = const_slot(x) c = []; ceq = [sum(x)-1]; end我需要在零值或指定的值,函數的值而不是變量x保持值fval。 – hexware

+0

@hexware查看我的編輯。 – user1884905

2

從您的意見,那就好像你正在努力尋找趴在域lb <= x <= ubsum(x) = 1func_slot全部爲零。
1.如果是這種情況,請重新說出您的問題以反映這一點 - 您將得到更好的答案。
2.約束sum(x)=1是一個線性約束,您可以使用Aeq = ones(1, size(x,1))beq = 1來實現相同的目的。這樣做,您現在可以使用const_slot來反映您的非線性要求

function [c, ceq] = const_slot(x) 
c = []; 
ceq = func_slot(x) - desired_fval;