failure in initial objective function evaluation

10 views (last 30 days)
Hello,
I want to solve an optimization problem by Matlab fmincon function. I defined my objective function as:
function f = objfun(x,p1,p2)
x1=x(1:24);
y1=x(25:48);
z1=x(48:end);
f=-mean(x1*(p1)'+y1*(p2)')+mean(z1*(p1)')
end
p1,p2 are 1x24 vectors which are calculated in the main file. I need to find the optimum values for x1, y1 and z1 which are 1x24 vectors (total 72 variables).
When I run the main code I get an error message about the objective function and then for fmincon by failure in initial objective unction evaluation. caused
Would you please advise how can I solve the problem? Thanks a lot!
  2 Comments
Alan Weiss
Alan Weiss on 5 Sep 2017
Please show us your fmincon call, the complete copy-pasted error message, and the value of x0. The problem could be as simple as you having x0 as a row vector instead of a column vector, or p1 a row vector, but without seeing the inputs to the problem and the complete error message, we cannot know.
Alan Weiss
MATLAB mathematical toolbox documentation
Sara
Sara on 5 Sep 2017
Thanks for your reply. I use below in my main code: fun=@(x)objfun(x,p1,p2) x0 is a 1x72 vector. p1 and p2 are 1x24 vectors. The error is as below: Error using * Inner matrix dimensions must agree.
Error in objfunFCR (line 6) f = -mean(x1*(p_FCRN/100)'+y1*(p_FCRD/100)')+mean(z1*(p_FCRN/100)');
Error in FCR_N_D>@(x)objfunFCR(x,p_FCRN,p_FCRD)
Error in fmincon (line 536) initVals.f = feval(funfcn{3},X,varargin{:});
Error in FCR_N_D (line 398) [x,fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,@chg_dischg,options)
Caused by: Failure in initial objective function evaluation. FMINCON cannot continue.
p.s. I made a bit changes to the names of inputs to simplify my question here.
I appreciate your comments!

Sign in to comment.

Accepted Answer

Alan Weiss
Alan Weiss on 5 Sep 2017
Thank you for the complete error message. z1 should be defined as
z1 = x(49:end);
I believe that, as you have defined it, z1 has 25 elements.
I am not really sure that you gave us the code of your function. The mean statements do not do anything, as all of the entries x1*p1' and the others are scalars, and mean leaves scalars alone.
Alan Weiss
MATLAB mathematical toolbox documentation
  2 Comments
Sara
Sara on 6 Sep 2017
Thank you very much for your reply. It was exactly my mistake in z definition. I used 'mean' because I need to calculate expectation values of x1*p1, x2*p2, .... . Is there any other way in Matlab to define expectation value?
Alan Weiss
Alan Weiss on 6 Sep 2017
The mean statement is superfluous. When you multiply, the matrix definition of multiply gives
x1*(p1') = x1(1)*p1(1) + x1(2)*p1(2) + ... + x1(24)*p1(24)
This is a scalar. To get the mean, you could divide by 24. But to optimize, the factor of 24 does not matter.
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!