clearclcA=[0.80;0.81];B=[0.1;0.1];C=[01];D=0;P=10;M=4;MAX=15;MIN=-MAX;N=50;%PerformancewightsQ=eye(P);R=0.01*eye(M);%Q1=eye(P*ncr);R1=ones(P,1);%PredefinedreferenceRe=zeros(50,1);Re(1:15)=2;Re(16:30)=5;Re(31:40)=2;[nbr,nbc]=size(B);[nar,nac]=size(A);[ncr,ncc]=size(C);%x0=[1;1];%u(0)=0;y=zeros(N,1);u=zeros(N,1);%%S=zeros(P*ncr,M*nbc);fori=0:P-1forj=0:M-1ifi=jS(i*ncr+1:(i+1)*ncr,j*nbc+1:(j+1)*nbc)=C*A^(i-j)*B;endendend%%T=zeros(P*ncr,nac);fori=0:P-1T(i*ncr+1:(i+1)*ncr,:)=C*A^(i+1);endS_S=S'*S;S_T=S'*T;S_R1=S'*R1;x0=[0;0];u0=0;%Uk=zeros(N,1);%H=2*(R1+S'*Q1*S);%f1=T*x0-Re;%Y=2*(Q+T'*Q1*T);%uk=u0;fork=1:NA_co=[eye(M);-eye(M)];b_co=[MAX*ones(4,1);-1*MIN*ones(4,1)];U=quadprog(2*(S_S+R),-2*S'*(R1*Re(k)-T*x0),A_co,b_co);u(k)=U(1);ifk==1Uk(k)=u(k);elseUk(k)=Uk(k-1)+u(k);endy(k)=C*x0+D*u(k);x0=A*x0+B*u(k);end%figure(1)subplot(2,1,1)holdonplot(Re,'r')plot(y)title('outputy')%figure(2)text(40,5,'Red:set')text(40,4,'Blue:y')subplot(2,1,2)holdonplot(u,'r')%title('inputdeltau')%figure(3)plot(Uk)title('inputu')text(40,17,'Red:deltau')text(40,12,'Blue:u')