%rlocus_phasecondition.m
%Phase Calculation for a Test Point

%L(s)=(s+1)/(s*(s+5)*(s^2+4s+8))

clc
%echo on
clear all


num=[1 1];
den=conv([1 5 0],[1 4 8])

sys=tf(num,den)

figure(1)
pzmap(sys)

[zeros,poles,gain]=zpkdata(sys,'v')

so_real=input(['\nEnter real part of test point: ']);
so_imag=input(['\nEnter imaginary part of test point: ']);
so=so_real+i*so_imag;


n=length(poles)
angle_poles=0;
for j=1:n
   vec=so-poles(j);
   angle_cont=angle(vec);
   angle_poles=angle_poles+angle_cont;
   fprintf(['\nAngle contributed by pole ',num2str(j),': ',num2str(angle_cont/pi*180),' degrees'])
end
angle_poles=unwrap(angle_poles);

m=length(zeros)
angle_zeros=0;
for j=1:m
   	vec=so-zeros(j);
   	angle_cont=angle(vec);
   	angle_zeros=angle_zeros+angle_cont;
	   fprintf(['\nAngle contributed by zero ',num2str(j),': ',num2str(angle_cont/pi*180),' degrees'])
end
angle_zeros=unwrap(angle_zeros);

angle_tot=angle_zeros-angle_poles;
angle_tot=unwrap(angle_tot);

fprintf('\n\nAngle: %d degrees\n',angle_tot/pi*180)

figure(2)
rlocus(sys)
hold on
plot(so_real,so_imag,'kx')
hold off

