set(0,'DefaultAxesFontName','times');
set(0,'DefaultTextFontName','times');
set(0,'DefaultAxesFontSize',18);
set(0,'DefaultAxesFontWeight','normal');
set(0,'DefaultTextFontSize',18);
set(0,'DefaultTextFontWeight','normal');

close all;
clear all

load res3
load pl
t = 5*(0:length(u)-1)';
clear ue ye
N3 = 350;
load trsart4; ue = DATA(100:300,5); ye = DATA(100:300,2);
load tssart3; ue = [ue; DATA(10:281,5)]; ye = [ye; DATA(10:281,2)];
[ye ue ye1] = regres1([ye ue],NA,NB,ND);
inputse = [ye ue];

tic;
figure(1); set(1,'visible','off');
pos = get(0,'screensize');
set(gcf,'resize','on','menubar','none','position',pos,'Name','Input-Output Data','NumberTitle','off');
set(gcf,'Name','Antecedent space','NumberTitle','off');

subplot(221);
plot(u,y,'.','markersize',8); 
set(gca,'ylim',[1 2.2]);
xlabel('Valve [% closed]'); 
ylabel('Pressure [1e5 Pa]'); 
title('Training and validation data')
hold on;

subplot(222);
meshz(ug,yg,dofm); hold on; view(322,55); axis([0 100 1 2.15 0 1.1])
xlabel('Valve [% closed]'); ylabel('Pressure [1e5 Pa]'); 
zlabel('Membership grade'); 
title('Membership functions')

figure(1); set(1,'visible','on'); 
%disp('Hit any key to continue ...'); pause; hold on;

ye = ye(1:340);
ue = ue(1:340);

H1 = plot(ue(1),ye(1),'ko','LineWidth',5,'EraseMode','xor');
%H3 = plot3(ue(1),ye(1),max(dofprod(rls,mfs,[ye(1) ue(1)])),'k-','LineWidth',1,'EraseMode','none');
hold off

subplot(212);
H2 = plot(Ts*(1:length(ue)),[ye],'k'); 
title('Pressure prediction');
xlabel('Time [s]'); ylabel('Pressure [1e5 Pa]'); drawnow; hold on; 
set(1,'visible','on'); drawnow;


disp('Press any key to start ...'); pause

F(1) = getframe(gcf);
t = Ts*(0:length(ye)-1)';
yml = ye(1);
yme = ye(1);
for i = 2 : length(ye),
%   pause(.03);
   dofe = dofprod(rls,mfs,[yme(i-1) ue(i-1)]); 
   yme(i) = sugval(p,[yme(i-1) ue(i-1) 1],dofe,yme(i-1));
   yml(i) = sugval(pl2,[yml(i-1) ue(i-1) 1],1,yml(i-1));
   subplot(221);
   plot(ue(i-1),yme(i-1),'r.','MarkerSize',6);
   subplot(212);
   [trash,ind] = max(dofe);
   if ind == 1,
      plot(Ts*[i-1 i],[ye(i-1) ye(i)],'g-','EraseMode','none','linewidth',1.5); 
   elseif ind == 2,
      plot(Ts*[i-1 i],[ye(i-1) ye(i)],'r-','EraseMode','none','linewidth',1.5); 
   else   
      plot(Ts*[i-1 i],[ye(i-1) ye(i)],'b-','EraseMode','none','linewidth',1.5); 
   end;   
   set(H1,'xdata',ue(i-1),'ydata',yme(i-1),'zdata',max(dofe),'color','k');
   drawnow;
   F(i) = getframe(gcf);
%   pause(.2);
end;
time = toc;

subplot(212); hold off
%disp('Press any key to plot linear model ...'); pause
hold on;
plot(t,yml,'k');
hold off;
title('Comparison with a linear model (black)');

save_video('test.mpg',F);
