%                                                                   
%   BODEDEMO2.M  -  This demo gives an example of some frequency response plots 
%                       for  H(s) = (s+0.1)/[s(s+1)] = b(s)/a(s)       
%
%   This file uses Maltab's Bode, Nyquist, and Nichols commands to plot the 
%   frequency response for the above system (and its individual components).
%
%   A related file, BODEDEMO1.M, does the same thing, but all the plots are done 
%   manually (i.e. explicitly by the user).  In this file, we let Matlab produce its
%   own default plots (no left hand arguments are used in the Bode, Nyquist, and
%   Nichols commands).  Once you understand the process, this is clearly the easier 
%   path to follow!
%
%   File prepared by J. R. White, UMass-Lowell (last update: Feb. 2020)
%

      clear all,  close all,   nfig = 0;
%
%   set frequency range (logarithmic spacing)       
      w = logspace(-2,2,160);       
%                  
%   Bode plot for the s+.1 factor                       
      b = [1 0.1];  a = 1;  sys1 = tf(b,a);
      nfig = nfig+1;   figure(nfig)                     
      subplot(2,2,1),bode(sys1,'r',w),grid,title('H(s) = s+0.1')                         
%
%   Bode plot for the 1/s factor                
      b = 1;  a = [1 0];   sys2 = tf(b,a);        
      subplot(2,2,2),bode(sys2,'b',w),grid,title('H(s) = 1/s')            
%
%   Bode plot for the 1/(s+1) factor      
      b = 1;  a = [1 1];  sys3 = tf(b,a); 
      subplot(2,2,3),bode(sys3,'g',w),grid,title('H(s) = 1/(s+1)')  
%
%   Bode plot for the composite function:  H(s) = (s+.1)/[s(s+1)] = b(s)/a(s) 
      b = [1 0.1];  a = [1 1 0];  sys = tf(b,a);        
      subplot(2,2,4),bode(sys,'k',w),grid,title('H(s) = (s+0.1)/[s(s+1)]')   
%   
%   set all the curves to have a larger linewidth
      h = findobj(gcf,'LineStyle','-'); nh = length(h); 
      for i = 1:nh; set(h(i),'LineWidth',2); end
%     
%   the individual factors and the composite could also be plotted on a single plot
      nfig = nfig+1;   figure(nfig)
      bode(sys1,'r',sys2,'b',sys3,'g',sys,'k'),grid
      h1 = findobj(gcf,'Color','r'); nh1 = length(h1); 
      for i = 1:nh1; set(h1(i),'LineWidth',2); end
      h2 = findobj(gcf,'Color','b'); nh2 = length(h2); 
      for i = 1:nh2; set(h2(i),'LineWidth',2); end
      h3 = findobj(gcf,'Color',[0 0.65 0]); nh3 = length(h3); % would not find 'g'???
      for i = 1:nh3; set(h3(i),'LineWidth',2); end
      h4 = findobj(gcf,'Color','k'); nh4 = length(h4); 
      for i = 1:nh4; set(h4(i),'LineWidth',2); end
      legend([h1(1),h2(1),h3(1),h4(1)],'s+0.1','1/s','1/(s+1)','(s+0.1)/[s(s+1)]', ...
             'Location','East')  
%                 
%   Nichols and Nyquist plots 
      nfig = nfig+1;   figure(nfig)
      subplot(1,2,1),nichols(sys,'r.',w),grid   % Nichols -- log-mag vs angle      
      subplot(1,2,2),nyquist(sys,'b.',w),grid   % Nyquist -- Im vs Re   
%
%   end of example