Interior-point and sequential quadratic programming give me the same answer. Is there an error with my code?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I am attempting to minimise a least squares regression model and compare answers given by SQP and IP, and the system is constrained by x(1) + x(2) + x(3) = 1, and each variable cannot be smaller than 0 or bigger than 1. However, I consistently get the same numerical answers for both methods, which should not be the case. I am wondering if I missed a step or something along those lines? My code is as follows;
fun=@(x) (x(1)-147/505).^2+(x(1)-167/530).^2+(x(2)-504/2525).^2+(x(2)-471/2120).^2+(x(3)-1011/2120).^2+(x(3)-1256/2525).^2;
x0=[0.3, 0.5, 0.2];
Aeq=[1, 1, 1];
beq=1;
lb=[0, 0, 0];
ub=[1, 1, 1];
IP=fmincon(fun,x0,[],[],Aeq,beq,lb,ub);
fprintf('%.13f\n',IP)
options = optimoptions('fmincon','Algorithm','sqp');
SQP=fmincon(fun,x0,[],[],Aeq,beq,lb,ub);
fprintf('%.13f\n',SQP)
These are my results;
IP:
0.3027134331491
0.2105086010671
0.4867779657839
SQP:
0.3027134331491
0.2105086010671
0.4867779657839
Any help or pointers would be appreciated. Thank you in advance.
4 Commenti
Torsten
il 13 Nov 2023
Maybe one needs more iterations with one of the methods (use 'Display','iter' in the options structure). But if you prescribe the accuracy of the solution to be equal for both methods (as you implicitly do in your code because you didn't change them in the optimoptions structure), in theory both solutions should be equally precise, shouldn't they ?
Risposta accettata
Torsten
il 13 Nov 2023
Modificato: Torsten
il 13 Nov 2023
I just saw in your code that you don't pass the options structure to fmincon. Thus in both calls, the same solver is used.
Use
fun=@(x) (x(1)-147/505).^2+(x(1)-167/530).^2+(x(2)-504/2525).^2+(x(2)-471/2120).^2+(x(3)-1011/2120).^2+(x(3)-1256/2525).^2;
x0=[0.3, 0.5, 0.2];
Aeq=[1, 1, 1];
beq=1;
lb=[0, 0, 0];
ub=[1, 1, 1];
options = optimoptions('fmincon','Algorithm','interior-point','Display','iter');
IP=fmincon(fun,x0,[],[],Aeq,beq,lb,ub,[],options);
fprintf('%.13f\n',IP)
options = optimoptions('fmincon','Algorithm','sqp','Display','iter');
SQP=fmincon(fun,x0,[],[],Aeq,beq,lb,ub,[],options);
fprintf('%.13f\n',SQP)
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!