In the previous post, we derived the expression
$$\begin{equation} dG = Vdp -SdT \end{equation}$$
which represents the changes in energy of the system (battery) due to non-expansionary work only. We want to find a way to write the Gibbs energy as a direct function of the state variables. This requires explicit relations between the various state variables to allow integration of the above Gibbs energy equation. In order to do this, we need to re-examine our assumptions.
The underlying assumption throughout the derivation is that the system is at constant composition. Therefore, the state function $f(p,V,n,T) = 0$ should really be written $f(p,V,T;n) =0$, where the terms after the semicolon are considered parameters. We will allow composition to be variable later, but for now we will make even tighter assumptions.
Perfect Gas Equation
Since, ultimately, our battery won’t be thermally insulated from its environment, we can take its temperature (at thermal equilibrium) to be that of the environment. This will be a constant value. Hence, $f(p,V;n,T) = 0$. In other words, we should be able to take the implicit solution to the empirical equation and solve for $V = V(p)$. Moreover, assuming that the temperature is constant means that $dT=0$. Therefore,
$$\begin{equation} dG = V(p)dp \end{equation}$$
and so,
$$\begin{equation} G(p_f) = G(p_i) + \int_{p_i}^{p_f}V(p)dp \end{equation}$$
where $p_i$ and $p_f$are the pressure of the battery in its initial and final states. In practical terms, this means that if we could solve for $V=V(p)$ and we knew the initial and final pressures of the battery, we could state how much energy was released or absorbed by the battery through electric work. Unfortunately, we do not know $V=V(p)$. Except in one special case:
For a perfect gas, we have that $pV = nRT$. Therefore, with the terms on the right hand side all considered constants, $V = nRT/p$. Therefore,
$$\begin{eqnarray} G(p_f) &=& G(p_i) + \int_{p_i}^{p_f}V(p)dp \nonumber \\ &=& G(p_i) + nRT \int_{p_i}^{p_f}\frac{1}{p}dp \nonumber \\ &=&G(p_i) + nRT \ln\left(\frac{p_f}{p_i}\right) \end{eqnarray}$$
It is common to set the initial pressure as the standard pressure $p^{\theta}$. Therefore, we can simply write the final pressure as $p$ to get,
$$\begin{equation} G(p) = G^{\theta} + nRT\ln\left(\frac{p}{p^{\theta}}\right)\end{equation}$$
The final adjustment needed is a division by amount, $n$. This allows us to discuss intensive changes rather than extensive changes. Ultimately we want to discuss changes in compositions (ratios of chemicals), so it will be better to formulate our equations this way now. Thus, we get the molar Gibbs energy equation for a perfect gas,
$$\begin{equation} G_m(p) = G_m^{\theta} + RT\ln\left(\frac{p}{p^{\theta}}\right)\end{equation}$$
Fugacity
But what about when we aren’t dealing with perfect gases? The way to go about solving for the Gibbs energy is via an empirical function called the fugacity, $f(p) = \phi(p)p$. This is an empirical function which is defined such that the experimental measurement of Gibbs energy exactly fits the formulation,
$$\begin{equation} G_m(p) = G_m^{\theta} + RT\ln\left(\frac{\phi(p) p}{p^{\theta}}\right)\end{equation}$$
when temperature is constant.
The way I like to think about this is by going back to
$$\begin{equation} G(p_f) = G(p_i) + \int_{p_i}^{p_f}V(p)dp \end{equation}$$
which holds for any substance. We can think of $V(p)$ as an unknown empirical function. If the pressure is very low then we can treat the substance as a perfect gas. But we want to allow pressure to increase. Therefore, we can make a series expansion for $V(p)$ about $p\approx 0$. What we’ll get is something like,
$$\begin{equation} V(p) = nRT\frac{1}{p} + a_0 + a_1p + a_2p^2 + \cdots \end{equation}$$
for unknown constants $a_0,a_1,a_2,\cdots$. We could try to experimentally solve for each term to make our approximation more accurate and then integrate a truncated series. Alternatively, we could put all of these unknowns into $\phi$ through the form,
$$\begin{equation} G_m(p) = G_m^{\theta} + RT\ln\left(\frac{\phi(p) p}{p^{\theta}}\right)\end{equation}$$
Chemists have chosen the latter route.