Why do I get the following error using fmincon?
2 views (last 30 days)
Show older comments
I am trying to run the following fmincon problem, where evaluating fun requires to solve a linear programming problem.
Before showing you my code, let me write the optimisation problem in math symbols.
where x is a vector, is a vector of zeros, means that every element of x should be non-negative, are vectors of real numbers.
clear
rng default
XZW_temp=[0.5450 0.8175 -0.5451 0.2724]; %this is A1 above
X1_temp=[0 0.0852 0 -0.0852]; %this is A2 above
X2_temp=[2.0132 1.0066 -2.0132 -1.0066]; %this is A3 above
options = optimset('linprog');
options.Display = 'off';
fun=@(x)inner_max(x,XZW_temp, X1_temp, X2_temp, options);
ub=Inf*ones(4,1);
lb=zeros(4,1);
x0=ones(4,1);
[~, f]=fmincon(fun,x0,[],[],[],[],lb,ub);
The function inner_max is
function i_m=inner_max(x,XZW_temp, X1_temp, X2_temp, options)
f=-[x.'*XZW_temp.'; x.'*X1_temp.'; x.'*X2_temp.'];
Aeq=[1 0 0];
beq=1;
lb=-Inf*ones(3,1);
ub=Inf*ones(3,1);
[~,fval] = linprog(f,[],[],Aeq,beq,lb,ub,options);
i_m=-fval;
end
I get the following error
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in finitedifferences
Error in computeFinDiffGradAndJac
Error in barrier
Error in fmincon (line 798)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...
Could you help me to understand what is wrong?
5 Comments
Accepted Answer
Matt J
on 27 Feb 2020
Edited: Matt J
on 27 Feb 2020
Because the are unconstrained, the only way the inner max operation can have a finite result is if [A1;A2]*x=[0;0] and therefore the problem is equivalent to the linear program,
You can plug this into linprog
f=[0.5450 0.8175 -0.5451 0.2724]; %this is A1 above
A2=[0 0.0852 0 -0.0852]; %this is A2 above
A3=[2.0132 1.0066 -2.0132 -1.0066]; %this is A3 above
Aeq=[A2;A3]; beq=[0;0];
lb=zeros(4,1);
x=linprog(f,[],[],Aeq,beq,lb)
However, it finds that the problem is unbounded,
Problem is unbounded.
x =
[]
0 Comments
See Also
Categories
Find more on Nonlinear Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!