clear all
close all;
figure('visible','off');
set(gcf,'resize','on','menubar','none','position',[0   32   325   549],'Name','Fermenter','NumberTitle','off');
load opstel
image(x); colormap(map); set(gca,'position',[0 0 1 1]); 
figure(1); set(1,'visible','on'); drawnow;
disp('Hit any key to continue ...'); pause; 

load res3
load pl
t = 5*(0:length(u)-1)';
figure('visible','off');
set(gcf,'resize','on','menubar','none','position',[332   32   466   549],'Name','Input-Output Data','NumberTitle','off');
subplot(211);
plot(t,u,'-'); ylabel('Valve position [% closed]'); xlabel('Time [s]');
subplot(212);
plot(t,y); ylabel('Pressure [1e5 Pa]'); xlabel('Time [s]');
figure(2); set(2,'visible','on'); drawnow;
disp('Hit any key to continue ...'); pause; 

close(1); close(2);

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('visible','off');
set(gcf,'resize','on','menubar','none','position',[332   32   466   549],'Name','Input-Output Data','NumberTitle','off');
set(gcf,'Name','Antecedent space','NumberTitle','off');
subplot(211);
meshz(ug,yg,dofm); hold on; view(322,55); axis([0 100 1 2.15 0 1.1])
xlabel('Valve position [% closed]'); ylabel('Pressure [1e5 Pa]'); 
zlabel('Membership grade'); 
subplot(212);
plot(u,y,'.','markersize',8); 
xlabel('Valve position [% closed]'); 
ylabel('Pressure [1e5 Pa]'); 
figure(1); set(1,'visible','on'); 
disp('Hit any key to continue ...'); pause; hold on;
subplot(211);
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
figure('visible','off')
set(gcf,'resize','on','menubar','none','position',[3 8 796 295]);
set(gcf,'Name','Pressure prediction','NumberTitle','off');
H2 = plot(Ts*(0:length(ue)-1),[ye],'k'); 
xlabel('Time [s]'); ylabel('Pressure [1e5 Pa]'); drawnow; hold on; 

figure(2); set(2,'visible','on'); drawnow;


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

yme = ye1;
yml = ye1;
t = Ts*(0:length(ye1)-1)';
for i = 2 : length(ue),
   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));
   figure(2);
   [trash,ind] = max(dofe);
   if ind == 1,
      plot(Ts*[i-1 i],[ye(i-1) ye(i)],'r-','EraseMode','none'); 
   elseif ind == 2,
      plot(Ts*[i-1 i],[ye(i-1) ye(i)],'b-','EraseMode','none'); 
   else   
      plot(Ts*[i-1 i],[ye(i-1) ye(i)],'g-','EraseMode','none'); 
   end;   
   set(H1,'xdata',ue(i-1),'ydata',yme(i-1),'zdata',max(dofe),'color','k');
   drawnow; for ii = 1 : 50000; end;
end;
time = toc;

figure(2); hold off
disp('Press any key to plot linear model ...'); pause
hold on;
plot(t,yml,'k');
hold off;