Why sym2poly function converts a random string to s polynomial?

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 日

0 投票

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 日
That does not appear to be a solution, not unless you have unstated requirements!
Suppose, for example, that the user entered 'rt'. Then rt='rt' and eval(rt) will not fail, but it probably is not a polynomial for your purposes.
Suppose the user entered 'delete(''*.*'')' . You go ahead and blindly eval() that and get all your files deleted but the evaluation succeeds. What kind of polynomial is it that deletes all of your files?
sadel
sadel 2011 年 5 月 30 日
hmmmm you are right. Any other idea then?
Walter Roberson
Walter Roberson 2011 年 5 月 30 日
You haven't defined your requirements.
Paulo recommended symvar and that is likely a good place to start. Extract the variables from the expression, and if any of them in the expression are not on the approved list, veto the expression.
It is also possible to extract the names of all of the functions used and compare them to your approved list. Note, though, that the internal name of functions might not be the obvious one, so experiment to see what the names actually are. In Maple, you would use indets() with fairly specific parameters to extract the function names; I am not sure what the MuPad equivalent would be.
sadel
sadel 2011 年 5 月 30 日
Ok now I think I found it!!!!
syms s
insertfunction='dvsdvcsc';
F = inline(insertfunction);
isParameter = ismember('s',argnames(F));
if (isParameter == 1) | (~isempty(str2num(insertfunction)))
num=sym(insertfunction);
snum=sym2poly(num)
else
warn='Invalid variable'
end
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 日

1 投票

The result [1 0] means that there's a symbolic variable with 1 for it's coefficient, the polynomial is:
1*cacd+0

6 件のコメント

sadel
sadel 2011 年 5 月 29 日
yes but i use "syms s" and I expect that matlab will recognize only the character 's' as a symbolic variable. So?
Paulo Silva
Paulo Silva 2011 年 5 月 29 日
that's a wrong assumption, syms s just defines s as symbolic, the next lines of code have nothing to do with the first, in the third line you define the string inside the variable rt as symbolic and store it in the variable num, it's the symbolic variable num contents that are converted.
Oleg Komarov
Oleg Komarov 2011 年 5 月 29 日
Yes but you declare sym(rt) which is sym('cacd')
sadel
sadel 2011 年 5 月 29 日
Ok, now I understand. And how can I make it to recognize only strings which have the type of a polynomial as a polynomial?
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.

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

タグ

タグが未入力です。

質問済み:

2011 年 5 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by