Insert a function from function file into FMINUNC command

Hello, I have the following function file to generate the required function in unknown variable 'c'.
Then i have the following command which takes this function (last line in above command).
c = fminunc(@(c) chi(c, K0_com, M_com_vec, lambda, Steps, Steps, ns1, ns2), cint, options);
When I run the command I am getting the following message.
Error using chi
Too many output arguments.
Error in @(c)chi(c,K0_com,M_com_vec,lambda,Steps,Steps,ns1,ns2)
Error in fminunc (line 254)
f = feval(funfcn{3},x,varargin{:});
Error in In (line 83)
c = fminunc(@(c) chi(c, K0_com, M_com_vec, lambda, Steps, Steps, ns1, ns2),
cint);
Caused by:
Failure in initial user-supplied objective function evaluation. FMINUNC
cannot continue.
Can you please let me know how to fix this? Thanks

回答(1 个)

chi has no return argument. How do you expect fminunc to operate? Should it be able to guess which variable to optimize over?
doc function
I think that perhaps you have some romantic idea that MATLAB will see this line of chi
((0.5*c'*(G + lambda*eye(ns1*ns2))*c) - (c'*M_com_vec));
and somehow magically understand that this is the return argument. You assign the result to nothing, then terminate the line with a semi-colon. It just gets dumped into the bit bucket. MATLAB cannot read your mind. It does exactly what you tell it to do.
Of course, from your comments, it is also not obvious if you have put the call to fminunc INSIDE the function chi itself. This is another problem that people often seem to make.

4 个评论

Hi, Thanks for the help. I am getting the same error even after modifying the function file.
function fun = chi(c, K0_com, M_com_vec, lambda, Steps1, Steps2, ns1, ns2)
Hv = zeros(Steps1*Steps2,Steps1*Steps2);
for i=1:Steps1*Steps2
Hv(i,i) = (heaviside(K0_com(:,i)*c));
end
G = K0_com*Hv*K0_com';
fun = (0.5*c'*(G + lambda*eye(ns1*ns2))*c) - (c'*M_com_vec);
end
I am using the following options for fminunc
options = optimset('GradObj','on', 'LargeScale', 'off', 'HessUpdate', 'bfgs', 'InitialHessMatrix', 5*ones(961,1), 'InitialHessType',...
'user-supplied', 'MaxIter', 1000,'Display','iter','MaxFunEvals',100000);
As I look more closely at your function chi, I see that the problem is completely irrelevant.
fminunc cannot solve this class of problem. Your objective function is not continuous. It uses heaviside.
Normally, I'd suggest you learn to use the debugger to solve MATLAB problems like this. But here it looks like you have given me enough information to see what you did wrong.
In that options call, you set 'gradobj' to 'on'. According to the doc for fmincon, it will then expect a second output argument fro mfun, the gradient of your objective.
Again, it is completely irrelevant, because fminunc will fail miserably to solve this problem. Case closed.
Are you attempting to estimate a piecewise function of some ilk?
I want to solve for 'c' by minimizing the chi function. What I understand from your reply is that I cannot run fminunc on the chi function because it has heaviside in it right? Is there any other command that I could use to minimize the chi function. I also know the first and second derivatives of the chi function. Thank you.
The tools in the optimization toolbox, thus functions of the class of fminunc, fmincon, etc., require a continuous objective. That does not appear to be true for your function.
You might try a tool like a genetic optimizer, or a particle search of some sort, any of which will be less sensitive to such singularities.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Quadratic Programming and Cone Programming 的更多信息

提问:

2015-1-4

编辑:

2015-1-8

Community Treasure Hunt

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

Start Hunting!

Translated by