Failure in initial user-supplied objective function evaluation.

Hi I am a beginner in matlab. Hope that someone can help me with this problem. I am doing a function minimization. The function depends on three variables (say x(1),x(2),q). But I want the function to be optimized over the first two variables (x(1),x(2)) only. So, for each value of q I assign, I will do fmincon on the function
here is my fmincon command.
x0= [0.01;0.007];
options=optimset('Algorithm','active-set');
[x fval]=fmincon(@(x)calc(x,q),x0,[ 0.5 -1 ; -1 1 ],[ 0 ; 0 ],[],[],[0;0],[1;1],[],[],options);
where calc is my function.
The warning that I get is this
Warning: Trust-region-reflective algorithm does not solve this type of problem,
using active-set algorithm. You could also try the interior-point algorithm: set
the Algorithm option to 'interior-point' and rerun.
> In fmincon at 460
In prog at 22
??? Error using ==> @(x)calc(x,q)
Too many input arguments.
Error in ==> fmincon at 540
initVals.f = feval(funfcn{3},X,varargin{:});
Error in ==> prog at 22
[x fval] = fmincon(@(x)calc(x,q),x0,A,b,Aeq,beq,lb,ub,c,ceq,options);
Caused by:
Failure in initial user-supplied objective function evaluation. FMINCON
cannot continue.
Cannot find the problem with my fmincon command especially the "too many input argument" problem. can someone tell me what is the problem that can cause the Failure in initial user-supplied objective function evaluation?
Thanks in advance.

4 Comments

What is calc? A user defined function? If yes, post the code.
Hi Mr. Komarov, here is the code for calc
function[Result] = calc(y,q)
Q = y(1);
x = y(2);
%let L_1 be the eigenvalues of rho_E
L_1 = 1 - Q - x;
L_2 = x;
L_3 = x - Q/2;
L_4 = (3*Q/2) - x;
rho_e = L_1*log(L1) + L_2*log(L_2) + L_3*log(L_3) + L_4*log(L_4) ;
Result = 1 - rho_e;
And what do you need q for? It's not used within the fcn you pasted.
I posted the full code down there. Sorry for simplifying the code, i thought I would be easier to solve the problem

Sign in to comment.

 Accepted Answer

This is a very common issue. These MATLAB functions require a handle to a function of one variable. Hence "too many input arguments". The solution is to make an anonymous function handle by embedding the parameter q:
q = %set value of q
f = @(x) calc(x,q);
[x fval]=fmincon(f,x0,[...etc...])
Here f is a function of one variable x, with the value of q hard-coded in. But it's easy to wrap that in a loop over values for q.

6 Comments

Hi Matt, I followed your instruction, but I still got the same problem. Too many input argument.
By the way, I copied my function calc.m up there, hope that it helps.
First, I have to second Oleg's question -- why have q in there? But anyway, you also need to remove the last [] before options. I didn't notice that before, sorry:
...[1;1],[],options);
Oh, and you also have a typo in calc. In the line where you define rho_e, you use L1 instead of L_1
Oh ya, that's a typo, because I retyped it here so that it is readable. But then, the calc function works well. okay, let me just post the full code. sorry for making everyone confused.
%fmincon constraint
c = [];
ceq = [];
Aeq = [];
beq = [];
b = [ 0 ; 0 ];
A = [ 0.5 -1 ; -1 1 ];
lb = [ 0 ; 0 ];
ub = [ 1 ; 1 ];
%option
options=optimset('Algorithm','active-set');
for q = 0.01 : 0.01 : 0.5
x0 = [ 0.01 ; 0.007 ];
f = @(x) calc(x,q);
[x fval] = fmincon(f,x0,A,b,Aeq,beq,lb,ub,c,ceq,options);
end
here is the calc function.
function[Result] = calc(y,q)
Q = y(1);
x = y(2);
B_1 = -2*q*(-1 + q);
B_2 = 1 - 2*q + 2*q^2;
%let L_1 be the eigenvalues of rho_E
L_1 = 1 - Q - x;
L_2 = x;
L_3 = x - Q/2;
L_4 = (3*Q/2) - x;
sigma_b = elog(B_1) + elog(B_2) ;
rho_e = elog(L_1) + elog(L_2) + elog(L_3) + elog(L_4) ;
Result = 1 - rho_e - sigma_b;
I called a new function elog, because I need to deal with the case when the argument goes to zero. here is the elog function
function[value] = elog(x)
if x < 0.0000001
value = 0;
else
value = - x*log(x);
end
those are the code. May I know what is the usual problem that arises when we have this warning?
Error in ==> fmincon at 540
initVals.f = feval(funfcn{3},X,varargin{:});
Same problem as I mentioned above: you have too many constraint inputs before the options. The nonlinear constraints are specified in a single input, so take out ceq. This works:
[x,fval] = fmincon(f,x0,A,b,Aeq,beq,lb,ub,c,options);

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!