clc
clear all

load Data

Kc0 = -0.1;
dy_s = 1;
t_init = 200;
dt = 0.1;

t = Data(:,1);
r = Data(:,2);
y = Data(:,3);
u = Data(:,4);
%%
figure(1)
clf
subplot(2,1,1)
plot(t,r,'r',t,y,'k','Linewidth',1.5)
xlim([0,1000])
title('Inlet pressure(controlled variable)')
ylabel('P [Kpa]')
legend('Setpoint','Data',2)
grid on
hold on

subplot(2,1,2)
plot(t,u,'k','Linewidth',1.5)
xlim([0,1000])
xlabel('time(sec)')
ylabel('Z')
title('Valve position (manipulated variable)')
grid on


%%
i_init = find(t==t_init);
y_plant = y(i_init:end);
t_plant = t(i_init:end);
u_plant = u(i_init:end);
y_init = y(i_init-200);    
u_init = u(i_init-200);
yp = max(y_plant);    
dy_p = abs(yp - y_init);
i_yp = find(y_plant==yp);
t_p1 = mean(t_plant(i_yp));
yu = min(y_plant(i_yp:10*i_yp));
dy_u = abs(yu - y_init);
i_yu = find(y_plant==yu);
t_u = mean(t_plant(i_yu));
%dy_inf = 0.45*(dy_p1+dy_u);
y_inf = y_plant(end);
dy_inf = abs(y_inf - y_init);
tp = t_p1 - t_init;



Overshoot = abs((dy_p-dy_inf)/dy_inf);
D = Overshoot
Offset = abs((dy_s-dy_inf)/dy_inf)
B = Offset;
A = 1.152*D^2 - 1.607*D +1;
r = 2*A/B;
K = 1/(Kc0 * B);
Tetha = tp*(0.309 + 0.209*exp(-0.61*r));
tau1 = r*Tetha;
Kc = tau1/(K*2*Tetha)
tauI = min (tau1, 8*Tetha)
Tau_c=Tetha



