Accelerating the pace of engineering and science

# linsolve

Solve linear system of equations given in matrix form

## Syntax

X = linsolve(A,B)
[X,R] = linsolve(A,B)

## Description

X = linsolve(A,B) solves the matrix equation AX = B. In particular, if B is a column vector, linsolve solves a linear system of equations given in the matrix form.

[X,R] = linsolve(A,B) solves the matrix equation AX = B and returns the reciprocal of the condition number of A if A is a square matrix, and the rank of A otherwise.

## Input Arguments

 A Coefficient matrix. B Matrix or column vector containing the right sides of equations.

## Output Arguments

 X Matrix or vector representing the solution. R Reciprocal of the condition number of A if A is a square matrix. Otherwise, the rank of A.

## Examples

Define the matrix equation using the following matrices A and B:

syms x y z
A = [x 2*x y; x*z 2*x*z y*z+z; 1 0 1];
B = [z y; z^2 y*z; 0 0];

Use linsolve to solve this equation. Assigning the result of the linsolve call to a single output argument, you get the matrix of solutions:

X = linsolve(A, B)
X =
[       0,       0]
[ z/(2*x), y/(2*x)]
[       0,       0]

To return the solution and the reciprocal of the condition number of the square coefficient matrix, assign the result of the linsolve call to a vector of two output arguments:

syms a x y z
A = [a 0 0; 0 a 0; 0 0 1];
B = [x; y; z];
[X, R] = linsolve(A, B)
X =
x/a
y/a
z

R =
1/(max(abs(a), 1)*max(1/abs(a), 1))

If the coefficient matrix is rectangular, linsolve returns the rank of the coefficient matrix as the second output argument:

syms a b x y
A = [a 0 1; 1 b 0];
B = [x; y];
[X, R] = linsolve(A, B)
Warning: The system is rank-deficient. Solution is not
unique.
In sym.linsolve at 67
X =
x/a
-(x - a*y)/(a*b)
0
R =
2

expand all

### Matrix Representation of a System of Linear Equations

A system of linear equations

$\begin{array}{l}{a}_{11}{x}_{1}+{a}_{12}{x}_{2}+\dots +{a}_{1n}{x}_{n}={b}_{1}\\ {a}_{21}{x}_{1}+{a}_{22}{x}_{2}+\dots +{a}_{2n}{x}_{n}={b}_{2}\\ \cdots \\ {a}_{m1}{x}_{1}+{a}_{m2}{x}_{2}+\dots +{a}_{mn}{x}_{n}={b}_{m}\end{array}$

can be represented as the matrix equation $A\cdot \stackrel{\to }{x}=\stackrel{\to }{b}$, where A is the coefficient matrix:

$A=\left(\begin{array}{ccc}{a}_{11}& \dots & {a}_{1n}\\ ⋮& \ddots & ⋮\\ {a}_{m1}& \cdots & {a}_{mn}\end{array}\right)$

and $\stackrel{\to }{b}$ is the vector containing the right sides of equations:

$\stackrel{\to }{b}=\left(\begin{array}{c}{b}_{1}\\ ⋮\\ {b}_{m}\end{array}\right)$

### Tips

• If the solution is not unique, linsolve issues a warning, chooses one solution and returns it.

• If the system does not have a solution, linsolve issues a warning and returns X with all elements set to Inf.

• Calling linsolve for numeric matrices that are not symbolic objects invokes the MATLAB® linsolve function. This function accepts real arguments only. If your system of equations uses complex numbers, use sym to convert at least one matrix to a symbolic matrix, and then call linsolve.