Documentation

linsolve

Solve linear system of equations

Syntax

X = linsolve(A,B)
X = linsolve(A,B,opts)

Description

X = linsolve(A,B)solves the linear systemA*X = Busing LU factorization with partial pivoting when A is square and QR factorization with column pivoting otherwise. The number of rows ofAmust equal the number of rows ofB. IfAis m-by-n andBis m-by-k, thenXis n-by-k.linsolvereturns a warning ifAis square and ill conditioned or if it is not square and rank deficient.

[X, R] = linsolve(A,B)suppresses these warnings and returnsR, which is the reciprocal of the condition number ofAifAis square, or the rank ofAifAis not square.

X = linsolve(A,B,opts)solves the linear systemA*X = BorA'*X = B, using the solver that is most appropriate given the properties of the matrixA, which you specify inopts. For example, ifAis upper triangular, you can setopts.UT = trueto makelinsolveuse a solver designed for upper triangular matrices. IfAhas the properties inopts,linsolveis faster thanmldivide, becauselinsolvedoes not perform any tests to verify thatAhas the specified properties.

Notes

IfAdoes not have the properties that you specify inopts,linsolvereturns incorrect results and does not return an error message. If you are not sure whetherAhas the specified properties, usemldivideinstead.

For small problems, there is no speed benefit in usinglinsolveon triangular matrices as opposed to using themldividefunction.

TheTRANSAfield of theoptsstructure specifies the form of the linear system you want to solve:

  • If you setopts.TRANSA = false,linsolve(A,B,opts)solvesA*X = B.

  • If you setopts.TRANSA=true,linsolve(A,B,opts)solvesA'*X = B.

The following table lists all the field ofoptsand their corresponding matrix properties. The values of the fields ofoptsmust belogical和默认值fields isfalse.

Field Name

Matrix Property

LT

Lower triangular

UT

Upper triangular

UHESS

Upper Hessenberg

SYM

Real symmetric or complex Hermitian

POSDEF

Positive definite

RECT

General rectangular

TRANSA

Conjugate transpose — specifies whether the function solvesA*X = BorA'*X = B

The following table lists all combinations of field values inoptsthat are valid forlinsolve. A true/false entry indicates thatlinsolveaccepts either true or false.

LT

UT

UHESS

SYM

POSDEF

RECT

TRANSA

true

false

false

false

false

true/false

true/false

false

true

false

false

false

true/false

true/false

false

false

true

false

false

false

true/false

false

false

false

true

true/false

false

true/false

false

false

false

false

false

true/false

true/false

Examples

The following code solves the systemA'x = bfor an upper triangular matrixAusing bothmldivideandlinsolve.

A = triu(rand(5,3)); x = [1 1 1 0 0]'; b = A'*x; y1 = (A')\b opts.UT = true; opts.TRANSA = true; y2 = linsolve(A,b,opts) y1 = 1.0000 1.0000 1.0000 0 0 y2 = 1.0000 1.0000 1.0000 0 0

Note

If you are working with matrices having different properties, it is useful to create an options structure for each type of matrix, such asopts_sym. This way you do not need to change the fields whenever you solve a system with a different type of matrixA.

Extended Capabilities

See Also

Introduced before R2006a

Was this topic helpful?