function [gp kp tp theta] = omodel(kc0,y,r,t,type)

% ------------------------------------------------------------%
%                                                             %
%      --  Function for Overshoot Model Approximations --     %
%                                                             %
% Determins an 1st order model from a closed-loop step change %
% Stepchange must occure after t=0                            %
%                       written by: Chriss Grimholt 19/10-10  %
% ----------------------------------------------------------- %

dyinf = y(end)-y(1);

[rs rs_i] = max(r);

dr = rs-r(1);

[yp yp_i] = findpeaks(y);

dyp = yp(1)-y(1);

typ = t(yp_i(1))-t(rs_i(1));

b = dyinf/dr;

S = (dyp-dyinf)/dyinf

A = 1.152*S^2-1.607*S+1;

% Calculating the 1st order process model parameters with different
% relations

if type == 1
    tp_theta = 2*A*abs(b/(1-b));
    theta = (0.209*exp(-0.610*tp_theta)+0.309)*typ;
    tp = tp_theta*theta;
    kp = (1/kc0)*abs(b/(1-b));
elseif type == 2;
    theta = 0.43*typ;
    kp = (1/kc0)*abs(b/(1-b));
    tp = 2*A*theta*abs(b/(1-b));
elseif type == 3;
    theta = 0.305*typ;
    kp = (1/kc0)*abs(b/(1-b));
    tp = 2*A*theta*abs(b/(1-b));
elseif type == 4
    kp = (1/kc0)*abs(b/(1-b));
    tp = 0.86*A*abs(b/(1-b))*typ;
    theta = 0.305*typ;
end

% Creating the process tf gp

s=tf('s');
gp = kp/(tp*s+1);
gp.outputd = theta;

end
