# template for solving equilibrium problems # 1: provide equilibrium constants for all equilibrium reactions Kw = 1.00e-14 Ka = 1.75e-5 # 2: provide total concentration for all mass balance equations C = 0.1 # 3: set up master variable pH = seq(1, 14, 0.01) # 4: calculate concentrations for all species in system H3O = 10^-pH OH = Kw/H3O A = H3O - OH HA = H3O * A/Ka # 5: define an error function using charge or mass balance error = abs(C - HA - A) # 6: find index for the minimum error (error closest to zero) id = which.min(error) id # 7: report out all values pH[id] H3O[id] OH[id] HA[id] A[id] error[id] # 8: plot error function (if of interest) plot(pH, log10(error), type = "l", xlab = "pH", ylab = "error", lwd = 2, col = "blue") abline(v = pH[id], lty = 2, col = "red", lwd = 2)