# entropy-and-the-second-law-of-thermodynamics.ipynb

In [1]:
from sympy import *

In [2]:
P, V, T, v, F, A, p, t, a, s = symbols('P, V, T, v, F, A, p, t, a, s')
E_k, deltau, deltaQ, deltaw, E, SH, deltaT, C_V, C_P, gamma = symbols('E_k, {\Delta}u, {\Delta}Q, {\Delta}w, E, SH, {\Delta}T, C_V, C_P, gamma')
S, Q, deltaS, Eff, W = symbols('S, Q, {\Delta}S, Eff, W')
du, dq, dw, dV, dT, dH, dP, dq_P = symbols('du, dq, dw, dV, dT, dH, dP, dq_P')
u = Function('u')
H = Function('H')
n, R, k_v, m, N_A, N, k, T_1, T_2, V_1, V_2, P_1, P_2, Q_1, Q_2, S_1, S_2 = symbols('n, R, k_v, m, N_A, N, k, T_1, T_2, V_1, V_2, P_1, P_2, Q_1, Q_2, S_1, S_2', constant = True)

In [3]:
Ideal_gas_Law = Eq(P*V, n * UnevaluatedExpr(R*T))
Ideal_gas_Law

Out[3]:
$\displaystyle P V = n R T$
In [4]:
Ideal_gas_Law = Eq(P*V, n*R*T)
Ideal_gas_Law

Out[4]:
$\displaystyle P V = R T n$
In [5]:
F_P_Law = Eq(P, F/A)
F_P_Law

Out[5]:
$\displaystyle P = \frac{F}{A}$

## Exercise

Find the work done if piston is push by the length dl

In [6]:
Energy_of_a_system_Law = Eq(du, dq - P*dV)
Energy_of_a_system_Law

Out[6]:
$\displaystyle du = - P dV + dq$
In [53]:
Diff_in_Ineternal_energy = Eq(du,
Derivative(u(T, V), T)*UnevaluatedExpr(dT) +
Derivative(u(T, V), V)*UnevaluatedExpr(dV))
Diff_in_Ineternal_energy

Out[53]:
$\displaystyle du = \frac{\partial}{\partial T} u{\left(T,V \right)} dT + \frac{\partial}{\partial V} u{\left(T,V \right)} dV$

## Exercise

### Find change in internal energy at constant volume

In [8]:
change_in_U_const_V = Eq(du, Derivative(u(T, V), T)*dT)
change_in_U_const_V

Out[8]:
$\displaystyle du = dT \frac{\partial}{\partial T} u{\left(T,V \right)}$
In [54]:
Heat_capacity_const_V = Eq(C_V, Derivative(u(T, V), T))
Heat_capacity_const_V

Out[54]:
$\displaystyle C_{V} = \frac{\partial}{\partial T} u{\left(T,V \right)}$
In [9]:
change_in_U_const_V = Eq(Eq(du, dq), C_V*dT, evaluate = False)
change_in_U_const_V

Out[9]:
$\displaystyle du = dq = C_{V} dT$
###### Enthalpy represents internal energy (u) plus entropy (PV)
In [10]:
Enthalpy_Law = Eq(H(T, P), u(T, V) + P*V)
Enthalpy_Law

Out[10]:
$\displaystyle H{\left(T,P \right)} = P V + u{\left(T,V \right)}$

## Exercise

Find change in Enthalpy

In [11]:
change_H = Eq(dH, Derivative(H(T, P), T)*dT + Derivative(H(T, P), P)*dP)
change_H

Out[11]:
$\displaystyle dH = dP \frac{\partial}{\partial P} H{\left(T,P \right)} + dT \frac{\partial}{\partial T} H{\left(T,P \right)}$
###### at constant P
In [55]:
change_H_const_P = Eq(dH, Derivative(H(T, P), T)*dT)
change_H_const_P

Out[55]:
$\displaystyle dH = dT \frac{\partial}{\partial T} H{\left(T,P \right)}$
In [14]:
Heat_capacity_const_P = Eq(C_P, Derivative(H(T, P), T))
Heat_capacity_const_P

Out[14]:
$\displaystyle C_{P} = \frac{\partial}{\partial T} H{\left(T,P \right)}$
In [13]:
change_H_const_P = Eq(Eq(dH, C_P*dT), dq_P)
change_H_const_P

Out[13]:
$\displaystyle dH = C_{P} dT = dq_{P}$
In [56]:
Heat_capacity_const_P

Out[56]:
$\displaystyle C_{P} = \frac{\partial}{\partial T} H{\left(T,P \right)}$
In [57]:
Heat_capacity_const_V

Out[57]:
$\displaystyle C_{V} = \frac{\partial}{\partial T} u{\left(T,V \right)}$
In [16]:
Enthalpy_Law

Out[16]:
$\displaystyle H{\left(T,P \right)} = P V + u{\left(T,V \right)}$
In [17]:
change_in_H_const_P = Eq( Derivative(H(T, P), T),  Derivative(u(T, V), T) +  Derivative(P*V, T))
change_in_H_const_P

Out[17]:
$\displaystyle \frac{\partial}{\partial T} H{\left(T,P \right)} = \frac{\partial}{\partial T} P V + \frac{\partial}{\partial T} u{\left(T,V \right)}$
In [18]:
Ideal_gas_Law

Out[18]:
$\displaystyle P V = R T n$
In [19]:
change_in_V = Eq(Derivative(V, T), n*R/P)
change_in_V

Out[19]:
$\displaystyle \frac{d}{d T} V = \frac{R n}{P}$
###### Assuming n = 1
In [20]:
Heat_capacity_Law = Eq(C_P, Derivative(u(T, V), T) + R)
Heat_capacity_Law

Out[20]:
$\displaystyle C_{P} = R + \frac{\partial}{\partial T} u{\left(T,V \right)}$
In [21]:
Heat_capacity_Law = Eq(C_P, C_V + R)
Heat_capacity_Law

Out[21]:
$\displaystyle C_{P} = C_{V} + R$

## Exercise

Find change in internal energy at constant T

In [49]:
change_in_U = Eq(Eq(du, dq - P*dV, evaluate = False), C_V*dT, evaluate = False)
change_in_U

Out[49]:
$\displaystyle du = - P dV + dq = C_{V} dT$
In [23]:
change_in_U_const_T = Eq(Eq(du,- P*dV, evaluate = False), C_V*dT, evaluate = False)
change_in_U_const_T

Out[23]:
$\displaystyle du = - P dV = C_{V} dT$
In [24]:
change_in_T = Eq(- R*T*dV/V, C_V*dT, evaluate = False)
change_in_T

Out[24]:
$\displaystyle - \frac{R T dV}{V} = C_{V} dT$
In [25]:
change_in_T = Eq(- Integral(R/V, (V, V_1, V_2)), Integral(C_V/T, (T, T_1, T_2)), evaluate = False)
change_in_T

Out[25]:
$\displaystyle - \int\limits_{V_{1}}^{V_{2}} \frac{R}{V}\, dV = \int\limits_{T_{1}}^{T_{2}} \frac{C_{V}}{T}\, dT$
In [26]:
change_in_T = Eq(Integral(C_V/T, (T, T_1, T_2)).doit() / C_V, - Integral(R/V, (V, V_1, V_2)).doit() / C_V, evaluate = False).simplify()
change_in_T

Out[26]:
$\displaystyle \log{\left(T_{1} \right)} - \log{\left(T_{2} \right)} = - \frac{R \left(\log{\left(V_{1} \right)} - \log{\left(V_{2} \right)}\right)}{C_{V}}$
In [27]:
change_in_T = Eq(logcombine(Integral(C_V/T, (T, T_1, T_2)).doit().simplify() / C_V, force = True),
logcombine(- Integral(R/V, (V, V_1, V_2)).doit().simplify() / C_V, force = True), evaluate = False)
change_in_T

Out[27]:
$\displaystyle \log{\left(\frac{T_{2}}{T_{1}} \right)} = - \log{\left(\left(\frac{V_{2}}{V_{1}}\right)^{\frac{R}{C_{V}}} \right)}$
In [28]:
change_in_T = Eq(exp(logcombine(Integral(C_V/T, (T, T_1, T_2)).doit().simplify() / C_V, force = True)),
exp(logcombine(- Integral(R/V, (V, V_1, V_2)).doit().simplify() / C_V, force = True)))
change_in_T

Out[28]:
$\displaystyle \frac{T_{2}}{T_{1}} = \left(\frac{V_{2}}{V_{1}}\right)^{- \frac{R}{C_{V}}}$
In [29]:
Heat_capacity_Law

Out[29]:
$\displaystyle C_{P} = C_{V} + R$
In [30]:
R_C_V_Ratio = Eq(R, C_P - C_V )
R_C_V_Ratio

Out[30]:
$\displaystyle R = C_{P} - C_{V}$
In [31]:
R_C_V_Ratio = Eq(R / C_V, (C_P - C_V) / C_V)
R_C_V_Ratio

Out[31]:
$\displaystyle \frac{R}{C_{V}} = \frac{C_{P} - C_{V}}{C_{V}}$
In [32]:
R_C_V_Ratio = Eq(R / C_V, (C_P / C_V) - 1)
R_C_V_Ratio

Out[32]:
$\displaystyle \frac{R}{C_{V}} = \frac{C_{P}}{C_{V}} - 1$
In [33]:
R_C_V_Ratio = Eq(R / C_V, gamma - 1)
R_C_V_Ratio

Out[33]:
$\displaystyle \frac{R}{C_{V}} = \gamma - 1$
In [34]:
change_in_T = Eq(exp(logcombine(Integral(C_V/T, (T, T_1, T_2)).doit().simplify() / C_V, force = True)),
exp(logcombine(- Integral(R/V, (V, V_1, V_2)).doit().simplify() / C_V, force = True))).subs({R / C_V: gamma - 1})
change_in_T

Out[34]:
$\displaystyle \frac{T_{2}}{T_{1}} = \left(\frac{V_{2}}{V_{1}}\right)^{1 - \gamma}$
In [35]:
Ideal_gas_Law

Out[35]:
$\displaystyle P V = R T n$
In [51]:
T_of_one_mole = Eq(T, P*V/R)
T_of_one_mole

Out[51]:
$\displaystyle T = \frac{P V}{R}$
In [37]:
change_in_P_V = change_in_T.subs({T_1: P_1*V_1/R,
T_2: P_2*V_2/R})
change_in_P_V

Out[37]:
$\displaystyle \frac{P_{2} V_{2}}{P_{1} V_{1}} = \left(\frac{V_{2}}{V_{1}}\right)^{1 - \gamma}$
In [38]:
change_in_P_V = Eq(change_in_P_V.lhs * (V_1 / V_2), expand_power_base(change_in_P_V.rhs * (V_1 / V_2), force = True)).simplify()
change_in_P_V

Out[38]:
$\displaystyle \frac{P_{2}}{P_{1}} = V_{2}^{- \gamma} \left(\frac{1}{V_{1}}\right)^{- \gamma}$
In [39]:
change_in_P_V = Eq(change_in_P_V.lhs * P_1 / V_2**-gamma, change_in_P_V.rhs * P_1/ V_2**-gamma).doit()
change_in_P_V

Out[39]:
$\displaystyle P_{2} V_{2}^{\gamma} = P_{1} \left(\frac{1}{V_{1}}\right)^{- \gamma}$

## Exercise

Find energy difference and efficiency in a carnot cycle

In [40]:
Entropy_Law = Eq(S, Q / T)
Entropy_Law

Out[40]:
$\displaystyle S = \frac{Q}{T}$
In [41]:
change_in_S = Eq(deltaS, deltaQ / T)
change_in_S

Out[41]:
$\displaystyle {\Delta}S = \frac{{\Delta}Q}{T}$
In [42]:
efficiency = Eq(Eff, Eq(W/Q_1, 1 - (T_2/T_1)), evaluate = False)
efficiency

Out[42]:
$\displaystyle Eff = \frac{W}{Q_{1}} = 1 - \frac{T_{2}}{T_{1}}$
In [43]:
less_efficiency = LessThan(W, Q_1*(1 - (T_2/T_1)))
less_efficiency

Out[43]:
$\displaystyle W \leq Q_{1} \left(1 - \frac{T_{2}}{T_{1}}\right)$
In [44]:
change_in_W = LessThan(Eq(W, Q_1 - Q_2), Q_1*(1 - (T_2/T_1)), evaluate = False)
change_in_W

Out[44]:
$\displaystyle W = Q_{1} - Q_{2} \leq Q_{1} \left(1 - \frac{T_{2}}{T_{1}}\right)$
In [45]:
change_in_W = LessThan(- Q_2, - Q_1*(T_2/T_1))
change_in_W

Out[45]:
$\displaystyle - Q_{2} \leq - \frac{Q_{1} T_{2}}{T_{1}}$
In [46]:
change_in_S = GreaterThan(Q_2/T_2, Q_1/T_1)
change_in_S

Out[46]:
$\displaystyle \frac{Q_{2}}{T_{2}} \geq \frac{Q_{1}}{T_{1}}$
In [47]:
change_in_S = GreaterThan(S_2, S_1)
change_in_S

Out[47]:
$\displaystyle S_{2} \geq S_{1}$
In [48]:
change_in_S = Eq(Eq(deltaS, deltaQ/T), n*R*log(V_2 /V_1), evaluate = False)
change_in_S

Out[48]:
$\displaystyle {\Delta}S = \frac{{\Delta}Q}{T} = R n \log{\left(\frac{V_{2}}{V_{1}} \right)}$

## Exercise

Calculate the change in entropy in a carnot cycle when the volume is doubled

In [ ]:


In [ ]:


In [ ]: