Getting the Polynomial coefficients

2 次查看(过去 30 天)
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.

采纳的回答

Mohammad Abouali
Mohammad Abouali 2015-4-26
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.

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by