function y = stateToOutput(x,u,d,p)
ME = x(1);    %[mol] mass of organic (extract) layer (acid, solvent, water)
MR = x(2);   %[mol] mass water (raffinate) layer (assumed to be pure water)
xAE = x(3);      % [-] mole fractions of acid in organic layer  (extract E)
xWE = x(4);     % [-] mole fractions of water in organic layer  (extract E)

F = u;                                        % [mol/s] Water and acid feed

S = d(1);                                    % [mol/s] organic solvent feed
xAF = d(2);                        % [-] mole frocation of acid in feed (F)

K = p(1);                  % [-] water acid equilibrium constant in extract
Kc1 = p(2);               % [1/s] proportional gain for holdup p-controller
Kc2 = p(3);     % [1/s] proportional gain for raffinate holdup p-controller

E0 = p(4);                  % [mol/s] steady state bias for extrate outflow
R0 = p(5);                % [mol/s] steady state bias for raffinate outflow

MEs = p(6);                              % [mol] extrate inventory setpoint
MRs = p(7);                            % [mol] raffinate inventory setpoint

structure = p(8);

CVtransform = p(9);

deltaE = Kc1*((MEs-ME)+(MRs-MR));
deltaR = Kc2*(MRs-MR);

E = E0 + deltaE;
R = R0 + deltaR;


% Transform the 
if CVtransform == 0
    y = xAE;
else
    switch structure
        case 1
            y = xAF*F/E;
        case 2
            y = 1/(1+K)*(F-R)/E;
        case 3
            y = 1/(1+K)*(F-R)/(F-R+S);
        case 4
            y = xAE;
        case 5
            y = xAE;
        case 6
            y = xAE;
        case 7
            y = xAF*F/E;
        case 8
            y = 1/(1+K)*(F-R)/(F-R+S);
        case 9
            y = xAF*F/E;
        case 10
            y = 1/(1+K)*(F-R)/(F-R+S);
        case 11
            y = xAE;
        otherwise
            y = xAE;
    end
end
end