finds an x that solves the matrix equation m.x==b.


generates a LinearSolveFunction[] that can be applied repeatedly to different b.

Details and Options

  • LinearSolve works on both numerical and symbolic matrices, as well as SparseArray objects.
  • The argument b can be either a vector or a matrix. »
  • The matrix m can be square or rectangular. »
  • LinearSolve[m] and LinearSolveFunction[] provide an efficient way to solve the same approximate numerical linear system many times.
  • LinearSolve[m,b] is equivalent to LinearSolve[m][b].
  • For underdetermined systems, LinearSolve will return one of the possible solutions; Solve will return a general solution. »
  • LinearSolve has the following options and settings:
  • MethodAutomaticmethod to use
    Modulus0whether to solve modulo n
    ZeroTestAutomatictest to determine when expressions are zero
  • The ZeroTest option only applies to exact and symbolic matrices.
  • With Method->Automatic, the method is automatically selected depending upon input.
  • Explicit Method settings for exact and symbolic matrices include:
  • "CofactorExpansion"Laplace cofactor expansion
    "DivisionFreeRowReduction"Bareiss method of division-free row reduction
    "OneStepRowReduction"standard row reduction
  • Explicit Method settings for approximate numeric matrices include:
  • "Banded"banded matrix solver
    "Cholesky"Cholesky method for positive definite Hermitian matrices
    "Krylov"iterative Krylov sparse solver
    "Multifrontal"direct sparse LU decomposition
    "Pardiso"parallel direct sparse solver


open allclose all

Basic Examples  (2)

With no righthand side, a LinearSolveFunction is returned:

Scope  (6)

Find the solution to with exact arithmetic:

Find the solution using numerical methods with machine precision:

Find the solution using 20digit arbitrary precision:

Find a solution for a singular matrix:

A solution is not always available:

Find a solution for a rectangular matrix:

Solve for when is a matrix:

When no righthand side for is given, a LinearSolveFunction is returned:

This contains data to solve the problem quickly for a few values of :

Sparse methods are used for sparse matrices:

Options  (7)

Method  (6)

"Banded"  (1)

Solve using a banded matrix method:

Check a relative error of the computed solution:

"Cholesky"  (1)

Solve using the Cholesky decomposition:

Check a relative error of the computed solution:

"Krylov"  (2)

The following suboptions can be specified for the method "Krylov":

  • "BasisSize"the size of the Krylov basis (GMRES only)
    "MaxIterations"the maximum number of iterations
    "Method"methods to be used
    "Preconditioner"which preconditioner to apply
    "PreconditionerSide"how to apply a preconditioner ("Left" or "Right")
    "ResidualNormFunction"A norm function that computes a norm of the residual of the solution
    "StartingVector"the initial vector to start iterations
    "Tolerance"the tolerance used to terminate iterations
  • Possible settings for "Method" include:

  • "BiCGSTAB"iterative method for arbitrary square matrices
    "ConjugateGradient"iterative method for Hermitian positive definite matrices
    "GMRES"iterative method for arbitrary square matrices
  • Possible settings for "Preconditioner" include:

  • "ILU0"a preconditioner based on an incomplete LU factorization of the original matrix without fill-in
    "ILUT"a variant of ILU0 with fill-in
    "ILUTP"a variant of ILUT with column permutation
  • Possible suboptions for "Preconditioner" include:

  • "FillIn"upper bound on the number of additional nonzero elements in a row introduced by the ILUT preconditioner
    "PermutationTolerance"when to permute columns
    "Tolerance"drop tolerance (any element of magnitude smaller than this tolerance is treated as zero)
  • Solve using a Krylov method:

    Check a relative error of the computed solution:

    "Multifrontal"  (1)

    Solve using a direct multifrontal method:

    Check a relative error of the computed solution:

    "Pardiso"  (1)

    Solve using a direct multifrontal method:

    Check a relative error of the computed solution:

    Modulus  (1)

    Find the solution x to m.x==b modulo 47:

    Verify the solution:

    Applications  (2)

    Newton's method for finding a root of a multivariate function:

    Approximately solve the boundary value problem :

    Show the error compared with the exact solution:

    Properties & Relations  (4)

    m is a 3×3 matrix:

    A system of linear equations:

    The solution computed by Solve:

    The solution computed by LinearSolve:

    Verify that they are the same:

    If is nonsingular, the solution of is the inverse of when is the identity matrix:

    In this case there is no solution to :

    Use LeastSquares to minimize :

    Compare to general minimization:

    There are multiple solutions to if is singular:

    LinearSolve returns only one solution:

    Use NullSpace to get the complete spanning set of solutions:

    Possible Issues  (2)

    Solution found for an underdetermined system is not unique:

    All solutions are found by Solve:

    With ill-conditioned matrices, numerical solutions may not be sufficiently accurate:

    The solution is more accurate if sufficiently high precision is used:

    Neat Examples  (3)

    Solve 100,000 equations using a direct method:

    Solve a million equations using an iterative method:

    Check a relative error of the solution:

    Solve the same system of equations using a banded matrix method:

    Check a relative error of the solution:

    Introduced in 1988
    Updated in 1996