Skip to main content

Linear Algebra with SageMath: Learn math with open-source software

Section 4.2 Systems of Equations with Matrices

As an introduction to solving equations in Sage, we worked with equations symbolically. Now, we will learn how to solve systems of equations with vectors and matrices.
Let’s solve the following system of equations:
\begin{align*} \amp x + 2y \amp = 4\\ \amp y - z \amp = 0\\ \amp x + 2z \amp = 4 \end{align*}
In Sage, we can create an coefficient matrix using the matrix function.
Next, provide a list of constants to the vector function.
Finally, call the solve_right method on the matrix and pass the solution vector as its argument.
The output (4, 0, 0) expresses one solution to the system:
\begin{gather*} x = 4, \; y = 0, \; z = 0 \end{gather*}

Section 4.2.1 Differences in Solutions

To find all solutions we first need to augment matrix \(A\) with vector \(b\text{.}\) This is done with the augment method.
To solve this new matrix, call the rref method to output the matrix in reduced row echelon form.
The row of zeros at the bottom indicates that the system has infinitely many solutions.
These methods also differ in how they handle a system with no solutions. For example if we instead solve for a vector \(v = (1,2,8)\) with the solve_right method, an error will be thrown.
Where as the rref method will return a matrix which represents an inconsistent system.