Unconstrained Optimization with Additional Parameters
1 次查看(过去 30 天)
显示 更早的评论
Hello
I have a problem which is very similar to this unconstrained optimization example using fminunc with additional parameters here (bowlpeakfun function example):
My problem is that I want to use the large Scale algorithm where the derivatives of the objective function are supplied. Therefore if I rewrite my objective function as
function [y, grad] = bowlpeakfun(x, a, b, c)
y = (x(1)-a).*exp(-((x(1)-a).^2+(x(2)-b).^2))+((x(1)-a).^2+(x(2)-b).^2)/c;
if nargout >1
grad = gradient of y;
end
And then set the anonymous function/optimization as
a = 2;b = 3; c = 10;
f = @(x)bowlpeakfun(x,a,b,c)
x0 = [-.5; 0];
options = optimset('GradObj','on');
[x, fval] = fminunc(f,x0,options)
I get an error 'Failure in initial user-supplied objective function evaluation. FMINUNC cannot continue' which I think its related somehow to the fact that the anonymous function doesn't see that there is a gradient associated with bowlpeakfun. Redifining the anonymous function as
[f,grad] = @(x)bowlpeakfun(x,a,b,c)
does not work either. Any help much appreciated.
Thanks
0 个评论
回答(6 个)
Alan Weiss
2011-8-24
I assume that your line
grad = gradient of y;
is not intended to be taken literally, but you are just saving the space that would be taken by writing the entire gradient.
Have you tried to evaluate
[fval gradfval] = f(x0)
to see why MATLAB is throwing an error?
0 个评论
Javer
2011-8-24
2 个评论
Walter Roberson
2011-8-24
grady = f'(x,params);
is not a valid line of code. The "'" character cannot occur in that syntax.
Walter Roberson
2011-8-24
Use a subfunction instead of an anonymous function to pass the additional parameter, and pass the handle to the subfunction to fminunc .
0 个评论
Steve Grikschat
2011-9-6
We've yet to see your code for the analytical gradient calculation (hopefully we don't ;) ). I suspect that the error might be there.
Have you tried calling the function with two outputs as Alan suggested? i.e.
params = ...
f = @(x) myfun(x,params);
[fval0,grad0] = myfun(x0);
What do you get there?
0 个评论
Javer
2011-9-6
1 个评论
Steve Grikschat
2011-9-7
Let's go back to the nested function example you wrote a few posts back. From the error it appears to me that the problem lies inside the function with a computation involving unary minus (uminus) or negation.
Try using the debugger to step into the 1st evaluation of your objective and gradient function from within fminunc to see where the error lies.
If you're not familiar, see the help here:
http://www.mathworks.com/help/techdoc/matlab_env/brqxeeu-175.html
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!