Why do I have an error with minus sign
8 views (last 30 days)
Show older comments
I put a minus sign in the equation and it appears in the wrong place. Is this a bug in the program or something else wrong?

1 Comment
Dyuman Joshi
on 30 Oct 2023
No, it's not an error nor is it a bug. The symbolic engine just rearranges the expression and brings the minus sign out of the parenthesis.
Answers (1)
Torsten
on 30 Oct 2023
Moved: Torsten
on 30 Oct 2023
There is a minus sign in front of d^2M/dx^2 which is difficult to see. So everything is ok:
f_0*(x/L)*(1-(x/L)) = -(f_0*x*((x/L)-1))/L
7 Comments
Dyuman Joshi
on 1 Nov 2023
Edited: Dyuman Joshi
on 1 Nov 2023
Now that I think about it, one can write an expression in text format, use displayFormula() and then convert it to a symbolic term via str2sym. Doable.
One more question - Do we know how an expression is parsed? Or is it something that TMW has not made that information public?
Walter Roberson
on 1 Nov 2023
displayFormula() and str2sym() use nearly the same logic.
toolbox/symbolic/symbolic/private/str2symInternal.m
It starts with converting a few things like = to == and empty string to empty symbolic strings.
What is left after the pre-processing phase is something that should match MATLAB syntax -- not necessarily MATLAB semantics but syntax .
It proceeds to use mtree() to convert the string into a parse tree.
Then it replaces operators -- for example replacing 'PLUS' internal token with its two operands by recursively converting the two operands and then generating a MuPAD _plus call with the operands.
Eventually it tells MuPAD to evaluate the converted string.
The difference with displayFormula is that instead of generating (for example) _plus it generates hold(_plus) -- so it is generating the exact same kind of MuPAD tree, just with all of the operations told to remain inactive.
That is the logic for displayFormula() and str2sym().
In the case of symbolic expressions such as
syms x y
f = x^2 + sin(y)
then the processing is different. In such a case, MATLAB's internal parsing is used, because everything there is a MATLAB-level expression.
syms x y translates into a call to syms.m that calls into MuPAD to create MuPAD-level variables x and y, and syms.m does assignin('caller') of the name and the (wrapped) result of the call into MuPAD.
Then the x^2 part looks in the environment, sees that there is a variable of class sym for x, and sees that class sym has a power method, and invokes that power method of the class with input being the details of the symbolic expression associated with MATLAB-level x . In turn the sym power method calls into MuPAD with a MuPad _power operation request, and gets back the (wrapped) details of where to find the result of the expression in the MuPAD engine. Then the + operation is consulted, sees that the operand is sym (the result of the x^2), invokes the sym plus method, which tells MuPAD to do a _plus operation... and so on. (I skipped the fact that the sin(y) needs to be evaluated before the + . Same logic though.)
Thus expressions involving sym and symfun and symmatrix at the MATLAB level are all parsed at the MATLAB level, since they are all MATLAB syntax -- but there are class methods that lead to outcomes different than numeric outcomes. No special symbolic parsing is involved for these cases.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!