Unconstrained Optimization with Additional Parameters
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (6)
Alan Weiss
el 24 de Ag. de 2011
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 comentarios
Javer
el 24 de Ag. de 2011
2 comentarios
Walter Roberson
el 24 de Ag. de 2011
grady = f'(x,params);
is not a valid line of code. The "'" character cannot occur in that syntax.
Walter Roberson
el 24 de Ag. de 2011
Use a subfunction instead of an anonymous function to pass the additional parameter, and pass the handle to the subfunction to fminunc .
0 comentarios
Steve Grikschat
el 6 de Sept. de 2011
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 comentarios
Javer
el 6 de Sept. de 2011
1 comentario
Steve Grikschat
el 7 de Sept. de 2011
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
Ver también
Categorías
Más información sobre Get Started with Optimization Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!