%Dynamic Choice of Bode, Evans, or Nyquist
%Dynamic Choice of Loop Types
%Nyquist plot zooming
%	Changing the following parameters could give poor scaling.
%		They will be modified automatcally as the various kinds
%		of loops are chosen to improve plots.

Wz = 1000;
Wp = 100; 
K = 10; 

f = logspace(0,5,200); % Frequency range
% v is axis vector to scale Nyquist plots depending on loop type
% num and den are coefficients of open-loop transfer function

ny = 0; % nyquist looping control variable
go = 2; % main looping control variable
while (go ~= 0)
	order = input('enter order (1-3): ','s');  % SELECT LOOP
	if strcmp(order,'1'),
		disp('First Order');
		v = [-9 1 -9 1];
		num = [0 K];
		den = [1 0];
	elseif strcmp(order,'2'),
		typ = input('enter type (1a [lag], 1b [lag-lead] or 2 [integrator & lead]): ','s');
		if strcmp(typ,'1a'),
			v = [-2 1 -5 1];
			disp('Second Order, Type 1a [lag] with K*10');
			num = [0 K*10];
			den = [1/Wp 1 0];
		elseif strcmp(typ,'1b'),
			disp('Second Order, Type 1b [lag-lead] with K*100');
			v = [-6 1 -10 1];
			Kx = 100*K;
			num = [Kx/Wz Kx];
			den = [1/Wp 1 0];
		else,
			disp('Second Order, Type 2 with K*1E5');
			v = [-40 1 -7 1];
			Kx = 100000*K;
			num = [Kx/Wz Kx];
			den = [1 0 0];
		end % if typ
	else,
		disp('zp - Type 2 plus zero then pole');
		disp('ph - Type 1 plus pole, pole (Fig. 5.10), high gain');
		disp('pl - Type 1 plus pole, pole (Fig. 5.10), low gain');
		disp('zh - Type 1, 2 poles, 2 zeros (Fig. 5.11), high gain');
		disp('zm - Type 1, 2 poles, 2 zeros (Fig. 5.11), medium gain');
		disp('zl - Type 1, 2 poles, 2 zeros (Fig. 5.11), low gain');;
		typ = input('Enter zp, ph, pl, zh, zm or zl: ','s');
		if strcmp(typ,'zp')
			disp('Third Order, Type 2 with pole<->zero, K*1E4');
			v = [-20 1 -20 1];
			Kx = K*10000;
			num = [Kx/Wp Kx];
			den = [1/Wz 1 0 0];
		elseif strcmp(typ,'ph') | strcmp(typ,'pl'),
			disp('Type 1 with poles at Wz and Wp');
			if strcmp(typ,'ph'),
				Kx = K*200;
				disp('high gain');
			else,
				Kx = K*50;
				disp('low gain');
			end % if p
			v = [-3.5 .5 -1 .5];
			num = [Kx];
			den = [1/(Wz*Wp) (1/Wz+1/Wp) 1 0];
		else,
			disp('Type 1 with poles at Wp/1.5 & Wp and zeros higher by 5');
			v = [-3.5 .5 -1 .5];
			rpp = 1.5; % Wp2/Wp1 = Wz2/Wz1, Wp2 = Wp
			rzp = 5; % Wz1/Wp2
			rgg = 10; % gains, high/med = med/low
			Kmed = 1400;
			if strcmp(typ,'zh'), 
				Kx = rgg*Kmed;
				disp('high gain');
			elseif strcmp(typ,'zl'), 
				Kx = Kmed/rgg;
				disp('low gain');
			else % if strcmp(typ,'zm'), 
				Kx = Kmed;
				disp('medium gain');
			end % if z
			num = Kx*[1/(rzp^2*rpp*Wp^2)  (1+1/rpp)/(rzp*Wp)  1];
			den = [rpp/Wp^2  (1+rpp)/Wp  1  0];
		end % if typ
	end % if order
	go = 1;
	while(go == 1) % SELECT PLOT TYPE
		disp('bode, nyquist, evans, change loop or quit?');
		ctrl = input('Enter b, n, e, c, or q: ', 's');
		clg;
		if strcmp(ctrl,'e')|strcmp(ctrl,'evans'),
			rlocus(num,den);
		elseif strcmp(ctrl,'b')|strcmp(ctrl,'bode'),
			bode(num,den,f);
		elseif strcmp(ctrl,'n')|strcmp(ctrl,'nyquist'),
			ny = 1;
			vnow = v;
			while(ny == 1) % NYQUEST LOOP TO PERMIT ZOOMING
				axis(vnow); %****move down in v. 4.
				nyquist(num,den,f);
				xpnd = input('zoom or continue? ','s');
				if strcmp(xpnd,'z')|strcmp(xpnd,'zoom')
					xp = input('enter expansion factor ');
					vnow = vnow / xp;
				else
					ny = 0;
				end % if xpnd
			end % while ny
			axis; axis; %****change to axix('auto') in v. 4.
		elseif strcmp(ctrl,'q')|strcmp(ctrl,'quit'),
			go = 0;
		elseif strcmp(ctrl,'c')|strcmp(ctrl,'change'),
			go = 2;
		end % if ctrl
	end % while go = 1
end % while go~=0
