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!