How does one calculate the pH of an aqueous solution of a weak acid and a weak base?

In order to create a simple program which calculates titration curves (also to provide some closure in the comments [here](https://chemistry.stackexchange.com/questions/7162/why-should-colorimetric-titration-of-weak-acid-and-weak-base-should-not-be-done)) I am trying to come up with all the equations so I can calculate the equilibrium proton concentration for any given concentrations of any two given weak acids/bases.


This is a collection of what I've come up with so far and what I assume I still need. If someone could help me back on track I'd kindly appreciate it.




---


We're looking at the following reactions:
$$\begin{align} 
\ce{HA &<=>[$K$\_{\text{a, 1}}][$K$\_{\text{b, 1}}] H+ + A- && (1) }\\ 
\ce{H+ + B- &<=>[$K$\_{\text{b, 2}}][$K$\_{\text{a, 2}}] HB && (2)} 
\end{align}$$
A is the weak acid and B the weak base with the according equilibrium constants (the indices 1 and 2 refer to the acid and base respectively).


For the total masses I've got the following:
$$\begin{align}
\ce{[A]\_{tot} &= [HA] + [A- ] && (3) }\\
\ce{[B]\_{tot} &= [HB] + [B- ] && (4) }
\end{align}$$


The equilibrium constants are linked via
$$ K\_\text{a, 1} \times K\_\text{b, 2} = \frac{\ce{[A- ][HB]}}{\ce{[HA][B- ]}} $$


Furthermore:
$$
\ce{[H+ ]\_{eq}} = \frac{\ce{[A]\_{tot}} K\_\text{a, 1}}{\ce{[A- ]}} - K\_\text{a, 1}
 = \frac{\ce{[HB]}}{K\_\text{b, 2} (\ce{[B]\_{tot} - [HB]})}
$$


For the equilibrium proton concentration I've derived
$$ \ce{[H+ ]\_{eq}} = K\_\text{a, 1} \ce{[HA]} + K\_\text{a, 2} \ce{[HB]} + K\_\text{w} \ce{[H2O]}$$
where I neglect the influence of the water (the additional term $[\dots] + K\_\text{w} \ce{[H2O]}$) since it is so small.


Now, I'm struggling with the following:
$$ \ce{[H+ ]\_{tot}} = ? $$




---


I believe that with the last equation I will be able to solve for $\ce{[H+ ]\_{eq}}$. But maybe I'm wrong there as well.


Thanks for any pointers.




Equations and variables
-----------------------


To calculate the proton concentration in a solution as given in the question the following five equations are used for the following five unknown variables: $\ce{[H+ ]}$, $\ce{[HA]}$, $\ce{[A- ]}$, $\ce{[HB]}$ and $\ce{[B- ]}$.


**Ion balance:**
$$ \ce{[H+ ]} = \ce{[A- ] + [B- ]} + \frac{K\_\text{w}}{\ce{[H+ ]}} $$


**Mass balances:**
$$\begin{align}
\ce{[A]\_{tot}} &= \ce{[HA] + [A- ]} \\
\ce{[B]\_{tot}} &= \ce{[HB] + [B- ]} 
\end{align}$$


**Law of mass action:**
$$\begin{align}
K\_\text{a, 1} &= \frac{\ce{[H+ ][A- ]}}{\ce{[HA]}} \\
K\_\text{a, 2} &= \frac{\ce{[H+ ][B- ]}}{\ce{[HB]}}
\end{align}$$




---


Solution
--------


Combining the above equations we get:
$$
\ce{[H+ ]} = \frac{K\_\text{a, 1} \ce{[A]\_{tot}} }{\ce{[H+ ]}+K\_\text{a, 1}} + \frac{K\_\text{a, 2} \ce{[B]\_{tot}} }{\ce{[H+ ]}+K\_\text{a, 2}} + \frac{K\_\text{w}}{\ce{[H+ ]} }
$$


After some reforming we get a fourth-order polynomial for the proton concentration:
$$\begin{align}
0 = & \ce{[H+ ]^4} + \ce{[H+ ]^3}(K\_\text{a, 1} + K\_\text{a, 2}) \\
 &- \ce{[H+ ]^2}(\ce{[A]\_{tot}} K\_\text{a, 1} + \ce{[B]\_{tot}} K\_\text{a, 2} - K\_\text{a, 1} K\_\text{a, 2} + K\_\text{w}) \\
 & -\ce{[H+ ]}\left[ (\ce{[A]\_{tot}} + \ce{[B]\_{tot}}) K\_\text{a, 1} K\_\text{a, 2} + K\_\text{w}(K\_\text{a, 1} K\_\text{a, 2}) \right] \\
 & - K\_\text{w} K\_\text{a, 1} K\_\text{a, 2}
\end{align}$$


 5

This is the way I would tackle the problem:


$$\begin{align} 
\ce{HA &<=>[K\_\text{a}] H+ + A- && (1) }\\ 
\ce{B +H2O&<=>[K\_\text{b}] BH+ + OH- && (2)} 
\end{align}$$


The mass balance writes:


$$\begin{align}
\ce{[A]\_{tot} &= [HA] + [A- ] && (3) }\\
\ce{[B]\_{tot} &= [B] + [BH+ ] && (4) }
\end{align}$$


The charge balance writes:


$$\begin{align}
\ce{[A- ] + [OH- ]&=[H+] + [BH+ ] && (5) }
\end{align}$$


Therefore, by introducing eq. (3) into the definition of $K\_\text{a}$, one gets:


$$
\ce{[A- ]} = \frac{\ce{[A]\_{tot}} K\_\text{a}}{\ce{[H+ ]}+K\_\text{a}}
$$


And using the charge balance (eq. 5), one writes:


$$
\ce{[BH+ ] +[H+ ]}-\frac{K\_\text{w}}{\ce{[H+ ] }} = \frac{\ce{[A]\_{tot}} K\_\text{a}}{\ce{[H+ ]}+K\_\text{a}}
$$


The same can be done for the base:


$$
\ce{[BH+ ]} = \frac{\ce{[H+ ]}\ce{[B]\_{tot}} K\_\text{b}}{K\_\text{b}\ce{[H+ ] }+K\_\text{w}}
$$


Putting the last two equations together, one obtains:


$$
\frac{K\_\text{w}}{\ce{[H+ ]}} + \frac{\ce{[A]\_{tot}} K\_\text{a}}{\ce{[H+ ]}+K\_\text{a}}=\frac{\ce{[H+ ]}\ce{[B]\_{tot}} K\_\text{b}}{K\_\text{b}\ce{[H+ ] }+K\_\text{w}}+\ce{[H+ ]}
$$


which is the desired equation with $\ce{[H+ ]}$ as the unique unknown variable.


Hope that helps.


 3

[H+]tot would have to equal [H+]eq. All of your sources of H+ are involved in the equilibria. If you were working with a weak acid and it's conjugate base you could just use the Henderson-Hasselbach equation, eliminating the need for a Kb.


If I understand correctly, this is a titration of a weak acid by a weak base (or vice versa). Therefore H+ would be neutralized immediately, as you add base. So the pH would start low (~pKa, I believe), then slowly increase to where all the acid would be used, and continue to slowly increase until ~pKb. But wow, that endpoint would be hard to find any other way than graphically!


 0