function setup in fmincon

2 visualizaciones (últimos 30 días)
Chien-Chia Huang
Chien-Chia Huang el 5 de Jun. de 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 comentario
Chien-Chia Huang
Chien-Chia Huang el 5 de Jun. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Teja Muppirala
Teja Muppirala el 5 de Jun. de 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 comentario
Chien-Chia Huang
Chien-Chia Huang el 5 de Jun. de 2011
Thanks, Teja. This does help. Maybe combining x and w can be a way out.

Iniciar sesión para comentar.

Más respuestas (1)

Matt Fig
Matt Fig el 5 de Jun. de 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 comentario
Chien-Chia Huang
Chien-Chia Huang el 5 de Jun. de 2011
Thanks a lot, Matt. There's gotta be some fixing. Problems still occur. I'll try to fix them.

Iniciar sesión para comentar.

Categorías

Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by