フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

non-linear optimization problem - index out of bounds error

2 ビュー (過去 30 日間)
Nathan
Nathan 2016 年 1 月 27 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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 件のコメント
jgg
jgg 2016 年 1 月 27 日
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 2016 年 1 月 27 日
Obvi. Should have noticed that! Thanks.

回答 (0 件)

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by