Borrar filtros
Borrar filtros

Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

non-linear optimization problem - index out of bounds error

2 visualizaciones (últimos 30 días)
Nathan
Nathan el 27 de En. de 2016
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
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 comentarios
jgg
jgg el 27 de En. de 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.
Nathan
Nathan el 27 de En. de 2016
Obvi. Should have noticed that! Thanks.

Respuestas (0)

La pregunta está cerrada.

Community Treasure Hunt

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

Start Hunting!

Translated by