Accelerating the pace of engineering and science

# equationsToMatrix

Convert set of linear equations to matrix form

## Syntax

[A,b] = equationsToMatrix(eqns,vars)
[A,b] = equationsToMatrix(eqns)
A = equationsToMatrix(eqns,vars)
A = equationsToMatrix(eqns)

## Description

[A,b] = equationsToMatrix(eqns,vars) converts eqns to the matrix form. Here eqns must be linear equations in vars.

[A,b] = equationsToMatrix(eqns) converts eqns to the matrix form. Here eqns must be a linear system of equations in all variables that symvar finds in these equations.

A = equationsToMatrix(eqns,vars) converts eqns to the matrix form and returns only the coefficient matrix. Here eqns must be linear equations in vars.

A = equationsToMatrix(eqns) converts eqns to the matrix form and returns only the coefficient matrix. Here eqns must be a linear system of equations in all variables that symvar finds in these equations.

## Input Arguments

 eqns Vector of equations or equations separated by commas. Each equation is either a symbolic equation defined by the relation operator == or a symbolic expression. If you specify a symbolic expression (without the right side), equationsToMatrix assumes that the right side is 0. Equations must be linear in terms of vars. vars Independent variables of eqns. You can specify vars as a vector. Alternatively, you can list variables separating them by commas. Default: Variables determined by symvar

## Output Arguments

 A Coefficient matrix of the system of linear equations. b Vector containing the right sides of equations.

## Examples

Convert this system of linear equations to the matrix form. To get the coefficient matrix and the vector of the right sides of equations, assign the result to a vector of two output arguments:

```syms x y z
[A, b] = equationsToMatrix([x + y - 2*z == 0, x + y + z == 1,...
2*y - z + 5 == 0], [x, y, z])```
```A =
[ 1, 1, -2]
[ 1, 1,  1]
[ 0, 2, -1]

b =
0
1
-5```

Convert this system of linear equations to the matrix form. Assigning the result of the equationsToMatrix call to a single output argument, you get the coefficient matrix. In this case, equationsToMatrix does not return the vector containing the right sides of equations:

```syms x y z
A = equationsToMatrix([x + y - 2*z == 0, x + y + z == 1,...
2*y - z + 5 == 0], [x, y, z])```
```A =
[ 1, 1, -2]
[ 1, 1,  1]
[ 0, 2, -1]```

Convert this linear system of equations to the matrix form without specifying independent variables. The toolbox uses symvar to identify variables:

```syms s t
[A, b] = equationsToMatrix([s - 2*t + 1 == 0, 3*s - t == 10])```
```A =
[ 1, -2]
[ 3, -1]

b =
-1
10```

Find the vector of variables determined for this system by symvar:

`X = symvar([s - 2*t + 1 == 0, 3*s - t == 10])`
```X =
[ s, t]```

Convert X to a column vector:

`X = X.'`
```X =
s
t```

Verify that A, b, and X form the original equations:

`A*X == b`
```ans =
s - 2*t == -1
3*s - t == 10```

If the system is only linear in some variables, specify those variables explicitly:

```syms a s t
[A, b] = equationsToMatrix([s - 2*t + a == 0, 3*s - a*t == 10], [t, s])```
```A =
[ -2, 1]
[ -a, 3]

b =
-a
10```

You also can specify equations and variables all together, without using vectors and simply separating each equation or variable by a comma. Specify all equations first, and then specify variables:

```syms x y
[A, b] = equationsToMatrix(x + y == 1, x - y + 1, x, y)```
```A =
[ 1,  1]
[ 1, -1]

b =
1
-1```

Now change the order of the input arguments as follows. equationsToMatrix finds the variable y, then it finds the expression x — y + 1. After that, it assumes that all remaining arguments are equations, and stops looking for variables. Thus, equationsToMatrix finds the variable y and the system of equations x + y = 1, x = 0, x - y + 1 = 0:

`[A, b] = equationsToMatrix(x + y == 1, x, x - y + 1, y)`
```A =
1
0
-1

b =
1 - x
-x
- x - 1```

If you try to convert a nonlinear system of equations, equationsToMatrix throws an error:

```syms x y
[A, b] = equationsToMatrix(x^2 + y^2 == 1, x - y + 1, x, y)```
```Error using symengine (line 56)
Cannot convert to matrix form because
the system does not seem to be linear.```

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 you specify equations and variables all together, without dividing them into two vectors, specify all equations first, and then specify variables. If input arguments are not vectors, equationsToMatrix searches for variables starting from the last input argument. When it finds the first argument that is not a single variable, it assumes that all remaining arguments are equations, and therefore stops looking for variables.