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

65 次查看(过去 30 天)
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 个评论
Christopher Creutzig
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
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

Community Treasure Hunt

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

Start Hunting!

Translated by