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

88 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

Walter Roberson
Walter Roberson el 15 de Nov. de 2016
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 comentarios
Andreas
Andreas el 16 de Nov. de 2016
Thanks for the information and the warning!
Christopher Creutzig
Christopher Creutzig el 22 de Nov. de 2016
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.

Iniciar sesión para comentar.

Más respuestas (3)

Karan Gill
Karan Gill el 17 de Nov. de 2016
Editada: Karan Gill el 17 de Oct. de 2017
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 comentarios
Christopher Creutzig
Christopher Creutzig el 24 de Nov. de 2016
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 el 9 de Sept. de 2018
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)...

Iniciar sesión para comentar.


John D'Errico
John D'Errico el 15 de Nov. de 2016
syms x
y = x+1;
WTP? No warning. No problem.
  1 comentario
Andreas
Andreas el 15 de Nov. de 2016
That does not convert a string to a symbolic expression. I am aware, that it is possible to directly enter the symbolic expression.

Iniciar sesión para comentar.


Muhammed Al-barham
Muhammed Al-barham el 11 de Mzo. de 2019
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 comentarios
Walter Roberson
Walter Roberson el 12 de Mzo. de 2019
The above is not valid since R2018a. From R2017b you should use str2sym()
Muhammed Al-barham
Muhammed Al-barham el 12 de Mzo. de 2019
you might right.
I am using version 2016b

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by