fmincon non linear constraint: too many input arguments

Hello, I need to minimize a log-likelihood function subject to a non-linear constraint. So I created a function for this constraint to pass it to the fmincon. But it prompts the error too many inputs
The objective function (x are 5 parameters)
[a1 a2 a3]=objfun(x,data,data2)
The non linear constraint
[c ceq]=nonlinconfun(x)
When running fmincon
[xhat fval]= fmincon(@objfun, x0, [], [], [], [], [], [], lb, ub, @nonlinconfun , options, data, data2)
I get this error:
Error using nonlinconfun
Too many input arguments.
Error in fmincon (line 651)
[ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});
Error in origfun (line 31)
[xhat fval]= fmincon(@objfun, x0, [], [], [], [], [], [], lb, ub, @nonlinconfun , options, data, data2)
Caused by:
Failure in initial user-supplied nonlinear constraint function evaluation. FMINCON cannot continue.
Remarks:
A) In this example I know the final parameters in advance. I enter the final parameters as x0.
B) nonlinconfun evaluates the parameters x, it shouldn't need data or data2. So If I do
[xhat fval]= fmincon(@(x) objfun(x,data,data2), x0, [], [], [], [], [], [], lb, ub, @nonlinconfun , options)
Here fmincon computes xhat but these are very different from the final (known in this case) parameters.
C) And ignoring the non-lin constraint, fmincon outputs xhat in one iteration the correct params
[xhat fval]= fmincon(@objfun, x0, [], [], [], [], [], [], lb, ub,[] , options, data, data2)
But if I slightly change x0 it outputs very different xhat.
It might be the way I'm passing the constraint but I cannot seem to fix this. I tried to follow some examples but still same error. Let me know if it is not clear. Thanks

2 Comments

what is data and data2?
data is a vector 500x1, data2 is a scalar. data is used to estimate the parameters in the loglikelihood func.

Sign in to comment.

Answers (1)

Syntax (A) is incorrect. Syntax (B) is correct. Syntax (C) is correct, but deprecated. If you want to run without constraints, it would be better to do
[xhat fval]= fmincon(@(x) objfun(x,data,data2), x0, [], [], [], [], [], [], lb, ub, [],options)
As for the result you are getting, there is as yet, no reason to think the result is incorrect, even if it is unexpected. There could be multiple solutions. Or you could have coded the objective/constraints incorrectly. Or, there could be better solutions than you knew. Have you compared fval output in the various scenarios? Which gives the lowest value and how different are the values?

4 Comments

Hi, in A) I was just mentioning that I use the "correct" known final parameters as the initial parameters x0. Using these I can calculate the loglikelihood also, -3500. "correct" in the sense that I'm taking them and the logl from a paper.
In B) it outputs params very different from the correct params. Not only that, the params it outputs are some the lower bounds. Loglikelihood is -3510. So I think I'm doing something wrong here.
In C) it iterates one time only, and outputs ... yes, the "correct" params. But if I slightly change x0, then the estimates are very different, some are actually the lower bounds which doesn't look good, and the loglikelihood is -3510.
Hi, in A) I was just mentioning that I use the "correct" known final parameters as the initial parameters x0.
OK, but why is the title of your post based on the results of A) if you already know if and why A) is wrong?
Loglikelihood is -3510. So I think I'm doing something wrong here.
You might be doing something wrong, but it has nothing to do with the code that you've shown us. Clearly, fmincon is doing its job correctly because the loglikelihood value that you are getting is an improvement over your initial point. -3510 is lower than -3500.
I think we're talking about different "A"s. I was referring to the Remark A, where I mention that x0 is the same in all cases: the main case which is not working; case B w/ nonlinconstraint; and case C w/o nonlinconstraint.
In all these cases I use as x0 the parameters that I know in advance are the "final/correct" ones. I take these params as the correct ones since these are from a paper, which only publishes the params and logl. And I think your "A" refers to the code where I get the error. Yes that code is incorrect.
So, when I fix it with the nonlinconfun in B, the parameters it outputs are very different from the "final/correct" params. Hence my concern. To add, the params fmincon outputs most are actually the lower bounds I used.
I'm trying to see what I'm doing wrong, checked the objfun which is the logl, checked the nonlinconfun which is very simple. I thought the main doubts would be around how to actually set the fmincon because the rest is pretty standard
I thought the main doubts would be around how to actually set the fmincon because the rest is pretty standard
Again, clearly not. fmincon found a point that is provably better than the one you thought was the best. Clearly, the problem is in your implementation of the objective function or constraints. Either that, or the paper is wrong about which point is the best.
checked the objfun which is the logl
Probably not. If logl is the loglikelihood (to be maximized), then you should be minimizing -logl. Thus -logl should be the objective function.

Sign in to comment.

Categories

Asked:

on 9 Dec 2014

Edited:

on 10 Dec 2014

Community Treasure Hunt

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

Start Hunting!