I am considering an optimization where both the objective and non-linear constraint and functions of a function, such as (excluding some details):
f = @(w,Y) Y*w; g = @(w,Y) mean(f(w,Y)); h = @(w,Y) var(f(w,Y)); obj_con = @(w,Y) -g(w,Y); nonl_con = @(w,Y) deal(h(w,Y)-K,);
options = optimset('Algorithm','active-set');
out = fmincon(@(x) obj_con(x,Y),x0,,,Aeq,beq,lb,ub,@(x) nonl_con(x,Y),options);
I have purposefully made this simpler than what I actually plan to do to get the general point across (in particular, f and h will be more complicated and the optimization may take place with functions other than fmincon).
Using the Matlab profiler, I determined that the function f is called twice at each iteration of the optimization (once for g and once for h).
Is there any way to set up the optimization of the functions so that it is only called once?
One approach I am not interested in is to remove f and make the calculation within g and h. That still does not resolve the issue of having to make such a calculation twice.
Objective and Nonlinear Constraints in the Same Function has the information you seek.
MATLAB mathematical toolbox documentation