% function for simc tuning
% input: 1st order model + tuning parameter
% returns: simc tuning paramer for a PI controller on the form gc = K + I/s

function [tuning, gc] = simc(gp,tc)

% determining the model

t = gp.den{1}(1);
d = totaldelay(gp);
g = gp.num{1}(end);
z = zero(gp);
p = pole(gp);

if nargin == 1
    tc = d;
end

if length(gp.den{1}) > 2
    disp('model order to high')
    return
elseif isempty(z) == 0
    disp('model cannot contain zeros')
    return
end

if p == 0 % integrating process
    K = t/(g*(d+tc));
    I = K/(4*(tc+d));
    %disp('i')
elseif isempty(p)==1; % pure delay process
     K = 0;
     I = 1/g/(tc+d);
     %disp('p')
else
    K = t/(g*(tc+d));
    I = K/min(t,4*(tc+d));
    %disp(['norm, tau=', num2str(t)])
end
   

tuning = [K;I];
gc = controller(K,I);

return