setting up the ''options' in fmincon

Dear all,
I want to maximize a function, where the univariate unknown, x, with respect to which the maximization takes place is bound between -1 and 1. This is the only constraint. So, I use this
[xx,fval,exitflag,output,lambda,grad,HH]=fmincon('function',x0,...
[],[],[],[],-1,1,[], options);
The function is complicated which means that I can not solve the gradient or Hessian anallytically
However, I am not sure how to set up the 'options' part.
I thought of something like
options=optimset('LargeScale','off','display','off','TolFun',0.0001,'TolX',0.0001,...
'GradObj','off', 'Hessian','off','DerivativeCheck','off');
or
options = optimoptions('fmincon','Display','off','Algorithm','sqp');
Is there a more correct set up for the 'options'?
When I use the fminunc:
options=optimset('LargeScale','off','display','off','TolFun',0.0001,'TolX',0.0001,...
'GradObj','off', 'Hessian','off','DerivativeCheck','off');
[xx,fval,exitflag,output,G_sum,HH]=fminunc('function',x0,options);
I get better results but sometimes the algorithm stops and throws me the following warning
Error using roots (line 27)
Input to ROOTS must not contain NaN or Inf.
Error in lineSearch
Error in lineSearch
Error in lineSearch
Error in lineSearch
Error in fminusub (line 189)
lineSearch(funfcn,x,dir,f,dirDerivative, ...
Error in fminunc (line 457)
[x,FVAL,GRAD,HESSIAN,EXITFLAG,OUTPUT] = fminusub(funfcn,x, ...
Error in unified_2 (line 92)
[xx,fval,exitflag,output,G_sum,HH]=fminunc('function',x0,options,...
Thanks

 Réponse acceptée

Star Strider
Star Strider le 29 Déc 2017

0 votes

If you run it with no stated options (using the defaults instead) what is the result?
The NaN or Inf problem results when you get either 0/0 or something/0. See if you can ‘trap’ a zero denominator in your code for ‘function’. If the denominator is zero, substitute 1E-8 for it. Make further small changes or change your options structure if that causes misleading results.
Since you have a ‘complicated’ function, you will likely have to have to experiment on your own with solutions.

6 commentaires

ektor
ektor le 29 Déc 2017
Dear Strider,
Thanks for the reply. Can you provide a sample code on how to
1) use the defaults (without no stated options)
2) trap the zero and replace it with 1E-8
as I am not familiar with this kind of solutions
My pleasure.
1. Leave out the ‘options’ argument:
[xx,fval,exitflag,output,lambda,grad,HH]=fmincon('function',x0,...
[],[],[],[],-1,1,[]);
I always sbegin by using the defaults. I add an ‘options’ structure later if necessary.
2. With respect to ‘trapping’ the zero, I would do something like this:
threshold = 1E-10;
denominator = ...;
if abs(denominator) <= threshold
denominator = 1E-8
end
This is prototype code. Use your own variable in place of ‘denominator’. The result of your calculation involving ‘denominator’ can still be zero. It should remain finite.
This should get you started. You will have to experiment to get the result you want.
Thanks a lot. I will try and let you know asap. To deactivate the display I used instead
options=optimoptions(@fmincon, 'Display','off');
or
options=optimset( 'display','off');
[xx,fval,exitflag,output,lambda,grad,HH]=fmincon('function',x0,...
[],[],[],[],-1,1,options);
I think that either of the two 'options' choices maintains the defaults. Am I right?
The second approach will work:
options=optimset( 'display','off');
[xx,fval,exitflag,output,lambda,grad,HH] = fmincon('function',x0,...
[],[],[],[],-1,1,options);
As I read the documentation, name-value pair arguments are not permitted in fmincon, so the first will likely throw an error.
ektor
ektor le 29 Déc 2017
Many thanks!
As always, my pleasure!
Note that the correct call is:
options = optimset(@fmincon, 'display','off');
I am certain that you already discovered that.
If my Answer helped you solve your problem, please Accept it!

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by