Info

This question is closed. Reopen it to edit or answer.

non-linear optimization problem - index out of bounds error

1 view (last 30 days)
I am attempting to solve a relatively simple nonlinear optimization problem with both equality and inequality constraints. However, when running the code I receive the error:
Attempted to access x(3); index out of bounds because numel(x)=2.
My code is below. I attempted to follow the MATLAB example codes as best I could. Any ideas? Thanks in advance...
function [c,ceq]=constraints(x)
c=(-1*x(1))+(-1*x(2))+(-1*x(3));
ceq = (x(1)*sin(1.4015)*.04)+(x(2)*sin(1.199)*.035)+(x(3)*sin(0.4014)*.20)-8.25;
function f = opfun(x)
f=(x(1)*0.051)^3+(x(2)*0.054)^3+(x(3)*0.012)^3;
x0=[0,0];
options = optimoptions(@fmincon,'Display','iter','Algorithm','sqp');
[x,fval]=fmincon(@opfun,x0,[],[],[],[],[],[],@constraints,options);
  2 Comments
jgg
jgg on 27 Jan 2016
This looks like your problem:
x0=[0,0];
You're passing two values for x0 to start the optimizer, but it looks like the constraints and f both take 3.
x0 = [0,0,0]; should fix this.

Answers (0)

Community Treasure Hunt

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

Start Hunting!