function [Y,X]=sys_sim(U)
% [Y,X]=sys_sim(U)
% Purpose. Simulate a system to generate some
% output data Y from given inputs in U

A=0.9; B=0.5; D=1.0; E=-1.0; 

% Define the initial state x0
Hd=D*inv(1-A)*B+E;  % The system gain
x0=inv(1-A)*B*U(1); % Initial state, steady state

% Check size of U to obtain N
N=length(U);
Y=zeros(N,1); X=zeros(N,1); 

x=x0;
for k=1:N
    
    u=U(k);
    
    w=randn*0.5; % Measurements noise
    y=D*x+E*u+w;  % Calculate output
    
    Y(k,1)=y;   % Storing y in matrix Y
    X(k,1)=x;   % Storing the states
    
    v=randn*0.5; % Process noise
    x = A*x + B*u;    
    
end