
% This function takes in an array of parameters
% nomval=[qr_nom, q_nom, cin_nom, qin_nom, c_nom],
% cr_nom and the setpoints.
%
% It calculates the unscaled steady-state gains for different disturbances 
% and the control input gains for each tank, which is returned in the
% matrix kval.

function kval = kcalc(nomval,cr_nom,sp)


% Nominal values:
qr_nom = nomval(1,:);
q_nom = nomval(2,:);
cin_nom = nomval(3,:);
qin_nom = nomval(4,:);
c_nom = [sp(1) sp(2) sp(3)];


% Calculating steady-state gains for different disturbances and the gain 
% from the control input for Tank 1-3, collecting the values in 1 by 3
% arrays:

for i=1:3
    
kd_cin(i) = qin_nom(i)/q_nom(i);                % When d = c_in
kd_qin(i) = (cin_nom(i)-c_nom(i))/q_nom(i);     % When d = q_in
kd_cr(i) = qr_nom(i)/q_nom(i);                  % When d = c_r
kd_qr(i) = (cr_nom-c_nom(i))/q_nom(i);          % When d = q_r

k(i) = (cr_nom-c_nom(i))/q_nom(i);              % Controller

end

k_tank1 = [kd_cin(1)  kd_qin(1)  kd_cr(1)  kd_qr(1)  k(1)];
k_tank2 = [kd_cin(2)  kd_qin(2)  kd_cr(2)  kd_qr(2)  k(2)];
k_tank3 = [kd_cin(3)  kd_qin(3)  kd_cr(3)  kd_qr(3)  k(3)];


% Creating a matrix kval with the vectors of disturbance gains, control
% input gains and tau for each tank. This is what the function returns.
kval = [k_tank1 ; k_tank2 ; k_tank3];

