function setup in fmincon
Show older comments
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
on 5 Jun 2011
Accepted Answer
More Answers (1)
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
on 5 Jun 2011
Categories
Find more on Choose a Solver 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!