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?

 Accepted Answer

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 Comments

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?
hmmmm you are right. Any other idea then?
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.
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
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.
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().

Sign in to comment.

More Answers (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 Comments

yes but i use "syms s" and I expect that matlab will recognize only the character 's' as a symbolic variable. So?
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.
Yes but you declare sym(rt) which is sym('cacd')
Ok, now I understand. And how can I make it to recognize only strings which have the type of a polynomial as a polynomial?
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.

Sign in to comment.

Tags

No tags entered yet.

Asked:

on 29 May 2011

Community Treasure Hunt

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

Start Hunting!