How can I use structs as x input for Matlabs optimization methods?

7 ビュー (過去 30 日間)
Carolin Katzschke
Carolin Katzschke 2015 年 6 月 3 日
コメント済み: Carolin Katzschke 2015 年 6 月 4 日
My functions look like that:
function area_rectangle = area_rectangle(input_variables)
if ~isfield(input_variables,'height') || ~isfield(input_variables,'width')
area_rectangle = NaN;
else
area_rectangle = input_variables.width * input_variables.height;
end
Suppose I want to use one of Matlabs optimization methods, e.g. fmincon, and pass a struct as input. How could I do that?
fmincon(@area_rectangle, input_variables, ...)
Error using fmincon (line 224)
FMINCON requires the following inputs to be of data type double: 'X0'.
Thanks in advance! Carolin

採用された回答

Walter Roberson
Walter Roberson 2015 年 6 月 3 日
You would not do that. Remember that the optimization routines control the values of x themselves, adjusting its value until the optimum value is found. The optimization routines have no idea how to update a struct.
You would instead use one of the optimization routines that can work on vectors of values, and you would pass the values of the fields as elements of x. For example,
function area = calc_area(x)
height = x(1,:);
width = x(2,:);
area = height .* width;
end
  1 件のコメント
Carolin Katzschke
Carolin Katzschke 2015 年 6 月 4 日
Okay. That was the answer I was looking for. Thank you very much.

サインインしてコメントする。

その他の回答 (1 件)

Konstantinos Sofos
Konstantinos Sofos 2015 年 6 月 3 日
Hi Carolin,
Have you seen Create Problem Structure ?
Regards,
  1 件のコメント
Carolin Katzschke
Carolin Katzschke 2015 年 6 月 4 日
Hi Konstantinos, I had a closer look at the problem structure, but I tried to pass a struct for x/x0. As Walter stated, Matlabs optimization methods only work with double matrices. Thank you for your answer anyway. Regards, Carolin

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by