fmincon sometimes returns "incorrect number of elements of objective gradient" error.
5 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
This is my first time asking question on the forum.
I am using the fmincon function to optimize my propeller design. I have 14 design variables to define my propeller geometry and use the propeller_analysis function to do propeller analysis. My objective function is to find the highest propeller efficiency, and the only inequality constraint is that the resultant thrust has to be larger or equal to the required thrust.
Here is my fmincon setup:
xLast = [];
myf = [];
ineq = [];
eq = [];
outputFcn_global_data = [];
options = optimoptions(@fmincon,'Display','iter-detailed', ...
'Algorithm','sqp', ...
'FiniteDifferenceType', 'forward',...
'FunValCheck', 'off',...
'UseParallel', true, ...
'DiffMinChange', 0.1,...
'OptimalityTolerance', 1e-3, ...
'StepTolerance', 5e-5, ...
'PlotFcn', {@optimplotfval, @optimplotx, ...
@optimplotfirstorderopt, @optimplotfunccount, ...
@optimplotstepsize, @optimplotconstrviolation, ...
@optimplotfunccount}, ...
'OutputFcn', @outputFcn_global, ...
'MaxIterations', 10, ...
'MaxFunctionEvaluations', 200);
[x,f,eflag,output] = fmincon(@(x) objfun(x),x0,[],[],[],[],lb,ub,@(x) constr(x),options);
And my objective function and constraints:
[myf, ineq, eq, ~, ~] = propeller_analysis(x, rOverR, Oper, const);
Lastly, this is how I normalized the design variables, objective value, and the constraint:
% Design variable
dist = abs(ub-lb); % Distance between bounds
mid_p = (ub+lb) / 2; % Find the midpoint
xdes_0 = (x0 - mid_p).*(2./dist); % Normalized design variables
lb_norm = (lb - mid_p).*(2./dist); % Normalized lower bound
ub_norm = (ub - mid_p).*(2./dist); % Normalized upper bound
% obejctive and constraints
myf = (2-effy)/2;
ineq = 1 - (T/T_req); % Inequality constraints
eq = []; % Consistency constraints (equality)
This way I makes sure that the normalized upper and lower bounds are 1 and -1, respectively. Morevoer, the normalized deisgn variables are always within this range. For the objective value and constraint, the output values are normalized by the ideal efficiency and required thrust. The efficiency from XROTOR is 4 decimal precision.
However, if there is an infeasible design that trigger my conditions, I return the following:
myf = NaN;
ineq = NaN;
eq = [];
However, I have a trouble of getting the following problem:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1436713/image.png)
I suspected some settings causing this issue:
- The step change is too small and cause the objective gradient doesn't change.
- The NaN return causes the error.
Yet, I couldn't pinpoint the root cause. My MATLAB version is 2021b. Hope anyone can help me. Thanks.
2 个评论
Matt J
2023-7-18
The error message mentions a "provided objective gradient", but your optimoptions do not show SpecifyobjectiveGradient=true. This makes me wonder if you are showing the code faithfully.
回答(1 个)
Matt J
2023-7-18
编辑:Matt J
2023-7-18
My best guess is that the MaxFunctionEvaluations limit was reached midway through finite differencing operations needed to complete a gradient evaluation. There is no reason that I can see to set MaxFunctionEvaluations=200 if you are only running 10 iterations. You should just set it to Inf.
Incidentally, your OptimalityTolerance and StepTolerance seem high as well. I wonder if you have a good reason not to use the default values.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!