フィルターのクリア

Why sym2poly function converts a random string to s polynomial?

4 ビュー (過去 30 日間)
sadel
sadel 2011 年 5 月 29 日
Why sym2poly function converts a random string ('cacssd' for example) to [1 0] polynomial?
syms s
rt='cacd';
num=sym(rt);
snum=sym2poly(num)
I want to handle an error for this but unfortunatly there is no error. Any idea?

採用された回答

sadel
sadel 2011 年 5 月 30 日
Ok , I found the answer!!!
syms s
rt='cacd';
try
u=eval(rt)
num=sym(rt);
snum=sym2poly(num)
catch
warndlg('Invalid variable','!!Warning!!')
end
  6 件のコメント
Walter Roberson
Walter Roberson 2011 年 5 月 30 日
If that's what you wanted, they just use
if ismember('s',symvar(insertfunction))
snum = sym2poly(sym(insertfunction));
else
warn='Invalid variable'
end
However, the presence of s as a free variable in insertfunction does not establish that insertfunction codes a polynomial.
sadel
sadel 2011 年 5 月 31 日
Thank you but this doesn't work right if I insert the polynomial '5'.
With my code I receive the number 5. And with yours I receive warn='Invalid variable'. My requirements were my function to recognize polynomials of any order and not recognize expressions like exp() sqrt() pow().

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

その他の回答 (1 件)

Paulo Silva
Paulo Silva 2011 年 5 月 29 日
The result [1 0] means that there's a symbolic variable with 1 for it's coefficient, the polynomial is:
1*cacd+0
  6 件のコメント
Paulo Silva
Paulo Silva 2011 年 5 月 30 日
symvar
ismember
Walter Roberson
Walter Roberson 2011 年 5 月 30 日
casd *does* have "the type of a polynomial", just as much as 1*x+0 does.
Perhaps what you want is to restrict to certain variables and then use coeff() or coeffs() to detect whether those variables occur.

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

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by