%#3
clc
clear;
b=[4];
a=[1, 0.5];
n=[0:1500]';	%a column vectors
H=tf(b,a)
x=n;

%set location of ramp
index=find(n <0);
x(index)=0;

y=filter(b,a,x);

%Finding the steady state and transient 
yss=y(end)
ytr=y-yss;

figure(1);
subplot(2,1,1)
plot(n,x,'b');
xlabel('Time-index, n');
ylabel('Amplitude');
title('X(n),Unit Ramp Input');
grid on

subplot(2,1,2)
plot(n,y,'r');
xlabel('n');
ylabel('Amplitude');
title('Y(n)-Ramp Response');
grid on

figure(2);
subplot(2,1,1);
plot(n,yss,'x');
xlabel('n');
ylabel('Amplitude');
title(' Steady state response, yss')
grid on;

subplot(2,1,2)
plot(n,ytr);
xlabel('n');
ylabel('Amplitude');
title(' Transient response, ytr')
grid on;

%general form-finding the boundary of transient/steady state response

Nss=0; stop=0; test_plus=0.01; test_minus=-0.01;

for i=1:length(ytr)-1
    if test_minus<=ytr(i+1) & ytr(i+1)<=test_plus & stop==0
        Nss=i;
        stop=1;
    elseif (test_plus<ytr(i+1)) | (test_minus>ytr(i+1)) & stop==1
        Nss=0;
        stop=0;
    end
end
Nss





