MATLAB: Failure in initial objective function evaluation

fminconobjective-functionoptimization

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 1×24 vectors which are calculated in the main file. I need to find the optimum values for x1, y1 and z1 which are 1×24 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!

Best Answer

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
Related Question