In [3]:

```
#example 1
#coefficient of performance of refrigerator
Th=60 #temperature at which heat is rejected from R-134a
Tl=0 #temperature at which heat is absorbed into the R-134a
s1=1.7262 #specific entropy at 0 Celsius
s2=s1 #process of state change from 1-2 is isentropic
s3=1.2857 #specific entropy at 60 celsius
s4=s3 #process of state change from 3-4 is isentropic
print"if Pressure is 1400 kPa,then s=1.7360 kJ/kg-K and if P=1600 kPa,then s=1.7135 kJ/kg-K.Therefore"
P2=1400+(1600-1400)*(1.7262-1.736)/(1.7135-1.736) #pressure after compression in kPa
B=(Th+273.15)/(Th-Tl) #coefficient of performance of refrigerator
print" \n hence,pressure after compression is ",round(P2,1),"kPa"
print"\n and coefficient of performance of refrigerator is",round(B,2)
```

In [12]:

```
#example 2
#heat transfer in a given process
u1=87.94 #specific internal energy of R-12 at state 1 in kJ/kg
u2=276.44 #specific internal energy of R-12 at state 2 in kJ/kg
s1=0.3357 #specific entropy at state 1 in kJ/kg-K
s2=1.2108 #specific entropy at state 2 in kJ/kg-K
V=0.001 #volume of saturated liquid in m^3
v1=0.000923 #specific volume in m^3/kg
m=V/v1 #mass of saturated liquid in kg
T=20 #temperature of liquid in celsius
Q12=m*(T+273.15)*(s2-s1) #heat transfer in kJ to accomplish the process
W12=m*(u1-u2)+Q12 #work required to accomplish the process
print" \n hence,work required to accomplish the process is",round(W12,1),"KJ"
print" \n and heat transfer is",round(Q12,1),"KJ"
```

In [17]:

```
#example 3
#entropy change
import math
C=4.184 # specific heat of water in kJ/kg-K
T1=20 #initial temperature of water in celsius
T2=90 #final temperature of water in celsius
dS1=C*math.log((T2+273.2)/(T1+273.2)) #change in entropy in kJ/kg-K
dS2=1.1925-0.2966 #in kJ/kg-K using steam tables
print"\n hence,change in entropy assuming constant specific heat is",round(dS1,4),"KJ/kg-k"
print"\n using steam table is",round(dS2,4),"KJ/kg-k"
```

In [19]:

```
#example 4
#entropy change with different assumptions
import math
T1=300 #initial temperature in kelvins
T2=1500 #final temperature in kelvins
P1=200.0 #initial pressure in kPa
P2=150.0 #final pressure in kPa
R=0.2598 # in kJ/kg-K
Cp=0.922 #specific heat in kJ/kg-K at constant pressure
dsT2=8.0649 #in kJ/kg-K
dsT1=6.4168 #in kJ/kg-K
dS1=dsT2-dsT1-R*math.log(P2/P1) #entropy change calculated using ideal gas tables
dS3=Cp*math.log(T2/T1)-R*math.log(P2/P1) #entropy change assuming constant specific heat in kJ/kg-K
dS4=1.0767*math.log(T2/T1)+0.0747 #entropy change assuming specific heat is constant at its value at 990K
print"\n hence,change in entropy using ideal gas tables is",round(dS1,4),"KJ/kg-k"
print"\n hence,change in entropy using the value of specific heat at 300K is",round(dS3,4),"KJ/kg-k"
print"\n hence,change in entropy assuming specific heat is constant at its value at 900K is ",round(dS4,4),"KJ/kg-k"
```

In [1]:

```
#example 5
#entropy change
import math
Cp=1.004 #specific heat at constant pressure in kJ/kg-K
R=0.287 #gas constant in kJ/kg-K
P1=400.0 #initial pressure in kPa
P2=300.0 #final pressure in kPa
T1=300 #initial temperature in K
T2=600 #final temperature in K
dS1=Cp*math.log(T2/T1)-R*math.log(P2/P1) #entropy change assuming constant specific heat
s1=6.8693 #specific entropy at T1
s2=7.5764 #specific entropy at T2
dS2=s2-s1-R*math.log(P2/P1) #entropy change assuming variable specific heat
print"\n hence,entropy change assuming constant specific heat is",round(dS1,4),"KJ/kg-k"
print"\n and assuming variable specific heat is",round(dS2,4),"KJ/kg-k"
```

In [2]:

```
#example 6
#work done by air
import math
T1=600 #initial temperature of air in K
P1=400.0 #intial pressure of air in kPa
P2=150.0 #final pressure in kPa
u1=435.10 #specific internal energy at temperature T1 in kJ/kg
sT1=7.5764 #specific entropy at temperature T1 in kJ/kg-K
R=0.287 #gas constant in kJ/kg-K
ds=0
sT2=ds+sT1+R*math.log(P2/P1) #specific entropy at temperature T2 in kJ/kg-K
print"we know the values of s and P for state 2.So,in order to fully determine the state,we will use steam table"
T2=457 #final temperature in K
u2=328.14 #specific internal energy at temperature T2 in kJ/kg
w=u1-u2 #work done by air in kJ/kg
print"\n hence,work done by air is",round(w,4),"KJ/kg"
```

In [5]:

```
#example 7
#work and heat transfer
P2=500 #final pressure in cylinder in kPa
P1=100 #initial pressure in cylinder in kPa
T1=20+273.2 #initial temperature inside cylinder in Kelvins
n=1.3
T2=(T1)*(P2/P1)**((n-1)/n) #final temperature inside cylinder in K
R=0.2968 #gas constant in kJ/kg-K
w12=R*(T2-T1)/(1-n) #work in kJ/kg
Cvo=0.745 #specific heat at constant volume in kJ/kg-K
q12=Cvo*(T2-T1)+w12 #heat transfer in kJ/kg
print" \n hence,work done is",round(w12,2),"KJ/kg"
print"\n and heat transfer are",round(q12,1),"KJ/kg"
```

In [8]:

```
#example 3
#calculating increase in entropy
m=1 #mass of saturated water vapour
sfg=6.0480 #in kJ/K
T=25 #temperature of surrounding air in celsius
dScm=-m*sfg #change in entropy of control mass in kJ/K
hfg=2257.0 #in kJ/kg
Qtosurroundings=m*hfg #heat transferred to surroundings in kJ
dSsurroundings=Qtosurroundings/(T+273.15) #in kJ/K
dSnet=dScm+dSsurroundings #net increase in entropy in kJ/K
print" hence,net increase in entropy of water plus surroundings is ",round(dSnet,3),"KJ/k"
```

In [13]:

```
#example 9
#entropy generation
Qout=1.0 #value of heat flux generated by 1kW of electric power
T=600.0 #temperature of hot wire surface in K
Sgen=Qout/T #entropy generation in kW/K
print"\n hence,entropy generation is",round(Sgen,5),"KW/k"
```

In [16]:

```
#example 10
#Determiining the entropy generated
B=4.0#COP of air conditioner
W=10.0 #power input of air conditioner in kW
Qh=B*W #in kW
Ql=Qh-W #in kW
Thigh=323 #in Kelvin
Tlow=263 #in Kelvin
SgenHP=(Qh*1000/Thigh)-(Ql*1000/Tlow) #in W/K
Tl=281 # in K
Th=294 #in K
SgenCV1=Ql*1000/Tlow-Ql*1000/Tl #in W/K
SgenCV2=Qh*1000/Th-Qh*1000/Thigh #in W/K
SgenTOT=SgenCV1+SgenCV2+SgenHP #in W/K
print" \n Hence,Total entropy generated is",round(SgenTOT,1),"W/k"
```