Azzera filtri
Azzera filtri

Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

non-linear optimization problem - index out of bounds error

2 visualizzazioni (ultimi 30 giorni)
Nathan
Nathan il 27 Gen 2016
Chiuso: MATLAB Answer Bot il 20 Ago 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 Commenti
jgg
jgg il 27 Gen 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 il 27 Gen 2016
Obvi. Should have noticed that! Thanks.

Risposte (0)

Questa domanda è chiusa.

Community Treasure Hunt

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

Start Hunting!

Translated by