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

조회 수: 97 (최근 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