equationsToMatrix

Convert linear equations to matrix form

Description

example

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

example

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

example

A = equationsToMatrix(___) returns only the coefficient matrix of the system of equations.

Examples

collapse all

Convert a system of linear equations to matrix form. equationsToMatrix automatically detects the variables in the equations by using symvar. The returned coefficient matrix follows the variable order determined by symvar.

syms x y z
eqns = [x+y-2*z == 0,
        x+y+z == 1,
        2*y-z == -5];
[A,b] = equationsToMatrix(eqns)
vars = symvar(eqns)
A =
[ 1, 1, -2]
[ 1, 1,  1]
[ 0, 2, -1]
 
b =
  0
  1
 -5
 
vars =
[ x, y, z]

You can change the arrangement of the coefficient matrix by specifying other variable order.

vars = [x, z, y];
[A,b] = equationsToMatrix(eqns,vars)
A =
[ 1, -2, 1]
[ 1,  1, 1]
[ 0, -1, 2]
 
b =
  0
  1
 -5

Convert a linear system of equations to the matrix form by specifying independent variables. This is useful when the equation are only linear in some variables.

For this system, specify the variables as [s t] because the system is not linear in r.

syms r s t
eqns = [s-2*t+r^2 == -1
        3*s-t == 10];
vars = [s t];
[A,b] = equationsToMatrix(eqns,vars)
A =
[ 1, -2]
[ 3, -1]

b =
 - r^2 - 1
        10

Return only the coefficient matrix of the equations by specifying a single output argument.

syms x y z
eqns = [x+y-2*z == 0,
        x+y+z   == 1,
        2*y-z   == -5];
vars = [x y z];
A = equationsToMatrix(eqns,vars)
A =
[ 1, 1, -2]
[ 1, 1,  1]
[ 0, 2, -1]

Input Arguments

collapse all

Linear equations, specified as a vector of symbolic equations or expressions. Symbolic equations are defined by using the == operator, such as x + y == 1. For symbolic expressions, equationsToMatrix assumes that the right side is 0.

Equations must be linear in terms of vars.

Independent variables in eqns, specified as a vector of symbolic variables.

Output Arguments

collapse all

Coefficient matrix of the system of linear equations, specified as a symbolic matrix.

Vector containing the right sides of equations, specified as a symbolic matrix.

More About

collapse all

Matrix Representation of System of Linear Equations

A system of linear equations

a11x1+a12x2++a1nxn=b1a21x1+a22x2++a2nxn=b2am1x1+am2x2++amnxn=bm

can be represented as the matrix equation Ax=b. Here, A is the coefficient matrix.

A=(a11a1nam1amn)

b is the vector containing the right sides of equations.

b=(b1bm)

Introduced in R2012b