How to convert strings to symbolic expressions without sym()?
43 views (last 30 days)
Show older comments
Andreas
on 15 Nov 2016
Commented: Muhammed Al-barham
on 12 Mar 2019
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?
0 Comments
Accepted Answer
Walter Roberson
on 15 Nov 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 Comments
Christopher Creutzig
on 22 Nov 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.
More Answers (3)
Karan Gill
on 17 Nov 2016
Edited: Karan Gill
on 17 Oct 2017
>> str2sym('x+1')
ans =
x + 1
Karan. (Symbolic documentation)
7 Comments
Christopher Creutzig
on 24 Nov 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
on 9 Sep 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)...
Muhammed Al-barham
on 11 Mar 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 Comments
Walter Roberson
on 12 Mar 2019
The above is not valid since R2018a. From R2017b you should use str2sym()
See Also
Categories
Find more on Conversion in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!