Fmincon error "Provided objective gradient does not have the correct number of elements."

11 ビュー (過去 30 日間)
Yi-Kai Peng
Yi-Kai Peng 2023 年 8 月 8 日
コメント済み: Yi-Kai Peng 2023 年 8 月 9 日
Hello,
I am optimizing my propeller design by fmincon function with sqp algorithm. In order to decrease the computational cost, I loose the constraint to an acceptable level. I use XFOIL + XROTOR to design my propeller. Those Fortran programs only have 4 demical precision. Below is my setup:
function [x, f, eflag, output, grad] = runoptimiser(x0, lb, ub, rOverR, Oper, const)
xLast = [];
myf = [];
ineq = [];
eq = [];
options = optimoptions(@fmincon,'Display','iter-detailed', ...
'Algorithm','sqp', ...
'FunValCheck', 'off',...
'FiniteDifferenceType', 'forward',...
'UseParallel', true, ...
'DiffMinChange', 0.05,...
'OptimalityTolerance', 1e-3, ...
'StepTolerance', 1e-3, ...
'ConstraintTolerance', 1e-3, ...
'PlotFcn', {@optimplotfval, @optimplotx, ...
@optimplotfirstorderopt, @optimplotfunccount, ...
@optimplotstepsize, @optimplotconstrviolation, ...
@optimplotfunccount}, ...
'OutputFcn', @outputFcn_global, ...
'MaxIterations', 15);
[x,f,eflag,output,~,grad,~] = ...
fmincon(@(x) objfun(x),x0,[],[],[],[],lb,ub,@(x) constr(x),options);
function f = objfun(x)
if ~isequal(x,xLast)
[myf, ineq, eq] = propeller_analysis(x, rOverR, Oper, const);
xLast = x;
end
f = myf;
end
function [c,ceq] = constr(x)
if ~isequal(x,xLast)
[myf, ineq, eq] = propeller_analysis(x, rOverR, Oper, const);
xLast = x;
end
c = ineq;
ceq = eq;
end
If there is a infeasible design, I will return NaN to objective function and constraints.
However, when I use the code, this error appears sometime:
Provided objective gradient does not have the correct number of elements.
I have checked the code and try to play with DiffMinChange, but it doesn't really solve the problem. Does anyone have experience on this error?
I really appreciate any help. Thanks!
  13 件のコメント
Walter Roberson
Walter Roberson 2023 年 8 月 8 日
Is it possible that when you return NaN that you are returning scalar NaN in a situation that expects a vector of NaN ?
Yi-Kai Peng
Yi-Kai Peng 2023 年 8 月 9 日
Hi Walter,
This is my way to return NaN
myf = NaN;
ineq = [NaN NaN];

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

回答 (0 件)

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by