% This function takes in an array of parameters, calculates the
% nominal values for the 3 tanks: flow rate and concentration of acid, flow
% rate of base, flow rate of outlet stream and residence times. These
% values are returned in arrays collected in a matrix nom. 

function nom = nomcalc(setpoints,par_nom)

% Exctracting the parameters:
cin_nom(1) = par_nom(2);
qin_nom(1) = par_nom(3);
cr_nom = par_nom(4);
c_nom(1) = setpoints(1);
c_nom(2) = setpoints(2);
c_nom(3) = setpoints(3);
V = par_nom(1); 

% Calculating nominal values and for Tank 1: flow rate of base, outlet flow
% rate, and the residence time tau.
    qr_nom(1) = (cin_nom(1) - c_nom(1))/(c_nom(1)-cr_nom)*qin_nom(1);

    q_nom(1) = qin_nom(1)+qr_nom(1); %Outlet flow rate=acid+base flow rates

    tau(1) = V/q_nom(1); 

% Calculating nominal values for Tank 2 and 3: flow rate of base and acid,
% acid concentration and outlet flow rate, and the residence time.
for  i=2:3
    cin_nom(i) = c_nom(i-1);    %Acid conc.=conc. of outlet stream from prev.tank
    qin_nom(i) = q_nom(i-1);    %Acid flow=outlet flow rate from prev.tank

    qr_nom(i) = (cin_nom(i) - c_nom(i))/(c_nom(i)-cr_nom)*qin_nom(i);

    q_nom(i) = qin_nom(i)+qr_nom(i);%Outlet flow rate=acid+base flow rates

    tau(i) = V/q_nom(i);

end

% Creating a matrix nom with the arrays of nominal values.
nom = [qr_nom ; q_nom ; cin_nom ; qin_nom ; tau];