function [ Tout1 ,W,hout,hin] = compressor( Pin,Tin,Pout,x,N)
%This function corresponds to the recursive routine to calculate the
%compressor. This function uses a simplified isoenthropic model.
% % Input 
% Pin = Inlet pressure [Pa]
% Tin = Inlet temperature [K]
% Pout = Outlet pressure [Pa]
% x = Inlet molar composition 
% N = Inlet molar flow [Kmol/s]
% % Output
% Tout1 = Outlet temperature [K]
% W = Compressor work [Kj/s] 
% hout = Outlet molar enthalpy [Kj/Kmol]   
% hin = Inlet molar enthalpy [Kj/Kmol]

% % First estimation of the average Cp
c=cpav(Tin,x); % Calculates the average Cp [Kj/K] for a given mixture at a especified temperature.    
R = 8.314; % Universal gas constant [Kj/KmolK]
Tout1=Tin*(Pout/Pin)^(R/c); %Initial calculation for the output temperature
b=1; % Initial value for difference 
n=0.74; %Compressor efficiency

% % Loop to find a better estimate of the themperature. 
while b > 1e-6 %Tolerance between calculations 1e-6 
Ta=(Tin+Tout1)/2; % Average temperature
c=cpav(Ta,x); % Cp for average temperature
Tout=Tin*(Pout/Pin)^(R/c); % New estimate
b=abs(Tout1-Tout); % Difference between new and old
Tout1=Tout; % Overwrite last estimate
end

%Once the temperature is set, calculates enthalpy for in and outlet
%conditions.
[Zi,phii,Hi,Vi]= srks(x,Tin,Pin,2);
[Zo,phio,Ho,Vo]= srks(x,Tout1,Pout,2);

%Compressor work is calculated
W=N*((Ho-Hi)/n);

%Enthalpies extracted
hout=Ho;
hin=Hi;
end

