From: Markus Staudinger on
Dear all,

I have a problem with the constraint function in fmincon. The error message is:

??? Error using ==> fmincon at 612
FMINCON cannot continue because user supplied nonlinear constraint function
failed with the following error:
Error using ==> myconstr
Too many input arguments.

Error in ==> Optim at 38
[optparams(s,1:4,3),lik(s,3)] = fmincon(@lnLIL,cfg.iniparamsIL,[],[],[],[],...

Optim determines the model to fit and calls fmincon:
case 'IL'
[optparams(s,1:4,3),lik(s,3)] = fmincon(@lnLIL,cfg.iniparamsIL,[],[],[],[],...
cfg.lboundIL,cfg.uboundIL,@myconstr,options,cfg,choice(:,s),rew(:,s));

[L(s,3),model(s,3)] = lnLIL(optparams(s,:,3),cfg,choice(:,s),rew(:,s));
...
myconstr reads:
function [c,ceq,gradc,gradceq] = myconstr(x)

c(1) = x(1) - 1/x(4);
c(2) = x(2) - 1/x(4);
ceq = [];

if nargout > 2
gradc = [1, 1; 1/x(4)^2, 1/x(4)^2];
gradceq = [];
end
end

Even when I change [optparams(s,1:4,3),lik(s,3)] to [x,lik(s,3)] in Optim it doesn't work.
Has anybody got any idea?

Many thanks
Markus
From: Alan Weiss on
On 7/14/2010 7:48 AM, Markus Staudinger wrote:
> Dear all,
>
> I have a problem with the constraint function in fmincon. The error
> message is:
>
> ??? Error using ==> fmincon at 612
> FMINCON cannot continue because user supplied nonlinear constraint function
> failed with the following error:
> Error using ==> myconstr
> Too many input arguments.
***SNIP***
> myconstr reads:
> function [c,ceq,gradc,gradceq] = myconstr(x)
>
> c(1) = x(1) - 1/x(4);
> c(2) = x(2) - 1/x(4);
> ceq = [];
>
> if nargout > 2
> gradc = [1, 1; 1/x(4)^2, 1/x(4)^2];
> gradceq = [];
> end
> end
***SNIP***

Try the following:
if nargout > 2
gradc = [1, 0; 0, 1; 0, 0; 1/x(4)^2, 1/x(4)^2];
gradceq = [];
end

There might remain other problems, but at least your gradient will be
correct.

Alan Weiss
MATLAB mathematical toolbox documentation
From: Steven Lord on

"Markus Staudinger" <m.staudinger(a)uke.uni-hamburg.de> wrote in message
news:i1k804$4iv$1(a)fred.mathworks.com...
> Dear all,
>
> I have a problem with the constraint function in fmincon. The error
> message is:
>
> ??? Error using ==> fmincon at 612
> FMINCON cannot continue because user supplied nonlinear constraint
> function
> failed with the following error:
> Error using ==> myconstr
> Too many input arguments.
>
> Error in ==> Optim at 38
> [optparams(s,1:4,3),lik(s,3)] =
> fmincon(@lnLIL,cfg.iniparamsIL,[],[],[],[],...
>
> Optim determines the model to fit and calls fmincon:
> case 'IL'
> [optparams(s,1:4,3),lik(s,3)] =
> fmincon(@lnLIL,cfg.iniparamsIL,[],[],[],[],...
>
> cfg.lboundIL,cfg.uboundIL,@myconstr,options,cfg,choice(:,s),rew(:,s));

When you pass input arguments after the options structure like this, ALL
functions involved in the FMINCON call (the objective function, the
nonlinear constraint function, and any function handles specified in the
options structure) MUST accept those additional inputs, even if they don't
use them. I recommend, if you're using MATLAB 7.0 (R14) or later, that you
use anonymous functions instead.

% Assuming that no function handle inside the options structure requires
those additional inputs
% The signature you showed for myconstr indicated that it does not
[optparams(s,1:4,3),lik(s,3)] = fmincon(@(x) lnLIL(x,
cfg,choice(:,s),rew(:,s)), ...
cfg.iniparamsIL,[],[],[],[],...
cfg.lboundIL,cfg.uboundIL,@myconstr,options);

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com


From: Markus Staudinger on
"Steven Lord" <slord(a)mathworks.com> wrote in message <i1keh3$a48$1(a)fred.mathworks.com>...
>
> "Markus Staudinger" <m.staudinger(a)uke.uni-hamburg.de> wrote in message
> news:i1k804$4iv$1(a)fred.mathworks.com...
> > Dear all,
> >
> > I have a problem with the constraint function in fmincon. The error
> > message is:
> >
> > ??? Error using ==> fmincon at 612
> > FMINCON cannot continue because user supplied nonlinear constraint
> > function
> > failed with the following error:
> > Error using ==> myconstr
> > Too many input arguments.
> >
> > Error in ==> Optim at 38
> > [optparams(s,1:4,3),lik(s,3)] =
> > fmincon(@lnLIL,cfg.iniparamsIL,[],[],[],[],...
> >
> > Optim determines the model to fit and calls fmincon:
> > case 'IL'
> > [optparams(s,1:4,3),lik(s,3)] =
> > fmincon(@lnLIL,cfg.iniparamsIL,[],[],[],[],...
> >
> > cfg.lboundIL,cfg.uboundIL,@myconstr,options,cfg,choice(:,s),rew(:,s));
>
> When you pass input arguments after the options structure like this, ALL
> functions involved in the FMINCON call (the objective function, the
> nonlinear constraint function, and any function handles specified in the
> options structure) MUST accept those additional inputs, even if they don't
> use them. I recommend, if you're using MATLAB 7.0 (R14) or later, that you
> use anonymous functions instead.
>
> % Assuming that no function handle inside the options structure requires
> those additional inputs
> % The signature you showed for myconstr indicated that it does not
> [optparams(s,1:4,3),lik(s,3)] = fmincon(@(x) lnLIL(x,
> cfg,choice(:,s),rew(:,s)), ...
> cfg.iniparamsIL,[],[],[],[],...
> cfg.lboundIL,cfg.uboundIL,@myconstr,options);
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
>
Dear Steve,

many many many thanks!

Best regards
Markus
From: Markus Staudinger on
Alan Weiss <aweiss(a)mathworks.com> wrote in message <i1k9mc$nt6$1(a)fred.mathworks.com>...
> On 7/14/2010 7:48 AM, Markus Staudinger wrote:
> > Dear all,
> >
> > I have a problem with the constraint function in fmincon. The error
> > message is:
> >
> > ??? Error using ==> fmincon at 612
> > FMINCON cannot continue because user supplied nonlinear constraint function
> > failed with the following error:
> > Error using ==> myconstr
> > Too many input arguments.
> ***SNIP***
> > myconstr reads:
> > function [c,ceq,gradc,gradceq] = myconstr(x)
> >
> > c(1) = x(1) - 1/x(4);
> > c(2) = x(2) - 1/x(4);
> > ceq = [];
> >
> > if nargout > 2
> > gradc = [1, 1; 1/x(4)^2, 1/x(4)^2];
> > gradceq = [];
> > end
> > end
> ***SNIP***
>
> Try the following:
> if nargout > 2
> gradc = [1, 0; 0, 1; 0, 0; 1/x(4)^2, 1/x(4)^2];
> gradceq = [];
> end
>
> There might remain other problems, but at least your gradient will be
> correct.
>
> Alan Weiss
> MATLAB mathematical toolbox documentation

Thank you very much, Alan!