num = 2; den = [1 3 2]; G = tf(num, den); T = 1; N = 10; M = 2; Mp = 5; lambda = 1; gamma = 1; h = impulse(G, 0:T:(N-1)*T); Phi = zeros(N, M); for i = 1:N for j = 1:M if (i >= j) Phi(i,j) = h(i-j+1); end end end Delta = eye(N) * lambda; K = (Phi' * Phi + lambda * eye(M)) \ Phi' * gamma; t = linspace(0, 100, length(u)); r = ones(size(t)); y = zeros(size(t)); u = zeros(size(t)); for k = N+1:length(t) y(k) = step(G, u(k-N:k-1),t(k)); % pass the time vector 't' as the third input argument e = r(k) - y(k); du = K * e'; u(k) = u(k-1) + du(1); end figure; plot(t, r, 'k--', t, y, 'b', 'linewidth', 2); xlabel('Time (s)'); ylabel('Output'); legend('Reference', 'Output'); title('DMC Controller with Unit Step Reference');上述代码报错错误使用 DynamicSystem/step (第 95 行) Invalid syntax for time or frequency response command. See command help for more information.的错误请修改
时间: 2024-03-12 15:47:17 浏览: 69
代数状态空间方程的代数 ss2tf:ss2tf 函数-matlab开发
这段Matlab代码中的错误是因为在第27行中的step函数调用中,第二个输入参数应该是u而不是t。正确的代码如下:
```
figure;
plot(t, r, 'k--', t, y, 'b', 'linewidth', 2);
xlabel('Time (s)');
ylabel('Output');
legend('Reference', 'Output');
title('DMC Controller with Unit Step Reference');
function y = step(G, u, t)
[y, ~, ~] = lsim(G, u, t);
end
num = 2;
den = [1, 3, 2];
G = tf(num, den);
T = 1;
N = 10;
M = 2;
Mp = 5;
lambda = 1;
gamma = 1;
h = impulse(G, 0:T:(N-1)*T);
Phi = zeros(N, M);
for i = 1:N
for j = 1:M
if (i >= j)
Phi(i,j) = h(i-j+1);
end
end
end
Delta = eye(N) * lambda;
K = (Phi' * Phi + lambda * eye(M)) \ Phi' * gamma;
t = linspace(0, 100, length(u));
r = ones(size(t));
y = zeros(size(t));
u = zeros(size(t));
for k = N+1:length(t)
y(k) = step(G, u(k-N:k-1), t(k)); % pass the input signal 'u' as the second input argument
e = r(k) - y(k);
du = K * e';
u(k) = u(k-1) + du(1);
end
```
这个修改后的代码应该可以正确地运行了。
阅读全文