Main Content


Collect coefficients



collect(P) collects coefficients in P of the powers of the default variable of P. The default variable is found by symvar.


collect(P,expr) collects coefficients in P of the powers of the symbolic expression expr. If P is a vector or matrix, then collect acts element-wise on P. If expr is a vector, then collect finds coefficients in terms of all expressions in expr.


Collect Coefficients of Powers of Default Variable

Collect the coefficients of a symbolic expression.

syms x
coeffs = collect((exp(x) + x)*(x + 2))
coeffs =
x^2 + (exp(x) + 2)*x + 2*exp(x)

Because you did not specify the variable, collect uses the default variable defined by symvar. For this expression, the default variable is x.

symvar((exp(x) + x)*(x + 2),1)
ans =

Collect Coefficients of Powers of a Particular Variable

Collect coefficients of a particular variable by specifying the variable as the second argument to collect.

Collect coefficients of an expression in powers of x, and then in powers of y.

syms x y
coeffs_x = collect(x^2*y + y*x - x^2 - 2*x,x)
coeffs_y = collect(x^2*y + y*x - x^2 - 2*x,y)
coeffs_x =
(y - 1)*x^2 + (y - 2)*x
coeffs_y =
(x^2 + x)*y - x^2 - 2*x

Collect coefficients in powers of both x and y by specifying the second argument as a vector of variables.

syms a b
coeffs_xy = collect(a^2*x*y + a*b*x^2 + a*x*y + x^2,[x y])
coeffs_xy =
(a*b + 1)*x^2 + (a^2 + a)*x*y

Collect Coefficients in Terms of i and pi

Collect coefficients of an expression in terms of i, and then in terms of pi.

syms x y
coeffs_i = collect(2*x*i - 3*i*y,i)
coeffs_pi = collect(x*pi*(pi - y) + x*(pi + i) + 3*pi*y,pi)
coeffs_i =
(2*x - 3*y)*1i
coeffs_pi =
x*pi^2 + (x + 3*y - x*y)*pi + x*1i

Collect Coefficients of Symbolic Expressions and Functions

Collect coefficients of expressions and functions by specifying the second argument as an expression or function. Collect coefficients of multiple expressions or functions by using vector input.

Expand sin(x + 3*y) and collect coefficients of cos(y), and then of both sin(x) and sin(y).

syms x y
f = expand(sin(x + 3*y));
coeffs_cosy = collect(f,cos(y))
coeffs_cosy =
4*sin(x)*cos(y)^3 + 4*cos(x)*sin(y)*cos(y)^2 + (-3*sin(x))*cos(y) - cos(x)*sin(y)
coeffs_sinxsiny = collect(f,[sin(x) sin(y)])
coeffs_sinxsiny =
(4*cos(y)^3 - 3*cos(y))*sin(x) + (4*cos(x)*cos(y)^2 - cos(x))*sin(y)

Collect coefficients of the symbolic function y(x) in a symbolic expression.

syms y(x)
f = y^2*x + y*x^2 + y*sin(x) + x*y;
coeffs_y = collect(f,y)
coeffs_y(x) =
x*y(x)^2 + (x + sin(x) + x^2)*y(x)

Collect Coefficients for Each Element of Matrix

Call collect on a matrix. collect acts element-wise on the matrix.

syms x y
collect([(x + 1)*(y + 1), x^2 + x*(x -y); 2*x*y - x, x*y + x/y], x)
ans =
[ (y + 1)*x + y + 1, 2*x^2 - y*x]
[       (2*y - 1)*x, (y + 1/y)*x]

Collect Coefficients of Function Calls

Collect coefficients of calls to a particular function by specifying the function name as the second argument. Collect coefficients of function calls with respect to multiple functions by specifying the multiple functions as a cell array of character vectors.

Collect coefficients of calls to the sin function in F, where F contains multiple calls to different functions.

syms a b c d e f x
F = a*sin(2*x) + b*sin(2*x) + c*cos(x) + d*cos(x) + e*sin(3*x) + f*sin(3*x);
ans =
(a + b)*sin(2*x) + (e + f)*sin(3*x) + c*cos(x) + d*cos(x)

Collect coefficients of calls to both the sin and cos functions in F.

collect(F,{'sin' 'cos'})
ans =
(a + b)*sin(2*x) + (e + f)*sin(3*x) + (c + d)*cos(x)

Input Arguments

collapse all

Input expression, specified as a symbolic expression, function, vector, or matrix.

Expression in terms of which you collect the coefficients, specified as a symbolic number, variable, expression, function, or vector; a character vector; a cell array of character vectors.

Example: i, pi, x, sin(x), y(x), [sin(x) cos(y)], {'sin' 'cos'}.


  • collect returns an output that is syntactically different from the input expression (although the input and output expressions might look the same). For this reason, functions like isequal might not return true when checking for equality. Instead, use isAlways to prove equivalence between the input and output expressions.

Introduced before R2006a