Main Content

Collect coefficients

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 = x

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

`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 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)

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 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); collect(F, 'sin')

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 = (c + d)*cos(x) + (a + b)*sin(2*x) + (e + f)*sin(3*x)

`combine`

| `expand`

| `factor`

| `horner`

| `numden`

| `rewrite`

| `simplify`

| `simplifyFraction`

| `symvar`