This is documentation for Mathematica 6, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

Solving Linear Systems

Many calculations involve solving systems of linear equations. In many cases, you will find it convenient to write down the equations explicitly, and then solve them using Solve.
In some cases, however, you may prefer to convert the system of linear equations into a matrix equation, and then apply matrix manipulation operations to solve it. This approach is often useful when the system of equations arises as part of a general algorithm, and you do not know in advance how many variables will be involved.
A system of linear equations can be stated in matrix form as m.x=b, where x is the vector of variables.
Note that if your system of equations is sparse, so that most of the entries in the matrix m are zero, then it is best to represent the matrix as a SparseArray object. As discussed in "Sparse Arrays: Linear Algebra", you can convert from symbolic equations to SparseArray objects using CoefficientArrays. All the functions described here work on SparseArray objects as well as ordinary matrices.
LinearSolve[m,b]a vector x which solves the matrix equation m.xb
NullSpace[m]a list of linearly independent vectors whose linear combinations span all solutions to the matrix equation m.x0
MatrixRank[m]the number of linearly independent rows or columns of m
RowReduce[m]a simplified form of m obtained by making linear combinations of rows

Solving and analyzing linear systems.

Here is a 2x2 matrix.
Click for copyable input
This gives two linear equations.
Click for copyable input
You can use Solve directly to solve these equations.
Click for copyable input
You can also get the vector of solutions by calling LinearSolve. The result is equivalent to the one you get from Solve.
Click for copyable input
Another way to solve the equations is to invert the matrix m, and then multiply {a, b} by the inverse. This is not as efficient as using LinearSolve.
Click for copyable input
RowReduce performs a version of Gaussian elimination and can also be used to solve the equations.
Click for copyable input
If you have a square matrix m with a non-zero determinant, then you can always find a unique solution to the matrix equation m.x=b for any b. If, however, the matrix m has determinant zero, then there may be either no vector, or an infinite number of vectors x which satisfy m.x=b for a particular b. This occurs when the linear equations embodied in m are not independent.
When m has determinant zero, it is nevertheless always possible to find non-zero vectors x that satisfy m.x=0. The set of vectors x satisfying this equation form the null space or kernel of the matrix m. Any of these vectors can be expressed as a linear combination of a particular set of basis vectors, which can be obtained using NullSpace[m].
Here is a simple matrix, corresponding to two identical linear equations.
Click for copyable input
The matrix has determinant zero.
Click for copyable input
LinearSolve cannot find a solution to the equation m.x=b in this case.
Click for copyable input
There is a single basis vector for the null space of m.
Click for copyable input
Multiplying the basis vector for the null space by m gives the zero vector.
Click for copyable input
There is only 1 linearly independent row in m.
Click for copyable input
NullSpace and MatrixRank have to determine whether particular combinations of matrix elements are zero. For approximate numerical matrices, the Tolerance option can be used to specify how close to zero is considered good enough. For exact symbolic matrices, you may sometimes need to specify something like ZeroTest->(FullSimplify[#]0&) to force more to be done to test whether symbolic expressions are zero.
Here is a simple symbolic matrix with determinant zero.
Click for copyable input
The basis for the null space of m contains two vectors.
Click for copyable input
Multiplying m by any linear combination of these vectors gives zero.
Click for copyable input
An important feature of functions like LinearSolve and NullSpace is that they work with rectangular, as well as square, matrices.
When you represent a system of linear equations by a matrix equation of the form m.x=b, the number of columns in m gives the number of variables, and the number of rows gives the number of equations. There are a number of cases.
number of equations less than the number of variables; no solutions or many solutions may exist
number of equations more than the number of variables; solutions may or may not exist
number of independent equations equal to the number of variables, and determinant non-zero; a unique solution exists
at least one solution exists
no solutions exist

Classes of linear systems represented by rectangular matrices.

This asks for the solution to the inconsistent set of equations x=1 and x=0.
Click for copyable input
This matrix represents two equations, for three variables.
Click for copyable input
LinearSolve gives one of the possible solutions to this underdetermined set of equations.
Click for copyable input
When a matrix represents an underdetermined system of equations, the matrix has a non trivial null space. In this case, the null space is spanned by a single vector.
Click for copyable input
If you take the solution you get from LinearSolve, and add any linear combination of the basis vectors for the null space, you still get a solution.
Click for copyable input
The number of independent equations is the rank of the matrix MatrixRank[m]. The number of redundant equations is Length[NullSpace[m]]. Note that the sum of these quantities is always equal to the number of columns in m.
LinearSolve[m]generate a function for solving equations of the form m.x=b

Generating LinearSolveFunction objects.

In some applications, you will want to solve equations of the form m.x=b many times with the same m, but different b. You can do this efficiently in Mathematica by using LinearSolve[m] to create a single LinearSolveFunction that you can apply to as many vectors as you want.
This creates a LinearSolveFunction.
Click for copyable input
You can apply this to a vector.
Click for copyable input
You get the same result by giving the vector as an explicit second argument to LinearSolve.
Click for copyable input
But you can apply f to any vector you want.
Click for copyable input
LeastSquares[m,b]give a vector x that solves the least-squares problem m.xb

Solving least-squares problems.

This linear system is inconsistent.
Click for copyable input
LeastSquares finds a vector x that minimizes m.x-b in the least-squares sense.
Click for copyable input