function sJ = objective_3(x)

h = actxserver('UnisimDesign.Application');     %creating active x server, a link between matlab and unisim
uniCase = h.ActiveDocument;                     %entering document in unisim
f = uniCase.Flowsheet;                          %entering flowsheet in unisim
sol = uniCase.Solver;                           %entering solver in unisim

sol.CanSolve = 0;

xscaled = x;

notscaled = scaleback_3(xscaled);

f.MaterialStreams.Item('MR1').MassFlowValue = notscaled(1); %Mlar flow, M1
f.MaterialStreams.Item('1').TemperatureValue = notscaled(2);  %Temperature after before turbine, MR2
f.MaterialStreams.Item('MR3').PressureValue = notscaled(3); % Pressure after choke valve
N = notscaled(4);   % compressor speed

%Calculating P2 and compressor efficiency  

m = notscaled(1);
N0 = 0.8; 
P1 = 450000; %Pa
T1 = 284; %K
W0 = 0.0698;
MW = 0.032; %kg/mol  
Rhat = 8.314/MW;
D = 1.7; %meters
H0 = 18.125;
Pr0 = -29;
eta0 = 0.822;

W = W0*(N/N0)^(1/3);
mr2 = m*sqrt(Rhat*T1)/(D^2*P1);
H = H0-1.2*(H0+Pr0/2-1)*(1-N/N0);
Pr = Pr0+H*(1+(3/2)*(mr2/W-1)-0.5*(mr2/W-1)^3);
P2 = Pr*P1;
eta = eta0*((1-((H-H0)/H0)^2)-1000*(mr2-2*W)^2);

if P2<5.5e+4 || eta<0.01 
   
    m = 550 ; N = 0.5;
    W = W0*(N/N0)^(1/3);
    
    mr2 = m*sqrt(Rhat*T1)/(D^2*P1);
    H = H0-1.2*(H0+Pr0/2-1)*(1-N/N0);
    Pr = Pr0+H*(1+(3/2)*(mr2/W-1)-0.5*(mr2/W-1)^3);
    P2 = Pr*P1;
    eta2 = eta0*((1-((H-H0)/H0)^2)-1000*(mr2-2*W)^2);
    
    f.MaterialStreams.Item('MR5').PressureValue = P2*1e-3;
    f.Operations.Item('Compressor').CompAdiabaticEffValue = eta2*100;

    sol.CanSolve = 1;

    sJ = 100;
   
else
    f.MaterialStreams.Item('MR5').PressureValue = P2*1e-3;
    f.Operations.Item('Compressor').CompAdiabaticEffValue = eta*100; 
    
    sol.CanSolve = 1;
        
    if f.EnergyStreams.Item('Energy').Power.IsKnown == 1 && f.MaterialStreams.Item('MR4').VapourFractionValue == 1
            
            J = f.Operations.Item('SPRDSHT-1').Cell('A2').CellValue; 
            sJ = J*1e-6;
        else
            sJ = 100;
    end
end




