# Can I disable intermediate calculations for fmincon

5 views (last 30 days)
Fangning Zheng on 24 Jun 2022
Commented: John D'Errico on 24 Jun 2022
In the optimization fmincon, there is always a lot of the 'intermediate calculations' (can be referred to https://www.mathworks.com/help/optim/ug/iterations-and-function-counts.html#mw_dc044841-a6b6-43c0-8b29-0af2fbbcb66c) that increase the function counts during optimization. In the link it says that the "intermediate calculations can involve evaluating the objective function and any constraints at points near the current iterate x_i. For example, the solver might estimate a gradient by finite differences."
What is the purpose of these intermediate calculations? Since I have provided the gradient calculation for my objective function, why would the optimizer need to calculate the finite difference gradient? My example objective function does not have any constaints.
My example code and the output for optimization are shown below. From the output, the 'Iter' term and 'F-count' term show that there are many intermediate calculations involved.
If the calculations for the objective and the gradient are expensive, the intermediate calculation can take a lot of time.
'iter');
fun = @rosenboth;
x0 = [-1,2];
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
nonlcon = [];
[x,f] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
First-order Norm of Iter F-count f(x) Feasibility optimality step 0 1 1.040000e+02 0.000e+00 3.960e+02 1 7 1.028667e+02 0.000e+00 6.444e+02 7.071e-01 2 9 8.035769e+01 0.000e+00 4.797e+02 7.071e-01 3 10 6.132722e+00 0.000e+00 7.816e+00 1.155e+00 4 11 6.065238e+00 0.000e+00 5.189e+00 3.681e-02 5 12 5.678075e+00 0.000e+00 6.330e+00 2.437e-01 6 14 5.112684e+00 0.000e+00 3.119e+01 5.825e-01 7 15 4.769085e+00 0.000e+00 2.229e+01 8.987e-02 8 16 4.630101e+00 0.000e+00 4.064e+01 6.700e-01 9 17 3.708221e+00 0.000e+00 7.080e+00 1.786e-01 10 18 3.175089e+00 0.000e+00 7.950e+00 2.845e-01 11 20 3.165815e+00 0.000e+00 2.115e+01 2.951e-01 12 21 2.899436e+00 0.000e+00 9.888e+00 1.282e-01 13 22 2.725372e+00 0.000e+00 7.164e+00 6.340e-02 14 23 2.382814e+00 0.000e+00 1.316e+01 3.822e-01 15 24 2.129017e+00 0.000e+00 4.236e+00 1.134e-01 16 25 1.874512e+00 0.000e+00 4.274e+00 1.216e-01 17 27 1.784218e+00 0.000e+00 1.310e+01 2.576e-01 18 28 1.522263e+00 0.000e+00 3.092e+00 1.092e-01 19 29 1.353081e+00 0.000e+00 2.848e+00 7.749e-02 20 31 1.178302e+00 0.000e+00 8.209e+00 1.660e-01 21 32 1.014260e+00 0.000e+00 3.229e+00 2.716e-02 22 33 7.723798e-01 0.000e+00 5.463e+00 1.596e-01 23 34 6.002908e-01 0.000e+00 3.264e+00 8.887e-02 24 35 4.638434e-01 0.000e+00 4.710e+00 1.346e-01 25 36 2.907823e-01 0.000e+00 5.478e+00 2.276e-01 26 39 1.881240e-01 0.000e+00 6.312e+00 1.949e-01 27 40 1.729287e-01 0.000e+00 1.782e+00 9.193e-02 28 41 1.396410e-01 0.000e+00 8.969e-01 6.110e-02 29 43 1.223560e-01 0.000e+00 2.758e+00 6.740e-02 30 44 1.073474e-01 0.000e+00 2.984e+00 4.174e-02 First-order Norm of Iter F-count f(x) Feasibility optimality step 31 45 5.633254e-02 0.000e+00 1.855e+00 1.399e-01 32 46 3.253577e-02 0.000e+00 1.257e+00 9.971e-02 33 47 1.470709e-02 0.000e+00 1.393e+00 1.220e-01 34 48 1.418260e-02 0.000e+00 3.657e+00 9.564e-02 35 50 2.088770e-04 0.000e+00 4.220e-01 1.271e-01 36 51 1.699139e-04 0.000e+00 4.750e-02 7.371e-03 37 52 6.403872e-05 0.000e+00 7.905e-02 1.168e-02 38 53 7.152289e-06 0.000e+00 9.437e-02 1.448e-02 39 54 3.937940e-07 0.000e+00 2.330e-02 2.254e-03 40 55 1.379737e-10 0.000e+00 6.095e-05 4.873e-04 41 56 3.901588e-14 0.000e+00 1.103e-06 2.559e-05 42 57 1.179907e-20 0.000e+00 4.271e-09 4.383e-07 Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
function [f, g] = rosenboth(x)
f = 100*(x(2) - x(1)^2)^2 + (1-x(1))^2;
if nargout > 1 % gradient required
g = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1));
200*(x(2)-x(1)^2)];
end
end

Matt J on 24 Jun 2022
Edited: Matt J on 24 Jun 2022
Since you are specifying the objective gradient, finite difference calculations will not be executed for that particular piece of the iteration loop. However, if second derivatives are needed and you haven't provided a Hessian calculation, finite differences will still be need for that. Also, multiple function evaluations may still be necessary, depending on the algorithm, for things like line searches.
John D'Errico on 24 Jun 2022
You can. However, you should note that a basic gradient descent will be extremely slowly convergent on many problems.

R2019b

### Community Treasure Hunt

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

Start Hunting!