Passing multiple function handles to fminimax

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.

답변 (1개)

Rik
Rik 2021년 11월 27일

1 개 추천

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

John Rönn
John Rönn 2021년 11월 27일
Hey, thank you very much! I still get the following error code when I run the fminimax function:
FMINIMAX requires all values returned by functions to be of data type double.
Rik
Rik 2021년 11월 27일
Then you need to make sure your functions return a double. Cellfun should not change the datatype. How did you try to confirm that assumption?
John Rönn
John Rönn 2021년 11월 27일
Hey, I think the problem is that when I try to evaluate my vecFun now with e.g., x0, I get a cell array, let's say 1x2 (with 2 functions). This cell array contains the output values (double) of both functions.However, fminimax does not accept this cell array as the input.
John Rönn
John Rönn 2021년 11월 27일
편집: John Rönn 2021년 11월 27일
% An example code here:
a = @(x)x;
b = @(x)x.^2;
c = @(x)x.^3;
x0 = 1;
LB = 0;
UB = 1;
Funs = {a,b,c};
vecFun = @(x) cellfun(@(f) f(x), Funs,'UniformOutput',false);
L = fminimax(vecFun, x0, [],[],[],[],LB,UB,[]);
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Entering Commands에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2021년 11월 27일

댓글:

2021년 11월 27일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by