I'm trying to automatically create a function handle that is a sum of function handles. When I tried to do this manually, it worked:
f1 = @(x) x(1);
f2 = @(x) x(2);
f3 = @(x) x(3);
f = @(x) f1(x)+f2(x)+f3(x);
But when I tried to do this automatically (using a for-loop):
aux = {f1,f2,f3};
f = @(x) 0;
for i=1:3
f = @(x) f(x) + cell2mat(aux(i));
end
I get the following error:
Undefined operator '+' for input arguments of type 'function_handle'.
My goal is to use this function handle with fmincon
function.
So an alternative solution would also help.