%  This function solves homework problem [H1.2]
%  --------------------------------------------
%
diary nsds_hw1_2.dry
echo on
%
%  Enter the continuous-time system
%
A = [ zeros(3,1) , eye(3) ; [ -2:-1:-5 ] ];
B = [ 0 ; 0 ; 0 ; 1 ];
C = [ 1 , 0 , 0 , 0 ];
D = 10;
%
%  Convert to equivalent discrete-time system
%
h = 0.01;
F = eye(size(A)) + A*h;
G = B*h;
H = C;
I = D;
%
%  Simulate the discrete-time system
%
tf = 5;
n = tf/h;
x0 = ones(4,1);
x = x0;
xx = x;
t = 0;
u = 5*sin(2*t);
y = H*x + I*u;
yy = y;
for i=1:n,
    xnew = F*x + G*u;
    t = i*h;
    u = 5*sin(2*t);
    x = xnew;
    y = H*x + I*u;
    xx = [xx , x];
    yy = [yy , y];
end;
%
%  For control purposes, simulate also the continuous-time system
%
S = ss(A,B,C,D);
t = 0:h:tf;
u = 5*sin(2*t);
y = lsim(S,u,t,x0);
%
%  Plot the results
%
subplot(2,1,1)
plot(t,y,'k');
pause
plot(t,yy,'r');
grid on
title('Homework [H1.2]')
xlabel('time')
ylabel('y')
%
print -deps nsds_hw1_2.eps
diary off
return


