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

10 views (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 8 Aug 2023
Is it possible that when you return NaN that you are returning scalar NaN in a situation that expects a vector of NaN ?

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!