Main Content

collect

Collect coefficients of identical powers

Description

C = collect(P) collects the coefficients of identical powers of the default variable in the symbolic input P. The default variable in P is determined by symvar.

example

C = collect(P,expr) collects the coefficients of identical powers in terms of the specified expression. 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.

example

Examples

collapse all

Collect the coefficients of identical powers of the default variable in a symbolic expression. Here, collect returns the expression as a polynomial in terms of x by collecting the coefficients of x^2 and x.

syms x
P = (exp(x) + x)*(x + 2)
P = x+exx+2
C = collect(P)
C = x2+ex+2x+2ex

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

defaultvar = symvar(P)
defaultvar = x

Collect the coefficients of identical powers of a specified variable in a symbolic expression by specifying the variable as the second argument to collect.

Create a symbolic expression in terms of the variables x and y.

syms x y
P = x^2*y + y*x - x^2 - 2*x
P = xy-2x+x2y-x2

Collect the coefficients of identical powers of x. Here, collect returns the expression as a polynomial in terms of x by collecting the coefficients of x^2 and x.

Cx = collect(P,x)
Cx = y-1x2+y-2x

Collect the coefficients of identical powers of y. Here, collect returns the expression as a polynomial in terms of y by collecting the coefficients of y.

Cy = collect(P,y)
Cy = x2+xy-x2-2x

You can also collect coefficients in terms of multiple variables by specifying the second argument as a vector of variables. Create another symbolic expression in terms of the variables x and y. Then collect the coefficients of identical powers of x and y. Here, collect returns the expression as a polynomial in terms of two variables, x and y, by collecting the coefficients of x^2 and x*y.

syms a b
Q = a^2*x*y + a*b*x^2 + a*x*y + x^2
Q = ya2x+bax2+yax+x2
Cxy = collect(Q,[x y])
Cxy = ab+1x2+a2+axy

Collect the coefficients of a symbolic expression in terms of i, and then in terms of pi.

syms x y
P = y*pi*(pi - 1i) + x*(pi + 1i) + 3*pi
P = 3π+xπ+i+πyπ-i
Ci = collect(P,1i)
Ci = x-πyi+3π+πx+yπ2
Cpi = collect(P,pi)
Cpi = yπ2+x+3-yiπ+xi

You can specify the symbolic expression or function in terms of which you collect coefficients as the second argument of collect.

Expand the expression sin(x + 3*y) by using expand. Then collect the coefficients in terms of sin(x).

syms x y
P = expand(sin(x + 3*y))
P = 4sin(x)cos(y)3+4cos(x)sin(y)cos(y)2-3sin(x)cos(y)-cos(x)sin(y)
C = collect(P,sin(x))
C = 4cos(y)3-3cos(y)sin(x)+4cos(x)cos(y)2sin(y)-cos(x)sin(y)

Collect the coefficients in terms of both sin(x) and sin(y) by specifying a vector input.

Cxy = collect(P,[sin(x) sin(y)])
Cxy = 4cos(y)3-3cos(y)sin(x)+4cos(x)cos(y)2-cos(x)sin(y)

You can also collect coefficients in terms of the symbolic function y(x) in a symbolic expression.

syms y(x)
P = y^2*x + y*x^2 + y*sin(x) + x*y
P(x) = xy(x)2+x2y(x)+sin(x)y(x)+xy(x)
Cy = collect(P,y)
Cy(x) = xy(x)2+x+sin(x)+x2y(x)

If you specify a matrix of symbolic inputs, collect acts element-wise on the matrix.

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

(x+1y+1xx-y+x22xy-xxy+xy)

C = collect(P,x)
C = 

(y+1x+y+12x2+-yx2y-1xy+1yx)

Collect the coefficients in terms of calls to a particular function by specifying the function name as a string scalar in the second argument. Collect the coefficients of function calls with respect to multiple functions by specifying the multiple functions as a string array.

Collect the coefficients in terms of calls to the sin function in P, where P is a symbolic expression that contains multiple calls to different functions.

syms a b c d e f x
P = a*sin(2*x) + b*sin(2*x) + c*cos(x) + ...
    d*cos(x) + e*sin(3*x) + f*sin(3*x)
P = asin(2x)+bsin(2x)+esin(3x)+fsin(3x)+ccos(x)+dcos(x)
C1 = collect(P,"sin")
C1 = a+bsin(2x)+e+fsin(3x)+ccos(x)+dcos(x)

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

C2 = collect(P,["sin" "cos"])
C2 = a+bsin(2x)+e+fsin(3x)+c+dcos(x)

Create a symbolic expression in terms of x and y.

syms x y
P = -12/((x - 2)*(x + 2)) + 8/(y + 2)
P = 

8y+2-12x-2x+2

Collect the coefficients of identical powers of x and then y in the symbolic expression P. Here, the collect function returns a rational function in the form of the division of two polynomials. It collects the coefficients of identical terms with positive integer powers in the numerator and denominator separately.

Cx = collect(P,x)
Cx = 

8x2-12y-56y+2x2-4y-8

Cy = collect(P,y)
Cy = 

-12y+8x2-56x2-4y+2x2-8

If the expression cannot be expressed as a rational function that is the division of two polynomials with positive integer powers on the indeterminates, then collect might not collect the coefficients of identical powers.

For example, create a symbolic expression that involves the square root of x. Here, collect does not collect identical powers of x.

Q = -12/(sqrt(x)*(x + 2)) + 8/(y + 2)
Q = 

8y+2-12xx+2

C = collect(Q,x)
C = 

16x-12y+8x3/2-242xy+x3/2y+4x+2x3/2

Input Arguments

collapse all

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

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

Example: sin(x)

Example: [sin(x) cos(y)]

Example: ["sin" "cos"]

Tips

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

Version History

Introduced before R2006a