Attempted to access x(10); index out of bounds because numel(x)=2.

1 view (last 30 days)
Hello, I'm trying to solve a non linear optimization problem. And I get this error. I have no idea what is happening.
objfun:
function f = objfun(x)
f = 76500*x(1)*x(2);
confun:
function [c, ceq] = confun(x)
% Nonlinear inequality constraints
c = [0.0075-x(1)*x(2)^2; 0.000066425-x(1)*x(2)^3; 0.04-x(10); x(1)-0.12; 0.06-x(2);
x(2)-0.2; x(1)-x(2)];
% Nonlinear equality constraints
ceq = [];
Command Window:
>> x0=[0.04 0.060];
>> options = optimoptions(@fmincon,'Algorithm','sqp');
>> [x,fval] = fmincon(@objfun,x0,[],[],[],[],[],[],@confun,options);
output
Attempted to access x(10); index out of bounds because numel(x)=2.
Error in confun (line 3)
c = [0.0075-x(1)*x(2)^2; 0.000066425-x(1)*x(2)^3; 0.04-x(10); x(1)-0.12; 0.06-x(2);
Error in fmincon (line 651)
[ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});
Caused by: Failure in initial user-supplied nonlinear constraint function evaluation. FMINCON cannot continue.

Answers (1)

Geoff Hayes
Geoff Hayes on 17 Jan 2015
Kaushik - your confun function is defined as
function [c, ceq] = confun(x)
% Nonlinear inequality constraints
c = [0.0075-x(1)*x(2)^2; 0.000066425-x(1)*x(2)^3; 0.04-x(10); x(1)-0.12; 0.06-x(2); ...
x(2)-0.2; x(1)-x(2)];
% Nonlinear equality constraints
ceq = [];
Note how in all cases except one, you reference x(1) or x(2). Do you really mean to use x(10)? The error message is telling you that your input vector x has only two elements, yet third element of c is initialized using x(10). Is this intentional or is it a typo and should be x(1) instead?
  2 Comments
Geoff Hayes
Geoff Hayes on 18 Jan 2015
There must be a different error message if you changed the x(10) to x(1). Please copy and paste the full error message to this thread.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!