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

1 次查看(过去 30 天)
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.

回答(1 个)

Geoff Hayes
Geoff Hayes 2015-1-17
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 个评论
Geoff Hayes
Geoff Hayes 2015-1-18
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by