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

10 次查看(过去 30 天)
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 ?

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Solver 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