% main_ex1_lecture1.m
% Example in first lecture in IIA2217 SYSID course 2023

% Simulate a 1st order dynsmic system to generate data. Using a binary
% signal input.

N=1000;
U=prbs1(N,200,50);
A=0.9; B=0.5; D=1; E=0;     % Fixed 1st order model
Y=dsrsim(A,B,D,E,U);
Y=Y+randn(N,1)*1;           % Add measurements noise with standard deviation 1

% Plotting the data
figure(1)
subplot(211), plot(U), grid, title('Input u_k')
subplot(212), plot(Y), grid, title('Output y_k'), xlabel('Samples: 1 \leq k \leq N')

% Identify a model using DSR
[a,b,d,e]=dsr(Y,U,5);

% Simulate the model and plot against true data
Yp=dsrsim(a,b,d,e,U);

figure(2)
plot([Y Yp]), grid, title('True Y and predicted  Y_poutputs'), xlabel('samples')