Systems of Equations

Physics and mechanical engineering students spend a lot of time solving systems of equations (a.k.a. simultaneous equations), often in the context of free body diagrams. I’m taking a Statics course, and we’ve been doing these sorts of problems a lot. I wanted to write a post exploring what techniques are available for solving systems of equations, and what software tools you can use to solve them automatically.

Basically, there are three ways to solve systems of equations:

  • Graphically, by plotting and seeing where the equations intersect. For equations in two variables, this is quite practical, as you can assign one to x and the other to y on the 2D Cartesian plane. For 4 variables and greater though, I’m not sure if it’s possible.
  • Algebraically, through substitution (finding the value of a variable in terms of other variables, and substituting that value in for the variable), elimination (combining equations such that certain variables cancel out entirely), or a combination of both.
  • Using matrix operations, usually involving a technique called Gaussian elimination to get an initial matrix into a reduced form called reduced row echelon form. This is a very efficient method for complex systems of equations.

Let’s pick this simple system of equations to solve:

2x + y – z = 24
x + 2y + 3z = 11
y + z = 6

How could we solve this with software tools?

Mathematica

Using the Solve function makes this a cinch in Mathematica. Specify a list of equations as your first argument, and a list of variables as your second argument:

In[1]:= Solve[{2x + y - 2z == 24, x + 2y + 3z == 11,
          y + z == 6}, {x, y, z}]
out[1]= {{x -> 3, y -> 10, z -> -4}}

TI Calculators

On TI calculators, you can express the system of equations as a matrix, like so:

\left( \begin{array}{cccc}  2 & 1 & -2 & 24 \\  1 & 2 & 3 & 11 \\  0 & 1 & 1 & 6\end{array} \right)\

You can see above that each row is one of our equations, and each column is the x, y, z and right-hand value, respectively.

We can then reduce that matrix to reduced row echelon form, where each row directly expresses the value of a single variable. The resulting matrix will be in this format:

\left( \begin{array}{cccc}  1 & 0 & 0 & x \\  0 & 1 & 0 & y \\  0 & 0 & 1 & z\end{array} \right)\

Solving for several variables is as simple as inputting the matrix of equations on our calculator, then using the RREF (reduced row echelon form) function to transform the matrix. Here’s a video showing the steps (full-screen to see the details better):

As you can see, the last column of the resulting matrix gives us 3, 10, and -4, which are the values for x, y, and z, respectively.

Newer TI calculators (TI-85 and later) have simultaneous equation solvers that make this process easier. Check your instruction manual or Google.

Python

There’s an excellent module for symbolic manipulation called SymPy, which can solve various types of complicated equations. Solving a system of equations with SymPy is pretty easy.

After downloading and installing SymPy, fire up the Python interpreter.

First, we need the Symbol class to define a symbol, and the solve function from the solvers sub-module to solve a system of equations. Let’s import these:

>>> from sympy import Symbol
>>> from sympy.solvers import solve

Now, to represent our system of equations, we’ll first declare the symbols we’re going to use:

>>> x = Symbol('x')
>>> y = Symbol('y')

Finally, we give the solve function two arguments: a list of expressions that equal zero, and a list of variables for which to solve:

>>> solve([2*x + y - 2*z - 24, x + 2*y + 3*z - 11,
      y + z + -6], [x, y, z])
{x: 3, y: 10, z: -4}

Do you have any tips for solving systems of equations with these tools or any others? I’m still looking for a clean way to do it in Matlab/Octave. Share your thoughts and tricks in the comments.