# Multiple initial points in fmincon optimization

16 views (last 30 days)
xin lin on 10 Apr 2023
Commented: xin lin on 10 Apr 2023
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)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
x = 2×3
1.5310 -0.6000 -1.1386 2.3448 2.2000 0.6139
fval = 0.2821
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?

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
xin lin on 10 Apr 2023
Hi Alan,
Best regards,
Xin

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.
xin lin on 10 Apr 2023
Hi Jon,
Thanks for the detailed explanation. I previously misunderstood the definition of X0. Now, it should be fine.
Best regards,
Xin

### Categories

Find more on Solver Outputs and Iterative Display in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!