How to get all the coefficients of a polynomial?
8 views (last 30 days)
Show older comments
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/163372/image.jpeg)
2 Comments
Answers (3)
John D'Errico
on 27 Apr 2017
Edited: John D'Errico
on 27 Apr 2017
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 Comments
Walter Roberson
on 28 Apr 2017
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
on 27 Apr 2017
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.
0 Comments
Nhan Nguyen
on 26 Apr 2017
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.
See Also
Categories
Find more on Assumptions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!