Fmincon error "Provided objective gradient does not have the correct number of elements."
Afficher commentaires plus anciens
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 commentaires
Torsten
le 8 Août 2023
We don't see from your code where you return the gradient. Do you return a vector of NaN which has the same size as x in case of an infeasible design ?
Yi-Kai Peng
le 8 Août 2023
Torsten
le 8 Août 2023
Does the problem also arise if you set "UseParallel" to "false" ?
Yi-Kai Peng
le 8 Août 2023
Torsten
le 8 Août 2023
In the course of time, I read about some "strange things" happening in parallel computations.
If this is not the reason, why else should the error message appear if you don't provide the objective gradient ?
Yi-Kai Peng
le 8 Août 2023
Yi-Kai Peng
le 8 Août 2023
Modifié(e) : Yi-Kai Peng
le 8 Août 2023
Yi-Kai Peng
le 8 Août 2023
The fact that you coupled MATLAB with two external programs will of course complicate things further.
Yi-Kai Peng
le 8 Août 2023
Modifié(e) : Yi-Kai Peng
le 8 Août 2023
Walter Roberson
le 8 Août 2023
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
le 9 Août 2023
Réponses (0)
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!