function dd_example
% example of one iteration of the double description method
%
% C.N. Jones, EPFL, 2013
% Revised by M. Herceg, ETH Zurich 2014
%
% requires MPT3, YALMIP

close all

%% printing parameters
label_font_size = 14;
tick_font_size = 10;
line_width = 0.8;
axeswidth=0.2;
% create new folder for storing figures
mkdir('figures');
figure_name = ['.',filesep,'figures',filesep,'dd_example'];

%% core code
% yalmip variables
x = sdpvar(2,1);
T = [1.9818    0.4795; 0.4795    0.6242];
% define ellipsoidal set S
S = YSet(x, x'*T*x <= 1);
% % yalmip variables
% q = sdpvar(2,1);
% % parametrize conic problem in q
% proj = optimizer(x'*T*x <= 1, (x-q)'*(x-q), [], q, x);


% plot outer box of the set S
fig1 = figure;
O = S.outerApprox;
h=O.plot('linewidth',line_width,'wire',true);
set(h,'FaceColor','none');
hold on

% plot the set S in gray
S.plot('color',0.9*[1 1 1],'grid',100,'linewidth',line_width);

% take the vertices of the outer box
V = O.V;
% project each vertex V on the set S
Z = V;
dist = zeros(size(V,1),1);
for i=1:size(V,1)
    res = S.project(V(i,:)');
    Z(i,:) = res.x';
    dist(i) = res.dist;
end

% find the maximum distance
[~,ind_max] = max(dist);
% if there are more maxima, take the first one
i = ind_max(1);

% plot one the vertex with the maximum distance
plot(V(i,1),V(i,2),'s','markersize',8,'markerfacecolor','k','markeredgecolor','k');

% hyperplane that passes the projected point Z
ae = Z(i,:)-V(i,:);
be = ae*Z(i,:)';
t = Polyhedron('Ae', ae, 'be', be);

% plot the point Z
plot(Z(i,1),Z(i,2),'o','markersize',8,'markerfacecolor','k','markeredgecolor','k');

% plot the hyperplane
h2 = t.plot('linewidth',line_width,'color','k');
axis([-2 2 -2 2])

%% save book figures
set(gca,'LineWidth',axeswidth)
set(gca,'FontSize', tick_font_size);
xt = [];
yt = [];
set(gca,'YTickLabel',num2str(xt));
set(gca,'XTickLabel',num2str(yt));

hx1 = xlabel('$x_1$');
set(hx1, 'FontSize', label_font_size);
hy1 = ylabel('$x_2$');
set(hy1, 'FontSize', label_font_size);
axis off
grid off

% generate FIG file
saveas(fig1, [figure_name,'_matlab'], 'fig');

% print 
set(fig1,'renderer','OpenGL');
laprint(fig1, [figure_name,'_tex'],'scalefonts','off');

end