I have a symbollic expression
5*w - 5*z - u*((603367941593515*x)/4503599627370496 - y/2) - v*(x/2 + (603367941593515*y)/4503599627370496) - 1/8
which I want to extract the coefficient of u from. But the coeff command gives an erroneous result:
ans =
5*w - 5*z - u*((603367941593515*x)/4503599627370496 - y/2) - v*(x/2 + (603367941593515*y)/4503599627370496) - 1/8
coeffs(ans,u)
ans =
[5*w - 5*z - v*(x/2 + (603367941593515*y)/4503599627370496) - 1/8, y/2 - (603367941593515*x)/4503599627370496]
The first 'ans' is a multilinear polynomial in x y z u v and w, so shouldn't this work as coeffs is for polynomials.

 採用された回答

Paul
Paul 2026 年 2 月 5 日

1 投票

syms w z u y v x
5*w - 5*z - u*((603367941593515*x)/4503599627370496 - y/2) - v*(x/2 + (603367941593515*y)/4503599627370496) - 1/8
ans = 
coeffs(ans,u).'
ans = 
The second term in the result is the coefficient of u and the first term is everything else.
Is that not the expected result?

4 件のコメント

Steven Lord
Steven Lord 2026 年 2 月 5 日
Note that the convention used by coeffs (returning in ascending order of power when called with 1 output) is different than the convention used by some of the other polynomial functions in MATLAB like polyfit, which returns the coefficients in descending order of power.
p = [1 2 3]; % x^2+2*x+3
x = 1:10;
y = polyval(p, x);
polyfitCoefficients = polyfit(x, y, 2) % Quadratic term first
polyfitCoefficients = 1×3
1.0000 2.0000 3.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
syms z
polynomialSym = p(1)*z^2+p(2)*z+p(3)
polynomialSym = 
coeffsCoefficients1Output = coeffs(polynomialSym, z) % Constant term first
coeffsCoefficients1Output = 
But if you call coeffs with two outputs, it will return it in descending order of power like polyfit does.
[coeffsCoefficients2Output, powersOfZ] = coeffs(polynomialSym, z)
coeffsCoefficients2Output = 
powersOfZ = 
Sam Chak
Sam Chak 2026 年 2 月 5 日
The coeffs(ans, u) function essentially extracts the coefficients for the and terms. If you specify the output arguments clearly using [coefficients, terms], you will observe the results.
syms w z u y v x
expr = 5*w - 5*z - u*((603367941593515*x)/4503599627370496 - y/2) - v*(x/2 + (603367941593515*y)/4503599627370496) - 1/8
expr = 
[coefficients, terms] = coeffs(expr, u)
coefficients = 
terms = 
If you only want to retrieve the coefficient for the u term, then enter this:
coefficients(1)
ans = 
Steven Lord
Steven Lord 2026 年 2 月 5 日
The coeffs(ans, u) function essentially extracts the coefficients for the and terms.
That's true if called with two outputs. If not it returns them in the opposite order, and .
syms w z u y v x
expr = 5*w - 5*z - u*((603367941593515*x)/4503599627370496 - y/2) - v*(x/2 + (603367941593515*y)/4503599627370496) - 1/8;
oneOutput = coeffs(expr, u);
[twoOutputs, terms] = coeffs(expr, u);
isequal(oneOutput, twoOutputs) % false
ans = logical
0
isequal(flip(oneOutput), twoOutputs) % true
ans = logical
1
Ali Kiral
Ali Kiral 2026 年 2 月 6 日
@Paul I didn't know this command also gives an output for the zeroth power of u. You're right.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeNumerical Integration and Differential Equations についてさらに検索

製品

リリース

R2022a

タグ

質問済み:

2026 年 2 月 5 日

コメント済み:

2026 年 2 月 6 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by