linsolve

Solve symbolic linear equations in matrix form

Syntax

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

Description

example

````X = linsolve(A,B)` solves the matrix equation AX = B, where `A` is a symbolic matrix and `B` is a symbolic column vector.```

example

``````[X,R] = linsolve(A,B)``` also returns the reciprocal of the condition number of `A` if `A` is a square matrix. Otherwise, `linsolve` returns the rank of `A`.```

Examples

collapse all

Solve this system of linear equations in matrix form by using `linsolve`.

`$\left[\begin{array}{ccc}2& 1& 1\\ -1& 1& -1\\ 1& 2& 3\end{array}\right]\left[\begin{array}{c}\mathit{x}\\ \mathit{y}\\ \mathit{z}\end{array}\right]=\left[\begin{array}{c}2\\ 3\\ -10\end{array}\right]$`

```A = [ 2 1 1; -1 1 -1; 1 2 3]; B = [2; 3; -10]; X = linsolve(A,B)```
```X = 3×1 3 1 -5 ```

The solutions are $\mathit{x}=3$, $\mathit{y}=1$, and $\mathit{z}=-5$.

Compute the reciprocal of the condition number of the square coefficient matrix by using 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 =  $\left(\begin{array}{c}\frac{x}{a}\\ \frac{y}{a}\\ z\end{array}\right)$```
```R =  $\frac{1}{\mathrm{max}\left(|a|,1\right) \mathrm{max}\left(\frac{1}{|a|},1\right)}$```

If the coefficient matrix is rectangular, `linsolve` returns the rank of the coefficient matrix as the second output argument. Show this behavior.

```syms a b x y A = [a 0 1; 1 b 0]; B = [x; y]; [X,R] = linsolve(A,B)```
```Warning: Solution is not unique because the system is rank-deficient. ```
```X =  $\left(\begin{array}{c}\frac{x}{a}\\ -\frac{x-a y}{a b}\\ 0\end{array}\right)$```
`R = $2$`

Input Arguments

collapse all

Coefficient matrix, specified as a symbolic matrix.

Right side of equations, specified as a symbolic vector or matrix.

Output Arguments

collapse all

Solution, returned as a symbolic vector or matrix.

Reciprocal condition number or rank, returned as a symbolic number of expression. If `A` is a square matrix, `linsolve` returns the condition number of `A`. Otherwise, `linsolve` returns the rank of `A`.

collapse all

Matrix Representation of System of Linear Equations

A system of linear equations is as follows.

`$\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}$`

This system 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)$`

$\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`.

Version History

Introduced in R2012b