Clear Filters
Clear Filters

How to pass user-defined gradients for anonymous non-linear constraints in fmincon

2 views (last 30 days)
Hi to all,
I have a non-linear optimization problem where both non-linear constraints and objective functions have to be defined anonymously. I want to use fmincon as efficiently as possible by providing user-supplied gradients for both gradients and objective, and user-supplied Hessian for objective.
How can I pass these gradients for anonymously defined functions?
Thanks in advance
Ebru
  6 Comments
Matt J
Matt J on 6 Sep 2022
You should just modify non_linear_constraints to (optionally) return the gradients:
function [c,ceq,gc,gceq]=non_linear_constraints(x,model_disservice,alpha,q,n,k,X_design);
c=zeros(q-1,1);
ceq=[];
z_a = norminv(1-(alpha/2));
B_predict=1;
[y_hat_disservice,mspe_disservice] = SKpredict_new(model_disservice,x,B_predict);
c(1)= y_hat_disservice-z_a * sqrt(mspe_disservice);
if nargout>3 %check if fmincon really asked for the gradients
gc=...
gceq=[];
end
end

Sign in to comment.

Accepted Answer

Matt J
Matt J on 6 Sep 2022
Edited: Matt J on 6 Sep 2022
I suspect you've misled yourself into believing the functions must be defined anonymously. I don't know of any situation where that truly is the case. Nevertheless, you can accomplish what you ask as in the example below. The convergence of fmincon will benefit from providing the gradients, however, you will never be able to get the same efficiency as a non-anonymous function implemention. This is because an anonymous function must always compute the function gradients whenever it is invoked, even at steps where fmincon requires only the function values.
nonlcon=@(x)f(nargout,{x^2-1,tan(x)+3,2*x,sec(x)^2});
[c,ceq]=nonlcon(1)
c =
0
ceq =
4.5574
[c,ceq,gc,gceq]=nonlcon(1)
c =
0
ceq =
4.5574
gc =
2
gceq =
3.4255
function varargout=f(n,c)
varargout=c(1:n);
end
  3 Comments
Matt J
Matt J on 6 Sep 2022
For example, some of the fmincon algorithms involve line search steps (e.g. SQP and, with certain settings, interior-point). Line searches will involve repeated evaluation of the function, but not its derivatives.
Ebru Angun
Ebru Angun on 6 Sep 2022
I see, thanks for the info. I will check interior-point since this is the algorithm I am planning to use.

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!