Method on @sym: c = coeffs (p, x)
Method on @sym: c = coeffs (p)
Method on @sym: c = coeffs (…, 'all')
Method on @sym: [c, t] = coeffs (p, x)
Method on @sym: [c, t] = coeffs (p)
Method on @sym: [c, t] = coeffs (…, 'all')

Return non-zero (or all) coefficients of symbolic polynomial.

c contains the coefficients and t the corresponding terms.

Example:

syms x
[c, t] = coeffs (x^6 + 3*x - 4)
  ⇒ c = (sym) [1  3  -4]  (1×3 matrix)
  ⇒ t = (sym 1×3 matrix)
       ⎡ 6      ⎤
       ⎣x   x  1⎦

The polynomial can be multivariate:

syms x y
[c, t] = coeffs (x^2 + y*x)
  ⇒ c = (sym) [1  1]  (1×2 matrix)
  ⇒ t = (sym 1×2 matrix)
      ⎡ 2     ⎤
      ⎣x   x⋅y⎦

[c, t] = coeffs (x^2 + y*x, [x y])   % same
  ⇒ c = (sym) [1  1]  (1×2 matrix)
  ⇒ t = (sym 1×2 matrix)
      ⎡ 2     ⎤
      ⎣x   x⋅y⎦

[c, t] = coeffs (x^2 + y*x, {x y})   % same
  ⇒ c = (sym) [1  1]  (1×2 matrix)
  ⇒ t = (sym 1×2 matrix)
      ⎡ 2     ⎤
      ⎣x   x⋅y⎦

You can use the second argument to specify a vector or list of variables:

[c, t] = coeffs (x^2 + y*x, x)
  ⇒ c = (sym) [1  y]  (1×2 matrix)
  ⇒ t = (sym 1×2 matrix)
      ⎡ 2   ⎤
      ⎣x   x⎦

Omitting the second output is not recommended, especially for non-interactive code, because it gives only the non-zero coefficients, and additionally the output is in the “wrong order” compared to other polynomial-related commands:

c = coeffs (x^6 + 3*x - 4)
  ⇒ c = (sym) [-4  3  1]  (1×3 matrix)

Warning: Again, note the order is reversed from the two-output case; this is for compatibility with Matlab’s Symbolic Math Toolbox.

If the optional input keyword 'all' is passed, the zero coefficients are returned as well, and in the familiar order.

c = coeffs (x^6 + 3*x - 4, 'all')
  ⇒ c = (sym) [1  0  0  0  0  3  -4]  (1×7 matrix)

Note: The 'all' feature does not yet work with multivariate polynomials (https://github.com/gnu-octave/symbolic/issues/720).

See also: @sym/sym2poly.

Package: symbolic