function [objval DOF d01 d02 nfull nred Nsim]= dif_mb_WCE(N,ibtype)
%% Solutions for a set of different reduced oreder models
% clc
% clear all
% close all

for Nsim =  10
nfull = 16;
nred  = 16;
% N = 8;  %prediction horizon
% ibtype = [2 1];   %input blocking type
% ibtype = {[8] [4 4] [2 3 3] [1 2 2 3] [1 1 1 2 3] [1 1 1 1 1 3] [1 1 1 1 1 1 2]};   %input blocking type for different calc.DOF 1-7
% ibtype = {[8] [4 4] [1 2 5] [1 1 3 3] [1 1 1 2 3] [1 1 1 1 2 2] [1 1 1 1 1 1 2]};   %input blocking type for different calc.DOF 1-7


% ibtype = {[1 3 4] [4 3 1] [4 2 1 1] [1 1 2 4]};   % compar input blocking types with same DOF = 3,4
% ibtype = {[1 1 6] [1 2 5] [1 3 4] [2 2 4] [2 3 3]};   % compar input blocking types with same DOF = 3 a
% ibtype = {[6 1 1] [5 2 1] [4 3 1] [4 2 2] [3 3 2]};   % compar input blocking types with same DOF = 3 b
% ibtype = {[1 4 3]};   %input blocking type for ploting u differences

d01 = [];
d02 = [];
d0n = [];

% calculation for different red. order models

%     for i = 1:length(nred)
%         [objval(i) d0 UR{i} UF{i} XR{i} XF{i} Nsim Mbk DOF] = ...
%             moveblocking_onestep(nfull,nred(i),N,ibtype)
%         d01 = [d01; d0(1,1)];
%         d02 = [d02; d0(2,1)];
%         d0n = [d0n; d0];
%         DOF(i) = DOF; 
%     end


%% calculation for different IB types

    for i = 1:length(ibtype)
        [objval(i) d0 UR{i} UF{i} XR{i} XF{i} Nsim M{i} DOF(i)] = ...
            moveblocking_onestep(Nsim,nfull,nred,N,double(ibtype{i}))
        d01 = [d01; d0(1,1)];
        d02 = [d02; d0(2,1)];
        d0n = [d0n; d0];
       
    end
    
%     %updating objective function
%     for i = 1:length(ibtype)
%         [objval(i) d0 UR{i} UF{i} XR{i} XF{i} Nsim M{i} DOF(i)] = ...
%             moveblocking_updatingobj(Nsim,nfull,nred,N,double(ibtype{i}))
%         d01 = [d01; d0(1,1)];
%         d02 = [d02; d0(2,1)];
%         d0n = [d0n; d0];
%        
%     end

d01 = double(d01);
d02 = double(d02);

% ur = UR{1};
% ur = double(ur{1});

%% ploting
% figure
% plot(DOF,objval,'LineWidth',2);
% xlabel('degrees of freedom (DOF)')
% ylabel('worst-case error (outputs)')
% title('Input blocking');
% hold on ;
% plot(DOF,objval,'r*');
% grid on;
% legend('IB','ibtype = ([8], [4 4], [2 3 3], [1 2 2 3], [1 1 1 2 3], [1 1 1 1 1 3], [1 1 1 1 1 1 2])')
% 
% figure
% plot(DOF,objval,'LineWidth',2);
% xlabel('degrees of freedom (DOF)')
% ylabel('worst-case error (outputs)')
% title('Input blocking');
% hold on ;
% plot(DOF,objval,'r*');
% grid on;
% XLIM([3.9999999 7])
% legend('IB','ibtype = ( [1 2 2 3], [1 1 1 2 3], [1 1 1 1 1 3], [1 1 1 1 1 1 2])')
% 
% % plot u differences
% for IB = 1:length(ibtype)
% ur = UR{IB};
% ur = double(ur{1});
% % obtaining ur1, ur2 when we have nu = 2
% for i = 1 : (size(ur,1)/2)
% 
% j = i - 1;   
% ur1(i) = ur(2*j+1);    
% ur2(i) = ur(2*i);
% 
% end
% 
% figure;
% plot((1:1:N),ur1,'LineWidth',2);
% hold on
% plot((1:1:N),ur1,'*r');
% xlabel('u(i1)')
% ylabel('values')
% title('')
% grid on
% 
% figure;
% plot((1:1:N),ur2,'LineWidth',2);
% hold on
% plot((1:1:N),ur2,'*r');
% xlabel('u(i2)')
% ylabel('values')
% title('')
% grid on
% end
%% calculation to comapre different IB types with same DOF

% %DOF = 3,4
% % colors for plot
% G = cell(5,1);
% G{1,1} = 'b*';
% G{2,1} = 'g*';
% G{3,1} = 'r*';
% G{4,1} = 'c*';
% G{5,1} = 'k*';
% G{6,1} = 'y*';
% 
% 
% % calculation for different IB types
% 
%     for i = 1:length(ibtype)
%         [objval(i) d0 UR{i} UF{i} XR{i} XF{i} Nsim M{i} DOF(i)] = ...
%             moveblocking_onestep(Nsim,nfull,nred,N,double(ibtype{i}))
%         d01 = [d01; d0(1,1)];
%         d02 = [d02; d0(2,1)];
%         d0n = [d0n; d0];
%         % ploting
%         hold on;
%         plot(DOF(i),objval(i), G{i,1});
%         xlabel('degrees of freedom');
%         ylabel('worst case error (outputs)');
%         legend('IB = [2 2 4]','IB = [4 2 2]','IB = [2]','IB = [4 2 1]');
%         grid on;
%     end

%% DOF = 3    
%  % colors for plot
% G = cell(5,1);
% G{1,1} = 'b*';
% G{2,1} = 'g*';
% G{3,1} = 'rO';
% G{4,1} = 'c*';
% G{5,1} = 'k*';
% G{6,1} = 'y*';
% 
% 
% % calculation for different IB types
% 
%     for i = 1:length(ibtype)
%         [objval(i) d0 UR{i} UF{i} XR{i} XF{i} Nsim M{i} DOF(i)] = ...
%             moveblocking_onestep(Nsim,nfull,nred,N,double(ibtype{i}))
%         d01 = [d01; d0(1,1)];
%         d02 = [d02; d0(2,1)];
%         d0n = [d0n; d0];
%         % ploting
%         hold on;
%         plot(DOF(i),objval(i), G{i,1}),'Linewidth',3;
%         xlabel('degrees of freedom');
%         ylabel('worst case error (outputs)');
% %         legend('IB = [1 3 4]','IB = [4 3 1]','IB = [4 2 1 1]','IB = [1 1 2 4]');
% %         legend('IB = [1 1 6]','IB = [1 2 5]','IB = [1 3 4]','IB = [2 2 4]','IB = [2 3 3]');
%         legend('IB = [6 1 1]','IB = [5 2 1]','IB = [4 3 1]','IB = [4 2 2]','IB = [3 3 2]');    
%         grid on;
%         xlim([2.9 3.1])
%     end




%% save Nsimtest.mat nred objval objval_scaling
% save(['IB_Nsim' num2str(Nsim) '.mat'])
% save(['IB_type' num2str() '.mat'])

end