Browse Source

feat: exercise-9 (WIP)

master
parent
commit
fbe4b6bbce
  1. 13
      .gitignore
  2. 89
      exercise-9/exercise.m

13
.gitignore

@ -0,0 +1,13 @@
exercise-1/SystemID_HS2022_Exercise1.pdf
exercise-1/SystemID_HS2022_Solution1.pdf
exercise-2/SysID_lecture03_small.pdf
exercise-2/SystemID_HS2022_Exercise02.pdf
exercise-3/SystemID_HS2022_Exercise03.pdf
exercise-3/SystemID_HS2022_Solution03.pdf
exercise-4/SystemID_HS2022_Solution4.pdf
exercise-5/Exercise5_SysID_2022.pdf
exercise-5/Exercise5_SysID_2022_Solution.pdf
exercise-8/HS2022_Exercise8_Solution.pdf
exercise-9/exercise.asv
exercise-8/exercise.asv

89
exercise-9/exercise.m

@ -0,0 +1,89 @@
clear, close, clc
B = [0.0, 0.3, -1.5];
A = [1.0, 1.1, 0.7];
ts = 0.1;
%% Exercise 9.1
N = 100;
e_v = 0.5;
u_v = 0.5;
rng(1);
u = randn(N, 1) * sqrt(e_v);
rng(2);
e = randn(N, 1) * sqrt(u_v);
B_A = tf(B, A, ts);
y = lsim(B_A, u) + e;
%% Exercise 9.2
ns = [1, 2, 3, 2, 3];
ms = [2, 2, 2, 3, 3];
% TODO error is a bit lower than solution
mse_ls = zeros(length(ns), 1);
systems = cell(5, 1);
for i=1:5
[A_ls, B_ls] = ls_estimate(y, u, ns(i), ms(i));
systems{i} = estimate_to_sys(A_ls, B_ls, ts);
y_pred = lsim(systems{i}, u);
mse_ls(i) = mean((y_pred - y).^2);
end
%% Exercise 9.3
u_instr = randn(N, 5) * sqrt(u_v);
y_instr = zeros(N, 5);
rng(3)
u_instr(:, 2) = randn(N, 1) * sqrt(u_v);
mse_iv = zeros(5, 1);
inst_systems = cell(5, 1);
for i=1:5
y_instr(:, i) = lsim(systems{i}, u_instr(:, i));
[A, B] = inst_estimate(y, y_instr(:, i), u_instr(:, i), ns(i), ms(i));
inst_systems{i} = estimate_to_sys(A, B, ts);
y_pred = lsim(inst_systems{i}, u_instr(:, i));
mse_iv(i) = mean((y_pred - y_instr(:, i)) .^ 2);
end
[mse_ls mse_iv]
%% Common function
function [A, B]=inst_estimate(y, y_inst, u, na, nb)
phi_u_F = toeplitz(u(1:end-1), zeros(nb, 1));
phi_y_F = toeplitz(-y(1:end-1), zeros(na, 1));
phi = [phi_y_F, phi_u_F];
theta = (y_inst(2:end).' * phi) \ (y_inst(2:end).' * y(2:end));
A = theta(1:na).';
B = theta(na+1:end).';
end
function [A,B]=ls_estimate(y, u, na, nb)
phi_u_F = toeplitz(u(1:end-1), zeros(nb, 1));
phi_y_F = toeplitz(-y(1:end-1), zeros(na, 1));
phi = [phi_y_F, phi_u_F];
theta = phi \ y(2:end);
A = theta(1:na).';
B = theta(na+1:end).';
end
function sys=estimate_to_sys(A, B, ts)
A = [A zeros(1, max(0, length(B) - length(A)))];
B = [B zeros(1, max(0, length(A) - length(B)))];
sys = tf(B, [1 A], ts);
end
Loading…
Cancel
Save