clear;clc;close all;
% H(s)=s+1/S^2+5s+6
H=tf([1 1],[1 5 6]);
t=0:0.1:10;
y1=impulse(H,t);
%y1=lsim(H,[1 zeros(1,length(t)-1)],t);
y2=step(H,t);
%y2=lsim(H,ones(size(t)),t);
y3=lsim(H,sin(2*t),t);
y4=lsim(H,exp(-t),t);
plot(t,y1,t,y2,t,y3,t,y4);
legend('a','b','c','d');
xlabel('thoi gian');
ylabel('do lon');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
num=[2 0 1 -3 1 4]; 
den=[5 2 -1 -3 5 2 -4 2 -1];
G=tf([2 0 1 -3 1 4],[5 2 -1 -3 5 2 -4 2 -1]);
[z,p,k]=tf2zp(num,den)

%%
[R,p,K]=residue(num,den);
% H(s)=num(s)/den(s)=R(1)/(s-p(1))+R(2)/(s-p(2))+...+R(8)/(s-p(8))+K(s)
R
K
r1=abs(R(1))
r3=R(3)
r4=abs(R(4))
r6=R(6)
r7=abs(R(7))
phi1=angle(R(1))
phi4=angle(R(4))
phi7=angle(R(7))
h=2*r1*exp(real(p(1))*t).*cos(imag(p(1))*t+phi1)+r3*exp(p(3)*t)...
+2*r4*exp(real(p(4))*t).*cos(imag(p(4))*t+phi4)+r6*exp(p(6)*t)...
+2*r7*exp(real(p(7))*t).*cos(imag(p(7))*t+phi7);
figure()
plot(t,h); grid; xlabel('thoi gian'); ylabel('dap ung xung')

figure()
impulse(G,t)
%%%%%%%%%%%%%%%%%%%%%
% phan 3
clear;clc;
t1=linspace(0,2,200);
 x1=-t1+1;
 t2=linspace(2.01,3,99);
 x2=-ones(size(t2));
 t3=linspace(3.01,5,199);
 x3=zeros(size(t3));
 t=[t1 t2 t3];
 x=[x1 x2 x3];
 figure()
 subplot(211)
 plot(t,x)
 title('tin hieu vao');xlabel('thoi gian')
 axis([0 5 -1.1 1.1])
 H=tf(1,[1 5 4])
 subplot(212)
 y=lsim(H,x,t) ;
 plot(t,y)
 title('tin hieu ra');xlabel('thoi gian')
 %%%%
 %phan 4
 clear;
 H2=tf([1 0.966],[1 0.966 463 97.8 12131 8.11 0]);
 figure();
 impulse(H2,10000)