Retro programming nostalgia IV: Acid/Base Balance and Titration (Part II)

This second article continues my journey of acid/base titrations.  In the previous article (https://daniloroccatano.blog/2020/04/14/acidsand-bases-equilibrium/), I showed how to calculate an acid-base equilibrium for strong acids and bases. This article also describes subroutines for titrations of monoprotic weak acids and bases. The method I used solves the pH calculation precisely and is based on an article published in the chemistry journal “Rassegna chimica” by Prof Luigi Campanella (and Dr G. Visco) in 1985. I received a copy of the article from the author while attending his analytical chemistry course at the University “La Sapienza” in Rome. I remember writing a program for the study of titrations was fun and stimulating, but it helped me understand the subject thoroughly. I recommend that the young reader try to convert the program into a modern language more familiar to you (e.g., Python) to understand its functioning better.


Titration of a weak acid with a strong base

Let us consider the ion [H_3O^+]  concentrations at various stages of titration. We indicate the concentration of the acid C_a in a volume V_A solution, with the concentration of the titrant C_T and V_T the volume of the titrant added.

The acid dissociation constant is equal to

K_a=\frac{[H_3O^+] [B^-]}{[HB]}

from which

K_a[HB]=[H_3O^+] [B^-]

using the relation  C_a=[HB]+[B^{-}] and replacing [HB]=C_a-[B^{-}] we obtain

K_a(C_a-[B^{-}])=[H_3O^+] [B^-]

from which we can derive

[B^-]= \frac{K_aC_a}{K_a+[H_3O^+]}

Substituting the previous relation into the charge equilibrium

[H_3O^+]=[OH^{-}]+[B^{-}]

we get

[B^-]= \frac{K_aC_a}{K_a+[H_3O^+]}

Expanding the previous relation, we get the following cubic equation:

[H_3O^+]^3 +K_a[H_3O^+]^2-(K_aC_a+K_W)[H_3O^+]-K_WK_a=0

Solving the equation using, for example, Newton’s method, one can find the initial pH.

After the addition of the titrant, the electroneutrality condition changes to

[H_3O^+]+[Me^+]=[OH^{-}]+[B^{-}] 

with [Me^+]=C_b and  [HB]+[B^{-}]=C_a

The values of these concentrations change with the addition of the titrant volume (v) as

C_a=\frac{C_a' V}{V+v}

C_b=\frac{C_b' V}{V+v}

with V, C_a'  and C_b'  the solution’s initial volume, the acid’s initial concentration and the titrant base.

Then using the charge equilibrium condition as in the previous derivation, we arrive at the new equation:

[H_3O^+]^3 +(K_a+C_b)[H_3O^+]^2+(K_aC_b-K_aC_a+K_W)[H_3O^+]-K_WK_a=0

The following figure shows an example of a titration curve for a weak monoprotic acid with a strong base.

Figure 1: Example of titration curve of a weak monoprotic acid with a strong base.

Titration of a weak base with a strong acid

Proceeding as in the previous case, we first obtain a similar equation from which to calculate the concentration of hydroxyl ions:

[OH^-]^3 +K_b[O^-]^2-(KbCb+K_W)[O^-]-K_WK_b=0

With the addition of the volume of the acid titrant, the above equation changes to 

[OH^-]^3 +(K_b+C_a)[OH^-]^2+(K_bC_a-K_bC_b+K_W)[OH^-]-K_WK_b=0

The following figure shows an example of a titration curve on a weak base with a strong acid.

Figure 2: Example of a titration curve of a weak base with a strong acid.

Figure 2: Example of a titration curve of a weak base with a strong acid.

Titration of a weak base with a weak acid

For completeness, we add equations for the titration of a weak acid with a weak base. This type of titration is rarely performed because it is challenging to identify the endpoint.

The derivation follows the same procedure based on the electroneutrality condition of the solution. The following are only the final fourth-order equations used in the BASIC program.

The following equation gives the initial proton concentration of the weak acid as in the previous case:

[H_3O^+]^3 +K_a[H_3O^+]^2-(KaCa+K_W)[H_3O^+]-K_WK_a=0

With the addition of the volume of the weak base as a titrant the above equation change to

\begin{aligned}K_b[H_3O^+]^4 +(K_aK_b+K_bC_b+K_w)[H_3O^+]^3 \\ -(K_aK_bC_b-K_aC_aK_b +K_aK_W-K_bK_W)[H_3O^+]^2 \\-(K_aK_bK_W+K_aC_aK_W+K_W^2)[H_3O^+]-K_W^2K_a =0 \end{aligned}


The following figure shows an example of a titration curve for a weak acid with a weak base.

Figure 3: Example of a titration curve of a weak acid with a weak base

The calculation of the roots of polynomials of proton concentrations with the Newton-Raphson

The values of the concentration of [H_3O^+] and [OH^-]  are obtained using the method of tangents or Newton–Raphson. The method consists in substituting the tangent to the curve to y=f([H_3O^3])  for the curve itself, starting from a concentration point close to the one sought. For example, the value of the first point ([H_3O^+]_0)  can be taken corresponding to to pH=0.1

It is therefore assumed that the approximate value of the root is the value of the abscissa:

[H_3O^+]_1= [H_3O^+]_0 -\frac{f([H_3O^+]_0)}{ f'([H_3O^+]_0)}

where the tangent line (of inclination  f'([H_3O^+]_0)) intersects the axis of [H_3O^+] in the corresponding interval at the pH range 1-14.  Iteratively, the new point at [H_3O^+]_1  is used to obtain a new approximation using the general formula:

[H_3O^+]_{n+1}= [H_3O^+]_n -\frac{f([H_3O^+]_n)}{ f'([H_3O^+]_n)}

which increases the accuracy of the root value. The procedure is stopped when the accuracy level is reached.

The program in BASIC language


The curves in this article were obtained with the BASIC program shown in the appendix of the Italian version of this article. This is an updated version of the program I mentioned in the introduction. I used the QB64 open-source compiler (https://www.qb64.org/portal) to develop the program. This BASIC compiler is compatible with the QuickBasic. QB64 first translates the program into C ++ which then compiles it with a C ++ compiler. Therefore the resulting executable is considerably faster than a BASIC interpreter.

This program version also allows the simulation of titrations with weak base or weak (mono) protic acid. The program shall use predetermined values for titration parameters which may be modified. Titration can be done step by step by adding aliquots of acid and bases or entering a negative number. 

The program is distributed for educational purposes and is not guaranteed to be free from programming bugs. So, if you find errors or have suggestions, feel free to comment. If you find it interesting, you could also develop it further.

Figure4: The initial screen of the program for selecting the titration
Figure 5: Screen for changing parameters.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.