Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

fmincon function weird error

Asked by seldeeno on 27 Apr 2013

Hello,

I am wondering why am I getting this weird error:

Error using myfun
Too many input arguments.
Error in fmincon (line 631)
      initVals.f = feval(funfcn{3},X,varargin{:});

Although my function myfun has only one argument that is x, but looks like the implementation of fmincon calls it with 3!

Help please

2 Comments

Zhang lu on 27 Apr 2013

show your all code please

seldeeno on 27 Apr 2013

All my code is large (4 long files) but the related parts are as follows: In the first file this is the call to fmincon, with initialization:

for i=1:NP
    pop(i,:)=XRmin+(XRmax-XRmin).*rand(1,D);
end

Call to the function:

[temppop,tempval,exitflag,output] = fmincon(@myfun,xx,[],[],[],[],...
                    XRmin(1,:),XRmax(1,:),@mycon,opts,varargin{:});

This is the function that is referenced:

function f = myfun(x)
  [f, c, ceq] = mlbsuite(x, 38, 0, 'g16');
  f = - f; % because we want to minimize and its a MAX problem
  return
end

And this is mycon:

    function [c,ceq] = mycon(x)
      [f, c, ceq] = mlbsuite(x, 38, 0, 'g16');

Thanks

seldeeno

Products

No products are associated with this question.

1 Answer

Answer by Matt J on 27 Apr 2013

The varargin{:} that you are passing to FMINCON are being passed to myfun and mycon as extra arguments. However, the input signature of these functions is written only to accept a single input argument x.

If the extra varargin parameters are not needed by your objective/constraints, then remove them. If they are needed, then passing them through varargin is an outdated method. See here for the newer techniques

http://www.mathworks.com/help/optim/ug/passing-extra-parameters.html

2 Comments

seldeeno on 27 Apr 2013

Are you talking about fmincon or myfun? I am just passing x to myfun, but in fmincon it is calling my function with extra parameters as you can see from the error and that is what is making me dont understand this

Matt J on 27 Apr 2013

I'm talking about FMINCON. If you don't want FMINCON to send extra parameters to myfun, then don't call FMINCON with those extra parameters,

 [temppop,tempval,exitflag,output] = fmincon(@myfun,xx,[],[],[],[],...
                    XRmin(1,:),XRmax(1,:),@mycon,opts); %omit varargin{:}
Matt J

Contact us