Why do I get Index exceeds the number of array elements (1).

2 Ansichten (letzte 30 Tage)
Maxim Baurov
Maxim Baurov am 1 Jul. 2020
Beantwortet: bharath pro am 2 Jul. 2020
Hello, this is my function
fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
x0 = 0.5;
A = [1;2];
b = [1;2];
[x,fval] = fmincon(fun,x0,A,b,[],[],[],[],[]);
when I try to run it I get an error, but i dont understand why

Antworten (1)

bharath pro
bharath pro am 2 Jul. 2020
The error is caused because x0 is scaler but the function fun needs a vector ( x(1) and x(2) ). The following code works fine after I changed the dimensions of X0, A and B.
fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
x0 = [0.5,0.6];
A = [1,2];
b = 1;
[x,fval] = fmincon(fun,x0,A,b,[],[],[],[],[]);

Kategorien

Mehr zu Nonlinear Optimization finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by