function []=plotpredakk(dUK,YS,YPD,yk,yr,ymin,ymax,uk,ur,umin,umax,ntime,nk,delta_t)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% []=plotpredakk(dUK,YS,YP,yk,yr,uk,ur,ntime,nk)
% 
% Plot accumulated predictions
% Output plot of: - open-loop prediction                 (blue  -)
%                 - closed-loop prediction               (black -)	
%                 - setpoint                             (red  --)	
%                 - upper and lower bounds               (blue  :)
%                 - current output / measurement         (black *)
%
% Input plot of:  - inputs when open-loop prediction     (blue  -)
%                 - inputs when closed-loop prediction   (black -)
%                 - ideal value                          (red  --)
%                 - upper and lower bounds               (blue  :)
%                 - current input                        (black *)
%
% INPUTS:
%  dUK : Future N input changes  [N*dimu x 1] [dUK1 dUK2 .. dUKm]       
%  YS  : Open-loop prediction    [P*dimy x 1] [y1 .. y1 .... yn .. yn]
%  YP  : Closed-loop prediction  [P*dimy x 1] [y1 .. y1 .... yn .. yn]
%  yk  : Output / measurement    [dimy x 1] 
%  yr  : Setpoint                [dimy x 1]    
%  ymin: Lower bound             [dimy x 1]
%  ymax: Upper bound             [dimy x 1]
%  uk  : Input (current)         [dimu x 1] 
%  ur  : Ideal value             [dimu x 1]    
%  umin: Lower bound             [dimu x 1]
%  umax: Upper bound             [dimu x 1]
%  ntime:Total simulation time (total number of simulation steps)
%  nk  : Simulation time (number of simulation step)
%
% OUTPUTS:
%
% Written by : msg20001203
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

dimu=size(uk,1);
dimy=size(yk,1);
N=size(dUK,1)/dimu;
P=size(YS,1)/dimy;

dimU=N+ntime;
dimY=P+ntime;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Inputs
TK=(1:dimU)';
%figure(2)
for i=1:dimu
   figure(30+i)
   UPK=NaN*zeros(dimU,1);
   USK=NaN*zeros(dimU,1);
   UR=NaN*zeros(dimU,1);
   UPK(nk:nk+N-1)=cumsum(dUK(N*(i-1)+1:N*i))+uk(i);
   USK(nk:nk+N-1)=uk(i);   
   UR(nk:nk+N-1)=ur(i);
   subplot(2,1,2)
   %subplot(dimu,1,i)
   hold on
   if isempty(umax)
      plot(TK,USK,'b-',TK,UPK,'k-',TK,UR,'r--',nk-1,uk(i),'k*');
   else
      UMIN=NaN*zeros(dimU,1);
      UMIN(nk:nk+N-1)=umin(i*ones(N,1));
      UMAX=NaN*zeros(dimU,1);
      UMAX(nk:nk+N-1)=umax(i*ones(N,1));
      plot(TK,USK,'b-',TK,UPK,'k-',TK,UR,'r--',nk-1,uk(i),'k*',TK,UMIN,'b:',TK,UMAX,'b:');
   end  
   ylabel(sprintf('u_%d',i));
   xlabel('Time in hours');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Outputs
%figure(1)
TK=(1:dimY)';
for i=1:dimy
   figure(30+i)
   YPK=NaN*zeros(dimY,1);
   YSK=YPK;
   YRK=YPK;
   YPK(nk:nk+P-1)=YPD(P*(i-1)+1:P*i);
   YSK(nk:nk+P-1)=YS(P*(i-1)+1:P*i);
   YRK(nk:nk+P-1)=yr(i);
   subplot(2,1,1)
   %subplot(dimy,1,i)
   hold on
   if isempty(ymax)
      plot(TK,YPK,'k-',TK,YSK,'b-',TK,YRK,'r--',nk-1,yk(i),'k*');
   else
      YMIN=NaN*zeros(dimY,1);
      YMIN(nk:nk+P-1)=ymin(i*ones(P,1));
      YMAX=NaN*zeros(dimY,1);
      YMAX(nk:nk+P-1)=ymax(i*ones(P,1));
      plot(TK,YPK,'k-',TK,YSK,'b-',TK,YRK,'r--',nk-1,yk(i),'k*')%,TK,YMIN,'b:',TK,YMAX,'b:');
   end   
   ylabel(sprintf('y_%d',i));
end

%pause