Passing multiple function handles to fminimax
7 views (last 30 days)
Show older comments
Hey!
I am using fminimax to optimize an arbitrary number of functions that all depend on the same variable, e.g., x. I have a cell array of function handles called Funs, e.g., {@(x)f(x)} {@(x)g(x)}. If I pass this cell array to fminimax as follows:
L = fminimax(Funs, x0, [],[],[],[],LB,UB,[],options),
where x0 is a vector of starting values and LB and UB the lower and upper bounds, respectively, fminimax optimizes only the first function, i.e., f(x).
Now, Let's say I have two function handles and I concatenate the functions into a vector like this
f = Funs{1};
g = Funs{2};
vecFun = @(x)[a(x);b(x)];
and then call fminimax, it correctly optimizes both functions.
My question therefore is, how can I directly call the fminimax with an arbitrary number of functions by using my cell array of function handles? I guess it's just a matter of finding the syntax to convert the cell array into vector function, but I can't quite get it right.
0 Comments
Answers (1)
Rik
on 27 Nov 2021
You will have to create a wrapper that calls the functions in your cell array and returns the result as a vector.
Something like the code below should do it (written on mobile, so some edits might be required).
vecFun = @(x)myfun(x,funs);
function val=myfun(x,funs)
val=cellfun(@(f)feval(f,x),funs);
end
5 Comments
Walter Roberson
on 27 Nov 2021
The function you pass to fminmax cannot return a cell. It must return a numeric vector the same size as your x0, and the result must be the function evaluated at the locations passed in. In other words, it must be vectorized.
fminmax cannot be used to optimize several functions simultaneously.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!