How to convert strings to symbolic expressions without sym()?

94 ビュー (過去 30 日間)
Andreas
Andreas 2016 年 11 月 15 日
コメント済み: Muhammed Al-barham 2019 年 3 月 12 日
Since sym() outputs a warning, that it is deprecated to use it with a string input since R2016a as follos:
>> syms x
>> sym('x+1')
Warning: Support of strings that are not valid variable names or define a number will be removed in a future release. To create symbolic expressions,
first create symbolic variables and then use operations on them.
> In sym>convertExpression (line 1536)
In sym>convertChar (line 1441)
In sym>tomupad (line 1198)
In sym (line 177)
Is there a different way to convert a string into a symbolic expression?

採用された回答

Walter Roberson
Walter Roberson 2016 年 11 月 15 日
y = evalin(symengine, 'x+1')
which does the same thing as
y = sym('x+1')
except that going through sym() has more overhead for this situation and will give you the warning.
Caution: when you use
syms x
sym('x+1')
then the x of the second version does not necessarily refer to the same thing that the first one refers to. For example if you had used
syms x y
x = y + 2
sym('x+1')
then you will not get y + 3
  2 件のコメント
Andreas
Andreas 2016 年 11 月 16 日
Thanks for the information and the warning!
Christopher Creutzig
Christopher Creutzig 2016 年 11 月 22 日
As an additional warning, this will not use MATLAB syntax (nor pure MuPAD syntax either) and will not work with some variable names, including D, E, I, beta, and will have unexpected effects with some functions such as zeta(a,b) swapping its arguments.

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

その他の回答 (3 件)

Karan Gill
Karan Gill 2016 年 11 月 17 日
編集済み: Karan Gill 2017 年 10 月 17 日
Why do you need to convert strings to symbolic expressions? EDIT: Starting R2017b, use str2sym
>> str2sym('x+1')
ans =
x + 1
Karan. (Symbolic documentation)
  7 件のコメント
Christopher Creutzig
Christopher Creutzig 2016 年 11 月 24 日
You can't really differentiate a function without simplifying it, either. Well, technically you probably could, in a lot of cases, but I doubt that most of the code SMT has for differentiating would care about noncommutativity and such, likely reasons for not wanting a simplified formula. Nor does the system have any idea how to differentiate mod.
The same would be true for solve and int as well, and most other functions. Such an extension would be absolutely nontrivial.
The prime reason for symfun, to me, is to specify the order of variables, followed closely by having a simpler syntax for subs. These reasons are very different from the reasons for having functions in MuPAD, so it's not a surprise the resulting design is different.
mod(x,2) returns x for me, as expected. (I do realize my expectations may be different from anyone else's and colored by knowing what the function does to start with.) mod(3*x,2) returns x, too, because mod(3,2) is 1, and mod(1.5*x,1) returns x/2 for pretty much the same reason. This is explained in the third example in doc symbolic/mod.
Walter Roberson
Walter Roberson 2018 年 9 月 9 日
My reasons for constructing symbolic functions are often similar to to the reasons for having functions in MuPAD: which is to say that I am constructing something procedural that involves symbolic expressions.
Recently someone was computing things with terms equivalent to sinc() and asked how to rewrite them in terms of an explicit sinc() instead of in terms of sin(pi*x)/(pi*x) . The position of the sin() in the expression was not fixed. I was not able to get subs() to replace sin() calls
>> subs(sin(pi*x)+x, 'sin', 'SIN')
ans =
x + sin(pi*x)
and I was not able to get feval(symengine) to do a subs() for this case either. I could get feval(symengine) of subsop() to do something, but that required knowing which operand number to substitute.
Now, if I could write symbolic functions that could bind parameters and use op() and pattern matching and local variable names (normal MATLAB level symbolic variables are effectively global, inheriting any assume() that has been done at any level)...

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


John D'Errico
John D'Errico 2016 年 11 月 15 日
syms x
y = x+1;
WTP? No warning. No problem.
  1 件のコメント
Andreas
Andreas 2016 年 11 月 15 日
That does not convert a string to a symbolic expression. I am aware, that it is possible to directly enter the symbolic expression.

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


Muhammed Al-barham
Muhammed Al-barham 2019 年 3 月 11 日
You can do that by using sub function as follows (Simple trick) :
example : f(x) = x+1 ;
if you input f as string >> f = 'x+1'
then use sub to substitute the value ex. 1
subs(f,'x',1)
So , matlab will deal with it as function wihtout any wrong message
then the answer will be :
2
  2 件のコメント
Walter Roberson
Walter Roberson 2019 年 3 月 12 日
The above is not valid since R2018a. From R2017b you should use str2sym()
Muhammed Al-barham
Muhammed Al-barham 2019 年 3 月 12 日
you might right.
I am using version 2016b

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

Community Treasure Hunt

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

Start Hunting!

Translated by