clc;close all;clear

blueHC = [0 68 136]/255; blue = [68 119 170]/255; cyan = [102 204 238]/255;
pos   = [100 100 750 600]; % figure position on screen
s=tf('s');
opt = timeoptions; opt.TickLabel.FontSize=18; opt.XLabel.FontSize = 18;
opt.YLabel.FontSize = 18; opt.Title.String = {};

bopt = bodeoptions; bopt.TickLabel.FontSize=18; bopt.XLabel.FontSize = 18;
bopt.YLabel.FontSize = 18; 
bopt.MagUnits = 'abs'; bopt.PhaseUnits = 'deg'; bopt.Grid = 'on';
bopt.FreqUnits = 'Hz'; bopt.MagScale = 'log'; bopt.Title.String = {};


% SIMC-tunings for integrating process, k’ * Kc * taui = 4. MV response 
taui  = 2; Kc = 4/taui;
g = (taui*s+1)/((taui/Kc)*s^2 + taui*s + 1);
N=10;
L=g^N;
figure(1); step(L,opt);
set(findall(gcf,'type','line'),'linewidth',4);
set(findall(gcf,'type','line'),'Color','k');
set(gcf,'Position',pos)

figure(2); step((1/(1-0.9*L)),100,opt) % unstable for N>10 because of “fast” ringing
set(findall(gcf,'type','line'),'linewidth',4);
set(findall(gcf,'type','line'),'Color','k');
set(gcf,'Position',pos)

figure(3); bodeplot(-0.9*L,bopt)
set(findall(gcf,'type','line'),'linewidth',4);
set(findall(gcf,'type','line'),'Color','k');
set(gcf,'Position',pos)
% test if get problems with different tunings (Krister’s point)
taui  = 1; Kc = 4/taui; %
g1 = (taui*s+1)/((taui/Kc)*s^2 + taui*s + 1);
taui  = 4; Kc = 4/taui;
g2 = (taui*s+1)/((taui/Kc)*s^2 + taui*s + 1);
L=g1^7*g2^8;  % well, now get instability at N=15, so much better now
figure(4); step((1/(1-0.9*L)),100,opt) % unstable for N>15 because of “fast” ringing
set(findall(gcf,'type','line'),'linewidth',4);
set(findall(gcf,'type','line'),'Color','k');
set(gcf,'Position',pos)

figure(1); print('-depsc2','-r600','stepL.eps')
figure(2); print('-depsc2','-r600','stepL2.eps')
figure(3); print('-depsc2','-r600','bode.eps')
figure(4); print('-depsc2','-r600','stepL3.eps')
  
