MATLAB: Nonlcon : Too many input arguments.

constrian function error

Hi friends, I have a problem with optimization algorithm like GA, fmincon and patternsearch. When I try to add constrain to my problem error below appear:
Error using Constrain
Too many output arguments.
Error in poptimfcnchk (line 58)
[cineq,ceq] = feval(nonlcon,reshapeinput(Xin,X),conFcnArg{:});
Error in patternsearch (line 343)
[Iterate,OUTPUT.funccount] = poptimfcnchk(FUN,nonlcon,initialX,Iterate, ...
Error in Main (line 30)
[x,Fval,exitFlag,output]=patternsearch(@(x)
Objective(x),X0,[],[],[],[],LB,UB,@Constrain,options)
Caused by:
Failure in initial user-supplied nonlinear constraint function evaluation.
PATTERNSEARCH cannot continue.
However, when I omit the constrain from optimization it works very well. After adding adding constrain to my problem this error occur. Could anyone help???
Thanks.

Best Answer

To quote the documentation on nonlinear constraints, "Nonlinear constraint functions must return both c and ceq, the inequality and equality constraint functions, even if they do not both exist. Return empty [] for a nonexistent constraint."
In other words, you need to rewrite your constraint function as follows, assuming your existing constraints are inequality rather than equality constraints:
function [Con,coneq] = Constrain(x)
Con = [];
coneq = [];
input.accel.param = {'spds','max_speed_bool'} ;
input.accel.value = {[0 37.28]};%;37.28 55.9234]} ;
[error, resp] = adv_no_gui('accel_test',input) ;
accel_1 = resp.accel.times(1) ;
MaxSpeed = resp.accel.max_speed*1.60934 ;
if ~error
Con=[accel_1/10-1;1-MaxSpeed/100];
end
Alan Weiss
MATLAB mathematical toolbox documentation