function sys = cdhs(t,u,flag)
persistent num den c M Ts a b d thetae_1 na yk nb aa N1 N Nu gamma alpha E F G F1 F2 thetae P lambda Yr dUk Yk phi K ae be aae uk duk dyk w
switch flag
case 0
num = [4, 0.04];
den = [1, 0];
c = 1;
M = 2;
Ts = 1;
[a, b, d] = tflisan(num, den, M, Ts);
na = length(a) - 1;
b = [zeros(1, d - 1), b];
nb = length(b) - 1;
aa = conv(a, [1, -1]);
naa = na + 1;
N1 = d;
N = 80;
Nu = 80;
gamma = eye(Nu);
alpha = 0.7;
P = 10^6 * eye(na + nb - d + 2);
lambda = 1;
uk = zeros(d + nb, 1);
duk = zeros(d + nb, 1);
dyk = zeros(na, 1);
thetae_1 = 0.001 * ones(na + nb - d + 2, 1);
yk=zeros(na,1); %输出初值
[E, F, G] = multidiophantine(aa, b, c, N);
G = G(N1:N, :);
F1 = zeros(N - N1 + 1,Nu);
F2 = zeros(N - N1 + 1, nb);
for i = 1:N - N1 + 1
for j = 1:min(i, Nu)
F1(i, j) = F(i + N1 - 1, i + N1 - 1 - j + 1);
end
for j = 1:nb
F2(i, j) = F(i + N1 - 1, i + N1 - 1 + j);
end
end
sys = [];
sys.NumContStates = 5;
sys.NumDiscStates = 0;
sys.numInputs = 1;
sys.numOutputs = 1;
sys.DirFeedthrough = 1;
sys.NumSampleTimes = 1;
case 2
for k = 1:t
w = u(k); % 修改:直接从输入参数 u 中获取 w 的值
dy(k) = -a(2:na + 1) * dyk(1:na) + b * duk(1:nb + 1); % +xi(k);
y(k) = yk(1) + dy(k); % +xi(k); %采集输出数据
Yk = [y(k); yk(1:na)]; %构建向量Y(k)
dUk = duk(1:nb); %构建向量ΔU(k-j)
yr(k) = y(k);
for i = 1:N
yr(k + i) = alpha * yr(k + i - 1) + (1 -alpha) * w(k + d);
end
Yr = (yr(k + N1):k + N1 + N - 1)';
phi = [-dyk(1:na); duk(d:nb + 1)];
K = P * phi / (lambda + phi' * P *phi);
thetae(:, k) = thetae_1 + K * (dy(k) - phi' * thetae_1);
P = (eye(na + nb - d + 2) - K * phi') * P /lambda;
ae = [1, thetae(1:na, k)];
be = [zeros(1, d - 1), thetae(na + 1:na + nb - d + 2, k)];
aae = conv(ae, [1, -1]);
% 求控制量
dU = inv(F1' * F1 + gamma) * F1' * (Yr - F2 * dUk - G * Yk); % ΔU
du(k) = dU(1);
u(k) = uk(1) + du(k);
% 更新数据
thetae_1 = thetae(:, k);
for i = 1 + nb:-1:2
uk(i) = uk(i - 1);
duk(i) = duk(i - 1);
end
uk(1) = u(k);
duk(1) = du(k);
for i = na:-1:2
yk(i) = yk(i - 1);
dyk(i) = dyk(i - 1);
end
yk(1) = y(k);
dyk(1) = dy(k);
end
case 3
sys = y(t);
case {1,4,9}
sys=[];
otherwise
error('Unexpected flag value');
end
end