The goal is to solve the linear system

\begin{align} 3x-y+4z &= 6\\ y + 8z &=0\\ -2x + y &=-4 \end{align}

Note the matrix of coefficients on the left hand side of the '$=$' signs:

\begin{equation*} A= \begin{pmatrix} \phantom{-}3 & -1 & 4\\ \phantom{-}0 & \phantom{-}1 & 8\\ -2 & \phantom{-}1 & 0 \end{pmatrix} \end{equation*}

Note also the vector formed by the terms on the right hand sides of the '$=$' signs:

\begin{equation*} b= \begin{pmatrix} \phantom{-}6\\ \phantom{-}0\\ -4 \end{pmatrix} \end{equation*}

Now squash those into a single extended matrix

\begin{equation*} E= \begin{pmatrix} \phantom{-}3 & -1 & 4&\phantom{-}6\\ \phantom{-}0 & \phantom{-}1 & 8&\phantom{-}0\\ -2 & \phantom{-}1 & 0&-4 \end{pmatrix} \end{equation*}

This matrix encodes all of the information you need to pin down the system, and hence solve it for the unknowns $x$, $y$ and $z$. We are learning how to do this "by hand", but you can always check your work by computer.

In [1]:
from sympy import *
from sympy.solvers.solveset import linsolve
In [2]:
x, y, z = symbols('x, y, z')
In [3]:
linsolve(Matrix(([3, -1, 4, 6], [0, 1, 8, 0], [-2, 1, 0, -4])), (x, y, z))
Out[3]:
$\displaystyle \left\{\left( 2 - 4 z, \ - 8 z, \ z\right)\right\}$

That answer means that

  • $z$ can be chosen arbitrarily
  • $y$ will then be $-8z$
  • and $x$ will be equal to $2-4z$

So the solution is not unique: you have one degree of freedom in specifying it.

Most importantly: how did I know to do this? My day-to-day work does not involve matrix/linear algebra in Python, so I looked it up. As a first start, simply do a Google search: try searching for solving linear systems in sympy (since I knew SymPy is what I had on hand). One of the first few links sent me to a SymPy manual page on "solvers", i.e. subsystems specializing in solving equations. These are not all about systems of linear equations, but I scanned around that page until I found out about linsolve, which sounded promising. Then I read on and applied what I learned.

As important as anything else you learn is the very skill of learning; this is one tiny example of how to work your way up out of unfamiliarity with a tool.

In [ ]: