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 个)
John D'Errico
2015-1-4
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 个评论
Karthik
2015-1-4
John D'Errico
2015-1-4
编辑:John D'Errico
2015-1-4
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?
John D'Errico
2015-1-5
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 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!