function Q1 = ChainTransf(x,u,d,p,v2)

%% Parameters
F     = p(1);                                                    % flowrate
m1    = p(2);                                           % hold up in tank 1
m2    = p(3);                                            % holdup in tank 2
cp    = p(4);                                               % specific heat
tau01 = p(5);                                 % tuning parameter for tank 1
tau02 = p(6);                                 % tuning parameter for tank 2

%% States
T1 = x(1);                                          % temperature in tank 1
T2 = x(2);                                          % temperature in tank 2

%% Disturbances
T0 = d(1);                                    % inlet temperature to tank 1
Q2 = d(2);                                                 % heat to tank 2

% Calculate inputs
v1 = v2*m2/(tau02*F) + T2*(1-m2/(tau02*F))-Q2/(F*cp); % =T1 at steady-state
Q1 = cp*(m1/tau01*v1-F*T0 + T1*(F-m1/tau01));      % input (heat to tank 1)

end