In [3]:

```
# Variables
# Part (a)
P1 = 100.
P2 = 50.;
T1 = 273.+300;
# Calculation and Results
T2 = (P2/P1)*T1;
R = 0.287
cv = 0.718;
V1 = 0.8;
m = (P1*V1)/(R*T1);
Q = m*cv*(T2-T1);
print "If the fluid is in the air"
print "The final temperature is ",T2,"K"
print "The heat transferred is %.2f kJ/Kg"%Q
# Part (b)
t2 = 273+81.33
vf = 0.00103
vg = 3.24;
v1 = 2.6388
u1 = 2810.4;
x2 = (v1-vf)/(vg-vf);
u2 = 340.42+(x2*2143.4);
m_ = V1/v1;
Q_ = m_*(u2-u1);
print ("\nIf the fluid is in the steam")
print "The final temperature is ",t2,"K"
print "The heat transferred is %.2f kJ/Kg"%Q_
# rounding off error. please check . value of m is wrong in book please check.
```

In [3]:

```
# Variables
# Part (a)
R = 0.287
T1 = 273.+150
v1 = 0.96
v2 = 1.55
Cp = 1.005;
# Calculation and Results
P = (R*T1)/v1;
W = P*(v2-v1);
T2 = (v2/v1)*T1;
Q = Cp*(T2-T1);
print ("If the fluid is in the air")
print "The final temperature is %.2f K"%T2
print "The heat transferred is %.2f kJ"%Q
print "Work done is %.2f kJ"%W
# Part (b)
vg = 0.3928;
P1 = 200e03;
P2 = P1;
h1 = 2768.8;
t2 = 273+400; h2 = 3276.5;
Q_ = h2-h1;
W_ = P1*(v2-v1);
print ("\nIf the fluid is in the steam")
print "The final temperature is ",t2,"K"
print "The heat transferred is",Q_,"kJ"
print "Work done is",W_/1000,"kJ"
```

In [8]:

```
import math
# Variables
v = 16. ; # v = v1./v2
P1 = 300e03;
P2 = P1*v;
R = 0.287
T1 = 300+223;
# Calculation and Results
W12 = R*T1*math.log(1./v);
print ("If the fluid is in the air")
print "The final pressure is ",P2/1000,"kPa"
print "The change in internal energy is",0,"kJ"
print "Work done is %.2f kJ"%W12
# Part (b)
v1 = 0.7664; u1 = 2728.7;
v2 = v1/16;
x2 = (v2-0.00125)/(0.05013);
s2 = 2.7927+(x2*3.2802);
s1 = 7.5165
u2 = 1080.37+(x2*1522.0);
du = u2-u1;
T = 250+273;
Q12 = T*(s2-s1)
print ("\nIf the fluid is in the steam")
print "The final temperature is ",T,"K"
print "The heat transferred is",Q12,"kJ/kg"
print "The change in internal energy is",du,"kJ/kg "
# rounding off error. please check. value of x2 is wrong in book .
```

In [6]:

```
# Variables
# Part (a)
P1 = 10.
P2 = 1.;
T1 = 273.+300;
g = 1.4;
# Calculation and Results
T2 = T1*((P2/P1)**((g-1)/g));
R = 0.287;
W12 = ((R*T1)/(1-g))*(T2/T1-1);
v2 = (R*T2)/(100*P2);
print ("If the fluid is in the air")
print "The specific volume is %.3f m3/Kg"%v2
print "The work done per kg of the fluid is %.2f kJ"%W12
# Part (b)
u1 = 2793.2
v1 = 0.2579
s1 = 7.1228;
x2 = (7.1228-1.3025)/6.0568;
u2 = 417.33+(x2*2088.7);
v2 = 0.001043+(0.96*1.693);
W12 = u1-u2;
print ("\nIf the fluid is in the steam")
print "The specific volume is %.3f m3/Kg"%v2
print "The work done per kg of the fluid is %.1f kJ"%round(W12,-1)
# note : rounding off error. please check.
```

In [7]:

```
# Variables
# Part (a)
P1 = 10.
P2 = 1.;
T1 = 273.+200
n = 1.15
R = 0.287;
# Calculation and Results
v2 = ((R*T1)/(P1*100))*((P1/P2)**(1./1.15));
v1 = ((R*T1)/(P1*100));
T2 = T1*(P2/P1)*(v2/v1);
cv = 0.716;
Q = (cv+(R/(1-n)))*(T2-T1);
print ("If the fluid is in the air")
print "The specific volume is %.4f m3/Kg"%v2
print "The final temperature is %.1f K"%T2
print "Heat transferred per kg is %.f kJ"%Q
# Part (b)
v1 = 0.20596
u1 = 2621.9;
v2 = v1*(P1/P2)**(1./n);
x2 = (v2-0.001043)/(1.694-0.001043);
t2 = 99.62;
W = ((P1*100*0.13575)-(P2*100*1.525))/(n-1);
Q = 2366.1-u1+W;
print ("\nIf the fluid is in the steam")
print "The specific volume is %.3f m3/Kg"%v2
print "The final temperature is ",t2,"C"
print "Heat transferred per kg is %.2f kJ"%Q
```

In [14]:

```
import math
# Variables
P0 = 1000.;
T0 = 3.;
V0 = 0.001;
R = 287.;
# Calculation
n = (P0*V0)/(R*T0); # Number of moles
# Process ab
Wab = 0;
cv = (3/2.)*R;
Ta = T0; Tb = 300;
Qab = n*cv*(Tb-Ta);
Ua = 0; # Given internal energy
Ub = Qab+Ua;
Uab = Ub-Ua;
print Qab
# Process bc
Qbc = 0
Uc = 0
Ubc = Uc-Ub;
Wbc = -Ubc;
# Process ca
Tc = Ta;
g = 5./3; # gamma
Vcb = (Tb/Tc)**(3./2); # Vc/Vb
Wca = -n*R*Tc*math.log(Vcb);
Qca = Wca ;
Uca = 0;
# Results
print "Work done in the cycle is %.1f J"%(Wbc)
print "Internal energy change in the cycle is",Ub,"J"
print "Heat transfer in the cycle is %.2f"%(Wca)
# Part (b)
e = (Qab+Qca)/Qab;
print "Thermal efficiency of the system is %.3f %%"%(e)
```

In [8]:

```
import math
Pa = 1.5;
Ta = 273.+50;
ca = 0.5;
Pb = 0.6;
Tb = 20.+273;
mb = 2.5;
R = 8.3143;
# Calculation
Va = (ca*R*Ta)/(Pa*1e03);
ma = ca*28;
Rn = R/28.;
Vb = (mb*Rn*Tb)/(Pb*1e03);
V = Va + Vb ;
m = ma + mb ;
Tf = 27+273.;
P = (m*Rn*Tf)/V;
g = 1.4;
cv = Rn/(g-1);
U1 = cv*(ma*Ta+mb*Tb);
U2 = m*cv*Tf;
Q = U2-U1;
# Results
print "The final equillibrium pressure is %.3f MPa"%(P/1000)
print "The amount of heat transferred to the surrounding is %.1f kJ"%Q
T_ = (ma*Ta+mb*Tb)/m ;
P_ = (m*Rn*T_)/V;
print "If the vessel is perfectly inslulated"
print "The final temperature is %.1f k"%T_
print "The final pressure is %.2f MPa"%(P_/1000)
```

In [9]:

```
import math
# Variables
cp = 1.968;
cv = 1.507;
R_ = 8.314;
V = 0.3;
m = 2.;
T1 = 5.+273;
T2 = 100.+273;
# Calculation
R = cp-cv;
mu = R_/R;
Q12 = m*cv*(T2-T1);
W12 = 0 ;
U21 = Q12;
H21= m*cp*(T2-T1);
S21 = m*cv*math.log(T2/T1);
# Results
print "Molecular weight and the gas constant of the gas are %.2f kJ/Kg mol and %.3f kJ/Kg K"%(mu,R)
print "Work done",0,"kJ"
print "The heat transferred %.2f kJ"%Q12
print "The change in internal energy ",U21,"kJ"
print "Change in entropy %.1f kJ/K"%S21
print "Change in enthalpy",H21,"kJ"
# rounding off error would be there.
```

In [31]:

```
import math
from scipy.integrate import quad
# Variables
m = 1.5;
P1 = 5.6;
V1 = 0.06;
T2 = 273.+240;
a = 0.946; b = 0.662;
k = 0.0001;
# Calculation
# Part (b)
R = a-b;
T1 = (P1*1e03*V1)/(m*R);
def f8(T):
return m*(b+k*T)
W12 = - quad(f8,T1,T2)[0]
# Results
print "The work done in the expansion is %.0f kJ"%W12
# note : answer is different becaues of quad function.
```

In [34]:

```
import math
# Variables
m = 0.5;
P1 = 80e03;
T1 = 273.+60;
P2 = 0.4e06;
R = 0.287;
V1 = (m*R*T1)/P1 ;
g = 1.4; # Gamma
# Calculation
T2 = T1*(P2/P1)**((g-1)/g);
W12 = (m*R*(T1-T2))/(g-1);
V2 = V1*((P1/P2)**(1./g));
W23 = P2*(V1-V2);
W = W12+W23;
V3 = V1;
T3 = T2*(V3/V2);
cp = 1.005;
Q = m*cp*(T3-T2);
# Results
print "The work transfer for the whole path is %.1f kJ"%W
print "The heat transfer for the whole path %.2f kJ"%Q
# note : incorrect answer in the textbook
```

In [38]:

```
import math
# Variables
P1 = 700e03
T1 = 273.+260;
T3 = T1;
V1 = 0.028;
V2 = 0.084;
R = 0.287;
# Calculation
m = (P1*V1)/(R*T1);
P2 = P1;
T2 = T1*((P2*V2)/(P1*V1));
n = 1.5;
P3 = P2*((T3/T2)**(n/(n-1)));
cp = 1.005; cv = 0.718;
Q12 = m*cp*(T2-T1);
Q23 = m*cv*(T3-T2) + (m*R*(T2-T3))/(n-1);
Q31 = m*R*T1*math.log(P3/P1);
Q1 = Q12;
Q2 = -(Q23+Q31);
e = 1-(Q2/Q1);
# Results
print "The heat received in the cycle is",round(Q1/1000,2),"kJ"
print "The heat rejected in the cycle",round(Q2/1000,2),"J"
print "The efficiency of the cycle is %.2f"%e
# note : rounding error is there.
```

In [19]:

```
import math
# Variables
P1 = 300e03;
V1 = 0.07;
m = 0.25;
T1 = 80+273;
# Calculation
R = (P1*V1)/(1000*m*T1);
P2 = P1;
V2 = 0.1;
T2 = (P2*V2)/(1000*m*R);
W = -25;
cv = -W/(m*(T2-T1));
cp = R+cv;
S21 = m*cp*math.log(V2/V1); # S21 = S2-S1
# Results
print "cv of the gas is %.3f kJ/Kg K"%cv
print "cp of the gas is %.3f kJ/Kg K"%cp
print "Increase in the entropy of the gas is %.2f kJ/Kg K"%S21
```

In [15]:

```
import math
# Variables
P1 = 1.;
P2 = 15.;
V1 = 800e-06;
V2 = V1/8;
cv = 0.718;
g = 1.4;
# Calculation
n = (math.log(P2/P1))/(math.log(V1/V2))
T1 = 348; R = 0.287;
m = (P1*100*V1)/(R*T1);
T2 = T1*((P2*V2)/(P1*V1));
P3 = 50;
T3 = T2*(P3/P2);
S21 = m*(cv*math.log(T2/T1)+R*math.log(V2/V1));
S32 = m*cv*math.log(T3/T2);
Q = (m*cv*(g-n)*(T2-T1))/(1-n);
# Results
print "The index of compression process is %.1f"%n
print "T2 = %.1f K"%T2
print "S2-S1 is %.6f kJ/K"%S21
print "S3-S2 is %.6f kJ/k"%S32
print "The heat exchange is %.3f kJ"%Q
```

In [10]:

```
import math
# Variables
mn = 3.; # Mass of nitrogen in kg
mc = 5.; # mass of CO2 in kg
an = 28.; # Atomic weight of nitrogen
ac = 44.; # Atomic weight of CO2
# Calculation and Results
# Part (a)
xn = (mn/an)/((mn/an)+(mc/ac));
xc = (mc/ac)/((mn/an)+(mc/ac));
print "Mole fraction of N2 is %.3f"%xn
print "Mole fraction of CO2 is %.3f"%xc
# Part (b)
M = xn*an+xc*ac;
print "Equivalant molecular weight of mixture is %.2f Kg"%M
# Part (c)
R = 8.314;
Req = ((mn*R/an)+(mc*R/ac))/(mn+mc);
print "The equivalent gas consmath.tant of the mixture is %.3f kJ/Kg K"%Req
# Part (d)
P = 300.; # Pressure in kPa
T = 20.+273;
Pn = xn*P; # Partial pressure of Nitrogen
Pc = xc*P; # Partial pressure of CO2
Vn = (mn*R*T)/(P*an); # Volume of nitrogen
Vc = (mc*R*T)/(P*ac); # Volume of CO2
print "Partial pressures of nitrogen and CO2 are %.1f and %.1f kPa respectively"%(Pn,Pc)
print "Partial volume of nitrogen and CO2 are %.2f and %.2f m3 respectively"%(Vn,Vc)
# Part (e)
V = (mn+mc)*Req*T/P; # Total volume
rho = (mn+mc)/V;
print "Volume of mixture is %.2f m3"%V
print "Density of mixture is %.2f Kg/m3"%rho
# Part (f)
gn = 1.4; # Gamma
gc = 1.286;
cvn = R/((gn-1)*an); # cp and cv of N2
cpn = gn*cvn;
cvc = R/((gc-1)*ac); # cp and cv of CO2
cpc = gc*cvc;
cp = (mn*cpn+mc*cpc)/(mn+mc) ; # of mixture
cv = (mn*cvn+mc*cvc)/(mn+mc) ;
print "cp and cv of mixture are %.2f and %.2f kJ/Kg K respectively"%(cp,cv)
T1 = T;
T2 = 40.+273;
U21 = (mn+mc)*cv*(T2-T1);
H21 = (mn+mc)*cp*(T2-T1);
S21v = (mn+mc)*cv*math.log(T2/T1); # If heated at constant volume
print "Change in internal energy of the system heated at constant volume is %.1f kJ"%U21
print "Change in enthalpy of the system heated at constant volume is %.1f kJ"%H21
print "Change in entropy of the system heated at constant volume is %.3f kJ/Kg K"%S21v
S21p = (mn+mc)*cp*math.log(T2/T1); # If heated at constant Pressure
print "Change in entropy of the system heated at constant Pressure is %.2f kJ"%S21p
```

In [30]:

```
import math
# Variables
mo = 2.;
mn = 6.;
muo = 32.;
mun = 28.;
# Calculation
o = mo/muo;
n = mn/mun;
xo = o/(n+o);
xn = n/(n+o);
R = 8.314;
Ro = R/muo; Rn = R/mun;
dS = -mo*Ro*math.log(xo)-mn*Rn*math.log(xn);
# Results
print "Increase in entropy is %.4f kJ/Kg K"%dS
```

In [31]:

```
import math
# Variables
an = 20.183; # molecular weight of neon
Pc = 2.73; # Critical pressure
Tc = 44.5;
Vc = 0.0416;
Pr = 2; # Reduced Pressure
Tr = 1.3;
Z = 0.7;
# Calculation
P = Pr*Pc;
T = Tr*Tc;
R = 8.314;
v = (Z*R*T)/(P*1000*an);
vr = (v*an)/Vc ;
# Results
print "Specific volume is %.3e m3/Kg"%v
print "Specific temperature is",T,"K"
print "Specific pressure is",P,"MPa"
print "Reduced volume is %.2f"%vr
```