clc
clear
o=input('input the order of system');
n=input('enter the coeficient of numerator');
d=input('enter the coeficient of denomenator');
c=mod(o,2);
sys=tf(n,d)
r=input('input the order of reduced system');
% put the last two column as "0"
for i=o+1:o+2
n(1,i)=0;
d(1,i+1)=0;
end
%for the seperate out the even & odd term
%(1):for odd term
if c==1
j=1;
for i=1:2:o+1
ne(1,j)=n(1,i);
no(1,j)=n(1,i+1);
j=j+1;
end
j=1;
for i=1:2:o+1
do(1,j)=d(1,i);
de(1,j)=d(1,i+1);
j=j+1;
end
%(2) for even term
else
j=1;
for i=1:2:o+1
no(1,j)=n(1,i);
ne(1,j)=n(1,i+1);
j=j+1;
end
j=1;
for i=1:2:o+1
de(1,j)=d(1,i);
do(1,j)=d(1,i+1);
j=j+1;
end
end
n=[ne;no;de;do];
sn=tf(ne,no);
sd=tf(de,do);
mne=-transpose(zero(sn));
mno=-transpose(pole(sn));
mde=-transpose(zero(sd));
mdo=-transpose(pole(sd));
m=[mne; mno; mde; mdo];
for k=1:o-r
% set the maximum out of the row
[y,v] = max(m,[],2);
% for set the end coefficint for ao,bo,co,do
if c==0
n1=(o+2)/2;
m1=o/2;
else
m1=(o-1)/2;
n1=(o+1)/2;
end
di=zeros(4,1);
for j=1:4
for i=n1:-1:1
if i==0
elseif di(j,1)==0
di(j,1)=n(j,i);
end
end
end
%find the maximum from the matrix "m"
s=zeros(2,2);
if y(1,1)>y(2,1)
for i=1:n1-1
if y(1,1)==m(1,i)
s(1,:)=[1 i];
end
end
else
for i=1:n1-1
if y(2,1)==m(2,i)
s(1,:)=[2 i];
end
end
end
if y(3,1)>y(4,1)
for i=1:n1-1
if y(3,1)==m(3,i)
s(2,:)=[3 i];
end
end
else
for i=1:n1-1
if y(4,1)==m(4,i)
s(2,:)=[4 i];
end
end
end
% instead of zero set infinity
for j=1:4
for i=1:n1-1
if m(j,i)==0
i1(j,i)=-inf;
else
i1(j,i)=m(j,i);
end
end
end
i1(s(1,1),s(1,2))=-inf;
i1(s(2,1),s(2,2))=-inf;
m=i1;
end
% for generate the multiplication of the remaining component
b=zeros(4,n1*2+1);
for j=1:4
a=[0 0 di(1,1);0 di(2,1) 0 ;0 0 di(3,1); 0 di(4,1) 0 ];
b1=1;
for i=1:m1+1
b1=conv(a(j,:),b1);
if i==m1+1
else
a(j,:)=[ (1/i1(j,i)) 0 1];
end
end
b(j,:)=b1;
end
nu1=b(1,:)+b(2,:);
nu2=b(3,:)+b(4,:);
rsys=tf(nu1,nu2)
step(sys,rsys,'--')
legend('show')
figure
bode(sys,rsys,'--')
legend('show')