% plant example according to K.G. Papadopoulos, PID Controller Tuning 
% Using the Magnitude Optimum Criterion, Springer, 2015, p. 106
% here: ideal PID structure, no consideration of non-ideal D-terms

Vs4=1; nump4=1; Td4=5;
denop4=conv([1  0],conv([1 1],conv([1 1],conv([1 1],conv([1 1],[1 1])))));
FSdefault=optimset('fsolve');
OPT=optimset(FSdefault,'Display','off','Diagnostics','off','TolFun',1e-24,...
                 'Algorithm','trust-region-reflective','FinDiffType','forward',...
                 'TolX',1e-24);
% Algorithm for fsolve must be: 'trust-region-reflective',
%  'levenberg-marquardt', 'trust-region-dogleg',
% FinDiffType: must be 'forward' or 'central'

A2=1;i=0;
while (A2(1)>1.0e-16)
      [Rdex4PID,A2,Flag]=bo_d_gen(13,Vs4,nump4,denop4,Td4,39,[],OPT);
      i=i+1;
end
Rdex4PID, A2, Flag, i
if Flag ==1
% check fulfillment of MO basic equation system
PLT4a=tf(Vs4*nump4,denop4); % plant without dead time
      Tt_num=[-(1/120)*Td4^3  0.1*Td4^2  -0.5*Td4  1]; % approx. pade 3
      Tt_den=[+(1/120)*Td4^3  0.1*Td4^2   0.5*Td4  1]; % (code 23 below)
PLT4b=tf(Tt_num,Tt_den);    % dead time separatly as approx.
PLT4=series(PLT4a,PLT4b);
CON4=tf([Rdex4PID(4)*Rdex4PID(2)  Rdex4PID(2)  1],[0 Rdex4PID(3)  0]);% PID
FIL4=tf(1,[Rdex4PID(4)*Rdex4PID(2)   Rdex4PID(2)   1]);
order=3;
disp('Accuracy check for fulfillment of MO basic equation system:'),
[C1,C2,C3,C4]=bo_check(3,PLT4,CON4,FIL4),
else disp('Check rejected since Flag~=1, repeat computation till Flag==1'),
end

disp('"Pp_t2ex4_sim.slx" will be called - compare parameters')
Pp_t2ex4_sim; sim('Pp_t2ex4_sim',300),
disp('Repeat bo_d_gen call at MATLAB promt, if search algorithm fails')
