Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 39 · Projects and Practice

Project: An Equilibrium Solver and pH Explorer

Build a general equilibrium solver for any reaction, then a weak-acid pH calculator and a species-distribution plot, using real formulas and constants.

In this project you turn the chapter into software. You will write three small programs: a general equilibrium solver that works for any single reaction, a weak-acid calculator that includes the contribution of water, and a plot showing how an acid and its conjugate base share the material as the pH changes. Everything is built from the formulas you have already used.

These programs are calculators built from textbook formulas. They assume ideal solutions (concentrations instead of activities) and a fixed temperature, so they are good for learning and for estimates, not for precise laboratory prediction.

Part 1: a general equilibrium solver

For any reaction a A + b B ⇌ c C + d D started from given amounts, all species change in proportion to their coefficients. Let ξ (xi) be the extent of reaction: each species changes by ν ξ, where ν is its coefficient (negative for reactants). Then

[X] = [X]₀ + ν ξ

and Q becomes a function of ξ. Reactants run out as ξ grows, so Q rises steadily with ξ. We want the ξ that makes Q = K. Because Q increases monotonically over the allowed range, simple bisection always finds it:

  1. Find the allowed range of ξ: no concentration may become negative.
  2. Bisect: evaluate Q(ξ) − K at the middle. If Q is too small, move the lower bound up; otherwise move the upper bound down.

Pure solids and liquids are simply left out of the dictionary.

Python A general equilibrium solver

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Examples 1 and 2 reproduce the answers from the ICE-table lesson ([HI] = 1.559 M and [NO2] = 0.593 M). Example 3 starts on the product side, so ξ is negative: the reaction runs in reverse to reach the same ratio. This is the same result you reasoned out with Q versus K.

Part 2: a weak-acid pH calculator

Part 1 solves one reaction, but real solutions have water ionizing as well. Here we solve the full charge balance for a monoprotic weak acid HA with total concentration C:

[H⁺] = [A⁻] + [OH⁻], with [A⁻] = C Ka / (Ka + [H⁺]) and [OH⁻] = Kw / [H⁺]

This one equation for [H⁺] is solved by bisection on pH. The solution works from very strong to very weak acids and from concentrated to extremely dilute.

Python A complete weak-acid pH calculator

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first check (0.10 M acetic acid, pH 2.88) agrees with the weak-acid lesson. The second shows that a 1 × 10⁻⁸ M strong acid gives a pH just below 7, matching the dilute-solution analysis from the pH lesson. Even the near-hopeless case of a very weak acid at very low concentration comes out near 7, as water dominates.

Part 3: who holds the proton? A distribution plot

For a weak acid HA, the fractions of the acid present as HA and as A⁻ depend only on pH and pKa:

α(HA) = [H⁺] / ([H⁺] + Ka) and α(A⁻) = Ka / ([H⁺] + Ka)

At pH = pKa both are 0.5 (this is the half-equivalence point and the center of a buffer). Below pKa − 1 the acid form dominates, above pKa + 1 the base form does. The plot makes the buffer range of pKa ± 1 visible.

Python Acid and base fractions against pH

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each curve is an S-shape that crosses 0.5 at its own pKa. The steep part of each curve (about pKa ± 1) is where that acid makes a good buffer.

Ideas to extend the project

  • Add a polyprotic option with two Ka values and solve the charge balance with both steps.
  • Combine Part 1 with Ksp: treat a dissolving solid as an equilibrium with no initial ions and compare with the shortcut formulas.
  • Add temperature: use the van 't Hoff equation from Le Chatelier's lesson to change K before solving.
  • Use the Nernst equation to draw how a cell voltage decays as Q grows.
  • Turn the solver into a small command-line or web tool on your own computer.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. In the solver, what does the extent of reaction ξ represent?

    Each species changes by ν ξ, so one number describes the whole composition change.
  2. 2. Why does bisection work for finding equilibrium?

    Because Q is monotonic in ξ over the allowed range, there is exactly one point where Q = K.
  3. 3. At pH = pKa, what fraction of a weak acid is present as its conjugate base?

    At pH = pKa, [H+] = Ka, so α(A-) = Ka/(Ka + Ka) = 0.5.
  4. 4. Why is the water term included in the pH calculator?

    At low acid concentrations, the hydronium from water is comparable to that from the acid.

Summary

  • The extent of reaction ξ turns any equilibrium into a one-variable problem that bisection solves.
  • Negative ξ simply means the reaction runs in reverse (Q > K at the start).
  • A full charge balance gives accurate pH for weak acids at any concentration.
  • Distribution plots show why buffers work within pKa ± 1.
  • Calculators like these rest on ideal-solution assumptions.

Congratulations: you have now used the core ideas of chemical equilibrium, acids and bases, redox and solubility. Return to the track page to review the earlier chapters.