When I use fmincon, the optimised result does not satisfy my non liner constraints
Show older comments
clear variables
close all
clc
fun = @(x)4*x(1)+x(2);
x0=[0.4,0.28]
lb = [0.01,0.01];
ub = [5,0.8];
A = [];
b = [];
Aeq = [];
beq = [];
options = optimoptions('fmincon','Algorithm','sqp');
c = @(x)(4+4*x(1)+9*x(1)^2)*(1+x(1))^2-(2*x(1)^3*x(2)+2+3*x(1)+3*x(1)^2)^2+0.001;
nonlcon = @(x)deal(c(x),[]);
[x,fval,exitflag,output] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
exitflag
checkinitialpoint=(4+4*x0(1)+9*x0(1)^2)*(1+x0(1))^2-(2*x0(1)^3*x0(2)+2+3*x0(1)+3*x0(1)^2)^2+0.001;
checkconstraits=(4+4*x(1)+9*x(1)^2)*(1+x(1))^2-(2*x(1)^3*x(2)+2+3*x(1)+3*x(1)^2)^2+0.001;
The above is my matlab code, I input the nonlear constraints, but the results give to me is obviously not satisfy the constraints (you can see that checkconstraits is positive), my initial point is within the range.
Can anyone help me? Many thanks.
1 Comment
Torsten
on 29 Aug 2023
The solver converged to an infeasible point (see above). Your observation is the same as the exitflag from "fmincon" indicates.
Answers (2)
You would do better to use the default 'interior-point' algorithm, which arrives at a feasible solution.
fun = @(x)4*x(1)+x(2);
x0=[0.4,0.28];
lb = [0.01,0.01];
ub = [5,0.8];
A = [];
b = [];
Aeq = [];
beq = [];
% options = optimoptions('fmincon','Algorithm','sqp');
c = @(x)(4+4*x(1)+9*x(1)^2)*(1+x(1))^2-(2*x(1)^3*x(2)+2+3*x(1)+3*x(1)^2)^2+0.001;
nonlcon = @(x)deal(c(x),[]);
[x,fval,exitflag,output] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
Alan Weiss
MATLAB mathematical toolbox documentation
1 Comment
Tianshu Gao
on 30 Aug 2023
Since it is a 2D problem, it practically begs you to pre-sweep for a good initial guess:
fun = @(x)4*x(1)+x(2);
lb = [0.01,0.01];
ub = [5,0.8];
A = [];
b = [];
Aeq = [];
beq = [];
c = @(x)(4+4*x(1)+9*x(1)^2)*(1+x(1))^2-(2*x(1)^3*x(2)+2+3*x(1)+3*x(1)^2)^2+0.001;
nonlcon = @(x)deal(c(x),[]);
[x0,fval0]=sweep(fun,c,lb,ub)
options = optimoptions('fmincon','Algorithm','sqp');
[x,fval,exitflag,output] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
x,fval
function [x0,fval]=sweep(fun,c,lb,ub)
F=@(x1,x2) fun([x1,x2])+eps./(c([x1,x2])<=0);
[X1,X2]=ndgrid(linspace(lb(1),ub(1),30), linspace(lb(2),ub(2),30));
v=arrayfun(F,X1,X2);
[fval,i]=min(v(:));
if ~isfinite(fval)
disp 'No feasible point found'; x0=[];
else
x0=[X1(i),X2(i)];
end
end
2 Comments
Tianshu Gao
on 30 Aug 2023
Matt J
on 30 Aug 2023
You're welcome, but please Accept-click whichever answer solved your issue.
Categories
Find more on Solver Outputs and Iterative Display 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!