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.

 채택된 답변

Matt J
Matt J 2021년 4월 6일
편집: Matt J 2021년 4월 6일
fun = @(x) -100*(x(1,:)-x(2,:).^2).^2 + (test-x(1,:)).^2;

댓글 수: 8

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 2021년 4월 6일
편집: Matt J 2021년 4월 6일
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 2021년 4월 6일
편집: Matt J 2021년 4월 6일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Optimization Toolbox에 대해 자세히 알아보기

태그

질문:

2021년 4월 6일

편집:

2021년 4월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by