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

Documentation / Mathematica / The Mathematica Book / Advanced Mathematics in Mathematica / Linear Algebra /

3.7.8 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 , where is the vector of variables.

Note that if your system of equations is sparse, so that most of the entries in the matrix are zero, then it is best to represent the matrix as a SparseArray object. As discussed in Section 3.7.12, you can convert from symbolic equations to SparseArray objects using CoefficientArrays. All the functions described in this section work on SparseArray objects as well as ordinary matrices.

Solving and analyzing linear systems.

Here is a matrix.

In[1]:= m = {{1, 5}, {2, 1}}

Out[1]=

This gives two linear equations.

In[2]:= m . {x, y} == {a, b}

Out[2]=

You can use Solve directly to solve these equations.

In[3]:= Solve[ %, {x, y} ]

Out[3]=

You can also get the vector of solutions by calling LinearSolve. The result is equivalent to the one you get from Solve.

In[4]:= LinearSolve[m, {a, b}]

Out[4]=

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.

In[5]:= Inverse[m] . {a, b}

Out[5]=

RowReduce performs a version of Gaussian elimination and can also be used to solve the equations.

In[6]:= RowReduce[{{1, 5, a}, {2, 1, b}}]

Out[6]=

If you have a square matrix with a non-zero determinant, then you can always find a unique solution to the matrix equation for any . If, however, the matrix has determinant zero, then there may be either no vector, or an infinite number of vectors which satisfy for a particular . This occurs when the linear equations embodied in are not independent.

When has determinant zero, it is nevertheless always possible to find non-zero vectors that satisfy . The set of vectors satisfying this equation form the null space or kernel of the matrix . 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.

In[7]:= m = {{1, 2}, {1, 2}}

Out[7]=

The matrix has determinant zero.

In[8]:= Det[ m ]

Out[8]=

LinearSolve cannot find a solution to the equation in this case.

In[9]:= LinearSolve[m, {a, b}]

Out[9]=

There is a single basis vector for the null space of m.

In[10]:= NullSpace[ m ]

Out[10]=

Multiplying the basis vector for the null space by m gives the zero vector.

In[11]:= m . %[[1]]

Out[11]=

There is only 1 linearly independent row in m.

In[12]:= MatrixRank[ m ]

Out[12]=

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.

In[13]:= m = {{a, b, c}, {2 a, 2 b, 2 c}, {3 a, 3 b, 3 c}}

Out[13]=

The basis for the null space of m contains two vectors.

In[14]:= NullSpace[m]

Out[14]=

Multiplying m by any linear combination of these vectors gives zero.

In[15]:= Simplify[m . (x %[[1]] + y %[[2]])]

Out[15]=

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 , the number of columns in gives the number of variables, and the number of rows gives the number of equations. There are a number of cases.

Classes of linear systems represented by rectangular matrices.

This asks for the solution to the inconsistent set of equations and .

In[16]:= LinearSolve[{{1}, {1}}, {1, 0}]

Out[16]=

This matrix represents two equations, for three variables.

In[17]:= m = {{1, 3, 4}, {2, 1, 3}}

Out[17]=

LinearSolve gives one of the possible solutions to this underdetermined set of equations.

In[18]:= v = LinearSolve[m, {1, 1}]

Out[18]=

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.

In[19]:= NullSpace[m]

Out[19]=

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.

In[20]:= m . (v + 4 %[[1]])

Out[20]=

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.

Generating LinearSolveFunction objects.

In some applications, you will want to solve equations of the form many times with the same , but different . 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.

In[21]:= f = LinearSolve[{{1, 4}, {2, 3}}]

Out[21]=

You can apply this to a vector.

In[22]:= f[{5, 7}]

Out[22]=

You get the same result by giving the vector as an explicit argument to LinearSolve.

In[23]:= LinearSolve[{{1, 4}, {2, 3}}, {5, 7}]

Out[23]=

But you can apply f to any vector you want.

In[24]:= f[{-5, 9}]

Out[24]=