You are here

An open-source, web-based math solver for solving multi-variable equilibrium problems in general chemistry


Natalie Ulrich, College of Arts and Sciences, Maryville University, St. Louis, MO.
Thomas Spudich, College of Arts and Sciences, Maryville University, St. Louis, MO.
Eileen Kowalski, Department of Chemistry and Life Science, United States Military Academy, West Point, NY.
Melinda Z. Kalainoff, Department of Chemistry and Life Science, United States Military Academy, West Point, NY.


We discuss our use of SageMathCell, a web-based, open-source math-solver, in conjunction with the systematic method, to solve problems involving multi-variable equilibrium reactions while avoiding the use of simplifying approximations. Students initially face a steep learning curve, but after about a week are able to solve fairly complex equilibrium problems in just minutes.  We find this to be an excellent first introduction to coding, which students will likely encounter elsewhere in their academic or work careers. For those instructors who use traditional “ICE” tables but still want to give their students exposure to coding, we also provide a representative example of simple SageMath code.


SageMathCell is a web interface for SageMath, an open-source mathematics software system that uses a Python-based language. Our students use SageMathCell to solve problems involving equilibrium reactions using the systematic method. The systematic method relies on an engineering model that incorporates the principles of charge and mole balance to allow one to write a series of algebraic equations to model chemical systems at equilibrium.1,2,3,4  This method is an extremely versatile alternative to ICE tables as it allows one to solve for multiple unknowns simultaneously while avoiding simplifying approximations. For example, the systematic method accounts for the autoionization of water in problems involving weak acid and weak base equilibria; in contrast, most general chemistry textbooks arbitrarily avoid using dilute solutions of acids and bases and instead choose relatively concentrated solutions of acids and bases with comparatively large K values in order to ignore the contributions of the autoionization of water. Table 1 lists pH values of weak acid solutions calculated using both the systematic method and ICE tables. Acetic acid is a commonly used example in textbooks because it has a relatively high Ka value. Even at very dilute concentrations, the respective calculated pH values are equivalent up to three decimal places. The calculated pH values start to vary significantly, however, for an acid such as hypoiodous acid (Ka = 2.3x10-11), even at fairly high concentrations.





[HA]initial (M)

Calculated pH

(systematic method)

Calculated pH

(ICE table)

Acetic acid






























Hypoiodous acid






























Table 1. Comparison of pH values calculated via the systematic method and ICE tables.


We first introduce our students to the systematic method and SageMath using problems involving a single equilibrium reaction. Below is a representative example of student work using the systematic method to solve a simple gas phase equilibrium problem with three variables (figure 1):

Example 1: A chemist adds 5.0 M hydrogen gas and 4.0 M iodine gas into a closed container. Determine the concentrations of all the gases once the system has achieved equilibrium.

Figure 1. Student work using the systematic method to solve a simple equilibrium problem.

After assigning variables to each species, the student defined the input (initial) and output (equilibrium) concentrations, along with the known Kc value. Because there are three unknowns, the student then wrote three equations:  the Kc expression and mole balance equations for hydrogen and iodine.

Below is the corresponding SageMathCell code for the above problem (Figure 2):

Figure 2. SageMathCell code to solve the equations developed in Figure 1.

Students are provided with the last two lines of code as a template (these lines tell SageMath to output solutions in decimal form). Students write the remainder of the code themselves by simply listing the variables and equations they wrote. SageMath often provides multiple solution sets, including any negative solutions, and students must choose the solution set that makes physical sense and report their answers in terms of the actual chemical species:


Figure 3. Solutions to Example 1 calculated from equations in Figure 1 and SageMathCell code in Figure 2.

It generally takes students about a week to become proficient at using SageMath. At first, students often make syntax errors because they forget to include coding elements like multiplication symbols, double equal signs, etc. Frustration levels are generally high during this week as this is usually the first time students have encountered coding, but this frustration usually subsides as students grow accustomed to using the software. Even given this initial steep learning curve, however, we find this to be an excellent first introduction to coding, which students will likely encounter at some other point in their academic or work careers.

Within 2-3 class periods, students are ready to move on to more complex equilibrium systems (e.g., pH buffers and acid-base titrations), where they are required to account for each reaction occurring in aqueous solution. Below is a representative example of student work using the systematic method to solve a pH buffer problem (figure 4):

Example 2: Find the pH after adding 0.025 L of 1.20 M HCl to a 1.200 L buffer solution that consists of 1.3 M sodium phenolate (C6H5ONa) and 1.2 M phenol (C6H5OH) (pKa of phenol = 10.00).

Figure 4. Student work using the systematic method to solve a more complicated buffer problem.

The student first wrote the equations for each reaction occurring in solution: the dissociation of a salt, the equilibrium reaction of a weak acid in water, the autoionization of water, and the dissociation of a strong base. The student then assigned variables to each species in solution, determined the input and output concentrations (while accounting for dilution), and wrote four equations to solve for the four unknowns.

Below is the corresponding SageMathCell code:

Figure 5. SageMathCell code to solve the equations developed in Figure 4.

This code allows for the direct calculation of the pH (the last solution in set 2).

This is as complex as the problems get for general chemistry students at West Point and Maryville. Students in Maryville’s analytical chemistry courses use SageMath to solve for up to twenty unknowns simultaneously. Because SageMath can also complete dilution and pH calculations, students do not have to rely on multiple computing devices. Additionally, once students have written code for the first step of a multi-part problem, they can easily modify the code to incorporate any new parameters. This is especially useful in titration problems, where typically using ICE tables there is a significant amount of work associated with each different point in the titration. Using the systematic method, students can solve for the pH at multiple points in an acid-base titration in just a few minutes.

If you are loathe to relinquish ICE tables, but still want to expose your students to simple coding in a chemistry class, your students could still use SageMath to bypass any algebra involving successive approximations or the quadratic equation. Here is what the SageMath code would look like for example 1 (Figure 1) using ICE tables:

Figure 6.  SageMathCell code to solve an ICE table for Example 1.

Your students could then use the above value of x (0.56 M) to calculate the actual equilibrium concentrations of each species (or you could even have SageMath do that work for you).

In conclusion, the systematic method, in conjunction with SageMath, are powerful tools for solving multi-variable problems involving chemical equilibrium reactions. Although students initially encounter a steep learning curve, with practice they can become proficient at solving even relatively complex equilibrium problems. This exposure to an engineering model for solving for multiple unknowns also helps students hone their problem-solving skills and provides students with a first experience in coding.


1) de Levie, R. (1999).  Aqueous acid-base equilibria and titrations.  Oxford University Primers, v. 80. New York: Oxford University Press.

2) Kalainoff, M., Lachance, R., Riegner, D., Biaglow, A. “A computer algebra approach to solving chemical equilibria in general chemistry.”  Primus: Problems, Resources & Issues in Mathematics Undergraduate Studies; May/Jun2012, Vol. 22 Issue 4, p284-302, 19p

3) Kalainoff, M., Ulrich, N., Kowalski, E., Spudich, T. Chemical Equilibrium using the Systematic Method, unpublished document.

4) Baeza-Baeza, J. J., García-Álvarez-Coque, M. C. J. Chem. Educ. 88, 2, 169-173.

05/03/18 to 05/05/18



It's amazing what powerful tools are available to everyone nowadays, and I read with interest how you incorporate them into your teaching. I have a comment about how you present the code, and a question about the number of unknowns.

My comment: It would be great to teach students about comments by breaking up the code into conceptual blocks. Figure 1 is easy to read, and for people new to coding, Figure 2 is not. I tried to take the structure in Figure 1 and put some comments into your code to make that structure visible in the code.

Also about readibility, but of the output, I have a suggestion of formating the output so that it is easier to find the relevant numbers (lines 16-20). This would work for all of your examples, so students would not have to worry about editing a template, they could just reuse that bit of code. The output would be:

Solution 1
  z : -1.5118891
  y : 4.7559445
  x : 5.7559445
Solution 2
  z : 1.1281281
  y : 3.4359360
  x : 4.4359360

My question is about teaching the number of unknowns vs the number of equations. How do you tell students how to find the equations related to stoichiometry? In an ICE table, there is usually one 'x', and all other final amounts or concentrations are related to the initial values via that 'x', i.e. nNa+, final = nNa+, init + 2x. Here, you sometimes have equations of the form xi + 2yi = x + 2y to incorporate stoichiometry, and in one case you have a charge balance. How do you guide students to find an appropriate set of equations that determines the solution, and how do you ensure there is no redundancy (i.e. two equations that say the same thing, or an equation that is a linear combination of two other equations)?




Natalie Ulrich's picture

Thank you very much for your input!

Your suggestion to use comments in the code is incredibly helpful – I will start implementing it the next time I teach the course. Thank you.

Excellent question. I can let Tom talk about how he avoids redundancy in more complex problems, but can address your question for problems involving 4-5 equations.

Redundancy in equations is actually fairly rare in General Chemistry problems, and usually only occurs in gas phase problems. To help ensure that students obtain unique equations, we guide them to write their equations in a specific order: first, any K expression(s), then a charge balance equation (if there are charged species present), then as many mole balance equations as necessary (which is where the possibility of redundancy usually occurs). Also note that if, for example, students have 4 unknowns, then they only need to write 4 equations. We purposely provide students with at least a couple of problems where redundant equations are an issue; if they don’t notice the redundancy as they’re writing their equations, SageMath will let them know.

To answer the questions about number of equations and unknowns, I follow the manual and emphasize to have the same number of equations as unknowns as well as no identical equations, which is rare in general chemistry as of now. The students that I have taught rarely miss identical equations. The manual, which is about 130 pages, goes into great depth covering charge balance and mole balance. Currently the manual is written to complement the general chemistry curriculum, but is easily could veer into much more complex systems. I do usually assign a take home exam problem in my quantitative analysis class with approximately 20 unknowns using this method, so it is possible. I like the formating suggestions -- thank you! I did try the code and have it here for all to review: Thanks! Tom

SDWoodgate's picture

This is very cool.  As I commented earlier, I decided that ICE (RICE) tables were not the way to go with equilibrium after I started working with the Chemistry Olympiad students who needed to be able to do multiple-equilibrium problems.  This approach which focuses on setting up the mass balance and charge balance equations and minimises the grinding through solving these emphasises the chemistry, and in my experience it is expressing the chemistry in mathematical form that is more of a blockage than the maths on its own.  It is a pity that you can only use single letter variables.  The code would be much more readable of you could for example say V(CH3CO2H) = 0.1.  This would make the connections to the chemistry stronger, and I am all for that.

Natalie Ulrich's picture

Oh, but you *can* use the notation you're talking about. Tom wrote some code for that approach a while back- let me go see if I can dig it up.

Natalie Ulrich's picture

Here it is.

I did run across some issues using variables with numbers in them. I explored several options, but overall the students found the best way to go was x, y, z, a, b, c, p & h for most of the variables they would need.

Xavier Prat-Resina's picture


Congratulations for succeeding at having chemistry students learn some programming. I tried in the past without much success and I know how hard it is. I have a couple of questions.

1) I see that sagemath has very powerful plotting tools. Have you tried to use a graph to show the different functions and how the intersection between those functions give the solution? ...A little bit like those "logarithmic concentration" diagrams taught in acid/base equilibria. I'm wondering if students may use the solver just as a blackbox, if that were the case, it may help students learning by having the graph to give a more visual alternative to the algebraic solution.

2) In what context are these activities taught? Is this during General Chemistry lecture? lab?



Natalie Ulrich's picture

1) Great idea! As far as I know, we haven't used graphical representations to show what's happening in solution. Students are indeed using the math solver as a black box, and having graphs would really reinforce the chemical connections. I will put this on my to-do list!

2) We introduce the Systematic Method in lecture, then students have the opportunity to apply the method in one of the labs (determination of the equilibrium constant for the formation of iron(III)thiocyanate). Watching the students figure out how to apply what they've learned in lecture to an actual, concrete problem is *awesome*. 

1) I haven't used graphical representations as we were just introduced to SageMath in the summer of 2017. We previously used Mathematica, but could not justify the software purchase if we could accomplish what we wanted without it. In learning SageMath and R (for baseball analytics, not chemistry) the past 2 years, I have not explored the graphing possibilities with SageMath, which are probably much better than what R provides.

2) Both Natalie and I offer the systematic method in lecture and I have the students apply the method they learn in lecture in lab. Not all the instructors at Maryville present the systematic method in lecture. I have taught general chemistry II lab for the first time in 6 years this semester and Natalie is right with the formation constant determination work. Additionally, I would enjoy the opportunity to teach a system that cannot be solved with conventional RICE methods to show the advantages, such as a complex titration or equilibrium process. But it would be unfair to those who have instructors at Maryville who teach the RICE method. We do have to provide some systematic method information to the lab instructors for grading. We have (had) some issues in lab where corresponding quizzes and material were written with a RICE twist, which made it impossible for students to solve without preparation with that particular technique. Additionally, I worry about the long term implementation with respect to standardized testing where the focus is the RICE method. That's where we have to do an exceptional job of teaching approximations.

I use SageMath at the upper-level (P-Chem). We use the graphing capabilities quite often. They are pretty good. At one time I helped with getting Jmol to do 3-D plots. That is still part of SageMath, but they are moving more towards other tools. The 2-D plotting is quite good and relatively easy to use, but the 3-D plotting is less convenient.

Check out the following functions:

point(...) for displaying points in 2d and 3d space.

plot(...) for curves in 2d

plot3d(...) for curves and surfaces in 3d.


Natalie Ulrich's picture

Great to know, thank you. Will check those out.