%MPT example (needs MPT Toolbox)
model.A = [1 1; 0 1];
model.B = [1; 0.5];
model.C = [0 1];
model.D = [0];

model.umin = -1;
model.umax = 1;
model.xmin = [-1; -1];
model.xmax = [1; 1];

cost.N = 5;
cost.Q = [1 0; 0 1];
cost.R = 1;
cost.P_N = 0;
cost.norm = 2;

controller = mpt_control(model, cost);

% Labels
u1=sprintf('u = %.2f x_1 %+.2f x_2', controller.Fi{1}(1,1), ...
	   controller.Fi{1}(1,2)); 
u2 = 'u = -1';
u3 = 'u = 1';

plot(controller) %color figure
text(0,0,u1, 'HorizontalAlignment', 'center')
text(.8,.8,u2, 'HorizontalAlignment', 'center')
text(-.8,-.8,u3, 'HorizontalAlignment', 'center')
pause

figure
% Manual BW plot of the previous figure
% Region #1
[H1,K1] = double(controller.Pn(1));
%Construct line
x11 = [(K1(1)-H1(1,2)*(-1))/H1(1,1), -1];
x12 = [-1, (K1(1)-H1(1,1)*(-1))/H1(1,2)];
x21 = [(K1(2)-H1(2,2)*(1))/H1(2,1), 1];
x22 = [1, (K1(2)-H1(2,1)*(1))/H1(2,2)];
plot(x11, x12,'k', x21, x22,'k')
axis([-1,1,-1,1]);
xlabel('x_1'); ylabel('x_2')
text(0,0,u1, 'HorizontalAlignment', 'center')
text(.8,.8,u2, 'HorizontalAlignment', 'center')
text(-.8,-.8,u3, 'HorizontalAlignment', 'center')
set(gcf, 'PaperUnits', 'centimeters')
set(gcf, 'PaperPosition', [3 10 11 8.25])
print -deps 'omptex.eps'

pause

figure
% 3D plot of the same as before
plotu(controller)
view(-136,24)
colormap('gray')
set(gcf, 'PaperUnits', 'centimeters')
set(gcf, 'PaperPosition', [3 10 11 8.25])
%print -deps 'omptex2.eps'

