How to get all the coefficients of a polynomial?

8 次查看(过去 30 天)
I tried the sample from MATLAB documentation but I get something else which is different from the result in the documentation. Does anyone know where I did it wrong? The result in the documentation is be [3,0,0] which returns all the coefficients all the way from 0 degree. But I get 3*x^2 which makes no sense.

回答(3 个)

John D'Errico
John D'Errico 2017-4-27
编辑:John D'Errico 2017-4-27
One problem with text is knowing that you are writing the number 1, versus the lower case LETTER l. Some fonts make this more or less obvious than others of course.
syms x
coeffs(3*x^2,'all')
ans =
[ 3, 0, 0]
In the PICTURE you posted, you clearly wrote 'A11', as opposed to 'All'. In general, it is a silly idea to post a picture of text, because then we cannot copy the text and try to use it. Just paste in the TEXT! In this case, if you had put the text itself in, then I could have shown exactly what you did incorrectly even more conclusively.
In the first case above, those were ONES, not lower case L's. There is a difference.
If instead, I try it using numeric ones in there:
coeffs(3*x^2,'a11')
ans =
3*x^2
then I get the same wrong answer that you got.
  3 个评论
DmArcher
DmArcher 2017-4-28
Thank you so much. But if I have an other symbolic variable as the coefficient and I want to show all the coefficients. What should I do?
syms f1 f2 z
a = (f1-f2)*z^2
coeffes(a,z,'all')
This will report error that I have too many inputs.
Walter Roberson
Walter Roberson 2017-4-28
As I indicated, R2015b does not support the 'all' argument. You need to either upgrade to at least R2016a, or use the methods I outlined in my Answer.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2017-4-27
In R2015b, there was no 'all' option; http://www.mathworks.com/help/releases/R2015b/symbolic/coeffs.html; that was added in R2016a.
If you have only a single variable, then I suggest you use sym2poly instead:
>> sym2poly(3*x^2)
ans =
3 0 0
If you have multiple variables, then use the two-output form of coeffs, and then match through the items in the second output looking to see which powers are missing in order to know to assign the coefficient 0 to those.

Nhan Nguyen
Nhan Nguyen 2017-4-26
I don't know which version of Matlab that your book was based on.
I'm using Matlab 2015b and I can't find the option 'All' in description of the function "coeffs". To extract the coefficients of the polynomial, I just need to type
syms x
coeffs(3*x^2)
and the output is 3.
I think you should check if your Matlab version fit with those in the book.

类别

Help CenterFile Exchange 中查找有关 Assumptions 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by