I'm a new snopt-user, who just got his trial licence and would like to compare the results of fmincon against the solutions provided by snopt. Here's my first test problem, where the results of snopt don't look too promising. Does anyone know how to get a better performance?
%% Code
clear all; close all; clc; rng(0);
usefeasible = true;
if usefeasible
x0 = [0.5; 0.5]; % feasible inital guess
else
x0 = [2; 2]; % infeasible inital guess
end
% - fmincon
options = optimoptions('fmincon', 'Display', 'none', 'Algorithm', 'sqp', 'SpecifyObjectiveGradient', true, 'SpecifyConstraintGradient', true);
tic;
x1 = fmincon(@(x) obj(x), x0,[],[],[],[],[],[], @(x) con(x), options);
toc;
% - snopt
tic;
x2 = snsolve(@(x) obj(x), x0,[],[],[],[],[],[], @(x) con(x));
toc;
% - compare solutions
[x1 x2]
% - visualization
figure(1);
fcontour(@(x1,x2) 3*(x1-2).^2+2*(x2-3).^2); hold on; grid on;
fimplicit(@(x1,x2) x1.^2-x2,'k');
fimplicit(@(x1,x2) x1.^2+x2.^2-1,'k');
plot(x0(1),x0(2),'m+');
plot(x1(1),x1(2),'g*');
plot(x2(1),x2(2),'c*');
xlabel('x_1'); ylabel('x_2');
% - subfunctions
function [J, dJdx] = obj(x)
J = 3*(x(1)-2).^2+2*(x(2)-3).^2;
if nargout > 1
dJdx = [6*x(1)-12; 4*x(2)-12];
end
end
function [c, ceq, dcdx, dceqdx] = con(x)
c = [x(1)^2-x(2); x(1)^2+x(2)^2-1];
ceq = [];
if nargout > 2
dcdx = [2*x(1) -1; 2*x(1) 2*x(2)]';
dceqdx = [];
end
end