Getting the Polynomial coefficients

4 views (last 30 days)
Hello,
I am trying to identify if a given function is polynomial or not and if it is polynomial what the coefficients are. For example;
sym F x
F= 5*x^5+cos(x)*x^2+1 % This is not polynomial because of cos(x) and I want to identify that.
[c,t] = coeffs(F);
PolyCoef = sym2poly(F);
c =
[ 5, 1, 1] % no cos(x) is given in here
>> t
t =
[ [x^5, 1], [x^2, x], [1, 1]] % no cos (x) is given in here
and sym2poly will give error. I am okay with the error as long as it does not stop the program and tell me that input function is not polynomial.
Thanks for the help.

Accepted Answer

Mohammad Abouali
Mohammad Abouali on 26 Apr 2015
Part I
You are using SYM() while using SYMS style of command. They are different. So change sym F x to syms F X or if you insist on using sym() then you need to use it properly such as F=sym('F')'
Part II
You can use that error to determine both the coefficient and if it was polynomial or not. You can use try/catch as follows:
try
PolyCoef = sym2poly(F);
catch
disp('that was not a polynomial')
PolyCoef=[];
end
In this case the program is not halted. If the statement(s) between catch and end (catch block) is only executed if, the statements between try and catch (try block), i.e. PolyCoef = sym2poly(F) in this case, produces any error. Refer to try/catch for further info on that.
  1 Comment
Kamuran
Kamuran on 26 Apr 2015
Edited: Kamuran on 26 Apr 2015
Thanks, that is what I was looking for

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!