fmincon error too many input arguments

Hello, could you please help me. First, the matlab successfully calculate the following code.
expCov = cell2mat(CovM(1));
ExpReturnMat = ExpReturnMatrix';
expRet = ExpReturnMat(:,1);
NLLfunction = @(x) fm_fitnessERC(expCov, x);
nonlcon = @(x) nonlcon(wgts0,Aeq,x);
[ERWCwgtsTC, fval, sqpExit] = fmincon(NLLfunction, wgts0,[], [], [], [], lbnds, [], nonlcon,qoptions);
function fval = fm_fitnessERC(expCov, x)
N = size(expCov,1);
y = x.*(expCov*x);
fval = 0 ;
for i = 1:N
for j = 1+1:N
xij = y(i) - y(j) ;
fval = fval + xij*xij ;
end
end
fval = sqrt(fval)
end
function [c,ceq] = nonlcon(wgts0,Aeq,x)
c = []
ceq = Aeq*[x + 0.01*abs(x-wgts0)]-1
end
However, I try to make loop for the next ERWCwgtsTC, where I want to replace wgts0 with ERWCwgts(:,z-1).
for z = 2:20
expCov = cell2mat(CovM(z));
ExpReturnMat = ExpReturnMatrix';
expRet = ExpReturnMat(:,z);
ERWCwgtsTC0 = ERWCwgtsTC(:,z-1);
NLLfunction = @(x) fm_fitnessERC(expCov, x);
nonlconERWC = @(x) nonlconERWC(ERWCwgtsTC0, Aeq, x);
[ERWCwgtsTC(:,z), fval, sqpExit] = fmincon(NLLfunction, ERWCwgtsTC0,[], [], [], [], lbnds, [], nonlconERWC,qoptions);
end
NLLfunction remain the same as above. But I adjust...
function [c,ceq] = nonlcon(ERWCwgtsTC0, Aeq, x)
c = []
ceq = Aeq*[x + 0.01*abs(x-ERWCwgtsTC0)]-1
end
I receive ERWCwgtsTC(:,2) but after that the process is stopped
The error is...
Error using
@(x)nonlconERWC(ERWCwgtsTC0,Aeq,x)
Too many input arguments.
Error in @(x)nonlconERWC(ERWCwgtsTC0,Aeq,x)
Error in fmincon (line 621)
[ctmp,ceqtmp] =
feval(confcn{3},X,varargin{:});
Caused by:
Failure in initial user-supplied
nonlinear constraint function
evaluation. FMINCON cannot continue.
Thank you in advance.

Answers (1)

The line
nonlconERWC = @(x) nonlconERWC(ERWCwgtsTC0, Aeq, x);
has a recursive reference to nonlconERWC -- it appears on both the left and right hand side.
Considering your other code it appears the line should be
nonlconERWC = @(x) nonlcon(ERWCwgtsTC0, Aeq, x);
By the way, there is no reason you should have needed to adjust your nonlcon code, since the name of the variable you pass for the first parameter does not need to match the internal name of the parameter within the routine. Your logic for nonlcon did not change, just the value you pass to it.

1 Comment

I did not know that I don't need to write and save the same nonlcon function by changing a value. Many Thanks for the quick responce.

Sign in to comment.

Asked:

on 12 Jun 2016

Commented:

on 13 Jun 2016

Community Treasure Hunt

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

Start Hunting!