Partial Differentiation#
Often we encounter quantities that depend on more than one variable. For example, the enthalpy, \(H\), of a system depends on the pressure, \(p\), and temperature, \(T\). Another case is the ideal gas equation, as seen below:
The pressure, \(p\), of a gas depends on its volume, \(V\), and the temperature, \(T\), it is at. We often find it useful to calculate how one variable changes with another, while the remaining variables are seen to be constant.
Recell, we can use the notation \(y=f(x, z)\) to mean that \(y\) is a function of \(x\) and \(z\), where \(x\) and \(z\) are independent variables. For example:
We use partial derivatives to differentiate functions with multiple variables. We do this by differentiating with respect to one variable and treating all other variables as constants. So for \(y=f(x, z)\), the partial derivative of \(y\) with respect to \(x\) is denoted:
The curvey symbol, \(\partial\), informs us that we perform partial differentiation with respect to \(x\), while the subscript variable, in this case \(z\), is being considered as a constant.
Example
So, going back to our example, \(y=x^2 +zx\). Find \(\left(\frac{\partial y}{\partial x}\right)_z\)
Solution: We note:
The curvey symbol, \(\partial\), informs us that we perform partial differentiation.
We are differentiating \(y\) with respect to \(x\).
We treat \(z\) as a constant.
So we differentiate the \(x^2\) terms as usual to get \(2x\). Then we differentiate the \(zx\) term, treating \(z\) as a constants, so this gives \(z\). Hence:
sympy
can perform partial differentiation by passing a symbol to differentiate with respect to.
from sympy import symbols, diff
x, z = symbols('x z')
y = x ** 2 + z * x
diff(y, x)
Example
For \(y=2\ln(z) + \sin(zx)\), find \(\left(\frac{\partial y}{\partial x}\right)_z\) and \(\left(\frac{\partial y}{\partial z}\right)_x\).
Solution:
For \(\left(\frac{\partial y}{\partial x}\right)_z\), we treat \(z\) as a constant and differentiate with respect to \(x\).
For \(\left(\frac{\partial y}{\partial z}\right)_x\), we treat \(x\) like a constatn and then differentiate with respect to \(z\).
With sympy
, we jsut change the symbol argument.
from sympy import log, sin
y = 2 * log(z) + sin(z * x)
diff(y, x)
diff(y, z)
Ideal Gas Equation
The ideal gas equation is \(pV = nRT\). Find:
\(\left(\frac{\partial V}{\partial T}\right)_p\)
\(\left(\frac{\partial T}{\partial p}\right)_V\)
Solution:
To be able to find \(\left(\frac{\partial V}{\partial T}\right)_p\), we must first make \(V\) the subject in the equation. This is done by dividing both side by \(p\) to produce \(V = \frac{nRT}{p}\).
Now we differentiate with respect to \(T\) taking \(p\) to be a constant.
\[\begin{split} \begin{aligned} \left(\frac{\partial V}{\partial T}\right)_p & = \frac{\partial}{\partial T} \left(\frac{nRT}{p}\right) \\ & = \frac{nR}{p} \end{aligned} \end{split}\]This is Charles’ law, at constant pressure, the volume of a gas is directly proportional to the temperature.
We do the same process to find \(\left(\frac{\partial T}{\partial p}\right)_V\). First make \(T\) the subject by dividing both sides by \(nR\) to obtain \(T = \frac{PV}{nR}\).
As before, we then differentiate, treating \(V\) as a constant, this time we get:
\[\begin{split} \begin{aligned} \left(\frac{\partial T}{\partial p}\right)_V & = \frac{\partial}{\partial p}\left(\frac{pV}{nR}\right) \\ & = \frac{V}{nR} \end{aligned} \end{split}\]This is Gay-Lussac’s (or the pressure) law: under fixed volume, the pressure is proportional to the temperature.
We can use sympy
to define the following equality.
from sympy import solve, Eq
p, V, n, R, T = symbols('p V n R T')
ideal_gas = Eq(p * V, n * R * T)
Then for Charles’ law, we solve for \(V\).
ideal_gas_V = solve(ideal_gas, V)[0]
ideal_gas_V
This can be differentiated with respect to \(T\).
diff(ideal_gas_V, T)
The same process can then be followed with the ideal_gas
object to find \(\left(\frac{\partial T}{\partial p}\right)_V\).
diff(solve(ideal_gas, T)[0], p)