MATLAB: Failure in initial user-supplied objective function evaluation.

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.

Best 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.