collect
Collects the terms of an expression by powers of a variable.
Category: Mathematics > Formula Manipulation and Simplification
Syntax
R = collect(p)
Collects in the default variable.
R = collect(p, x)
Collects in powers of x.
Description
collect does not change the value of the expression, only its form. Like terms in x are added, and each power of x is multiplied by a factored coefficient. It is the inverse view of expand: where expand removes parentheses, collect reorganizes by variable.
Input arguments
p- Symbolic expression or array of expressions.
x- Variable whose powers group the terms.
Output arguments
R- Equivalent expression written as a polynomial in
x, with the other symbols inside the coefficients.
Example
Group by x
syms x y
collect(x^2*y + x*y + x^2 + 3, x)
collect((x+1)*(x+2) + x*y, x)
Output
ans = (y + 1)*x^2 + x*y + 3
ans = x^2 + (y + 3)*x + 2