Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

non-linear optimization problem - index out of bounds error

2 vues (au cours des 30 derniers jours)
Nathan
Nathan le 27 Jan 2016
Clôturé : MATLAB Answer Bot le 20 Août 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 commentaires
jgg
jgg le 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.
Nathan
Nathan le 27 Jan 2016
Obvi. Should have noticed that! Thanks.

Réponses (0)

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by