Clear Filters
Clear Filters

function setup in fmincon

1 view (last 30 days)
Chien-Chia Huang
Chien-Chia Huang on 5 Jun 2011
Hi there, my problem is how to set up the function using function handle in fmincon. The objective function is quadratic and the code is as follows.
function f = col_myfun(x,w)
f = x'*(w(1)*B+w(2)*eye(size(B,1)))*x;
end
The B matrix is calculated in another m-file. I code in fmincon with
fmincon(@(x,w) col_myfun,....)
But I met the error
??? Error using ==> fmincon at 399
FMINCON cannot continue because user supplied
objective function failed with the following
error:
Input argument "w" is undefined.
The x and w are supposed to be variables. I still have no idea how this could happen? Any suggestion will be truly appreciated!
  1 Comment
Chien-Chia Huang
Chien-Chia Huang on 5 Jun 2011
I have made some revision on the code
--
col_myfun = @(x, w) (x'*(w(1)*B+w(2)*eye(size(B,1)))*x);
x = fmincon(@(x, w) col_myfun,[zeros(size(B,1),1);0.5;0.5],[],[],[-C' zeros(1,2);zeros(1,length(C)) ones(1,2)],[c_star;1],[-inf*ones(size(B,1),1);zeros(2,1)],[])
--
But still met the error message
--
??? Undefined function or method 'minus' for
input arguments of type 'function_handle'.
Error in ==> finitedifferences at 200
gradf(gcnt,1) =
(fplus-fCurrent)/CHG(gcnt);
Error in ==> nlconst at 286
[gf,gnc,NEWLAMBDA,OLDLAMBDA,s]=finitedifferences(XOUT,x,funfcn,confcn,lb,ub,f,nc,
...
Error in ==> fmincon at 562
[X,FVAL,lambda,EXITFLAG,OUTPUT,GRAD,HESSIAN]=...
Error in ==> collinearity_mop at 64
x = fmincon(@(x, w)
col_myfun,[zeros(size(B,1),1);0.5;0.5],[],[],[-C'
zeros(1,2);zeros(1,length(C))
ones(1,2)],[c_star;1],[-inf*ones(size(B,1),1);zeros(2,1)],[])
--
What's gone wrong? Thanks a lot.

Sign in to comment.

Accepted Answer

Teja Muppirala
Teja Muppirala on 5 Jun 2011
FMINCON only passes in one variable (the variable to be optimized) to the objective function. If w is constant, and you are trying to optimize for x, then something like this will work:
w = [3 4]
fmincon(@(x) col_myfun(x,w), ... )
  1 Comment
Chien-Chia Huang
Chien-Chia Huang on 5 Jun 2011
Thanks, Teja. This does help. Maybe combining x and w can be a way out.

Sign in to comment.

More Answers (1)

Matt Fig
Matt Fig on 5 Jun 2011
I don't have the Optimization toolbox, so I can't really test it out. However, I am pretty sure you are passing your function incorrectly. Try this:
col_myfun = @(x, w) (x'*(w(1)*B+w(2)*eye(size(B,1)))*x);
x = fmincon(col_myfun,....% rest of your code. NOT: @(x,w)col_myfun
COL_MYFUN is already a function handle. When you pass it as @(x,w)col_myfun, you are doing making a new function which returns the function handle to another function, not a value. For example:
f = @(x) sin(x);
f(1) % Evaluates to sin(1), but now look:
g = @(x) f; % This is how you passed your function above.
g(1) % Returns f, a function handle, not sin(1).
g(arg) will always return f, never a value! So there may be other problems with your code that I cannot test, but this one definitely needs fixing.
  1 Comment
Chien-Chia Huang
Chien-Chia Huang on 5 Jun 2011
Thanks a lot, Matt. There's gotta be some fixing. Problems still occur. I'll try to fix them.

Sign in to comment.

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!