clear, clc;

A = [0 1 0; 0 0 1; -0.2 0.2 1.1];

B = [0; 0; 1];

Q = eye(3);

u = rand(1,14);

x0 = rand(3, 1);

% 生成响应

t=14;x=zeros(3,t);x(:,1)=x0;

for i=1:t

x(:,i+1)=A*x(:,i)+B*u(1,i)

end

x_i0=x(:,1:14);

x_i1=x(:,2:15);

U_i=u;

% 使用CVX求解优化问题

Q = eye(3);

%生成随机半正定矩阵R

T = randn(3);

R = T* T’;

[V, D] = eig(R); % 对A进行特征值分解

D(D < 0) = 0; % 将负特征值设为零，确保半正定性

R = V * D * V’; % 重构半正定矩阵

chol(R);

%求解x_i0的左伪逆

x_R=(x_i0’)*inv(x_i0*(x_i0’));

% %CVX工具箱

cvx_begin sdp

cvx_solver sedumi

variable P(3, 3) symmetric

minimize trace(Q*P)
subject to
[P-R,(x_i1*(x_R

*P));(x_i1*(x_R

*P))',P] >= 0; % 使用 >= 0 来表示半正定约束*

cvx_end

%求解K

K =U_i(x_R*P)*pinv(P);

cvx_end

%求解K

K =U_i