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

2 次查看(过去 30 天)
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

回答(1 个)

bharath pro
bharath pro 2020-7-2
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,[],[],[],[],[]);

类别

Help CenterFile Exchange 中查找有关 Nonlinear Optimization 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by