I am looking at the following design pattern:
x_min = minimize( 'f', x_initial, step_size, p1, p2, p3, p4, p5 );
... where f is some function ...
function y = f( x, p1, p2, p3 )
:
end
... for which minimize will attempt to find a local minimum:
function x_min = minimize( FUNC_NAME, x_initial, step_size, p1, p2, p3, p4, p5 )
X = x_initial;
% compose string used to call function
argstr = [FUNC_NAME, '(X'];
for i = 1:(nargin - 3)
argstr = [argstr, ',p', int2str(i)];
end
argstr = [argstr, ')'];
:
x1 = eval( argstr ); % etc
:
It is very ugly -- for a start it ties eval( argstr ) to only executing f on X.
It's rather old code (dated 2001).
Is there a preferred way to handle this scenario nowadays?
Is there any way to accomplish this so that the implementation of f is at the call-site? i.e. invisible to minimize.m? The existing pattern requires it is in separate f.m file.