function J = MCHE_obj_temp(x,d,parameters)

% Calculate objective for optimization of MCHE model (MCHE_tempspecs.usc)
global h

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

sol.CanSolve = 0 ;

x = parameters.scale_x*x ;

%Reset optimization variables

f.MaterialStreams.Item('MR10').PressureValue = x(1) ;
f.MaterialStreams.Item('MR17').PressureValue = x(2) ; 
f.MaterialStreams.Item('MR10').MolarFlowValue = x(3) ;
f.MaterialStreams.Item('NG5').TemperatureValue = x(4) ;
f.MaterialStreams.Item('MR12').TemperatureValue = x(5) ;
f.MaterialStreams.Item('MR15').TemperatureValue = x(6) ;
f.MaterialStreams.Item('Subcooled NG').TemperatureValue = x(7) ;
f.MaterialStreams.Item('MR16').TemperatureValue = x(8) ;
f.MaterialStreams.Item('C2').MolarFlowValue = x(9) ;
f.MaterialStreams.Item('C3').MolarFlowValue = x(10) ;
f.MaterialStreams.Item('N2').MolarFlowValue = x(11) ;

%Reset 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 ;
if f.Operations.Item('MCHE, cold part').Duty.IsKnown == 1 
J = f.Operations.Item('K-100').EnergyValue/100 ;
elseif f.Operations.Item('MCHE, cold part').Duty.IsKnown == 0
J = 1e5 ;
end

