## Optimization Where Both Objective and Non-Linear Constraint are Functions of a Function

on 22 Aug 2013

### Alan Weiss (view profile)

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.

## Products

### Alan Weiss (view profile)

on 22 Aug 2013

Objective and Nonlinear Constraints in the Same Function has the information you seek.

Alan Weiss

MATLAB mathematical toolbox documentation

ss4johnny Hall

### ss4johnny Hall (view profile)

on 22 Aug 2013

This looks great. Cheers.

#### Join the 15-year community celebration.

Play games and win prizes!

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi