%SPECIAL THIRD ORDER LOOP RESPONSES
%refer to Fig. 10.A.4, 10.A.6, 10.A.9, 10.A.10.
%  Choose response and parameters dynamically.
 
r2deg = 180/pi; % for use in changing radians to degrees
w = logspace(-1,1,51); % Frequency points
t = linspace(0,10,50); % Time points
WL = 1 % Normalized to Unity Open-Loop Gain

r = 10;
while (r >= 0)
	rtemp = r;
	r = input('Enter freq. ratio, r=(unity gain)/(added pole), r<0 for quit: ');
	if (isempty(r)), 
		r = rtemp
	end; % if empty
	if (r >= 0),
 	out = [WL^2*r  WL^3]; % from Eq. (10.A.8)
 	er = [1  WL*r  0  0]; % = den - out
		rer = [1  WL*r  0]; % derivative of er
 	den = [1  WL*r  WL^2*r  WL^3];

		disp('');
		disp('o=Step Output');
		disp('e=Step Error');
		disp('r=Ramp Error');
		disp('f=Frequency Response');
		typgr = input('Enter o, e, r, or f. ', 's');
		clf;
 	if strcmp(typgr,'e')
			step(er,den,t);
			grid;
		elseif strcmp(typgr,'o')
			step(out,den,t);
			grid;
		elseif strcmp(typgr,'r')
			step(rer,den,t);
			grid;
		else
 		hout = freqs(out, den, w);
 		magout =  20*log10(abs(hout));
 		phout = r2deg*angle(hout);
 		her = freqs(er, den, w);
 		mager = 20*log10(abs(her));
 		pher = r2deg*angle(her);
			subplot(211), semilogx(w, magout, 'r', w, mager, '--r')
 		grid
 		title('Frequency Response Magnitude.  solid = output   dashed = error')
 		xlabel('Omega / Omega-n')
 		ylabel('dB')
 		subplot(212), semilogx(w, phout, 'b', w, pher, '--b')
 		grid
 	 title('Frequency Response Phase.  solid = output   dashed = error')
 		xlabel('Omega / Omega-n')
 		ylabel('degrees')
		end % if typgr
	end % if r
end % while
