How to use fmincon to optimize two control vectors of a function?

7 views (last 30 days)
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);

Accepted Answer

Brendan Hamm
Brendan Hamm on 31 Mar 2015
Edited: Brendan Hamm on 31 Mar 2015
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 Comment
javiera de la carrera
javiera de la carrera on 9 Aug 2017
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!

Sign in to comment.

More Answers (1)

Alan Weiss
Alan Weiss on 31 Mar 2015
Perhaps this recent MATLAB Answer will help.
Alan Weiss
MATLAB mathematical toolbox documentation

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!