function dTdt = Tank(x,u,p,d)
% model for tank with heating element

%parameters
V   = p(1);                                              % [m3] tank volume
rho = p(2);                                               % [m3/kg] density
cp  = p(3);                                       % [kJ/kg/K] specific heat

%states
T = x;                                               % [K] tank temperature

% switch inputs to change between cases
MV  = p(6);                              % switch between MVs; 1:u=q; 2:u=Q

switch MV                                          % select if q or Q is MV
    case 1                                               % q is MV, Q is DV
        q = u;
        Q = d(2);                                     % [kW] heat flow rate
    case 2                                               % q is DV, Q is MV
        q = d(2);                                        % [m3/s] flow rate
        Q = u;
    otherwise
end

% other disturbances
T0 = d(1);

% energy balance
dTdt = q/V*(T0-T) + Q/(V*rho*cp);                            % energy balance

end