Main Content


Convert linear equations to matrix form



[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.


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


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


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)
A = 

(11-211102-1)[sym(1), sym(1), -sym(2); sym(1), sym(1), sym(1); sym(0), sym(2), -sym(1)]

b = 

(01-5)[sym(0); sym(1); -sym(5)]

vars = symvar(eqns)
vars = (xyz)[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-211110-12)[sym(1), -sym(2), sym(1); sym(1), sym(1), sym(1); sym(0), -sym(1), sym(2)]

b = 

(01-5)[sym(0); sym(1); -sym(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-23-1)[sym(1), -sym(2); sym(3), -sym(1)]

b = 

(-r2-110)[- r^2 - 1; sym(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 = 

(11-211102-1)[sym(1), sym(1), -sym(2); sym(1), sym(1), sym(1); sym(0), sym(2), -sym(1)]

Consider the following system of linear equations that are functions of time:


Declare the system of equations.

syms x(t) y(t) z(t) u(t) v(t)
eqn1 = 2*x + y + z == 2*u;
eqn2 = -x + y - z == v;
eqn3 = x + 2*y + 3*z == -10;
eqn = [eqn1; eqn2; eqn3]
eqn(t) = 

(2x(t)+y(t)+z(t)=2u(t)y(t)-x(t)-z(t)=v(t)x(t)+2y(t)+3z(t)=-10)[2*x(t) + y(t) + z(t) == 2*u(t); y(t) - x(t) - z(t) == v(t); x(t) + 2*y(t) + 3*z(t) == -10]

Specify the independent variables x(t), y(t), and z(t) in the equations as a symbolic vector vars. Use the equationsToMatrix function to convert the system of equations into the matrix form.

vars = [x(t); y(t); z(t)];
[A,b] = equationsToMatrix(eqn,vars)
A = 

(211-11-1123)[sym(2), sym(1), sym(1); -sym(1), sym(1), -sym(1); sym(1), sym(2), sym(3)]

b = 

(2u(t)v(t)-10)[2*u(t); v(t); -sym(10)]

Solve the matrix form of the equations using the linsolve function.

X = linsolve(A,b)
X = 

(10u(t)9-v(t)9+2094u(t)9+5v(t)9-109-2u(t)3-v(t)3-103)[(10*u(t))/9 - v(t)/9 + sym(20/9); (4*u(t))/9 + (5*v(t))/9 - sym(10/9); - (2*u(t))/3 - v(t)/3 - sym(10/3)]

Evaluate the z(t) solution for the functions u(t)=cos(t) and v(t)=sin(2t). Plot the z(t) solution.

zSol = subs(X(3),[u(t) v(t)],[cos(t) sin(2*t)])
zSol = 

-sin(2t)3-2cos(t)3-103- sin(2*t)/3 - (2*cos(t))/3 - sym(10/3)


Figure contains an axes. The axes contains an object of type functionline.

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 or symbolic functions.

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


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


b is the vector containing the right sides of equations.


Introduced in R2012b