Collect Coefficients of Powers of Default Variable
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,
uses the default variable defined by
For this expression, the default variable is
symvar((exp(x) + x)*(x + 2),1)
ans = x
Collect Coefficients of Powers of a Particular Variable
Collect coefficients of a particular variable
by specifying the variable as the second argument to
Collect coefficients of an expression in powers of
x, and then in powers of
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
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
Collect Coefficients in Terms of
Collect coefficients of an expression in terms of
i, and then in terms of
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 Symbolic Expressions and Functions
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.
sin(x + 3*y) and collect coefficients of
cos(y), and then of both
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
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)
Collect Coefficients for Each Element of Matrix
collect on a matrix.
collect acts element-wise on the
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 Function Calls
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
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
cos functions in
ans = (a + b)*sin(2*x) + (e + f)*sin(3*x) + (c + d)*cos(x)
P — Input expression
symbolic expression | symbolic function | symbolic vector | symbolic matrix
Input expression, specified as a symbolic expression, function, vector, or matrix.
expr — Expression in terms of which you collect coefficients
symbolic number | symbolic variable | symbolic expression | symbolic function | symbolic vector | character vector | cell array of character vectors
Expression in terms of which you collect the coefficients, specified as a symbolic number, variable, expression, function, or vector; a character vector; a cell array of character vectors.
collectreturns 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
isequalmight not return
truewhen checking for equality. Instead, use
isAlwaysto prove equivalence between the input and output expressions.
Introduced before R2006a