# collect

Collect coefficients of identical powers

## Syntax

``C = collect(P)``
``C = collect(P,expr)``

## 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 = $\left(x+{\mathrm{e}}^{x}\right) \left(x+2\right)$`
`C = collect(P)`
`C = ${x}^{2}+\left({\mathrm{e}}^{x}+2\right) x+2 {\mathrm{e}}^{x}$`

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 = $x y-2 x+{x}^{2} y-{x}^{2}$`

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 = $\left(y-1\right) {x}^{2}+\left(y-2\right) x$`

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 = $\left({x}^{2}+x\right) y-{x}^{2}-2 x$`

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 = $y {a}^{2} x+b a {x}^{2}+y a x+{x}^{2}$`
`Cxy = collect(Q,[x y])`
`Cxy = $\left(a b+1\right) {x}^{2}+\left({a}^{2}+a\right) x y$`

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 \pi +x \left(\pi +\mathrm{i}\right)+\pi y \left(\pi -\mathrm{i}\right)$`
`Ci = collect(P,1i)`
`Ci = $\left(x-\pi y\right) \mathrm{i}+3 \pi +\pi x+y {\pi }^{2}$`
`Cpi = collect(P,pi)`
`Cpi = $y {\pi }^{2}+\left(x+3-y \mathrm{i}\right) \pi +x \mathrm{i}$`

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 = $4 \mathrm{sin}\left(x\right) {\mathrm{cos}\left(y\right)}^{3}+4 \mathrm{cos}\left(x\right) \mathrm{sin}\left(y\right) {\mathrm{cos}\left(y\right)}^{2}-3 \mathrm{sin}\left(x\right) \mathrm{cos}\left(y\right)-\mathrm{cos}\left(x\right) \mathrm{sin}\left(y\right)$`
`C = collect(P,sin(x))`
`C = $\left(4 {\mathrm{cos}\left(y\right)}^{3}-3 \mathrm{cos}\left(y\right)\right) \mathrm{sin}\left(x\right)+4 \mathrm{cos}\left(x\right) {\mathrm{cos}\left(y\right)}^{2} \mathrm{sin}\left(y\right)-\mathrm{cos}\left(x\right) \mathrm{sin}\left(y\right)$`

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 = $\left(4 {\mathrm{cos}\left(y\right)}^{3}-3 \mathrm{cos}\left(y\right)\right) \mathrm{sin}\left(x\right)+\left(4 \mathrm{cos}\left(x\right) {\mathrm{cos}\left(y\right)}^{2}-\mathrm{cos}\left(x\right)\right) \mathrm{sin}\left(y\right)$`

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) = $x {y\left(x\right)}^{2}+{x}^{2} y\left(x\right)+\mathrm{sin}\left(x\right) y\left(x\right)+x y\left(x\right)$`
`Cy = collect(P,y)`
`Cy(x) = $x {y\left(x\right)}^{2}+\left(x+\mathrm{sin}\left(x\right)+{x}^{2}\right) y\left(x\right)$`

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 =  $\left(\begin{array}{cc}\left(x+1\right) \left(y+1\right)& x \left(x-y\right)+{x}^{2}\\ 2 x y-x& x y+\frac{x}{y}\end{array}\right)$```
`C = collect(P,x)`
```C =  $\left(\begin{array}{cc}\left(y+1\right) x+y+1& 2 {x}^{2}+\left(-y\right) x\\ \left(2 y-1\right) x& \left(y+\frac{1}{y}\right) x\end{array}\right)$```

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 = $a \mathrm{sin}\left(2 x\right)+b \mathrm{sin}\left(2 x\right)+e \mathrm{sin}\left(3 x\right)+f \mathrm{sin}\left(3 x\right)+c \mathrm{cos}\left(x\right)+d \mathrm{cos}\left(x\right)$`
`C1 = collect(P,"sin")`
`C1 = $\left(a+b\right) \mathrm{sin}\left(2 x\right)+\left(e+f\right) \mathrm{sin}\left(3 x\right)+c \mathrm{cos}\left(x\right)+d \mathrm{cos}\left(x\right)$`

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

`C2 = collect(P,["sin" "cos"])`
`C2 = $\left(a+b\right) \mathrm{sin}\left(2 x\right)+\left(e+f\right) \mathrm{sin}\left(3 x\right)+\left(c+d\right) \mathrm{cos}\left(x\right)$`

Create a symbolic expression in terms of `x` and `y`.

```syms x y P = -12/((x - 2)*(x + 2)) + 8/(y + 2)```
```P =  $\frac{8}{y+2}-\frac{12}{\left(x-2\right) \left(x+2\right)}$```

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 =  $\frac{8 {x}^{2}-12 y-56}{\left(y+2\right) {x}^{2}-4 y-8}$```
`Cy = collect(P,y)`
```Cy =  $\frac{-12 y+8 {x}^{2}-56}{\left({x}^{2}-4\right) y+2 {x}^{2}-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 =  $\frac{8}{y+2}-\frac{12}{\sqrt{x} \left(x+2\right)}$```
`C = collect(Q,x)`
```C =  $\frac{16 \sqrt{x}-12 y+8 {x}^{3/2}-24}{2 \sqrt{x} y+{x}^{3/2} y+4 \sqrt{x}+2 {x}^{3/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