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) 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 &amp;&lt;=&gt;[$K$_{\text{a, 1}}][$K$_{\text{b, 1}}] H+ + A- &amp;&amp; (1) }\\ 
\ce{H+ + B- &amp;&lt;=&gt;[$K$_{\text{b, 2}}][$K$_{\text{a, 2}}] HB &amp;&amp; (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} &amp;= [HA] + [A- ] &amp;&amp; (3) }\\
\ce{[B]_{tot} &amp;= [HB] + [B- ] &amp;&amp; (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}} &amp;= \ce{[HA] + [A- ]} \\
\ce{[B]_{tot}} &amp;= \ce{[HB] + [B- ]} 
\end{align}$$

Law of mass action:
$$\begin{align}
K_\text{a, 1} &amp;= \frac{\ce{[H+ ][A- ]}}{\ce{[HA]}} \\
K_\text{a, 2} &amp;= \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 = &amp; \ce{[H+ ]^4} + \ce{[H+ ]^3}(K_\text{a, 1} + K_\text{a, 2}) \\
  &amp;- \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}) \\
 &amp; -\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] \\
 &amp; - K_\text{w} K_\text{a, 1} K_\text{a, 2}
\end{align}$$