Exercises for system-id.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.1 KiB

clear, close, clc
N = 10000;
a = [1, -1.5, 0.7];
b = [0.0, 1.0, 0.5];
c = [1.0, -1.0, 0.2];
B_A = tf(b, a, 1.0);
C_A = tf(c, a, 1.0);
e = randn(N, 1);
e_u = randn(N, 1);
u = generate_input_sequence(e, e_u);
y = lsim(B_A, u) + lsim(C_A, u);
%% Exercise 8.1
C_inv = tf([1, 0, 0], c, 1.0);
u_F = lsim(C_inv, u);
y_F = lsim(C_inv, y);
phi_u_F = toeplitz(u_F(1:end-1), zeros(2, 1));
phi_y_F = toeplitz(-y_F(1:end-1), zeros(2, 1));
phi = [phi_y_F, phi_u_F];
theta = phi \ y_F(2:end)
% G_arx = arx([y_F u_F], [2 2 1])
%% Exercise 8.2
N_val = 1000;
y_val = y(end-N_val:end);
u_val = u(end-N_val:end);
G_pred = tf([theta(3), theta(4)], [1.0, theta(1), theta(2)], 1.0);
y_pred = lsim(G_pred, u_val);
plot(y_val, '--black');
title('Validation plot')
xlabel('Time')
ylabel('Output')
hold on
plot(y_pred, 'r')
%% Exercise 8.3 is too ez and I'm mega lazy
%% Helper functions
function u=generate_input_sequence(e, e_u)
N = size(e, 1);
u = zeros(N, 1);
for i=1:N
if i > 1
u(i) = u(i) + 0.1 * u(i - 1);
end
if i > 2
u(i) = u(i) + 0.12 * u(i - 2);
end
u(i) = u(i) + e(i) + e_u(i);
end
end