Here are chemical equilibrium equations:
$\begin{cases} \mathbf{N}^\text{T}\mathbf{X}+\mathbf{C}=\mathbf{Y}\\ \mathbf{N}\ln\mathbf{Y}=\ln\mathbf{K} \end{cases}$
Here $\mathbf{C}=\begin{pmatrix}c_1\\c_2\\\vdots\\c_n\end{pmatrix}$ are initial concentrations (mol/L) of n types of substances;
$\mathbf{N}=\begin{pmatrix} \nu_{11}&\nu_{12}&\cdots&\nu_{1n}\\ \nu_{21}&\nu_{22}&\cdots&\nu_{2n}\\ \vdots&\vdots&\ddots&\vdots\\ \nu_{m1}&\nu_{m2}&\cdots&\nu_{mn}\\ \end{pmatrix}$ are coefficients of m chemical reactions (negative for reagents, positive for products, and zero if the substance is not related to the reaction);
$\mathbf{X}=\begin{pmatrix}x_1\\x_2\\\vdots\\x_m\end{pmatrix}$ are amounts of reactions (concentration changes for each reaction);
$\mathbf{Y}=\begin{pmatrix}y_1\\y_2\\\vdots\\y_n\end{pmatrix}$ are final (equilibrium state) concentrations;
$\mathbf{K}=\begin{pmatrix}k_1\\k_2\\\vdots\\k_m\end{pmatrix}$ are equilibrium constants for each reaction.
I need to solve X (m variables) and Y (n variables) by given C, N and K, either analytic or numeric solutions.
For Example
0.1 mol/L ammonia solution, we have 2 equilibria:
(A) $\ce{H2O <=> H+ + OH-}$ (concentration of $\ce{H2O}$ is not considered)
(B) $\ce{NH3 + H2O <=> NH4+ + OH-}$ (concentration of $\ce{H2O}$ is not considered)
Equilibrium constants:
- $[\ce{H+}][\ce{OH-}] = 10^{-14}$
- $[\ce{NH4+}][\ce{OH-}]/[\ce{NH3}] = 1.77 \times 10^{-5}$
So we have 2 reactions (A and B) and 4 substances ($\ce{H+}$, $\ce{OH-}$, $\ce{NH3}$, and $\ce{NH4+}$), and known values are:
$\mathbf{C}=\begin{pmatrix}c_\ce{H+}\\c_\ce{OH-}\\c_\ce{NH3}\\c_\ce{NH4+}\end{pmatrix}=\begin{pmatrix}0\\0\\0.1\\0\end{pmatrix}$
$\mathbf{N}=\begin{pmatrix}\nu_{\text{A},\ce{H+}}&\nu_{\text{A},\ce{OH-}}&0&0\\0&\nu_{\text{B},\ce{OH-}}&\nu_{\text{B},\ce{NH3}}&\nu_{\text{B},\ce{NH4}}\end{pmatrix}=\begin{pmatrix}1&1&0&0\\0&1&-1&1\end{pmatrix}$
$\mathbf{K}=\begin{pmatrix}k_A\\k_B\end{pmatrix}=\begin{pmatrix}10^{-14}\\1.77\times10^{-5}\end{pmatrix}$
The solutions are:
$\mathbf{X}=\begin{pmatrix}x_A\\x_B\end{pmatrix}=\begin{pmatrix}7.57\times10^{-12}\\1.32\times10^{-3}\end{pmatrix}$
$\mathbf{Y}=\begin{pmatrix}y_\ce{H+}\\y_\ce{OH-}\\y_\ce{NH3}\\y_\ce{NH4+}\end{pmatrix}=\begin{pmatrix}7.57\times10^{-12}\\1.32\times10^{-3}\\0.0987\\1.32\times10^{-3}\end{pmatrix}$
Here I used the approximation of $y_\ce{OH-}=y_\ce{NH4+}$, so just solved a quadratic equation.
But how can I solve a more complicated system like:
\begin{align} \ce{H2O &<=> H+ + OH-} \\ \ce{NH3 + H2O &<=> NH4+ + OH-} \\ \ce{CO2 + H2O &<=> H+ + HCO3-} \\ \ce{HCO3- &<=> H+ + CO3^2-} \end{align}
which includes 4 reactions and 7 substances?
mhchem
package. $\endgroup$