%root locus by direct solution of the characteristic equation
%See example in your lecture notes. See the code for MATLAB
% and Octave compatibility
clear all
clc
close all

pkg load control % for Octave only

K=linspace(0,20,400);
n=length(K);
poles=zeros(3,n);

for i=1:n
%  char_eq=[6 41 76 21-24*K(i)];
  char_eq=[1    6   11    6+4*K(i)]; %closed-loop characteristic eq
  poles(:,i)=roots(char_eq);
  end
  
  x=real(poles);
  y=imag(poles);
  
  plot(x(1,:),y(1,:),'*',x(2,:),y(2,:),'*',x(3,:),y(3,:),'*')
  grid on
figure
  %root locus using built-in command
  sys=tf(4,[1 6 11 6]);
  %Use this if running in MATLAB
%   rlocus(sys,K);
  
  %Use this if unning in Octave
  rlocus(sys,K(2)-K(1),min(K),max(K));
  
  