function sc = scale(x, d, unit)
if unit=='com'
  sc = scale_compressor(x, d);
elseif unit=='gco'
  sc = scale_GasCooler(x, d);
elseif unit=='val'
  sc = scale_valve(x, d);
elseif unit=='vap'
  sc = scale_evaporator(x, d);
elseif unit=='ihx'
  sc = scale_InternalHX(x, d);
end

function out = scale_compressor(x, d)
out.x = abs(x);
A = co2_sw([x(3:4);d.Ni]);
s = s_given_TVN([x(3:4);d.Ni]);
P = -A.g(2);
out.res = abs([x(1);
               d.eta;
               s;
               P]);
               
function out = scale_GasCooler(x, d)
out.x = abs(x);
out.res = abs([x(2*d.ngco+1:3*d.ngco);
               x(2*d.ngco+1:3*d.ngco);
               x(2*d.ngco+1:3*d.ngco);
               x(end-1)*ones(d.ngco,1)]);

function out = scale_valve(x, d)
out.x = abs(x);
out.res = abs([x(end);
               x(2)]);

function out = scale_evaporator(x, d)
out.x = abs(x);
out.res = abs([x(5);
               x(5);
               x(end);
               x(end);
               my_given_TVN([x(1); x(4); x(2)]);
]);

function out = scale_InternalHX(x, d)
out.x = abs(x);
Q = x(2*d.nihx+1:3*d.nihx);
P1 = P_given_TVN([x(1); d.Vihx1/d.nihx; x(d.nihx+1)]);
P2 = P_given_TVN([x(3*d.nihx+1); d.Vihx1/d.nihx; x(4*d.nihx+1)]);
out.res = abs([Q;
               Q;
               Q;
               P1*ones(d.nihx, 1);
               P2*ones(d.nihx, 1)]);
