Documentation

# `collect`

Collect terms with the same powers

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

## Syntax

```collect(`p`, `g`, <`f`>)
collect(`p`, `[g1, g2, …]`, <`f`>)
```

## Description

`collect(p, g)` groups terms with the same powers of `g` in an expression `p`.

`collect(p, [g1, g2, ...])` groups terms with the same powers of g1, g2, … in a multivariate expression `p`.

If you pass a function name `f` as a third argument to `collect`, the procedure collects the powers of g (g1, g2, … for multivariate expression). Then it applies the function `f` to the coefficients.

`collect(p, g)` presents `p` as a sum $\sum _{i=0}^{n}{a}_{i}{g}^{i}$. The coefficients ai are not polynomials in g. These coefficients can contain some terms with g, for example, sin(g) or ${e}^{g}$.

`collect` returns a modified copy of a polynomial. The function does not change the polynomial itself. See Example 1.

If `p` is a rational expression in `g`, `collect` handles the numerator and denominator separately.

If `p` is a multivariate expression, collect(p, [g1, g2, …]) returns an expression in the following form:

`$\sum _{{i}_{1}{i}_{2}\dots }\left({a}_{{i}_{1},{i}_{2}\dots }{g}_{1}^{{i}_{1}}{g}_{2}^{{i}_{2}}\dots \right)$`

The coefficients ai1, i2, … are not polynomials in g. These coefficients can contain some terms with g1, g2, …, for example, $\mathrm{sin}\left({g}_{1}\right){e}^{{g}_{2}}$.

If `p` is a rational expression in g1, g2, …, the `collect` command handles the numerator and denominator separately.

For polynomial expressions, `collect` internally calls two functions: `poly` and then `expr`. The function `poly` converts an expression `p` into a polynomial in the given unknowns. This function returns a polynomial with the terms collected by the same powers. Then `expr` converts this polynomial into a polynomial expression. See `poly` for more information and examples. When applied to a rational expression, `collect` handles the numerator and denominator separately.

You can use arbitrary expressions as indeterminates. See Example 2.

You can specify a function name instead of a variable. In this case, `collect` treats all calls of the function with different arguments as different variables. See Example 4.

`collect` does not recursively collect the operands of nonpolynomial subexpressions of `p`. See Example 2.

If `p` is not a polynomial expression, `collect` can return the unchanged expression `p`. See Example 5.

## Examples

### Example 1

You can define a polynomial expression `p` and collect terms with the same powers of `x` and `y`:

```p := x*y + z*x*y + y*x^2 - z*y*x^2 + x + z*x; collect(p, [x, y])```
` `
` `

`collect` does not modify the original expression:

`p`
` `

You can collect terms with same powers of `x`:

`collect(p, [x])`
` `

If an expression contains only one indeterminate, you can omit the square brackets in the second argument of the function call:

`collect(p, x)`
` `

To factor coefficients in a resulting expression, pass `factor` as a third argument to `collect`:

`collect(p, x, factor)`
` `

### Example 2

`collect` does not modify nonpolynomial subexpressions even if they contain a given indeterminate. In particular, `collect` does not recursively handle the operands of a nonpolynomial subexpression:

`collect(sin((x + 1)^2)*(x + 1) + 5*sin((x + 1)^2) + x, x)`
` `

`collect` accepts nonpolynomial subexpressions as indeterminates:

```collect(sin((x + 1)^2)*(x + 1) + 5*sin((x + 1)^2) + x, sin((x + 1)^2))```
` `

### Example 3

`collect` normalizes a rational expression, and then handles the numerator and denominator separately:

`collect(z/(x+y) + 3*z/(x+z), z)`
` `

### Example 4

If you specify the name of a function as an indeterminate, `collect` handles functions calls with different arguments as different indeterminates:

`collect(a*f(1) + c*f(1) + f(2) + d*f(2), f)`
` `
`collect(a*sin(x) + b*sin(x) + c*sin(y) + d*sin(y), sin)`
` `
```p:= diff(besselJ(0, x), x \$ 4); collect(p, besselJ); collect(p, besselJ, expand);```
` `
` `
` `

### Example 5

If `p` is not a polynomial expression, `collect` can return the unchanged expression `p`:

```p := y^2*sin(x) + y*sin(x) + y^2*cos(x) + y*cos(x); collect(p, x)```
` `
` `

The expression `p` is a polynomial expression in `y`. You can group the terms with the same powers in this variable:

`collect(p, y)`
` `

## Parameters

 `p` `x, x1, x2, …` The indeterminates: typically, identifiers or indexed identifiers. `f` A function.

`p`