function varargout = recycles(T,x,d,parameters)

global h

hyCase = h.Activedocument;
sol=hyCase.Solver;
f = hyCase.Flowsheet;

sol.CanSolve = 0 ;

x = [x ; T] ; % Put the temperatures back into the temperature vector

x = parameters.scale_x*x ; % Scale the x vector

%Make sure recycles are ignored
f.Operations.Item('RCY-2').IsIgnored = 1;
f.Operations.Item('RCY-3').IsIgnored = 1;

% Give variable values to Unisim
f.MaterialStreams.Item('MR10').PressureValue = x(1) ;
f.MaterialStreams.Item('MR17').PressureValue = x(2) ; 
f.MaterialStreams.Item('MR10').MolarFlowValue = x(3) ;
f.MaterialStreams.Item('C2').MolarFlowValue = x(4) ;
f.MaterialStreams.Item('C3').MolarFlowValue = x(5) ;
f.MaterialStreams.Item('N2').MolarFlowValue = x(6) ;
f.MaterialStreams.Item('MR16a').TemperatureValue = x(7) ;
f.MaterialStreams.Item('MR19a').TemperatureValue = x(8) ;

%Disturbances
f.MaterialStreams.Item('NG4').TemperatureValue = d(1) ; % degrees C
f.MaterialStreams.Item('NG4').PressureValue = d(2) ; % kilopascal
f.MaterialStreams.Item('NG4').MolarFlowValue = d(3); %mol/s
f.MaterialStreams.Item('MR10').TemperatureValue = d(1);

sol.CanSolve = 1 ;

% Retrieve current error
if f.Operations.Item('MCHE, cold part').Duty.IsKnown==1
res = [ f.MaterialStreams.Item('MR16').TemperatureValue - f.MaterialStreams.Item('MR16a').TemperatureValue ; 
        f.MaterialStreams.Item('MR19').TemperatureValue - f.MaterialStreams.Item('MR19a').TemperatureValue ] ; 
else
    res = [100 ; 100];
end

switch parameters.solver
    case('fsolve')
        varargout={res};
	case('lsqnonlin')
        norm = res'*res ;
        varargout = {norm} ;
    case('fmincon')
        c=[];
        varargout={c,res} ;
end
end