function RobPreReachAdditive_AutandNot
% Robust forward and backward reachable sets
%
% T. Besselmann, ETH Zurich, 18.08.2008
% F. Borrelli, UC Berkeley, 05.07.2009
% M. Herceg, Automatic Control Laboratory, ETH Zurich 2014
%
% requires MPT3

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_name1 = ['.',filesep,'figures',filesep,'PreSet_ex_14_1'];
figure_name2 = ['.',filesep,'figures',filesep,'reachSet1_ex_14_1'];
figure_name3 = ['.',filesep,'figures',filesep,'PreSet_ex_14_2'];
figure_name4 = ['.',filesep,'figures',filesep,'reachSet1_ex_14_2'];
width = 6;
set(0,'defaulttextinterpreter','none');


%% core code
% Stable Autonomous system with additive noise
% x(k+1) = A*x(k) + w(k)
A=[0.5 0;1 -0.5];

% create model in MPT3 interface
model1= LTISystem('A',A);

% constraints on states
model1.x.min = [-10;-10];
model1.x.max = [ 10; 10];

% state constraints
X = Polyhedron('lb',model1.x.min,'ub',model1.x.max);
% noise constraints
W = Polyhedron('lb',[-1;-1],'ub',[1;1]);

% backward reachable set
B1 = model1.reachableSet('X', X-W, 'direction', 'backward');
P1 = B1.intersect(X);

% forward reachable set
F1 = model1.reachableSet('X', X, 'direction', 'forward') + W;


% Unstable system with additive noise
% x(k+1) = A*x(k) + B*u(k) + w(k)
A=[1.5 0;1 -1.5];
B=[1;0];

model2 = LTISystem('A',A,'B',B); 
model2.u.min = -5;
model2.u.max = 5;
model2.x.min = [-10;-10];
model2.x.max = [ 10; 10];

% input constraints
U = Polyhedron('lb',model2.u.min,'ub',model2.u.max);

% backward reachable set
B2 = model2.reachableSet('X', X-W, 'U', U, 'direction', 'backward');
P2 = B2.intersect(X);

% forward reachable set
F2 = model2.reachableSet('X', X, 'U', U, 'direction', 'forward') + W;


%% plot and save book figures
% plot backward reachable set P1
fig1 = figure;
plot(X,'color',[0.7 0.7 0.7],'linewidth',line_width);
hold on
plot(P1,'color',[0.5 0.5 0.5],'linewidth',line_width) 
border = 1;
axis([model1.x.min(1)-border,model1.x.max(1)+border,model1.x.min(2)-border,model1.x.max(2)+border]);


ht1=text(7,-8,'$\mathcal{X}$');
set(ht1, 'FontSize', label_font_size);
ht2=text(-8,-0.5,'Pre$(\mathcal{X},\mathcal{W})\cap\mathcal{X}$');
set(ht2, 'FontSize', label_font_size);
xt = transpose(-10:5:10);
yt = transpose(-10:5:10);
set(gca,'XTick',xt);
set(gca,'YTick',yt);
set(gca,'YTickLabel',num2str(xt));
set(gca,'XTickLabel',num2str(yt));


set(gca,'LineWidth',axeswidth)
set(gca,'FontSize', tick_font_size);

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

% plot forward reachable set F1
fig2 = figure;
plot(X,'color',[0.7 0.7 0.7],'linewidth',line_width);
hold on
plot(F1,'color',[0.5 0.5 0.5],'linewidth',line_width) 
border = 7;
axis([model1.x.min(1)-border,model1.x.max(1)+border,model1.x.min(2)-border,model1.x.max(2)+border]);

ht1=text(7,-8,'$\mathcal{X}$');
set(ht1, 'FontSize', label_font_size);
ht2=text(-4.5,-9,'Suc($\mathcal{X},\mathcal{W}$)');
set(ht2, 'FontSize', label_font_size,'Rotation',57);
xt = transpose(-10:10:10);
yt = transpose(-10:10:10);
set(gca,'XTick',xt);
set(gca,'YTick',yt);
set(gca,'YTickLabel',num2str(xt));
set(gca,'XTickLabel',num2str(yt));


set(gca,'LineWidth',axeswidth)
set(gca,'FontSize', tick_font_size);

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

% plot backward reachable set P2
fig3 = figure;
plot(X,'color',[0.7 0.7 0.7],'linewidth',line_width);
hold on
plot(P2,'color',[0.5 0.5 0.5],'linewidth',line_width) 
border = 1;
axis([model1.x.min(1)-border,model1.x.max(1)+border,model1.x.min(2)-border,model1.x.max(2)+border]);

ht1=text(7,-8,'$\mathcal{X}$');
set(ht1, 'FontSize', label_font_size);
ht2=text(-8,-0.5,'Pre$(\mathcal{X},\mathcal{W})\cap\mathcal{X}$');
set(ht2, 'FontSize', label_font_size);
xt = transpose(-10:5:10);
yt = transpose(-10:5:10);
set(gca,'XTick',xt);
set(gca,'YTick',yt);
set(gca,'YTickLabel',num2str(xt));
set(gca,'XTickLabel',num2str(yt));


set(gca,'LineWidth',axeswidth)
set(gca,'FontSize', tick_font_size);

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

% plot forward reachable set F2
fig4 = figure;
hold on
plot(F2,'color',[0.5 0.5 0.5],'linewidth',line_width) 
axis([-30 30 -30 30]);

ht2=text(-17,-0.5,'Suc($\mathcal{X},\mathcal{W}$)');
set(ht2, 'FontSize', label_font_size);
xt = transpose(-30:10:30);
yt = transpose(-30:10:30);
set(gca,'XTick',xt);
set(gca,'YTick',yt);
set(gca,'YTickLabel',num2str(xt));
set(gca,'XTickLabel',num2str(yt));


set(gca,'LineWidth',axeswidth)
set(gca,'FontSize', tick_font_size);

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


% generate FIG file
disp('Hit any key to print the figures.');
pause
% save
saveas(fig1, [figure_name1,'_matlab'], 'fig');

% print
laprint(fig1, [figure_name1,'_tex'],'scalefonts','off','width',width);


% save
saveas(fig2, [figure_name2,'_matlab'], 'fig');

% print
laprint(fig2, [figure_name2,'_tex'],'scalefonts','off','width',width);


% save
saveas(fig3, [figure_name3,'_matlab'], 'fig');

% print
laprint(fig3, [figure_name3,'_tex'],'scalefonts','off','width',width);


% save
saveas(fig4, [figure_name4,'_matlab'], 'fig');

% print
laprint(fig4, [figure_name4,'_tex'],'scalefonts','off','width',width);


end
