function [Pobjvalsum] = sum_N_WCE(d01,d02,nfull,nred,Nsim,Nsum,Nfull,Nred)
% Function to calculate sum of WCE over the Nsum from different initial conditions (Nsim)
for s = 1:length(Nred)
%% 1: Generate the MPC controllers
nu = 2; nd = 2;
umax  = 1; umin = -1;

[Gfull] = model_baltrunc(nfull);
[Gred Tl Tr] = baltrunc(Gfull,nred);
% make a discrete-time approximation
Ts	= 1;
Gfull = c2d(Gfull,Ts);
Gred = c2d(Gred,Ts);

%% make the MPC controllers
[Af Bf Cf Bd] = deal(Gfull.a,Gfull.b(:,1:2),Gfull.c,Gfull.b(:,3:4));
[Ar Br Cr Bdr] = deal(Gred.a,Gred.b(:,1:2),Gred.c,Gred.b(:,3:4));

%%  simulation

% load initial worst-case disturbance. Note that we use subscript 's'
% for simulation

objvalsum = 0;
d0s = [d01(s);d02(s)];
xfs(:,1) = Bd*d0s; 
xrs(:,1) = xfs(:,1);	% initial states
xfsum(:,1) = Bd*d0s; 
xrsum(:,1) = xfs(:,1);


%% calculation sum of obj functions
for i = 2:Nsum
    x0if = xfsum(:,i-1);
    x0ir = xrsum(:,i-1);
    % set up the controllers
    [Matricesf Matricesr] = matrices_analyze_dro(nfull,nred,Nfull,Nred(s),x0if,x0ir)
    
    % calculate inputs
    ufsum(:,i-1) = MPC_solv(Matricesf);
    ursum(:,i-1) = MPC_solv(Matricesr);
    
    % update states
    % results for full-order controller:
    xfsum(:,i)     = Af*xfsum(:,i-1) + Bf*ufsum(:,i-1);
    % results for low-order controller:
    xrsum(:,i)   = Af*xrsum(:,i-1) + Bf*ursum(:,i-1);
end

% calculate outputs
for i = 1:Nsum
% full-order controller:
yfsum(:,i)	 = Cf*xfsum(:,i);
% low-oerder controller:
yrsum(:,i)	 = Cf*xrsum(:,i);

objvalsum = objvalsum + norm((yfsum(:,i)-yrsum(:,i)),inf);
end
Pobjvalsum(s) = objvalsum;

end
%% save Objsum_Nsim.mat
save(['Objsum_N_Nsim' num2str(Nsim) '.mat'])





