function [Up2,up2] = problem2(N,Q,R,x_measuringP2,d0,alpha)

%% System Model
% alpha = 0.5;
% d0 = [1;2];
% N = 8; 
nfull = 16;
% number of inputs
nu = 2;
[Gfull] = model_baltrunc(nfull);
% make a discrete-time approximation
Ts	= 1;
Gfull = c2d(Gfull,Ts);
[A B C Bd] = deal(Gfull.a,Gfull.b(:,1:nu),Gfull.c,Gfull.b(:,nu+1:end));
% number of states
nx = size(Gfull.a, 2);
x0 = x_measuringP2;
% weight matrices
% Q = 1*(diag(ones(16,1),0));
% R = diag(ones(2,1),0);
umax = 1;
umin = -1;
% X,Y,Z,QQ,RR
[X,Y,Z]=xyfun(alpha,N,A,B,Bd);
[QQ,RR]=QRfun(Q,R,N);

% Matrixes for quadprog
Fx = Z'*QQ*Y;
Fd = X'*QQ*Y;
f = Fd'*d0 + Fx'*x0;
H = (Y'*QQ*Y)+RR;
G = [eye(nu*N);-1*eye(nu*N)];
W = [umax*ones(nu*N,1);-umin*ones(nu*N,1)];
[Uopt]=QUADPROG(H,f,G,W);
u0opt = Uopt(1:nu);

%     compare P1 & P2
Up2 = Uopt;
up2 = u0opt;