Getting the Polynomial coefficients

1 回表示 (過去 30 日間)
Kamuran
Kamuran 2015 年 4 月 26 日
編集済み: Kamuran 2015 年 4 月 26 日
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.
  1 件のコメント
Kamuran
Kamuran 2015 年 4 月 26 日
編集済み: Kamuran 2015 年 4 月 26 日
Thanks, that is what I was looking for

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePolynomials についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by