How to use fmincon with vectors? (error message: not enough input arguments)

Hello,
I am trying to use fmincon to find values that maximizes the function. (2 variables)
The document on the website provides sample that provides scalar and I want to return vectors instead of it.
Below is the sample code I wrote to understand how fmincon works.
test = [1 2 3 4];
fun = @(c,d)100*(c-d^2)^2 + (test(:)-c).^2;
fun(2,3)
lb = [0,0.2,0,0.2 ; 0,0.2,0,0.2];
ub = [0.5,0.8,0.5,0.8 ; 0.5,0.8,0.5,0.8];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [1/4,1/4,1/4,1/4 ; 1/4,1/4,1/4,1/4 ];
[x,val] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
So basically, I want to have (c,d) that maximizes fun for each value in test.
But when I run this code, it says 'Not enough input arguments'.
Seems like I made some mistake but couldn't find it.
Any help? Thanks in advance.

 Respuesta aceptada

Matt J
Matt J el 6 de Abr. de 2021
Editada: Matt J el 6 de Abr. de 2021
fun = @(x) -100*(x(1,:)-x(2,:).^2).^2 + (test-x(1,:)).^2;

8 comentarios

Chang seok Ma
Chang seok Ma el 6 de Abr. de 2021
Editada: Chang seok Ma el 6 de Abr. de 2021
Thanks for the answer but then I got an error message as "Supplied objective function must return a scalar value"
I guess you solved it (since you Accept-clicked the answer)?
Sorry, I thought it will solve it but I realized that it doesn't solve the problem.
test = [1 2 3 4];
fun = @(x) -100*(x(1,:)-x(2,:).^2).^2 + (test-x(1,:)).^2;
lb = [0,0.2,0,0.2 ; 0,0.2,0,0.2];
ub = [0.5,0.8,0.5,0.8 ; 0.5,0.8,0.5,0.8];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [1/4,1/4,1/4,1/4 ; 1/4,1/4,1/4,1/4 ];
[x,val] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
This is the code I have right now which gives me an error message "Supplied objective function must return a scalar value."
fun = @(x) -100*sum( (x(1,:)-x(2,:).^2).^2 + (test-x(1,:)).^2 ,'all');
Thanks now I can see some results and I think it is right but I just want to make sure I understand this correctly.
Here is how I thought.
Since the length of test is 4, (x(1,:)-x(2,:).^2).^2 + (test-x(1,:)).^2 will give me some (1,4) vector(A)
and I thought I need to choose x(1) and x(2) that will maximize each value in (1,4) vector. (x(1,1) and x(2,1) will maximize A(1)...)
By putting sum, now the problem becomes choosing x(1,1) x(2,1) x(1,2) x(2,2) x(3,1) x(3,2) x(4,1) x(4,2) that maximizes A(1) + A(2) + A(3) + A(4).
But this is also identical to choose x(1) and x(2) that will maximize each value in (1,4)
Am I understanding correctly?
Matt J
Matt J el 6 de Abr. de 2021
Editada: Matt J el 6 de Abr. de 2021
Yes. Although because the problem is additively separable, it might be better just to use a loop and solve 4 separate 2-dimensional problems. The finite differencing operations will be less wasteful that way.
So if the problem is not additively separable, is there other way I could solve this problem without loop?
Matt J
Matt J el 6 de Abr. de 2021
Editada: Matt J el 6 de Abr. de 2021
If the problem weren't separable, using a loop would not be an option. You would be forced to solve it as an 8D minimization problem.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Optimization Toolbox en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 6 de Abr. de 2021

Editada:

el 6 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by