Multiple initial points in fmincon optimization
18 views (last 30 days)
Show older comments
Hi
Thank you for reading this question!
I am studying "Fmincon" in Matlab. Based on "Fmincon" document, I wrote a code, as shown below.
close all;
clear;
clc;
fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2; % objective function
x0 = [-1, 2; -1, 2; -1, 2]'; % initial points
Aeq = [0.5, 0.1, 0, 0, 0, 0; 0, 0, 2, 1, 0, 0; 0, 0, 0, 0, 0.2, 2]; % constraints
beq = [1; 1; 1]; % constraints
[x,fval] = fmincon(fun,x0,[],[],Aeq,beq)
In this code, x0 contains six elements. Then, Aeq needs six columns of elements. Based on "Fmincon" document, Equality constraint equation can be written as below:
0.5*x(1)+0.1*x(2)+x(3)+x(4)+x(5)+x(6)=1 (1)
x(1)+x(2)+2*x(3)+x(4)+x(5)+x(6)=1 (2)
x(1)+x(2)+x(3)+x(4)+0.2*x(5)+2*x(6)=1 (3)
In fact, objective function only includes two variables that are x(1) and x(2). Therefore, how does this code ("Fmincon") work? I have not found the similar question in the community. Could anyone explain it or share the relevant link?
Many thanks in advance!
0 Comments
Accepted Answer
Alan Weiss
on 10 Apr 2023
fmincon takes the size of the x0 input as determining the number of input variables. Your x0 has 6 elements, so fmincon thinks that you have a 6-variable problem.
You are attempting to vectorize fmincon. However, fmincon cannot be vectorized, meaning it does not accept multiple input points.
Alan Weiss
MATLAB mathematical toolbox documentation
More Answers (1)
Jon
on 10 Apr 2023
As @Alan Weiss points out it seems that you are misunderstanding how MATLAB interprets the 2 by 3 array you are inputting as x0. You are thinking that you are optimizing to find the optimum value of a two element vector, with elements x(1) and x(2), and you think that each column of x0 is a different initial guess.
Infact MATLAB only allows one initial guess. So it interprets your 6 element array of x0's as telling it that you are optimizing to find 6 values (that happen to be in a 2 by 3 array). It then uses linear indexing (numbering elements columwise) to find the initial values of x, so x(1) = x0(1,1), x(2) = x0(2,1), x(3) = x0(1,2), x(4) = x(2,2) etc. It doesn't mind that your objective function only uses the first two of these six elements, but uses all 6 when evaluating the constraints.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!