How do I address "Error using barrier (line 31) Gradient at initial point contains Inf, NaN, or complex values. Fmincon cannot continue."

9 views (last 30 days)
I have created an output function that I am using with fmincon that provides a gradient as an output. When optimizing, I keep getting the following error:
----------------------------------------------
Error using barrier (line 31)
Gradient at initial point contains Inf, NaN, or complex values. Fmincon cannot continue.
Error in fmincon (line 799)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...
Error in loop_gradtest>(parfor body) (line 13)
wts_out(:,i) = fmincon(@(x)utility_gradtest(x,score_param1,
score_param2,rets),x,[],[],[mean(returns_grid_parallel)*12;ones(1,size(returns_grid_parallel,2))],[.05;1],double(limits_min(:,2)),double(limits_max(:,2)),[],fmin_options);
Error in loop_gradtest (line 9)
parfor i=1:10000;
------------------------------------------------------------
The function gradtest is used to all another function, utility_gradtest that is used to calculate maximum (min negative) utility given a vector of weights. In this function, using finite differencing, I calculate a gradient that is then supplied as part of the output for utility_gradtest.
I've used the following input in setting my options:
fmin_options = optimset('Algorithm','interior-point','GradObj','on','MaxFunEvals',10000,'MaxIter',500,'TolCon',10e-16,'TolX',10e-16,'Display','off','UseParallel',true);
Do I need to incorporate error handling capability to account for when the gradient contains an undefined point?
  6 Comments
J. Womack
J. Womack on 14 Aug 2015
Walter - thank you for the recommendation. I followed your advice and just focused on capturing the gradient from the parfor loop and found the problem. I've added in code to address NaNs and Infs and it's working.
I will run through the code and clean it up to try to capture additional speed and appreciate you pointing out the repetition. Where it's within the same function, I can make the change.
Thank you again for your help!

Sign in to comment.

Accepted Answer

J. Womack
J. Womack on 14 Aug 2015
This question was answered.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!