How to use fmincon to optimize two control vectors of a function?
2 次查看(过去 30 天)
显示 更早的评论
I have a function of 2 different vector. These are the control vector (decision variables) of the function. I want to use fmincon to optimize this function and also get the both control vector results separately. I have tried to use handle ,@, but I got an error. The function is:
function f = myFS(x,sv) % x is a vector (5,1) f = norm(x)^2-sigma*(sv(1)+sv(2)); end
%% I tried to write fmincone to consider both control vectors (x and sv)
[Xtemp(:,h2),Fval, fiasco] = fmincon(@(x,sv)myFS(x,sv)...
,xstart,[],[],[],[],VLB,VUB,@(x,sv)myCon(sv),options);
Here is the error I get:
Error using myFS (line 12) Not enough input arguments.
Error in fmincon (line 564) initVals.f = feval(funfcn{3},X,varargin{:});
Error in main_Econstraint (line 58) [Xtemp(:,h2),Fval, fiasco] = fmincon('myFS',xstart,[],[],[],[],VLB,VUB,@(x,sv)myCon(sv),options);
0 个评论
采纳的回答
Brendan Hamm
2015-3-31
编辑:Brendan Hamm
2015-3-31
In the optimization routines in MATLAB the objective function needs to take all of it's design variables as one input. So in your case you would want something like:
function f = myFS(y,sigma)
sv = y(6:7); % sv will be the last 2 elements in the input vector
x = y(1:5); % x is the first 5 elements of the input vector
f = norm(x)^2-sigma*(sv(1)+sv(2)); % Where does sigma come from?
end
I assume sigma is not a design variable so I pass as the next input. This means we need to now define sigma in the workspace and then create a function handle.
sigma = 1;
objective = @(y) myFS(y,sigma);
This will hardcode the value of 1 for sigma into the function 'objective'. Now we can use this with fmincon:
x = fmincon(objective,x0,...)
1 个评论
javiera de la carrera
2017-8-9
Hi Brendan. I have a similar problem, but I have a vector ''q'' that is a design variable that appears in the main function and in the restrictions, and another design variable that appears only in the restriction function. So, I don't know how to put all the design variables in the same vector. Can you help me?
Thanks!
更多回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!