how to pass nonlinear nested constraints in patternsearch?

Hi I have used patternsearch for minimizing a four-parameter nonlinear constraint function(J(x)). My program is:
%%aa, bb, t, A are inputs and are n×1 matrices
J=@(x)0;
for i=1:length(aa)
j=2;
S=@(x)(1/x(1))*(1-exp(-t(i)/x(2)));
while j<=i
S=@(x)S(x)-(exp(-t(i)/x(2))/(x(2)*x(3)))*(((aa(j)+aa(j-1)+x(4)*bb(j))*exp(t(j)/x(2))*(0.5*(t(j)-t(j-1))));
j=j+1;
end
R=@(x)(S(x)^(-1))-A(i);
J=@(x)J(x)+R(x);
c{i}=@(x)R(x)-0.001;
i=i+1;
end
A=[0 -1 0 1];
b=0;
Aeq=[];
beq=[];
lb=[0 0 0 0];
ub=[1 inf inf inf];
x0=[1 2 3 4];
opts=optimset('MaxFunEval',1e8,'TolFun',1e-3,'MaxIter',5000);
[x,fval,exitflag,output]=patternsearch(J,x0,A,b,Aeq,beq,lb,ub,c,opts)
When I run it without constraints, it works well but by considering the constraints an error ‘Error using @(x)R(x)-0.001.Too many input arguments… Caused by: Failure in initial user-supplied nonlinear constraint function evaluation. PATTERNSEARCH cannot continue.’ occurs. I guess it is because my constrains are nested functions and the algorithm considers the R(x) as an argument. Can anybody help me to eliminate this problem?

2 Comments

Please format your code using the {}Code button. Then we might have a chance to read it.
Alan Weiss
MATLAB mathematical toolbox documentation
i formatted the code. please help.

Answers (1)

Not only do I find your coding mysterious, but I am pretty sure that it doesn't do what you think it does. I suggest that you step through the code using the debugger to see what it is doing.
As for your question, it seems that your nonlinear constraint is wrong in two ways:
  1. Nonlinear constraint functions must return two outputs, c and ceq. Just calling yours c does not exempt your function from returning two outputs. If you don't have ceq, then set ceq to [].
  2. Your c seems to return a cell array. If that is correct, then that is not the correct form of a nonlinear constraint function. Instead, c must return a real vector. And, of course, it must return ceq as well.
For details, see nonlinear constraints.
Alan Weiss
MATLAB mathematical toolbox documentation

2 Comments

Dear Alan Thank you for the answer. I have tried
ceq = [];
nonlinfcn = @(x)deal(c(x),ceq(x));
but the same error massage appeared. My exact problem is that i do not know how to express my complicated nested nonlinear constraint array in the correct format.
Any help would be appreciated.
Is your c function returning a cell array or a vector? If it returns a cell array, then that is your problem. Once more, I suggest that you use the debugger to see what happens as your code runs.
Alan Weiss
MATLAB mathematical toolbox documentation

This question is closed.

Asked:

on 2 Dec 2014

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!