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

1 view (last 30 days)
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

Answers (1)

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

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!