필터 지우기
필터 지우기

function setup in fmincon

조회 수: 1 (최근 30일)
Chien-Chia Huang
Chien-Chia Huang 2011년 6월 5일
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
Chien-Chia Huang
Chien-Chia Huang 2011년 6월 5일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Teja Muppirala
Teja Muppirala 2011년 6월 5일
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
Chien-Chia Huang
Chien-Chia Huang 2011년 6월 5일
Thanks, Teja. This does help. Maybe combining x and w can be a way out.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by